On this page
A presigned upload URL authorizes a particular request, not just a destination. If a client changes a signed part of that request, the storage service may reject it with a signature mismatch. This guide uses Amazon S3 with Signature Version 4 to explain what to compare and how to reproduce the problem safely.
Official AWS documentation could not be consulted in this run, and the example below has not been executed. Check the current S3 and SDK documentation for your environment before relying on version-specific behavior. Other storage providers have different signing rules and error responses.
Key Takeaways
- Match the HTTP method, host, signed headers, and query parameters used when the URL was generated.
- A
Content-Typevalue matters to the signature when it was included in the signed headers; it is not automatically a guarantee about the file's actual contents. - Treat the complete URL as a temporary credential. Do not publish it, log it, or reuse a real customer's URL for debugging.
- Expiry, permissions, browser CORS restrictions, and network failures can look like upload problems but require different fixes.
What an S3 presigned upload URL signs
An S3 presigned URL carries authentication information in its query string. With Signature Version 4, the signature is derived from a canonical request that includes the HTTP method, path, canonical query parameters, and selected headers. The URL's X-Amz-SignedHeaders parameter identifies the headers included in the signature; the host is normally among them.
For example, if the backend presigns a PUT request with Content-Type: text/plain, the upload client must send a matching signed content type. Sending application/json, omitting the header, changing the method to POST, or modifying a signed query parameter can invalidate the request.
A content type is not necessarily signed on every presigned upload. Inspect X-Amz-SignedHeaders and the code that generates the URL rather than assuming every header is covered. Additional headers may also be subject to S3 rules or bucket policy even if they are not listed there.
Advertisement
Reproduce a presigned upload URL signature mismatch
The following lab generates a fresh URL for a uniquely named disposable object, sends one matching request, then changes one request detail at a time. It prints HTTP status codes and, when present, the S3 error code—not the URL or response body. Never substitute a live URL from an application.
Prerequisites
- Python and the
boto3package, installed withpython -m pip install boto3. - AWS credentials and region configured for the SDK. Use a disposable bucket you control and credentials authorized to upload and delete test objects in it.
- A bucket policy and encryption configuration that allow this simple upload. Additional required headers or policy restrictions can cause failures unrelated to the signature changes.
Set TEST_BUCKET to the name of that bucket. Save the script as presigned_lab.py and run python presigned_lab.py.
import http.client
import os
import uuid
import xml.etree.ElementTree as ET
from urllib.parse import urlsplit
import boto3
bucket = os.environ["TEST_BUCKET"]
key = f"presigned-lab/{uuid.uuid4().hex}.txt"
s3 = boto3.client("s3")
url = s3.generate_presigned_url(
"put_object",
Params={"Bucket": bucket, "Key": key, "ContentType": "text/plain"},
ExpiresIn=300,
HttpMethod="PUT",
)
parts = urlsplit(url)
if parts.scheme != "https" or not parts.hostname:
raise ValueError("Expected an HTTPS presigned URL")
path_and_query = parts.path + "?" + parts.query
def send(label, method, target, content_type):
connection = http.client.HTTPSConnection(parts.hostname, parts.port, timeout=10)
try:
connection.request(
method,
target,
body=b"lab upload\n" if method == "PUT" else b"",
headers={"Content-Type": content_type},
)
response = connection.getresponse()
body = response.read(65536)
error_code = ""
if response.status >= 400:
try:
error_code = ET.fromstring(body).findtext("Code", default="")
except ET.ParseError:
pass
print(f"{label}: HTTP {response.status} {error_code}")
finally:
connection.close()
try:
send("matching request", "PUT", path_and_query, "text/plain")
send("changed content type", "PUT", path_and_query, "application/json")
send("extra query parameter", "PUT", path_and_query + "&diagnostic=1", "text/plain")
send("changed method", "GET", path_and_query, "text/plain")
except (OSError, http.client.HTTPException) as exc:
# Avoid printing exception details that might contain request information.
print(f"Network or HTTP client error: {type(exc).__name__}")
finally:
try:
s3.delete_object(Bucket=bucket, Key=key)
except Exception as exc:
print(f"Cleanup failed: {type(exc).__name__}; remove the lab object manually.")
The matching request is intended to succeed. The changed requests are intended to demonstrate signature failures, often reported by S3 as SignatureDoesNotMatch. Actual responses can differ if, for example, credentials expire, a policy denies access, or a network intermediary changes the request. Inspect the status and error code rather than assuming every failure has the same cause.
The script attempts cleanup even if a request fails. If deletion fails, locate and remove the uniquely named object under presigned-lab/ using your authorized administrative tools. Avoid printing or sharing the generated URL while investigating.
Compare the generated and sent requests
- Method: Confirm that the backend presigned the operation the client actually sends. A presigned
PUTis not interchangeable with aPOSTform upload or aGETrequest. - Host and path: Send the URL to the host and object path supplied by the signer. Replacing the endpoint, changing the object key, or decoding and re-encoding the path can change the canonical request.
- Signed headers: Compare the header names in
X-Amz-SignedHeaderswith the outgoing request. Check values as well as presence, especiallyContent-Typeand any explicitly signed storage-specific headers. - Query string: Pass the URL through without adding, removing, or rewriting parameters. Query parameter order alone is not necessarily decisive because signing uses a canonical query representation; changes to names, values, or encoding can still break the signature.
- Credentials and time: Check the URL's signing time and expiration, the server clock, and whether the credentials used to sign it are still valid. Temporary credentials can expire before the URL's stated lifetime ends.
Be careful when using browser developer tools, proxies, or application logs: the query string contains a usable temporary credential. Restrict access to captures and redact the complete URL before sharing diagnostics.
Advertisement
Separate signature failures from other failures
| Symptom | What to investigate |
|---|---|
S3 returns SignatureDoesNotMatch | Compare the method, endpoint, path, signed headers, query parameters, and signing configuration. |
| URL has expired | Generate a fresh URL; also check the lifetime of temporary signing credentials and clock accuracy. |
| Access is denied | Check the signing principal's permissions, bucket policy, and conditions such as required encryption. A valid signature does not override authorization rules. |
| Browser reports a CORS error | Check the bucket's CORS configuration for the application's origin, PUT method, and requested headers. A failed preflight can prevent the upload request from being sent at all. |
| Connection, DNS, TLS, or timeout error | Investigate the endpoint and network path. A request that never reaches S3 cannot produce an S3 signature response. |
A browser may hide an otherwise useful service response when CORS is misconfigured. Compare behavior with an authorized non-browser client, without exposing the URL, and use server-side diagnostics where available. CORS controls browser access; it is not a replacement for storage authorization.
Common Mistakes
- Setting a different content type in the frontend. If the backend signs
text/plainbut the upload library chooses another value, agree on the value before generating the URL and send it unchanged. - Rebuilding the URL from parsed pieces. Frameworks and HTTP libraries can alter encoding or drop parameters. Prefer passing the original URL directly to the upload client.
- Assuming an unsigned content type is verified. A MIME header is client-supplied metadata, not proof of file format. Validate uploaded content separately when your application requires it.
- Treating all 403 responses as signature errors. Read the provider's error code where available and investigate authorization and expiry separately.
- Debugging with a real user's URL. Use a fresh disposable object and keep the URL out of tickets, logs, and screenshots.
Best Practices
Define an upload contract between backend and frontend: method, exact URL, required headers, expiration, and any application-level file restrictions. Generate a new URL when that contract changes instead of editing an existing one.
Grant the signing principal only the necessary permissions, keep URL lifetimes appropriate to the upload workflow, and clean up abandoned test objects. Validate uploaded objects before treating them as trusted. For another storage provider, consult its current documentation rather than assuming S3's canonicalization or error codes apply.
Conclusion
A presigned upload succeeds only when the request matches the signing contract and remains authorized. Start with the method, signed headers, host, path, and query string; then investigate expiry, permissions, CORS, and connectivity as distinct causes.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.