Skip to content

fix(common/http): add CSP nonce support to JsonpClientBackend#67923

Open
YooLCD wants to merge 1 commit intoangular:mainfrom
YooLCD:main
Open

fix(common/http): add CSP nonce support to JsonpClientBackend#67923
YooLCD wants to merge 1 commit intoangular:mainfrom
YooLCD:main

Conversation

@YooLCD
Copy link
Copy Markdown

@YooLCD YooLCD commented Mar 28, 2026

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix

What is the current behavior?

JsonpClientBackend does not set the nonce attribute on dynamically injected <script> tags. This causes JSONP requests to be blocked in applications that use Content Security Policy with strict-dynamic, since the browser requires a valid nonce on all dynamically created scripts.

All other places in Angular that create <script> or <style> elements already support CSP nonces via the CSP_NONCE injection token (e.g. SharedStylesHost, DomRendererFactory2, TransferState). JsonpClientBackend was the only exception.

Issue Number: N/A

What is the new behavior?

JsonpClientBackend now optionally injects the CSP_NONCE token and sets it as the nonce attribute on the created <script> element. The token is injected with @Optional(), so existing applications that do not provide CSP_NONCE are unaffected.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

MockScriptElement in jsonp_mock.ts has been updated to support setAttribute/getAttribute to enable nonce-related testing.

@pullapprove pullapprove bot requested a review from JeanMeche March 28, 2026 15:18
@angular-robot angular-robot bot added the area: common/http Issues related to HTTP and HTTP Client label Mar 28, 2026
@ngbot ngbot bot added this to the Backlog milestone Mar 28, 2026
@JeanMeche
Copy link
Copy Markdown
Member

Tests are failing. Can you PTAL.

@YooLCD
Copy link
Copy Markdown
Author

YooLCD commented Mar 29, 2026

Fixed the constructor issue (removed default = null from the nonce parameter) and updated the public API golden file.

constructor(
private callbackMap: JsonpCallbackContext,
@Inject(DOCUMENT) private document: any,
@Optional() @Inject(CSP_NONCE) private readonly nonce: string | null,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Possibly for CSP_NONCE we could use

private readonly nonce = inject(CSP_NONCE , { optional: true });

Or would there be some reason not to use it?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the suggestion! Updated to use inject(CSP_NONCE, { optional: true }) as a class field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: common/http Issues related to HTTP and HTTP Client

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants