Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
atscott merged 4 commits into
angular:mainfrom
alan-agius4:http-hash
Jun 5, 2026
Merged

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

Conversation

@alan-agius4

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

Copy link
Copy Markdown
Contributor

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.

@JeanMeche JeanMeche left a comment

Copy link
Copy Markdown
Member

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

@JeanMeche JeanMeche left a comment

Copy link
Copy Markdown
Member

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.

@alan-agius4 alan-agius4 added the merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note label Jun 5, 2026
@alan-agius4

Copy link
Copy Markdown
Contributor Author

caretaker note: G3 failures are preexisting.

@atscott
atscott merged commit 5f36274 into angular:main Jun 5, 2026
26 of 28 checks passed
@atscott

atscott commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

This PR was merged into the repository. The changes were merged into the following branches:

@angular-automatic-lock-bot

Copy link
Copy Markdown

This pull request has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Jul 6, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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 merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants