azure-sdk-for-python icon indicating copy to clipboard operation
azure-sdk-for-python copied to clipboard

generate_blob_sas omits backslash->forward-slash normalization in SAS canonical resource; tokens rejected for blob names containing '\' (Go/.NET normalize; JS has same gap)

Open annatchijova opened this issue 3 months ago • 3 comments

Library name and version

azure-storage-blob 12.30.0 (behavior also present on main)

Describe the bug

When building the SAS string-to-sign, the SDK inserts the blob name verbatim into the canonical resource, without normalizing backslash (\) to forward slash (/):

# azure/storage/blob/_shared_access_signature.py  (generate_blob path)
resource_path = container_name + "/" + blob_name        # blob_name used as-is

# azure/storage/blob/_shared_access_signature.py  _BlobSharedAccessHelper.add_resource_signature
canonicalized_resource = "/blob/" + account_name + path + "\n"   # path used as-is

Two sibling first-party SDKs do normalize \ → / in the same canonical-resource step, and the Azure Storage service treats \ as /:

  • Go (azure-sdk-for-go, sdk/storage/azblob/sas/service.go, getCanonicalName): strings.ReplaceAll(blobName, "\\", "/")
  • .NET (azure-sdk-for-net, Azure.Storage.Blobs/src/Sas/BlobSasBuilder.cs, GetCanonicalName): blobName.Replace("\\", "/")
  • JS (azure-sdk-for-js, storage-blob/src/sas/BlobSASSignatureValues.ts, getCanonicalName): inserts the blob name verbatim — same gap as Python

Because Python signs the canonical resource with the literal \ while the service canonicalizes it to /, the computed signature never matches the service's, and the generated SAS is rejected (HTTP 403) for any blob name containing a backslash.

Reproduction (executed, offline via Azurite)

Azurite (the official emulator) normalizes \ → / on both upload and SAS validation. Generating a SAS for a blob whose name contains \ and requesting it:

GET .../testc/dir%5Cfile?<SAS from generate_blob_sas(blob_name="dir\\file")>  -> 403
    (Python signed "/blob/acct/testc/dir\file")

GET .../testc/dir%5Cfile?<SAS whose canonical resource normalized "\"->"/">   -> 200
    (matches the service's "/blob/acct/testc/dir/file"; this is what Go/.NET produce)

The first token is what generate_blob_sas(..., blob_name="dir\\file", ...) produces; it is rejected. The second is what the Go/.NET SDKs produce (and what the service accepts). A minimal runnable script (Python + Azurite) is available on request.

Expected behavior

A SAS generated for a blob name containing \ should validate against the service, consistent with the Go and .NET SDKs. The canonical resource should normalize \ → /.

Impact / scope (stated honestly)

Correctness / availability, not a security issue: a SAS the caller intends for a backslash-containing blob name is rejected by the service (the delegate receives 403). No privilege escalation. Blob names with backslashes are uncommon but valid, and the four first-party SDKs are inconsistent (Go/.NET normalize; Python/JS do not).

Suggested fix

Normalize the blob name in the canonical resource, matching Go/.NET:

# in the resource-path / add_resource_signature construction
path = path.replace("\\", "/")
canonicalized_resource = "/blob/" + account_name + path + "\n"

(The same fix applies to azure-sdk-for-js getCanonicalName.)

Note on verification

Verified against Azurite (the official Storage emulator). Production Azure Storage is expected to behave identically — the Go and .NET SDKs independently implement the \ → / normalization specifically to match the service, which is strong corroboration that the service requires it.

annatchijova avatar Aug 21 '26 21:08 annatchijova

Thank you for your feedback. Tagging and routing to the team member(s) best able to assist.

//cc: @jalauzon-msft @vincenttran-msft @weirongw23-msft @anjaliratnam-msft

github-actions[bot] avatar Aug 21 '26 21:08 github-actions[bot]

🎯 Agentic Issue Triage

Summary: generate_blob_sas in azure-storage-blob does not normalize backslash (\) to forward slash (/) in the SAS canonical resource string, causing HTTP 403 errors for blob names containing backslashes. Go and .NET SDKs already implement this normalization.

📋 Issue Details

Package: azure-storage-blob 12.30.0 (also present on main) Affected API: generate_blob_sas / _BlobSharedAccessHelper.add_resource_signature in azure/storage/blob/_shared_access_signature.py Scenarios:

  • Generating a SAS token for a blob whose name contains \ — the resulting token is rejected (HTTP 403) by Azure Storage because the SDK signs the canonical resource with the literal \ while the service treats \ as /

Root ask: Normalize \ → / in the blob name when constructing the canonical resource, matching the behavior of the Go and .NET SDKs.

🔎 Debugging / Reproduction Notes

The issue has been reproduced offline against Azurite (the official Azure Storage emulator), which normalizes \ → / on both upload and SAS validation. The request with the unnormalized token returns 403; with manual normalization it returns 200.

Suggested investigation steps:

  1. Locate add_resource_signature in azure/storage/blob/_shared_access_signature.py and apply path = path.replace("\\", "/") before constructing canonicalized_resource.
  2. Verify that the same fix is needed for other SAS builders (e.g., container SAS) if they share the same code path.
  3. Add a regression test covering blob names with backslashes using Azurite or recorded tests.
  4. Optionally, file a corresponding issue for azure-sdk-for-js which has the same gap per the reporter.
🏷️ Label Confidence
  • Category: Client — azure-storage-blob is a data-plane client library
  • Service: Storage — issue is squarely in the Storage SDK (/sdk/storage/)
  • Confidence: High — package name, path, and issue content all unambiguously point to Client + Storage
👥 Owner Routing
  • Matched CODEOWNERS entry: # ServiceLabel: %Storage (line 768) — exact match on service label "Storage"
  • AzureSdkOwners: none listed
  • ServiceOwners: @jalauzon-msft, @vincenttran-msft, @weirongw23-msft, @anjaliratnam-msft
  • Routing action: Added "Service Attention" and "needs-team-attention"; ServiceOwners mentioned via routing comment
  • Scan notes: Scanned upward from end of file; line 768 entry was the first match for %Storage with no %Service Attention qualifier conflating it

Generated by Agentic Triage for issue #48690 · 25.7 AIC · ⌖ 5.94 AIC · ⊞ 9.6K · ◷

github-actions[bot] avatar Aug 21 '26 21:08 github-actions[bot]

Hi @annatchijova Anna, thanks for the report. We will look into this and get back to you. Also, thanks @Om-singhaI for the contribution. We will look into your change as well. There are some oddities with SAS and some additional places this fix may need to be applied so we may not end up taking your PR directly, but we do appreciate the contribution as it will help us get started with the fix faster.

jalauzon-msft avatar Aug 24 '26 21:08 jalauzon-msft