Repository navigation
fix(reliability): roll back partial filesystem artifact writes #355
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
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f498256
test(artifact): reject orphaned partial writes
seonghobae 74e77bc
test(artifact): preserve rollback failure evidence
seonghobae 90ec7d4
fix(artifact): roll back failed sidecar writes
seonghobae e63d818
test(reliability): require independent artifact rollback cleanup
seonghobae 6d6839b
fix(reliability): attempt each partial artifact cleanup
seonghobae 394db4a
test(artifact): preserve committed bytes on failed replacement
seonghobae a6372a3
fix(artifact): restore committed state after failed replacement
seonghobae e218b05
test(artifact): cover snapshot read failure
seonghobae a20503c
test(security): require filesystem artifact path confinement
seonghobae 1643123
fix(security): confine filesystem artifact paths
seonghobae b58e1b1
test(reliability): verify metadata rollback bytes
seonghobae 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
198 changes: 198 additions & 0 deletions
198
src/test/java/com/clearfolio/viewer/artifact/FileSystemArtifactStorePartialWriteTest.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,198 @@ | ||
| package com.clearfolio.viewer.artifact; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertArrayEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| /** | ||
| * Verifies that a failed multi-file artifact write does not publish orphaned | ||
| * PDF bytes without the matching metadata sidecar. | ||
| */ | ||
| class FileSystemArtifactStorePartialWriteTest { | ||
|
|
||
| @TempDir | ||
| Path temporaryDirectory; | ||
|
|
||
| @Test | ||
| void metadataWriteFailureRollsBackNewArtifactBytes() throws Exception { | ||
| Path root = temporaryDirectory.resolve("artifacts"); | ||
| UUID docId = UUID.randomUUID(); | ||
| FileSystemArtifactStore store = new FileSystemArtifactStore( | ||
| root, | ||
| (path, bytes) -> { | ||
| if (path.getFileName().toString().endsWith(".meta.properties")) { | ||
| throw new IOException("metadata disk full"); | ||
| } | ||
| Files.write(path, bytes); | ||
| }, | ||
| Files::readAllBytes | ||
| ); | ||
|
|
||
| IllegalStateException error = assertThrows( | ||
| IllegalStateException.class, | ||
| () -> store.putPdf(docId, "%PDF-1.7\npartial-write".getBytes(StandardCharsets.UTF_8)) | ||
| ); | ||
|
|
||
| assertEquals("failed to persist artifact for docId " + docId, error.getMessage()); | ||
| assertTrue(Files.notExists(root.resolve(docId + ".pdf"))); | ||
| assertTrue(Files.notExists(root.resolve(docId + ".meta.properties"))); | ||
| assertEquals(Optional.empty(), store.getPdf(docId)); | ||
| } | ||
|
|
||
| @Test | ||
| void failedReplacementPreservesPreviouslyCommittedArtifactAcrossRestart() throws Exception { | ||
| Path root = temporaryDirectory.resolve("replacement"); | ||
| UUID docId = UUID.randomUUID(); | ||
| byte[] originalPdf = "%PDF-1.7\noriginal".getBytes(StandardCharsets.UTF_8); | ||
| byte[] replacementPdf = "%PDF-1.7\nreplacement".getBytes(StandardCharsets.UTF_8); | ||
| new FileSystemArtifactStore(root).putPdf(docId, originalPdf); | ||
| byte[] originalMetadata = Files.readAllBytes(root.resolve(docId + ".meta.properties")); | ||
|
|
||
| FileSystemArtifactStore failingStore = new FileSystemArtifactStore( | ||
| root, | ||
| (path, bytes) -> { | ||
| if (path.getFileName().toString().endsWith(".meta.properties")) { | ||
| throw new IOException("metadata disk full"); | ||
| } | ||
| Files.write(path, bytes); | ||
| }, | ||
| Files::readAllBytes | ||
| ); | ||
|
|
||
| assertThrows(IllegalStateException.class, () -> failingStore.putPdf(docId, replacementPdf)); | ||
|
|
||
| assertTrue(Files.exists(root.resolve(docId + ".pdf"))); | ||
| assertTrue(Files.exists(root.resolve(docId + ".meta.properties"))); | ||
| assertArrayEquals(originalMetadata, Files.readAllBytes(root.resolve(docId + ".meta.properties"))); | ||
| FileSystemArtifactStore restartedStore = new FileSystemArtifactStore(root); | ||
| assertArrayEquals(originalPdf, restartedStore.getPdf(docId).orElseThrow()); | ||
| } | ||
|
|
||
| @Test | ||
| void rollbackFailureIsRetainedAsSuppressedEvidence() throws Exception { | ||
| Path root = temporaryDirectory.resolve("rollback-failure"); | ||
| UUID docId = UUID.randomUUID(); | ||
| FileSystemArtifactStore store = new FileSystemArtifactStore( | ||
| root, | ||
| (path, bytes) -> { | ||
| if (path.getFileName().toString().endsWith(".pdf")) { | ||
| Files.createDirectory(path); | ||
| Files.writeString(path.resolve("retained"), "evidence"); | ||
| return; | ||
| } | ||
| throw new IOException("metadata disk full"); | ||
| }, | ||
| Files::readAllBytes | ||
| ); | ||
|
|
||
| IllegalStateException error = assertThrows( | ||
| IllegalStateException.class, | ||
| () -> store.putPdf(docId, "%PDF-1.7\nrollback-failure".getBytes(StandardCharsets.UTF_8)) | ||
| ); | ||
|
|
||
| assertEquals("failed to persist artifact for docId " + docId, error.getMessage()); | ||
| assertEquals(1, error.getCause().getSuppressed().length); | ||
| assertTrue(Files.exists(root.resolve(docId + ".pdf").resolve("retained"))); | ||
| } | ||
|
|
||
| @Test | ||
| void metadataRollbackFailureStillAttemptsPdfCleanup() throws Exception { | ||
| Path root = temporaryDirectory.resolve("independent-rollback"); | ||
| UUID docId = UUID.randomUUID(); | ||
| Path pdfPath = root.resolve(docId + ".pdf"); | ||
| Path metadataPath = root.resolve(docId + ".meta.properties"); | ||
| FileSystemArtifactStore store = new FileSystemArtifactStore( | ||
| root, | ||
| (path, bytes) -> { | ||
| if (path.equals(pdfPath)) { | ||
| Files.write(path, bytes); | ||
| return; | ||
| } | ||
| Files.createDirectory(metadataPath); | ||
| Files.writeString(metadataPath.resolve("retained"), "evidence"); | ||
| throw new IOException("metadata disk full"); | ||
| }, | ||
| Files::readAllBytes | ||
| ); | ||
|
|
||
| IllegalStateException error = assertThrows( | ||
| IllegalStateException.class, | ||
| () -> store.putPdf(docId, "%PDF-1.7\nindependent-rollback".getBytes(StandardCharsets.UTF_8)) | ||
| ); | ||
|
|
||
| assertEquals(1, error.getCause().getSuppressed().length); | ||
| assertTrue(Files.exists(metadataPath.resolve("retained"))); | ||
| assertTrue(Files.notExists(pdfPath)); | ||
| } | ||
|
|
||
| @Test | ||
| void unreadableExistingArtifactSnapshotFailsClosedBeforeReplacement() throws Exception { | ||
| Path root = temporaryDirectory.resolve("snapshot-failure"); | ||
| Files.createDirectories(root); | ||
| UUID docId = UUID.randomUUID(); | ||
| Path pdfPath = root.resolve(docId + ".pdf"); | ||
| Files.createDirectory(pdfPath); | ||
| Files.writeString(pdfPath.resolve("retained"), "evidence"); | ||
| FileSystemArtifactStore store = new FileSystemArtifactStore(root); | ||
|
|
||
| IllegalStateException error = assertThrows( | ||
| IllegalStateException.class, | ||
| () -> store.putPdf(docId, "%PDF-1.7\nreplacement".getBytes(StandardCharsets.UTF_8)) | ||
| ); | ||
|
|
||
| assertEquals("failed to snapshot existing artifact for docId " + docId, error.getMessage()); | ||
| assertTrue(Files.isDirectory(pdfPath)); | ||
| assertTrue(Files.exists(pdfPath.resolve("retained"))); | ||
| assertTrue(Files.notExists(root.resolve(docId + ".meta.properties"))); | ||
| } | ||
|
|
||
| @Test | ||
| void nullDocumentIdentityFailsClosedBeforeFilesystemAccess() { | ||
| Path root = temporaryDirectory.resolve("null-authority"); | ||
| FileSystemArtifactStore store = new FileSystemArtifactStore(root); | ||
| byte[] pdf = "%PDF-1.7\nnull-authority".getBytes(StandardCharsets.UTF_8); | ||
|
|
||
| IllegalArgumentException putError = assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> store.putPdf(null, pdf) | ||
| ); | ||
| IllegalArgumentException getError = assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> store.getPdf(null) | ||
| ); | ||
| IllegalArgumentException deleteError = assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> store.deletePdf(null) | ||
| ); | ||
|
|
||
| assertEquals("docId is required", putError.getMessage()); | ||
| assertEquals("docId is required", getError.getMessage()); | ||
| assertEquals("docId is required", deleteError.getMessage()); | ||
| assertTrue(Files.notExists(root.resolve("null.pdf"))); | ||
| assertTrue(Files.notExists(root.resolve("null.meta.properties"))); | ||
| } | ||
|
|
||
| @Test | ||
| void artifactPathResolutionRejectsEscapesFromConfiguredRoot() { | ||
| Path root = temporaryDirectory.resolve("path-confinement"); | ||
| UUID docId = UUID.randomUUID(); | ||
|
|
||
| IllegalArgumentException error = assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> FileSystemArtifactStore.resolveArtifactPath(root, docId, "/../../outside") | ||
| ); | ||
|
|
||
| assertEquals("artifact path must remain within configured root", error.getMessage()); | ||
| } | ||
|
seonghobae marked this conversation as resolved.
|
||
| } | ||
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.