ISSUE DESCRIPTION [BUG] S3 dot-dot object requests return InvalidKey Service Amazon S3…
- User
ISSUE DESCRIPTION
[BUG] S3 dot-dot object requests return InvalidKey
Service
AWS API Action
Expected behavior
AWS behavior rejects object requests whose key causes a dot-dot path segment to appear in the request URI with HTTP 400 Bad Request and an empty response body.
Actual behavior
Floci returns an
InvalidKeyerror body:An error occurred (InvalidKey) when calling the PutObject operation: The specified key is invalid.Reproduction
printf 'safe-data' > body.txt aws --endpoint-url http://localhost:4566 s3api create-bucket \ --bucket raw-path-bucket aws --endpoint-url http://localhost:4566 s3api put-object \ --bucket raw-path-bucket \ --key ../bad.txt \ --body body.txtEnvironment
- Floci version / image tag: local Docker image built from main commit
a7b0826c - Java SDK version (if applicable): N/A; verified with AWS CLI
- How you're running Floci (Docker / native /
mvn quarkus:dev): Docker
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. - Floci version / image tag: local Docker image built from main commit
Assistant
I'll start by exploring the repository structure to understand the codebase.
Assistant
I'll start by exploring the repository structure.
Assistant
Let me understand the issue. The issue says that AWS rejects dot-dot path segments with HTTP 400 and an empty response body. But Floci returns an
InvalidKeyerror body with the message "The specified key is invalid."So the fix is: when a dot-dot path segment appears in the request URI, the response should be HTTP 400 with an empty body, not an
InvalidKeyerror body.Let me look at the
validateRawUrimethod more carefully, and how AwsException is mapped to a response.Assistant
Now let me understand how
validateRawUriis being called and what happens with the exception. Looking at line 156, 243, 297, thevalidateRawUri()is called inside try/catch blocks that catch AwsException and returnxmlErrorResponse(e). So the InvalidKey exception would produce an XML error body.But wait, the issue says the AWS behavior is to return HTTP 400 with an empty response body. So the fix is to make the dot-dot path segment rejection return an empty body, not an XML error body.
Let me look more at the structure. Let me look at lines 440-470 (the PUT object path) and lines 560-580 etc.
Assistant
Now I understand the issue. When a dot-dot path segment appears in the request URI, AWS returns HTTP 400 with an empty response body. But Floci returns an error body (XML error response).
Assistant
Now I understand the issue. When a dot-dot path segment appears in the request URI, AWS returns HTTP 400 with an empty response body. But Floci returns an
InvalidKeyerror body. ThevalidateRawUri()andvalidateKeyNoTraversal()methods throwAwsException("InvalidKey", ...)which gets turned into XML/JSON error bodies.Let me look at the PUT and DELETE object handlers more carefully.
Assistant
Now I understand the issue. For object operations,
extractObjectKeycallsvalidateKeyNoTraversal, which throwsAwsException("InvalidKey", ...). This is caught bycatch (AwsException e) { return xmlErrorResponse(e); }which produces an XML error body. AWS instead returns HTTP 400 with an empty body.Let me check the DELETE object handler and the S3Service validation too.
Assistant
Let me check the S3Service validation lines 2190-2230.
Assistant
Let me check line 2166 where
validateKeyNoTraversalis also called.Assistant
OK, so
validateKeyNoTraversalis also called at line 2166 for presigned POST operations. In this case, the key comes from a form field, not from the URI. The dot-dot traversal would be in the key field of the POST form.For presigned POST, the dot-dot is in the form field, not in the URI path. So this is a slightly different case. Let me think about whether AWS returns empty body for this too. Actually, the issue specifically mentions "dot-dot path segment to appear in the request URI". For presigned POST, the key is in the form body, not in the URI. So maybe I should only change
validateRawUri()and theextractObjectKey→validateKeyNoTraversalpath, not the presigned POST path.Hmm, but
validateKeyNoTraversalis called from bothextractObjectKeyand from the presigned POST handler (line 2166). If I change to throw the special exception, it would affect both.Assistant
Let me check the
handlePresignedPostcatch block.Assistant
Let me look at the existing tests related to dot-dot / path traversal to understand expected behavior.
Assistant
Let me look at the compatibility tests to understand expected behavior better.
Assistant
No compatibility tests about dot-dot traversal specifically. Let me look at the S3Test compatibility test to see if there's anything about path traversal.
Assistant
Let me check if there are tests for presigned POST with dot-dot keys, and look at the presigned post tests.
Assistant
Assistant
Let me think about the cleanest approach. I'll create a dedicated exception subclass
S3DotDotPathExceptionthat extendsAwsException, and then modify thexmlErrorResponsemethod to return an empty body when it receives this exception. This way, all the existing catch blocks that callxmlErrorResponse(e)will automatically return empty 400 bodies for dot-dot traversal errors.Wait, but
xmlErrorResponseis also called directly with explicitAwsExceptioninstances (e.g.,xmlErrorResponse(new AwsException(...))). Those won't be traversal exceptions, so they're fine.But there's also the
AwsExceptionMapper(global JAX-RS provider) which catches uncaughtAwsException. If a dot-dot exception escapes the catch blocks, it would go to the mapper and produce a JSON body. Let me check: isvalidateRawUrialways inside a try/catch? Line 297 (listObjects) - let me check.Assistant
So
validateRawUri()at line 297 is outside the try/catch. If it throws anAwsException, it would propagate to the globalAwsExceptionMapperwhich produces a JSON body. Let me check the rest of listObjects to see if there's a catch.Assistant
Assistant
Let me look at the presigned post test file to understand the structure.
Assistant
No tests for dot-dot in presigned POST keys. So I have freedom there.
Now, for the presigned POST case, the key with dot-dot is in the form body. The issue is specifically about "dot-dot path segment to appear in the request URI". For presigned POST, the key is NOT in the request URI - it's in the form body. So AWS would likely return a different error (InvalidKey with body). I'll keep the presigned POST behavior as-is (returning InvalidKey error body).
So my plan:
- Create
S3PathTraversalException extends AwsException(a marker exception for URI path traversal). - In
validateRawUri(), throwS3PathTraversalExceptioninstead ofAwsException("InvalidKey", ...).
- Create
Assistant
Assistant
Now let me create the
S3PathTraversalExceptionclass.Assistant
Now let me update the
S3Controller. First, let me updatevalidateRawUri()to throwS3PathTraversalExceptionfor dot-dot, andvalidateKeyNoTraversalto support both URI-based (empty body) and form-based (InvalidKey body) modes.Assistant
Now let me update the
validateRawUri()method to throwS3PathTraversalExceptionfor dot-dot, andvalidateKeyNoTraversalto support a flag for whether to throw the traversal exception or the regular InvalidKey.Let me view the
extractObjectKeyandvalidateKeyNoTraversalmethods again.Assistant
Now let me make the changes. First, I'll update
validateKeyNoTraversalto accept a booleanfromUriparameter, andextractObjectKeyto passtrue, while the presigned POST handler passesfalse.Let me update
validateKeyNoTraversal:Assistant
Now update
extractObjectKeyto passtrueforfromUri:Assistant
Now update
validateRawUri()to throwS3PathTraversalExceptionfor dot-dot:Assistant
Now I need to update the
xmlErrorResponsemethod to return an empty body forS3PathTraversalException. Let me view it.Assistant
Now let me update
xmlErrorResponseto return an empty body forS3PathTraversalException:Assistant
Now I need to handle the case where
validateRawUri()is called outside a try/catch block (line 297, listObjects). IfS3PathTraversalExceptionis thrown there, it would escape to the globalAwsExceptionMapperwhich returns a JSON body. I need to handle this.Let me check line 297 again - it's at the start of
listObjects, before the try block. Let me move it inside the try block, or add a separate handling.Actually, the cleanest fix is to move the
validateRawUri()call inside the try block forlistObjects. Let me check line 297.Assistant
I need to navigate to the line first.
Assistant
Now I also need to handle the
AwsExceptionMapperfor the case whereS3PathTraversalExceptionescapes to it (although now all call sites are inside try/catch). But to be safe, let me also handle it in the globalAwsExceptionMapper. Actually, sinceS3PathTraversalException extends AwsException, and the global mapper handles allAwsException, I should add the empty-body handling there too. But the global mapper returns JSON for all services... Adding S3-specific handling there is a bit odd.Actually, let me check: are there any other paths where
S3PathTraversalExceptioncould escape to the global mapper? All the S3Controller methods now catchAwsExceptionand callxmlErrorResponse. Let me verify by checking the headObject method catch block.Assistant
Good, all the object-level methods catch
AwsExceptionand callxmlErrorResponse(e). Now, there's one concern: thefullObjectResponseuses aStreamingOutputwhich writes in a different context. But theextractObjectKeyis called before that, so the exception would be thrown and caught before the streaming starts. Good.Now, let me also consider the
S3Service.resolveObjectPathandresolveVersionedPathmethods (lines 2196, 2213). These throwAwsException("InvalidKey", ...)when the resolved path escapes the bucket dir. But these are at the service layer, called afterextractObjectKeyhas already validated. SinceextractObjectKeyalready validates and throwsS3PathTraversalExceptionfor dot-dot, the service-layer check is a secondary defense. The service-layer check would only trigger if somehow a dot-dot key got past the controller validation. For consistency, should I also change these to throwS3PathTraversalException?Actually, the service-layer
resolveObjectPathis also called directly fromS3ServiceTest(line 569, 574) which expectsInvalidKey. Let me check those tests.Assistant
The
S3ServiceTest.resolvePathWithTraversalThrowstest expectsInvalidKeyerror code when callings3Service.putObjectdirectly (not through the controller). This is a unit test of the service layer. Since the service layer is called directly (not through the HTTP URI), the dot-dot is NOT in the request URI. So the service-layer behavior should remain .Assistant
Let me add an
isEmptyBody()method toAwsExceptionthat defaults tofalse, and override it inS3PathTraversalExceptionto returntrue. Then the globalAwsExceptionMappercan check this and return an empty body.Assistant
Now update
S3PathTraversalExceptionto overrideisEmptyBody():Assistant
Now update the global
AwsExceptionMapperto return an empty body whenisEmptyBody()returns true:Assistant
Now let me also update the
xmlErrorResponsein S3Controller to useisEmptyBody()instead ofinstanceoffor consistency:Assistant
Now the
S3Controllerno longer directly referencesS3PathTraversalExceptioninxmlErrorResponse, but it still references it invalidateRawUriandvalidateKeyNoTraversal. The import forS3PathTraversalExceptionis in the same package, so no import needed. Good.Now let me build the project to make sure it compiles.
Assistant
Compilation succeeds. Now let me run the existing tests that might be affected. Let me run the S3 integration tests related to path traversal.
Assistant
Four of the five failures are the expected behavior changes (the dot-dot tests now get empty body instead of InvalidKey, which is the correct fix per the issue). These are tests that need updating to reflect the new behavior, and per the instructions, I should ignore them.
But there's an unexpected failure:
copyObjectWithNonAsciiKeySucceeds:362— expected 200 but got 400. This is a regression I need to investigate! Let me look at that test.Assistant
The
copyObjectWithNonAsciiKeySucceedsfailure could be a regression. Let me check if this test was already failing before my changes by stashing and running it.Assistant
The stash kept the new untracked file. Let me pop the stash first and handle this differently.
Assistant
Let me temporarily move the new file and stash to test the original behavior.
Assistant
The
copyObjectWithNonAsciiKeySucceedstest was already failing before my changes. It's a pre-existing failure, not a regression. Let me restore my changes.Assistant
Good. The
copyObjectWithNonAsciiKeySucceedsis a pre-existing failure unrelated to my change. The other 4 failures are the expected behavior changes (dot-dot now returns empty body instead of InvalidKey).Now let me also check the
S3ServiceTestto make sure the service-layer tests still pass.Assistant
The
S3ServiceTest.copyObjectWithNonASCIIKeyerror is a pre-existing issue (related to the non-ASCII key problem, unrelated to my change - it's a filesystem encoding issue). The traversal tests inS3ServiceTest(which I need to pass) passed since the service layer still throwsInvalidKey.Let me verify that the traversal-related tests in
S3ServiceTestspecifically pass:Assistant
Both service-layer traversal tests pass. Now let me create a reproduction script to verify the fix works as described in the issue. Let me write a small test to verify the behavior: PUT object with
../bad.txtkey returns 400 with empty body.Let me check how the tests are set up (RestAssured config).
Assistant
Let me create a temporary test to verify the fix matches the issue's expected behavior (PUT and DELETE with
../bad.txtkey → 400 with empty body). I'll create it as a temporary file and remove it after.Assistant
is(emptyOrNull())isn't available. Let me fix the test to check empty body differently.Assistant
All 4 tests pass:
- PUT with
../bad.txtkey → 400 with empty body ✓ - DELETE with
../bad.txtkey → 400 with empty body ✓ - PUT with encoded
%2E%2E/bad.txtkey → 400 with empty body ✓ - Null byte key → 400 with
InvalidKeybody (still returns error body, as expected) ✓
Now let me clean up the temporary test file.
- PUT with
Assistant
Now let me also check the
S3ControlControllerandS3VirtualHostFilterfor any dot-dot handling that might be affected. Let me also check theS3LeadingSlashKeyIntegrationTestto make sure I didn't break leading-slash key handling.Assistant
Let me also run the well-known key and presigned post tests to make sure I didn't break anything.
Assistant
All pass. Now let me verify the full diff of my changes:
Assistant
Let me also check the
S3VirtualHostFilterto see if there's any path handling that might be relevant, and check whether theS3ControlControllerhas similar traversal checks.Assistant
No traversal checks in the virtual host filter. Now let me run a broader set of S3 tests to make sure nothing else is broken (excluding the known pre-existing and expected-to-change failures).
Assistant
Let me also run the
S3MultipartServiceTestandS3VersioningServiceTestto check service-layer tests.Assistant
All pass. Let me now run the full
S3IntegrationTestonce more to confirm only the expected failures remain (the 4 dot-dot tests that now return empty body, and the 1 pre-existing non-ASCII failure).Assistant
The 5 failures are:
copyObjectWithNonAsciiKeySucceeds— pre-existing failure (confirmed it fails without my changes too; unrelated to dot-dot) 2-5. The four dot-dot traversal tests — these now correctly return an empty body instead of anInvalidKeybody, which is exactly the behavior the issue requires. These tests need updating to reflect the new (correct) behavior.
Per the instructions, tests that break because they need to be updated to reflect the change should be ignored, and the pre-existing failure is not caused by my change.
Let me do a final compilation check to make sure everything is clean.
Assistant
Now let me verify the final state of the new exception file:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.