Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 57 ยท Storage

Storage Security and SAS Tokens

Secure Azure Storage: account keys vs Entra ID, SAS token types, how a signed URL is built with HMAC, stored access policies, firewalls and encryption, with a signing demo.

A storage account is reachable over the internet by default, so the question is never "can someone find it" but "what can a caller prove". Azure Storage answers with several layers: who you are (authentication), what you may do (authorization), where you may call from (network rules) and how data is protected on disk and on the wire (encryption). Mistakes here are among the most common causes of leaked data.

This lesson compares the ways to authenticate, explains the shared access signature (SAS) and how its signature is really just an HMAC over a list of permissions, and then covers stored access policies, firewalls and encryption. The real service needs an account, so the commands are read-only. The runnable example is a Node.js stand-in that signs and verifies a SAS-style URL with node:crypto. It is built to teach the idea and is not the exact Azure string-to-sign format, so never use it to produce real tokens.

Ways to authenticate

Method How it works Verdict
Account key Two long keys per account give full control of everything in it Powerful and dangerous; avoid in code
Microsoft Entra ID The caller gets a token for an identity and a role (for example Storage Blob Data Reader) decides what is allowed Recommended; see Managed Identities and Service Principals
SAS token A signed URL that grants limited access for a limited time Good for handing access to a party that has no Azure identity
Anonymous Public read on a container Only for deliberately public content

An account key works like a root password for the account. It cannot be limited to one container, cannot be tied to a person in the audit log, and if it leaks you must regenerate it, which breaks every client using it. The portal shows two keys (key1 and key2) so you can rotate one while clients use the other. Many organizations disable key access altogether (allowSharedKeyAccess set to false) and use Entra ID plus short-lived SAS tokens signed with a user delegation key (see below).

Entra ID is the preferred method because there are no secrets in your code, permissions follow the role-based model from Role-Based Access Control, and the access log names the caller. Management roles such as Contributor control the account itself; you also need a data role to read or write blobs, queues and tables. A common surprise is a Contributor who cannot read blobs through Entra ID because Contributor is a management role, not a data role.

Shared access signatures

Sometimes the caller has no Azure identity: a customer downloading an invoice, a mobile app uploading a photo straight to Blob Storage, a partner picking up a file. A shared access signature is a URL with a query string that carries the permissions, and a signature proving that someone who holds the key created it. Anyone with the full URL gets exactly that access, until it expires. That is why SAS tokens are called bearer tokens.

A SAS URL looks like this (values shortened):

TEXT
https://mydata12345.blob.core.windows.net/invoices/inv-2025-001.pdf
  ?sp=r            permissions: read
  &st=2025-03-01T10:00:00Z   start
  &se=2025-03-01T10:15:00Z   expiry
  &spr=https       protocol: HTTPS only
  &sv=2024-11-04   service version
  &sr=b            resource: a blob
  &sig=Zm9v...     the signature

There are three kinds:

  • Service SAS: delegates access to one resource in one service (a blob, a container, a queue, a table, a file share), signed with the account key.
  • Account SAS: delegates access to one or more services in the account, signed with the account key. It can allow service-level operations that a service SAS cannot.
  • User delegation SAS: for Blob Storage (and Data Lake) only, signed with a user delegation key that you obtain using Entra ID credentials. Because it hangs off an Entra identity rather than the account key, it is the most secure kind and Microsoft's recommendation when you can use it.
flowchart LR
    A["Your server<br/>(has Entra or key access)"] -->|"1. signs a SAS"| B["SAS URL"]
    B -->|"2. gives to"| C["Client / partner"]
    C -->|"3. GET with sig"| D["Blob Storage"]
    D -->|"4. recomputes HMAC,<br/>checks expiry & permissions"| C

The storage service never stores the SAS. It recomputes the signature from the request and compares.

Creating one from the CLI (read-only; the first signs with your Entra identity, the second with the account key):

Terminal
# User delegation SAS (recommended): signed with an Entra-derived key
az storage blob generate-sas \
  --account-name mydata12345 --container-name invoices --name inv-2025-001.pdf \
  --permissions r --expiry 2025-03-01T10:15Z --https-only \
  --auth-mode login --as-user --full-uri

# Service SAS signed with the account key
az storage blob generate-sas \
  --account-name mydata12345 --container-name invoices --name inv-2025-001.pdf \
  --permissions r --expiry 2025-03-01T10:15Z --https-only \
  --account-key "$STORAGE_KEY" --full-uri

How the signature works

The signature is an HMAC-SHA256. An HMAC combines a message and a secret key into a fixed-size fingerprint. Without the key, nobody can create a valid fingerprint for a different message; and changing one character of the message changes the fingerprint completely.

Azure builds a string from the fields of the SAS (permissions, start and expiry times, the canonical resource name, protocol, version and more, one per line), decodes your base64 account key, computes HMAC-SHA256(key, stringToSign), and base64-encodes the result as sig. When the request arrives, the service rebuilds the same string from the URL, computes the HMAC with its own copy of the key, and checks that it matches sig. If someone edits sp=r to sp=rw or extends the expiry, the recomputed signature no longer matches and the request fails.

The stand-in below follows this shape with a simplified string-to-sign: resource path, permissions, expiry (as a number of seconds on a fake clock) and a fixed protocol. It then verifies a good URL, a tampered one and an expired one.

Node.js Sign and verify a SAS-style URL

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Every attack changes the data the service signs, so the signature fails. A few things the stand-in shows by design: the permission check comes after the signature check (never trust unsigned fields), the comparison is constant-time (timingSafeEqual), and a signature is bound to one resource path. Things it omits: real SAS tokens include a start time, IP ranges, a service version and the signed resource type, and Azure uses a specific, versioned string-to-sign layout.

SAS best practices

  • Short lifetimes. A SAS cannot be revoked once issued (except in the ways below). Use minutes for downloads, a bit longer for uploads.
  • Least permission. Give r for downloads, c and w for uploads, never rwdl on the whole account.
  • HTTPS only. Always set spr=https.
  • User delegation SAS whenever possible, so a compromised token never involves the account key.
  • Issue tokens from your server, after you check who is asking; never ship the key to a browser or mobile app.

Stored access policies

A stored access policy lives on a container (or queue, table, file share). A service SAS can reference the policy by name (si=reader-policy) instead of embedding permissions and times. The benefit is control after the fact: change the policy's expiry or permissions, or delete it, and every SAS that references it changes or stops working immediately. Without a policy, the only way to cancel an account-key SAS is to regenerate the account key. A container can hold up to five stored access policies. User delegation SAS tokens do not use stored access policies; you revoke them by revoking the user delegation key.

Terminal
az storage container policy create \
  --account-name mydata12345 --container-name invoices \
  --name reader-policy --permissions r --expiry 2025-12-31T00:00Z --auth-mode login

Network rules

Authentication decides who; the storage firewall decides where from. In the account's networking settings you can:

  • Allow all networks (the default for new accounts, convenient and risky).
  • Allow selected virtual networks (via service endpoints) and public IP ranges.
  • Disable public access and use private endpoints, so the account has a private IP inside your network (see Private Endpoints, VPN and ExpressRoute).
Terminal
az storage account update --name mydata12345 --resource-group rg-data --default-action Deny
az storage account network-rule add --account-name mydata12345 --resource-group rg-data \
  --ip-address 203.0.113.0/24

Network rules apply to the data endpoints, including requests that carry a valid SAS. A request from a blocked address is rejected even with a correct signature. The 203.0.113.0/24 range is a documentation example.

Encryption

  • At rest: all data in a storage account is encrypted automatically with 256-bit AES, and you cannot turn this off. By default Microsoft manages the keys. You can bring your own key from Key Vault (customer-managed keys) if policy requires it. For extra isolation, infrastructure encryption adds a second layer.
  • In transit: set secure transfer required (it is on by default for new accounts) so HTTP requests are refused, and set a minimum TLS version (1.2 is the sensible floor).
Terminal
az storage account update --name mydata12345 --resource-group rg-data \
  --https-only true --min-tls-version TLS1_2 --allow-blob-public-access false

A security checklist

The next stand-in turns the advice into a tiny configuration checker. Each setting is a plain object (an invented shape, not an Azure API), and the function lists findings. Try changing values to see what clears.

JS Storage account configuration checker

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The checker follows the lesson's advice, not an official benchmark. Real posture checks come from Azure Policy and Defender for Cloud, covered later in Security Best Practices and Defender for Cloud.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why are account keys considered risky to use in application code?

    A key is like a root password for the account. If it leaks you must rotate it, and every client using it breaks.
  2. 2. Which type of SAS is signed with an Entra-derived key instead of the account key?

    A user delegation SAS (available for Blob and Data Lake) uses a user delegation key obtained with Entra ID credentials.
  3. 3. A caller changes sp=r to sp=rw in a SAS URL. What happens?

    The signature is an HMAC over the permissions and other fields. Any edit changes the data being signed, so recomputing gives a different value.
  4. 4. What is the main advantage of a stored access policy?

    A service SAS that points to a policy inherits the policy's permissions and expiry. Edit or delete the policy and those tokens change or stop working.
  5. 5. A request carries a valid SAS but comes from an IP address the storage firewall denies. What is the result?

    The firewall is an independent layer. Valid credentials from a blocked network are still refused.

Summary

  • Prefer Microsoft Entra ID and managed identities over account keys; consider disabling key access.
  • A SAS is a bearer URL; it carries permissions and an expiry, and an HMAC signature proves it was created with the key.
  • User delegation SAS is the most secure kind; keep lifetimes short, permissions minimal and HTTPS mandatory.
  • Stored access policies let you adjust or cancel service SAS tokens after they are issued.
  • Combine authentication with the storage firewall or private endpoints, and keep secure transfer and TLS 1.2 on. Data is always encrypted at rest.

Next: Disks and Redundancy.