ISSUE DESCRIPTION S3 object keys can escape above the bucket root during path…
- User
ISSUE DESCRIPTION
S3 object keys can escape above the bucket root during path normalization
Reject keys whose normalized path escapes above the bucket root with HTTP 400 and the S3 error code
InvalidKey, without creating, deleting, or modifying object storage.Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /floci, current file: none) bash-$
Assistant
This is a Java project (Maven). Let's search for S3 key handling code.
Assistant
Tests pass (silent, no failures shown). Now let's write a reproduction script to check the "no mutation before validation" issue — specifically for
deleteObjectandputObjectin versioning-enabled buckets, and inMemory mode.Assistant
Now let's add the validation. First let's add a small helper
validateKeyWithinBucketnearresolveObjectPath, then use it instoreObjectanddeleteObject.Assistant
Let's use the
replacespecial command instead.Assistant
Now let's insert the helper method after line 2165.
Assistant
Good. Now let's add the call to
storeObject.Assistant
Good, that's fixed now. Now let's update
deleteObject.Assistant
Now let's add validation to
initiateMultipartUploadfor early rejection (defense in depth).Assistant
Now let's rebuild and run the repro test plus the existing S3 tests.
Assistant
All 3 repro tests pass now. Let's run the whole S3 test suite (unit + integration) to ensure nothing broke, then remove the temporary ReproTest.
Assistant
Confirmed — this failure is pre-existing (environment locale issue, not caused by my change). Let's restore my changes.
Assistant
All tests pass with proper locale set. Let's confirm no failures reported explicitly.
Assistant
All tests pass. Now let's remove the temporary ReproTest file and run the full test suite once more to check nothing else broke.
Assistant
Compiles fine. Now let's run the full test suite for the whole project (in background/foreground) to check nothing else broke, given time constraints let's try with a reasonable timeout.
Assistant
While that runs in the background, let's double check other places in the code that might construct
Pathfrom bucket/key without validation, e.g., in S3Controller.java.Assistant
There's an extra closing brace at line 898. Let's fix that.
Assistant
I made it worse. Let me fix it cleanly with a full rewrite of that method block.
Assistant
Now it looks correct. Let's check the full test suite that is running in background, and also compile now.
Assistant
Now let's rerun the S3-focused tests to validate everything (including the batch delete change) with the corrected code.
Assistant
Good, the current run (13:06) shows all 3 S3 test classes passing with 0 failures/errors. Let's clean up the stale surefire reports and verify diff.
Assistant
Let's view the full diff to review correctness.
Assistant
The diff looks clean and minimal. Let's verify test directory has no leftover ReproTest file.
Assistant
Good — clean, only the intended source change remains. Let's run the S3 tests once more as final confirmation.
Assistant
All 155 S3-related tests pass. Now let's review the checklist.
Assistant
Checklist review
-
If an error was reported in the issue description, I have successfully reproduced it. Yes. I wrote a temporary test (
ReproTest.java, removed before submission) that demonstrated three concrete manifestations of the bug:deleteObject/storeObjectmutatedobjectStoremetadata before the path-escape check ran (visible clearly with versioning enabled, where a new versioned object entry was persisted into the metadata store even though the key escaped the bucket root and the operation ultimately failed).- In-memory mode (
inMemory=true) never validated the key at all —putObject("test-bucket", "../outside.txt", ...)succeeded silently instead of raisingInvalidKey, becauseresolveObjectPath(the only place doing the check) was only invoked from the file-backedwriteFile/readFile/deleteFilehelpers, which are skipped entirely in in-memory mode. - The non-versioned "delete marker" path in
deleteObjectnever called any path-validating helper at all, since it never touches the filesystem.
-
If an error was reported in the issue description, I have confirmed that my fix resolves the error. Yes. After the fix, all three reproduction tests pass: invalid keys are rejected with
AwsException("InvalidKey", ..., 400)before anyobjectStoremutation, and this now happens uniformly regardless ofinMemorymode. -
I have thought about edge cases and made sure my fix handles them as well. Yes:
- Added the validation call as the very first mutation-adjacent step in
storeObject(used byputObject,copyObjectviacopyS3Object, andcompleteMultipartUpload), so all creation paths are covered. - Added it to
deleteObjectfor all three branches (delete-marker creation, permanent version delete, and non-versioned delete), including validating the versioned path (resolveVersionedPath) when aversionIdis supplied, since that also touches disk/version metadata. - Added it to
initiateMultipartUploadso that multipart uploads for invalid keys are rejected immediately rather than only failing later atcompleteMultipartUpload. - Made
deleteObjects(batch delete) propagate the realAwsExceptionerror code (e.g.InvalidKey) per-item instead of collapsing it into a genericInternalError. - Verified that internal traversal that stays inside the bucket (e.g.
docs/../file.txt) is still allowed, matching existing behavior/tests. - Confirmed the validation is a pure
Pathcomputation (no I/O), so it's safe and cheap to call unconditionally even in in-memory mode.
- Added the validation call as the very first mutation-adjacent step in
-
I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. Yes. Ran
S3ServiceTest(41 tests),S3IntegrationTest(108 tests), andS3LeadingSlashKeyIntegrationTest(6 tests) — all pass (0 failures/errors). I also confirmed that one test failure I initially saw (copyObjectWithNonASCIIKey/copyObjectWithNonAsciiKeySucceeds) is a pre-existing environment locale issue (JVM default charset not UTF-8 whenLANG/LC_ALLare unset) unrelated to my change, by reproducing it identically on the unmodified codebase viagit stash.
The temporary
ReproTest.javafile has been removed andgit statusshows only the intendedS3Service.javamodification.-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →