중첩 라우트의 상위 리소스 ID 를 조회 스코프에 반영하지 않아 A 의 경로로 B 의 하위 리소스를 읽거나 변경할 수 있었다. 방어 위치를 Repository where 절(SSoT)로 통일하고 컨트롤러 사후 비교에 의존하지 않게 했다. 같은 리소스의 형제 엔드포인트가 서로 다른 강도로 검증하던 부분도 맞췄다. 계층 리소스는 자기 자신·자손을 부모로 지정하는 순환을 검증 계층에서 차단하고, 검증을 우회하는 경로(시더/훅/오염 데이터)를 위해 path 재귀에 방문 ID 가드를 뒀다. 설정값이 정하는 깊이 상한을 Service 가 리터럴로 재클램프하던 부분을 제거해 게시판 설정이 실제로 적용되게 했다. 쓰기 경로는 validated 기준으로 좁혀 FormRequest 미정의 필드가 fillable 로 새지 않게 했다. 기존 오염 데이터는 업그레이드 스텝이 정리한다(삭제 없이 최상위로 이동). 재발 방지로 audit 룰 3종을 추가하고, 규정 문서와 트러블슈팅 사례를 함께 갱신했다. 공개 이슈: gnuboard/g7
169 lines
5.4 KiB
PHP
169 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace Modules\Sirsoft\Board\Tests\Feature\User;
|
|
|
|
// ModuleTestCase를 수동으로 require (autoload 전에 로드 필요)
|
|
require_once __DIR__.'/../../ModuleTestCase.php';
|
|
|
|
use App\Models\Role;
|
|
use App\Models\User;
|
|
use Modules\Sirsoft\Board\Tests\BoardTestCase;
|
|
|
|
/**
|
|
* 사용자 댓글 API 상위 게시글 스코프 검증 테스트
|
|
*
|
|
* 검증 목적:
|
|
* - 게시글 A 의 댓글을 게시글 B 의 URL 로 수정/삭제할 수 없다 (교차 경로 차단)
|
|
* - 정상 경로(자기 게시글 URL)로는 기존과 동일하게 수정/삭제된다
|
|
*
|
|
* @group board
|
|
* @group comment
|
|
* @group scope
|
|
*/
|
|
class CommentScopeTest extends BoardTestCase
|
|
{
|
|
private User $memberUser;
|
|
|
|
protected function getTestBoardSlug(): string
|
|
{
|
|
return 'comment-scope';
|
|
}
|
|
|
|
protected function getDefaultBoardAttributes(string $slug): array
|
|
{
|
|
return [
|
|
'slug' => $slug,
|
|
'name' => ['ko' => '댓글 스코프 테스트 게시판', 'en' => 'Comment Scope Test Board'],
|
|
'is_active' => true,
|
|
'use_comment' => true,
|
|
'max_comment_depth' => 10,
|
|
];
|
|
}
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->setGuestPermissions(['posts.read', 'posts.write', 'comments.read', 'comments.write']);
|
|
$this->grantUserRolePermissions(['posts.read', 'posts.write', 'comments.read', 'comments.write']);
|
|
|
|
$this->memberUser = User::factory()->create(['email' => 'comment-scope-member@test.com']);
|
|
|
|
$userRole = Role::where('identifier', 'user')->first();
|
|
if ($userRole) {
|
|
$this->memberUser->roles()->attach($userRole->id);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 게시글 B 의 URL 로 게시글 A 의 댓글을 수정할 수 없다 → 404
|
|
*
|
|
* @scenario resource=board_comment_user, parent_scope=mismatched, actor=member
|
|
*
|
|
* @effects cross_scope_write_rejected, target_resource_unchanged_on_rejection
|
|
*/
|
|
public function test_cannot_update_comment_through_other_post_url(): void
|
|
{
|
|
$postA = $this->createTestPost(['title' => '게시글 A']);
|
|
$postB = $this->createTestPost(['title' => '게시글 B']);
|
|
|
|
$commentId = $this->createTestComment($postA, [
|
|
'user_id' => $this->memberUser->id,
|
|
'content' => '원래 내용',
|
|
]);
|
|
|
|
$this->actingAs($this->memberUser)
|
|
->putJson($this->url($postB, $commentId), ['content' => '교차 경로 수정 시도'])
|
|
->assertStatus(404);
|
|
|
|
$this->assertDatabaseHas('board_comments', [
|
|
'id' => $commentId,
|
|
'content' => '원래 내용',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 게시글 B 의 URL 로 게시글 A 의 댓글을 삭제할 수 없다 → 404
|
|
*
|
|
* @scenario resource=board_comment_user, parent_scope=mismatched, actor=member
|
|
*
|
|
* @effects cross_scope_write_rejected, target_resource_unchanged_on_rejection
|
|
*/
|
|
public function test_cannot_delete_comment_through_other_post_url(): void
|
|
{
|
|
$postA = $this->createTestPost(['title' => '게시글 A']);
|
|
$postB = $this->createTestPost(['title' => '게시글 B']);
|
|
|
|
$commentId = $this->createTestComment($postA, [
|
|
'user_id' => $this->memberUser->id,
|
|
]);
|
|
|
|
$this->actingAs($this->memberUser)
|
|
->deleteJson($this->url($postB, $commentId))
|
|
->assertStatus(404);
|
|
|
|
$this->assertDatabaseHas('board_comments', [
|
|
'id' => $commentId,
|
|
'deleted_at' => null,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 정상 경로(자기 게시글 URL)로는 댓글이 수정된다 → 200
|
|
*
|
|
* @scenario resource=board_comment_user, parent_scope=matching, actor=member
|
|
*
|
|
* @effects matching_scope_still_succeeds
|
|
*/
|
|
public function test_can_update_comment_through_own_post_url(): void
|
|
{
|
|
$postId = $this->createTestPost();
|
|
$commentId = $this->createTestComment($postId, [
|
|
'user_id' => $this->memberUser->id,
|
|
'content' => '원래 내용',
|
|
]);
|
|
|
|
$this->actingAs($this->memberUser)
|
|
->putJson($this->url($postId, $commentId), ['content' => '정상 수정'])
|
|
->assertStatus(200);
|
|
|
|
$this->assertDatabaseHas('board_comments', [
|
|
'id' => $commentId,
|
|
'content' => '정상 수정',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 정상 경로(자기 게시글 URL)로는 댓글이 삭제된다 → 200
|
|
*
|
|
* @scenario resource=board_comment_user, parent_scope=matching, actor=member
|
|
*
|
|
* @effects matching_scope_still_succeeds
|
|
*/
|
|
public function test_can_delete_comment_through_own_post_url(): void
|
|
{
|
|
$postId = $this->createTestPost();
|
|
$commentId = $this->createTestComment($postId, [
|
|
'user_id' => $this->memberUser->id,
|
|
]);
|
|
|
|
$this->actingAs($this->memberUser)
|
|
->deleteJson($this->url($postId, $commentId))
|
|
->assertStatus(200);
|
|
|
|
$this->assertSoftDeleted('board_comments', ['id' => $commentId]);
|
|
}
|
|
|
|
/**
|
|
* 사용자 댓글 API URL 을 생성합니다.
|
|
*
|
|
* @param int $postId 게시글 ID
|
|
* @param int $commentId 댓글 ID
|
|
* @return string 댓글 수정/삭제 엔드포인트 URL
|
|
*/
|
|
private function url(int $postId, int $commentId): string
|
|
{
|
|
return "/api/modules/sirsoft-board/boards/{$this->board->slug}/posts/{$postId}/comments/{$commentId}";
|
|
}
|
|
}
|