Skip to content

fix(@angular/build): support standard JavaScript MIME types and case insensitivity in auto-CSP - #34114

Merged
alan-agius4 merged 1 commit into
angular:mainfrom
clydin:fix-auto-csp-js-mime-types
Sep 18, 2026
Merged

alan-agius4 merged 1 commit into
angular:mainfrom
clydin:fix-auto-csp-js-mime-types

Conversation

@clydin

@clydin clydin commented Sep 17, 2026

Copy link
Copy Markdown
Member

Previously, isJavascriptMimeType() in auto-csp.ts only performed a case-sensitive check against 'text/javascript' on the slice prior to the first semicolon.

This caused several issues:

  1. Valid and common JavaScript MIME types specified in the HTML Living Standard such as application/javascript and text/ecmascript were not recognized as JavaScript.
  2. Case differences (e.g. type="text/JavaScript" or type="Module") were not matched, despite HTML attribute matching and MIME types being ASCII case-insensitive.
  3. Whitespace around the essence (e.g. type="text/javascript ; charset=utf-8" or type=" text/javascript") caused the strict equality check to fail.

When a script tag with one of these valid types was not recognized, auto-csp bypassed dynamic script rewriting and emitted the script element as-is into index.html. Under the generated strict CSP, the browser would then block the script from executing.

This change introduces JAVASCRIPT_MIME_TYPES containing all HTML-standard JavaScript MIME types, normalizes the essence (stripping parameters, trimming surrounding whitespace, and lowercasing), and updates shouldDynamicallyLoadScriptTagBasedOnType to support case-insensitive module types.

…insensitivity in auto-CSP

Previously, isJavascriptMimeType() in auto-csp.ts only performed a case-sensitive check against 'text/javascript' on the slice prior to the first semicolon.

This caused several issues:
1. Valid and common JavaScript MIME types specified in the HTML Living Standard such as application/javascript and text/ecmascript were not recognized as JavaScript.
2. Case differences (e.g. type="text/JavaScript" or type="Module") were not matched, despite HTML attribute matching and MIME types being ASCII case-insensitive.
3. Whitespace around the essence (e.g. type="text/javascript ; charset=utf-8" or type=" text/javascript") caused the strict equality check to fail.

When a script tag with one of these valid types was not recognized, auto-csp bypassed dynamic script rewriting and emitted the script element as-is into index.html. Under the generated strict CSP, the browser would then block the script from executing.

This change introduces JAVASCRIPT_MIME_TYPES containing all HTML-standard JavaScript MIME types, normalizes the essence (stripping parameters, trimming surrounding whitespace, and lowercasing), and updates shouldDynamicallyLoadScriptTagBasedOnType to support case-insensitive module types.
@clydin
clydin marked this pull request as ready for review September 17, 2026 20:42

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request updates the auto-CSP utility to support all standard JavaScript MIME types according to the HTML specification, ensuring robust handling of case-insensitivity, parameters, and whitespace. It also adds comprehensive unit tests to validate these changes. There are no review comments, so I have no feedback to provide.

@clydin clydin added target: patch This PR is targeted for the next patch release action: review The PR is still awaiting reviews from at least one requested reviewer labels Sep 17, 2026

@alan-agius4 alan-agius4 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@alan-agius4 alan-agius4 added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Sep 18, 2026
@alan-agius4
alan-agius4 merged commit 8e1b202 into angular:main Sep 18, 2026
44 checks passed
@alan-agius4

Copy link
Copy Markdown
Collaborator

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

@clydin
clydin deleted the fix-auto-csp-js-mime-types branch September 18, 2026 11:12
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: @angular/build target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants