Repository navigation
fix(security): authenticate artifact tokens before object lookup on current main #431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
seonghobae
wants to merge
7
commits into
main
Choose a base branch
from
fix/artifact-token-precondition-main-20260813-r2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cd5a8a9
test(security): reject invalid artifact tokens before object lookup
seonghobae a0a097f
fix(security): split artifact token preauthorization
seonghobae 3751c1b
fix(security): authenticate artifact token before object lookup
seonghobae 92ee302
test(security): preserve authenticated downstream artifact cases
seonghobae 2f16af8
test(security): cover post-lookup artifact token binding failure
seonghobae 65d986f
Merge branch 'main' into fix/artifact-token-precondition-main-2026081…
opencode-agent[bot] 2a1186c
Merge branch 'main' into fix/artifact-token-precondition-main-2026081…
opencode-agent[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/test/java/com/clearfolio/viewer/controller/ArtifactControllerPostLookupBindingTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package com.clearfolio.viewer.controller; | ||
|
|
||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.UUID; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.test.web.reactive.server.WebTestClient; | ||
|
|
||
| import com.clearfolio.viewer.api.ArtifactLinkRequest; | ||
| import com.clearfolio.viewer.api.ArtifactLinkResponse; | ||
| import com.clearfolio.viewer.artifact.ArtifactLinkService; | ||
| import com.clearfolio.viewer.artifact.ArtifactStore; | ||
| import com.clearfolio.viewer.artifact.InMemoryArtifactStore; | ||
| 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; | ||
|
|
||
| /** | ||
| * Verifies that token preauthorization does not replace the post-lookup tenant | ||
| * and artifact binding check performed immediately before bytes are returned. | ||
| */ | ||
| class ArtifactControllerPostLookupBindingTest { | ||
|
|
||
| @Test | ||
| void rejectsPreauthorizedTokenWhenResolvedJobBelongsToAnotherTenant() { | ||
| UUID docId = UUID.randomUUID(); | ||
| byte[] pdfBytes = new byte[] {0, 1, 2, 3}; | ||
| ArtifactStore artifactStore = new InMemoryArtifactStore(); | ||
| artifactStore.putPdf(docId, pdfBytes); | ||
| ArtifactLinkService artifactLinkService = new ArtifactLinkService(artifactStore, "test-secret"); | ||
|
|
||
| ConversionJob authorizedJob = succeededJob(docId, TenantContext.DEMO_TENANT_ID); | ||
| ArtifactLinkResponse link = artifactLinkService.createLink( | ||
| authorizedJob, | ||
| tenantContext(), | ||
| ArtifactLinkRequest.viewerPreview() | ||
| ); | ||
|
|
||
| DocumentConversionService conversionService = mock(DocumentConversionService.class); | ||
| when(conversionService.getJob(docId)).thenReturn(Optional.of(succeededJob(docId, "other-tenant"))); | ||
| ArtifactController controller = new ArtifactController( | ||
| conversionService, | ||
| artifactStore, | ||
| artifactLinkService, | ||
| new TenantAccessService() | ||
| ); | ||
|
|
||
| WebTestClient.bindToController(controller) | ||
| .build() | ||
| .get() | ||
| .uri(link.artifactUrl()) | ||
| .exchange() | ||
| .expectStatus().isForbidden(); | ||
| } | ||
|
|
||
| private static TenantContext tenantContext() { | ||
| return new TenantContext( | ||
| TenantContext.DEMO_TENANT_ID, | ||
| TenantContext.DEMO_SUBJECT_ID, | ||
| Set.of(TenantPermissions.ARTIFACT_LINK_CREATE, TenantPermissions.VIEWER_READ) | ||
| ); | ||
| } | ||
|
|
||
| private static ConversionJob succeededJob(UUID docId, String tenantId) { | ||
| ConversionJob job = new ConversionJob( | ||
| docId, | ||
| tenantId, | ||
| TenantContext.DEMO_SUBJECT_ID, | ||
| "report.docx", | ||
| "application/octet-stream", | ||
| "hash", | ||
| 4L, | ||
| 1 | ||
| ); | ||
| job.markSucceeded("/artifacts/" + docId + ".pdf", "done"); | ||
| return job; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/test/java/com/clearfolio/viewer/controller/ArtifactControllerTokenPreconditionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package com.clearfolio.viewer.controller; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verifyNoInteractions; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
|
|
||
| import com.clearfolio.viewer.artifact.ArtifactLinkService; | ||
| import com.clearfolio.viewer.artifact.ArtifactStore; | ||
| import com.clearfolio.viewer.auth.TenantAccessService; | ||
| import com.clearfolio.viewer.service.DocumentConversionService; | ||
|
|
||
| /** | ||
| * Proves that the public artifact route authenticates supplied credentials before | ||
| * reading conversion-job or artifact state that could become an existence oracle. | ||
| */ | ||
| class ArtifactControllerTokenPreconditionTest { | ||
|
|
||
| @Test | ||
| void missingArtifactTokenIsRejectedBeforeSensitiveObjectLookup() { | ||
| Fixture fixture = fixture(); | ||
|
|
||
| ResponseEntity<byte[]> response = fixture.controller().getPdf( | ||
| UUID.randomUUID(), null, null, null, null).block(); | ||
|
|
||
| assertUnauthorizedWithoutObjectLookup(response, fixture); | ||
| } | ||
|
|
||
| @Test | ||
| void unsupportedAuthorizationSchemeIsRejectedBeforeSensitiveObjectLookup() { | ||
| Fixture fixture = fixture(); | ||
|
|
||
| ResponseEntity<byte[]> response = fixture.controller().getPdf( | ||
| UUID.randomUUID(), null, " ", "Basic opaque-credential", null).block(); | ||
|
|
||
| assertUnauthorizedWithoutObjectLookup(response, fixture); | ||
| } | ||
|
|
||
| @Test | ||
| void invalidQueryTokenIsRejectedBeforeSensitiveObjectLookup() { | ||
| Fixture fixture = fixture(); | ||
|
|
||
| ResponseEntity<byte[]> response = fixture.controller().getPdf( | ||
| UUID.randomUUID(), null, "opaque-present-token", null, null).block(); | ||
|
|
||
| assertUnauthorizedWithoutObjectLookup(response, fixture); | ||
| } | ||
|
|
||
| @Test | ||
| void invalidBearerTokenIsRejectedBeforeSensitiveObjectLookup() { | ||
| Fixture fixture = fixture(); | ||
|
|
||
| ResponseEntity<byte[]> response = fixture.controller().getPdf( | ||
| UUID.randomUUID(), null, null, "Bearer opaque-present-token", null).block(); | ||
|
|
||
| assertUnauthorizedWithoutObjectLookup(response, fixture); | ||
| } | ||
|
|
||
| private static Fixture fixture() { | ||
| DocumentConversionService conversionService = mock(DocumentConversionService.class); | ||
| ArtifactStore artifactStore = mock(ArtifactStore.class); | ||
| ArtifactLinkService artifactLinkService = new ArtifactLinkService(artifactStore, "test-secret"); | ||
| ArtifactController controller = new ArtifactController( | ||
| conversionService, | ||
| artifactStore, | ||
| artifactLinkService, | ||
| mock(TenantAccessService.class) | ||
| ); | ||
| return new Fixture(controller, conversionService, artifactStore); | ||
| } | ||
|
|
||
| private static void assertUnauthorizedWithoutObjectLookup( | ||
| ResponseEntity<byte[]> response, | ||
| Fixture fixture) { | ||
| assertNotNull(response); | ||
| assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); | ||
| verifyNoInteractions(fixture.conversionService(), fixture.artifactStore()); | ||
| } | ||
|
|
||
| private record Fixture( | ||
| ArtifactController controller, | ||
| DocumentConversionService conversionService, | ||
| ArtifactStore artifactStore) { | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.