Skip to content

fix(common): use cryptographically secure SHA-256 for transfer cache key generation#69153

Open
alan-agius4 wants to merge 4 commits into
angular:mainfrom
alan-agius4:http-hash
Open

fix(common): use cryptographically secure SHA-256 for transfer cache key generation#69153
alan-agius4 wants to merge 4 commits into
angular:mainfrom
alan-agius4:http-hash

Conversation

@alan-agius4
Copy link
Copy Markdown
Contributor

@alan-agius4 alan-agius4 commented Jun 4, 2026

Replace the custom 64-bit non-cryptographic combined DJB2 hashing implementation in HttpTransferCache with a robust, pure JavaScript, synchronous SHA-256 algorithm.

Using DJB2 is vulnerable to pre-image and second-preimage attacks due to its small 64-bit keyspace and mathematical simplicity. An attacker could craft colliding request inputs to poison the cache, potentially causing a CDN or the application to serve the wrong cached response to legitimate users.

SHA-256 provides strong cryptographic collision resistance, preventing cache key collision attacks. A custom synchronous implementation is required because the Web Crypto API (crypto.subtle.digest) is asynchronous, whereas the transfer cache state lookup and interceptor flow must operate synchronously.

Also, update the unit tests to dynamically verify the custom SHA-256 output against the native Web Crypto API.

@pullapprove pullapprove Bot requested a review from crisbeto June 4, 2026 13:13
@angular-robot angular-robot Bot added area: common/http Issues related to HTTP and HTTP Client area: common Issues related to APIs in the @angular/common package labels Jun 4, 2026
@ngbot ngbot Bot added this to the Backlog milestone Jun 4, 2026
@alan-agius4 alan-agius4 requested review from JeanMeche and removed request for crisbeto June 4, 2026 13:14
@alan-agius4 alan-agius4 changed the title http hash fix(common): use cryptographically secure SHA-256 for transfer cache key generation Jun 4, 2026
…key generation

Replace the custom 64-bit non-cryptographic combined DJB2 hashing implementation in HttpTransferCache with a robust, pure JavaScript, synchronous SHA-256 algorithm.

Using DJB2 is vulnerable to pre-image and second-preimage attacks due to its small 64-bit keyspace and mathematical simplicity. An attacker could craft colliding request inputs to poison the cache, potentially causing a CDN or the application to serve the wrong cached response to legitimate users.

SHA-256 provides strong cryptographic collision resistance, preventing cache key collision attacks. A custom synchronous implementation is required because the Web Crypto API (`crypto.subtle.digest`) is asynchronous, whereas the transfer cache state lookup and interceptor flow must operate synchronously.

Also, update the unit tests to dynamically verify the custom SHA-256 output against the native Web Crypto API.
Copy link
Copy Markdown
Member

@JeanMeche JeanMeche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AGENT: I have left a few inline suggestions regarding performance optimization for the synchronous SHA-256 implementation. Please consider moving the constants and TextEncoder out of the generateHash function to avoid unnecessary allocations on every request.

Comment thread packages/common/http/src/transfer_cache.ts Outdated
Comment thread packages/common/http/src/transfer_cache.ts Outdated
@alan-agius4 alan-agius4 added action: review The PR is still awaiting reviews from at least one requested reviewer target: patch This PR is targeted for the next patch release labels Jun 4, 2026
@alan-agius4 alan-agius4 requested a review from JeanMeche June 4, 2026 13:45
Comment thread packages/common/http/src/transfer_cache.ts
Copy link
Copy Markdown
Member

@JeanMeche JeanMeche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AGENT: The updates look great! Thank you for addressing the performance feedback. Moving the constants and TextEncoder out of the function, along with the other optimizations, looks perfectly implemented.

@alan-agius4 alan-agius4 removed the action: review The PR is still awaiting reviews from at least one requested reviewer label Jun 4, 2026
@alan-agius4 alan-agius4 added the action: merge The PR is ready for merge by the caretaker label Jun 4, 2026
@JeanMeche
Copy link
Copy Markdown
Member

Looks like we'll need a patch backport.

@JeanMeche JeanMeche added target: minor This PR is targeted for the next minor release and removed target: patch This PR is targeted for the next patch release labels Jun 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: common/http Issues related to HTTP and HTTP Client area: common Issues related to APIs in the @angular/common package target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants