Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@
**Vulnerability:** The document hashing routine in `DefaultDocumentConversionService` processed file streams without enforcing any maximum size limit on the bytes read. An attacker could exploit this by uploading a maliciously large stream (or exploiting a compression bomb if unzipping), exhausting system memory, CPU, or disk space (DoS).
**Learning:** Checking the declared file size (e.g., `file.getSize()`) in initial validation is not always sufficient if the input stream itself can be spoofed or dynamically expanded during reading. The actual bytes read must be verified against bounds continuously.
**Prevention:** Always enforce a strict, configurable size limit (e.g., `ConversionProperties.maxUploadSizeBytes`) within the `while` loop that reads from untrusted input streams. Track `totalRead` and throw an exception immediately if the limit is exceeded.
## 2026-07-26 - 어드민 엔드포인트 권한 부여 취약점 수정
**Vulnerability:** AdminController 내 어드민 기능(job 삭제, 재시도, 목록 조회)에 대한 인증/인가(TenantAccessService)가 누락되어 누구나 접근 가능한 심각한 취약점이 발견되었습니다.
**Learning:** 중요 API 엔드포인트는 항상 강력한 권한 확인(TenantPermissions) 계층을 거쳐야 함을 확인했습니다.
**Prevention:** 모든 새로운 컨트롤러 및 민감한 API를 추가할 때는 TenantAccessService를 필수로 주입하고, 적절한 권한을 확인하는 로직을 반드시 포함해야 합니다.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public final class TenantPermissions {
*/
public static final String ANALYTICS_READ = "analytics:read";

/**
* Permission required for admin-specific operations.
*/
public static final String ADMIN_ACCESS = "admin:access";

private TenantPermissions() {
}
}
48 changes: 37 additions & 11 deletions src/main/java/com/clearfolio/viewer/controller/AdminController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

