fix S3 handling of special character and trailing slash #9143
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.
Motivation
This is a follow up from the Werkzeug upgrade, and the moto bump adapting to those changes, following the reported issue #9112
Moto now has a specific handling of special character to maintain compatibility between mocked botocore requests and werkzeuga requests. We've had issues when accessing directly moto's backend to get the key, with the proper key from the request.
I first suspected moto and tried working on a fix there and wrote some tests, but those were passing there, but not in LocalStack.
I've tracked it down to passing the request with
call_moto
, we would use therequest.url
, which is the decoded URL, which moto would then use to create its keys internally.Some examples of the issues:
Passing decoded request URL to moto:
Raw URL:
http://s3.localhost.localstack.cloud:4566/test-bucket-0ff73618/test%2540key/
request.url passed to moto:
http://s3.localhost.localstack.cloud:4566/test-bucket-0ff73618/test@key/
Key name in moto:
test@key/
when it should betest%40key/
Trailing slashes (once the issue above was done)
Raw path:
/test-bucket-18ba44f6/test%2540key/
Parsed key in the parser:
test%40key
(missing the trailing slash, but was properly parsed in moto)base_url:
http://s3.localhost.localstack.cloud:4566/test-bucket-18ba44f6/test@key/
The check for
base_url.endswith(key)
does not work here, so the trailing slash isn't added.Changes
Moto needs the raw URL, so we now use
get_full_raw_path
to get the original request URL, so moto can properly use its logic to decode the URL and create the proper key name out of it.I've also thought of another scenario where this could be problematic, with special characters and handling slashes, which was already an issue before (this has been changed often, every time werkzeug changes its handling of URL), but sadly there's isn't a catch-all way to check if the request has a trailing slash that needs to be kept.
Took the opportunity to fix the key returned by the multiple
List*
in the new provider as well, with some AWS validated tests, and removed some TODOs.