requests: breaking aws s3 usage with requests 2.32.0

requests 2.32.0 introduced a change https://github.com/psf/requests/pull/6644 that strips double /.

This has introduced an issue where generated presigned urls for s3 keys that start with a / can no longer be used. requests now strips that second / meaning that the key is modified and the signature does not match resulting in 403 errors. We can adjust to remove the leading / in our keys but this may be affecting other users or use cases

Expected Result

To be able to use presigned_urls for s3 keys with leading “/”

Actual Result

URL that was passed was modified resulting in a 403 error response from aws

Reproduction Steps

import requests
import boto3
s3 = boto3.client('s3')
bucket = "bucket"
key = "/key_with_leading_slash.txt"
presigned_url = s3.generate_presigned_url("get_object", Params={'Bucket': bucket, 'Key': key})
requests.get(presigned_url)

System Information

$ python -m requests.help
{
  "chardet": {
    "version": "5.1.0"
  },
  "charset_normalizer": {
    "version": "3.1.0"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "3.4"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.9.6"
  },
  "platform": {
    "release": "10",
    "system": "Windows"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.32.1"
  },
  "system_ssl": {
    "version": "101010bf"
  },
  "urllib3": {
    "version": "1.26.15"
  },
  "using_charset_normalizer": false,
  "using_pyopenssl": false
}

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 8
  • Comments: 20 (4 by maintainers)

Most upvoted comments

I tried a few other clients - Python stdlib, Curl, Go stdlib, Elixir Req, Groovy stdlib (probably same as Java) - and I couldn’t find another one that strips the slashes. Also, I’ve tested a few servers (Python http.server, Nginx and Apache) and neither of them “normalizes” the slashes.

https://gist.github.com/crabhi/080d746e3eb4fc53380bc8291cdd0f7d

@sigmavirus24 what leads you to believe the slash is “redundant and superfluous”? I see this is not an issue that would affect many people, so I’m willing to contribute a fix that would work in line with other clients. Other options I have are no joy - staying on 2.31, or maintaining a fork, or using Python stdlib for this particular request.

I don’t agree that stripping the redundant and superfluous slash is a bug. I also think that urllib3 can be improved in general around this but doing so in a backwards compatible fashion that doesn’t recreate this bug but in a lower part of the stack isn’t going to improve things

For what it’s worth, the issue I raised against boto got rejected, so it appears the python SDK developers are not planning to address this in the SDK.

Instead, the suggestion is for when using requests, to use a prepared request and escape the first slash. I have not tested this myself.

For what it’s worth, I don’t care that much about RFC legalese. The problem is I also see a web service (completely unrelated to S3, not even a storage) that doesn’t work correctly when queried from requests.

If we can agree the previous behaviour was a bug WRT the colon handling in the first path segment but the current fix introduced another bug, I can try to contribute a patch to urllib3 to fix the colon issue there, so that #6644 can be reverted.