import com.clearfolio.viewer.api.AdminJobListResponse;
import com.clearfolio.viewer.auth.TenantAccessService;
import com.clearfolio.viewer.auth.TenantPermissions;
import com.clearfolio.viewer.model.ConversionJob;
import com.clearfolio.viewer.service.DocumentConversionService;
import com.clearfolio.viewer.service.RetryDeadLetterResult;
Expand All @@ -25,26 +29,37 @@
@RestController
public class AdminController {

private final DocumentConversionService conversionService;
/** Conversion service. */
private final DocumentConversionService conversionSvc;
/** Tenant access service. */
private final TenantAccessService tenantAccessSvc;
Comment on lines +32 to +35

/**
* Creates a controller for admin operations.
*
* @param conversionService conversion service
* @param tenantAccessService tenant access service
*/
public AdminController(DocumentConversionService conversionService) {
this.conversionService = conversionService;
public AdminController(
final DocumentConversionService conversionService,
final TenantAccessService tenantAccessService) {
this.conversionSvc = conversionService;
this.tenantAccessSvc = tenantAccessService;
}

/**
* Retrieves all conversion jobs, optionally filtered by dead-letter status.
*
* @param deadLettered optional filter for dead-lettered jobs
* @param headers request headers carrying tenant claims
* @return list of conversion jobs
*/
@GetMapping("/api/v1/admin/convert/jobs")
public AdminJobListResponse getAllJobs(@RequestParam(required = false) Boolean deadLettered) {
Iterable<ConversionJob> allJobs = conversionService.getAllJobs();
public AdminJobListResponse getAllJobs(
@RequestParam(required = false) final Boolean deadLettered,
@RequestHeader final HttpHeaders headers) {
tenantAccessSvc.require(headers, TenantPermissions.ADMIN_ACCESS);
Iterable<ConversionJob> allJobs = conversionSvc.getAllJobs();

if (deadLettered == null) {
return AdminJobListResponse.from(allJobs);
Expand All @@ -63,28 +78,39 @@ public AdminJobListResponse getAllJobs(@RequestParam(required = false) Boolean d
* Deletes a conversion job.
*
* @param jobId conversion job identifier
* @param headers request headers carrying tenant claims
* @return no content on success
*/
@DeleteMapping("/api/v1/admin/convert/jobs/{jobId}")
public ResponseEntity<Void> deleteJob(@PathVariable UUID jobId) {
conversionService.deleteJob(jobId);
public ResponseEntity<Void> deleteJob(
@PathVariable final UUID jobId,
@RequestHeader final HttpHeaders headers) {
tenantAccessSvc.require(headers, TenantPermissions.ADMIN_ACCESS);
conversionSvc.deleteJob(jobId);
return ResponseEntity.noContent().build();
}

/**
* Retries a dead-lettered conversion job.
*
* @param jobId conversion job identifier
* @param headers request headers carrying tenant claims
* @return accepted response on success
*/
@PostMapping("/api/v1/admin/convert/jobs/{jobId}/retry")
public ResponseEntity<Void> retryDeadLettered(@PathVariable UUID jobId) {
RetryDeadLetterResult result = conversionService.retryDeadLettered(jobId, "admin");
public ResponseEntity<Void> retryDeadLettered(
@PathVariable final UUID jobId,
@RequestHeader final HttpHeaders headers) {
tenantAccessSvc.require(headers, TenantPermissions.ADMIN_ACCESS);
RetryDeadLetterResult result =
conversionSvc.retryDeadLettered(jobId, "admin");
if (result == RetryDeadLetterResult.NOT_FOUND) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "job not found");
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "job not found");
}
if (result == RetryDeadLetterResult.NOT_ELIGIBLE) {
throw new ResponseStatusException(HttpStatus.CONFLICT, "job is not eligible for retry");
throw new ResponseStatusException(
HttpStatus.CONFLICT, "job is not eligible for retry");
}
return ResponseEntity.accepted().build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
package com.clearfolio.viewer.controller;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.Set;
import java.util.UUID;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.test.web.reactive.server.WebTestClient;

import com.clearfolio.viewer.auth.TenantAccessService;
import com.clearfolio.viewer.auth.TenantContext;
import com.clearfolio.viewer.auth.TenantPermissions;
import com.clearfolio.viewer.model.ConversionJob;
import com.clearfolio.viewer.service.DocumentConversionService;
import com.clearfolio.viewer.service.RetryDeadLetterResult;

class AdminControllerTest {

private DocumentConversionService conversionService;
private TenantAccessService tenantAccessService;
private WebTestClient webTestClient;
private AdminController controller;

@BeforeEach
void setUp() {
conversionService = mock(DocumentConversionService.class);
controller = new AdminController(conversionService);
tenantAccessService = mock(TenantAccessService.class);
when(tenantAccessService.require(any(), eq(TenantPermissions.ADMIN_ACCESS)))
.thenReturn(new TenantContext("admin", "admin", Set.of(TenantPermissions.ADMIN_ACCESS)));

controller = new AdminController(conversionService, tenantAccessService);
webTestClient = WebTestClient.bindToController(controller)
.controllerAdvice(new ApiExceptionHandler())
.build();
Expand All @@ -37,6 +48,9 @@ void getAllJobsReturnsAllJobsWhenNoFilterProvided() {

webTestClient.get()
.uri("/api/v1/admin/convert/jobs")
.header("X-Clearfolio-Tenant-Id", "admin")
.header("X-Clearfolio-Subject-Id", "admin")
.header("X-Clearfolio-Permissions", "admin:access")
.exchange()
.expectStatus().isOk()
.expectBody()
Expand All @@ -55,6 +69,9 @@ void getAllJobsFiltersByDeadLetteredTrue() {

webTestClient.get()
.uri("/api/v1/admin/convert/jobs?deadLettered=true")
.header("X-Clearfolio-Tenant-Id", "admin")
.header("X-Clearfolio-Subject-Id", "admin")
.header("X-Clearfolio-Permissions", "admin:access")
.exchange()
.expectStatus().isOk()
.expectBody()
Expand All @@ -72,6 +89,9 @@ void getAllJobsFiltersByDeadLetteredFalse() {

webTestClient.get()
.uri("/api/v1/admin/convert/jobs?deadLettered=false")
.header("X-Clearfolio-Tenant-Id", "admin")
.header("X-Clearfolio-Subject-Id", "admin")
.header("X-Clearfolio-Permissions", "admin:access")
.exchange()
.expectStatus().isOk()
.expectBody()
Expand All @@ -85,6 +105,9 @@ void deleteJobReturnsNoContent() {

webTestClient.delete()
.uri("/api/v1/admin/convert/jobs/" + jobId)
.header("X-Clearfolio-Tenant-Id", "admin")
.header("X-Clearfolio-Subject-Id", "admin")
.header("X-Clearfolio-Permissions", "admin:access")
.exchange()
.expectStatus().isNoContent();
}
Expand All @@ -96,6 +119,9 @@ void retryDeadLetteredReturnsAcceptedWhenAccepted() {

webTestClient.post()
.uri("/api/v1/admin/convert/jobs/" + jobId + "/retry")
.header("X-Clearfolio-Tenant-Id", "admin")
.header("X-Clearfolio-Subject-Id", "admin")
.header("X-Clearfolio-Permissions", "admin:access")
.exchange()
.expectStatus().isAccepted();
}
Expand All @@ -107,6 +133,9 @@ void retryDeadLetteredReturnsNotFoundWhenNotFound() {

webTestClient.post()
.uri("/api/v1/admin/convert/jobs/" + jobId + "/retry")
.header("X-Clearfolio-Tenant-Id", "admin")
.header("X-Clearfolio-Subject-Id", "admin")
.header("X-Clearfolio-Permissions", "admin:access")
.exchange()
.expectStatus().isNotFound();
}
Expand All @@ -118,6 +147,9 @@ void retryDeadLetteredReturnsConflictWhenNotEligible() {

webTestClient.post()
.uri("/api/v1/admin/convert/jobs/" + jobId + "/retry")
.header("X-Clearfolio-Tenant-Id", "admin")
.header("X-Clearfolio-Subject-Id", "admin")
.header("X-Clearfolio-Permissions", "admin:access")
.exchange()
.expectStatus().isEqualTo(409); // isConflict() isn't always available depending on spring-test version, so using isEqualTo(409) is safer
}
Expand Down
Loading