This document describes Angular's built-in HTTP security mechanisms provided by HttpClient. These features protect against common web vulnerabilities and optimize data transfer in Server-Side Rendering (SSR) environments.
The security features covered here are:
Sources: packages/common/http/src/xsrf.ts1-20 packages/common/http/src/transfer_cache.ts1-22 adev/src/content/guide/http/security.md1-13
Cross-Site Request Forgery (XSRF or CSRF) is an attack where a malicious site tricks an authenticated user into unknowingly executing actions on a trusted site. Angular's HttpClient implements the cookie-to-header token pattern to prevent these attacks automatically.
Sources: adev/src/content/guide/http/security.md15-33 packages/common/http/src/xsrf.ts1-142
Title: XSRF Token Injection Flow
Sources: packages/common/http/src/xsrf.ts94-127 adev/src/content/guide/http/security.md15-33
Title: XSRF Code Entity Map
Sources: packages/common/http/src/xsrf.ts24-141 packages/common/http/src/provider.ts28
| Token | Default Value | Purpose |
|---|---|---|
XSRF_ENABLED | true | Global enable/disable flag packages/common/http/src/xsrf.ts47 |
XSRF_COOKIE_NAME | 'XSRF-TOKEN' | Name of cookie to read token from packages/common/http/src/xsrf.ts28 |
XSRF_HEADER_NAME | 'X-XSRF-TOKEN' | Name of header to set token in packages/common/http/src/xsrf.ts38 |
Sources: packages/common/http/src/xsrf.ts24-47
The HttpTransferCache is a critical feature for Angular Applications using Server-Side Rendering (SSR). It captures HTTP responses on the server and serializes them into the HTML document, allowing the client-side application to "replay" those responses during hydration without making new network requests.
Sources: packages/common/http/src/transfer_cache.ts33-56 adev/src/content/guide/ssr.md3-23
The shouldCacheRequest function determines if a request is eligible for transfer:
GET and HEAD are cached packages/common/http/src/transfer_cache.ts125-137Authorization, Proxy-Authorization, or Cookie are excluded unless includeRequestsWithAuthHeaders is enabled packages/common/http/src/transfer_cache.ts139-140withCredentials are never cached packages/common/http/src/transfer_cache.ts134-135transferCache property in HttpRequest packages/common/http/src/transfer_cache.ts133Sources: packages/common/http/src/transfer_cache.ts127-147
Title: HTTP Transfer Cache Flow
Sources: packages/common/http/src/transfer_cache.ts245-288 packages/common/http/src/transfer_cache.ts162-183
When the server uses internal URLs (e.g., http://localhost:8080) that differ from client URLs, the HTTP_TRANSFER_CACHE_ORIGIN_MAP token allows mapping these origins so the cache keys match.
Location: packages/common/http/src/transfer_cache.ts84-86 Usage: packages/common/http/src/transfer_cache.ts68-81
Cross-Site Script Inclusion (XSSI) is prevented by Angular automatically stripping the non-executable prefix )]}',\n from all JSON responses.
<script> tag, the prefix causes a syntax error, preventing data leakage.JSON.parse().Sources: adev/src/content/guide/http/security.md7-13 adev/src/content/guide/http/security.md7-13
HttpClient allows fine-grained control over which requests receive security tokens or are cached. Developers can use withInterceptors to add custom security logic.
Location: packages/common/http/src/provider.ts149-162
| Feature | Best Practice | Reference |
|---|---|---|
| XSRF | Ensure backend sets XSRF-TOKEN cookie and validates X-XSRF-TOKEN header. | adev/src/content/guide/http/security.md19-33 |
| SSR | Use provideHttpClient(withHttpTransferCache()) to prevent hydration flickering. | packages/common/http/src/transfer_cache.ts333-350 |
| Auth | Do not cache requests containing sensitive Cookie or Authorization headers. | packages/common/http/src/transfer_cache.ts55-56 |
| CORS | XSRF tokens are only added to same-origin requests to prevent leakage. | packages/common/http/src/xsrf.ts104-117 |
Sources: adev/src/content/guide/http/security.md1-66 packages/common/http/src/transfer_cache.ts127-147
Refresh this wiki