-
Notifications
You must be signed in to change notification settings - Fork 11.9k
feat(@angular/build): emit debug ids for stable subresource integrity hashes #33110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dgp1130
merged 1 commit into
angular:main
from
simpleclub-extended:feat/sub-resource-integrity-with-debug-id
Jul 15, 2026
+331
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/angular/build/src/builders/application/inject-debug-ids.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import { BuildOutputFile, BuildOutputFileType } from '../../tools/esbuild/bundler-files'; | ||
| import { | ||
| generateDebugId, | ||
| injectDebugIdIntoJs, | ||
| injectDebugIdIntoSourceMap, | ||
| } from '../../utils/debug-id'; | ||
|
|
||
| /** | ||
| * Embeds an ECMA-426 Debug ID into every browser JavaScript output that has a | ||
| * matching source map sibling. | ||
| * | ||
| * The Debug ID is derived deterministically (UUIDv5) from the source map bytes | ||
| * so rebuilds of the same source produce the same ID. The JS file gets a | ||
| * `//# debugId=<uuid>` comment placed above any existing | ||
| * `//# sourceMappingURL=` line and the source map JSON gets a top-level | ||
| * `"debugId"` field. Together they make build artifacts self-identifying as | ||
| * proposed by https://github.com/tc39/ecma426/blob/main/proposals/debug-id.md. | ||
| */ | ||
| export function injectDebugIds(outputFiles: BuildOutputFile[]): void { | ||
| const filesByPath = new Map<string, BuildOutputFile>(); | ||
| for (const file of outputFiles) { | ||
| filesByPath.set(file.path, file); | ||
| } | ||
|
|
||
| const encoder = new TextEncoder(); | ||
|
|
||
| for (const file of outputFiles) { | ||
| if (file.type !== BuildOutputFileType.Browser || !file.path.endsWith('.js')) { | ||
| continue; | ||
| } | ||
|
|
||
| const map = filesByPath.get(`${file.path}.map`); | ||
| if (!map) { | ||
| continue; | ||
| } | ||
|
|
||
| const id = generateDebugId(map.contents); | ||
| file.contents = encoder.encode(injectDebugIdIntoJs(file.text, id)); | ||
| map.contents = encoder.encode(injectDebugIdIntoSourceMap(map.text, id)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import { createHash } from 'node:crypto'; | ||
|
|
||
| /** | ||
| * Fixed RFC 4122 namespace UUID used to derive deterministic UUIDv5 build/Debug IDs. | ||
| * Treated as 16 raw bytes when fed into the SHA-1 of `namespace || name`. | ||
| * | ||
| * The exact value is arbitrary but must remain stable across releases so that | ||
| * the same source map content always yields the same Debug ID. | ||
| */ | ||
| const ANGULAR_BUILD_NAMESPACE = Buffer.from('6f9619ff8b86d011b42d00cf4fc964ff', 'hex'); | ||
|
|
||
| /** | ||
| * Generates a deterministic UUIDv5 (RFC 4122 §4.3) Debug ID from the given name bytes. | ||
| * | ||
| * Determinism is recommended by the ECMA-426 "Source Map Debug ID" proposal | ||
| * (https://github.com/tc39/ecma426/blob/main/proposals/debug-id.md) so that the | ||
| * produced artifacts are stable across builds with the same source content. | ||
| * | ||
| * @param name Bytes that uniquely identify the artifact (typically the source map content). | ||
| * @returns A canonical UUIDv5 string (lowercase, hyphenated). | ||
| */ | ||
| export function generateDebugId(name: string | Uint8Array): string { | ||
| const sha = createHash('sha1').update(ANGULAR_BUILD_NAMESPACE).update(name).digest(); | ||
|
|
||
| // Set version (5) in the high nibble of byte 6. | ||
| sha[6] = (sha[6] & 0x0f) | 0x50; | ||
| // Set RFC 4122 variant bits (10xx) in byte 8. | ||
| sha[8] = (sha[8] & 0x3f) | 0x80; | ||
|
|
||
| const h = sha.toString('hex'); | ||
|
|
||
| return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20, 32)}`; | ||
| } | ||
|
|
||
| /** Pattern matching an existing `//# debugId=<uuid>` comment anywhere in the file. */ | ||
| const DEBUG_ID_COMMENT = /\n\/\/# debugId=[^\r\n]*\n?/; | ||
|
|
||
| /** Pattern matching the `//# sourceMappingURL=` comment, used to position the debug-id line. */ | ||
| const SOURCE_MAPPING_URL_COMMENT = /\n\/\/# sourceMappingURL=[^\r\n]*\s*$/; | ||
|
|
||
| /** | ||
| * Inserts (or replaces) a `//# debugId=<id>` comment in the given JavaScript text. | ||
| * | ||
| * Per ECMA-426, the comment must appear within the last 5 lines and SHOULD be | ||
| * placed immediately above any `//# sourceMappingURL=` comment so that existing | ||
| * tools that only consult the final line still find the source-map URL. | ||
| */ | ||
| export function injectDebugIdIntoJs(text: string, id: string): string { | ||
| const comment = `//# debugId=${id}`; | ||
|
|
||
| // Replace any existing debugId comment to keep the operation idempotent. | ||
| if (DEBUG_ID_COMMENT.test(text)) { | ||
| return text.replace(DEBUG_ID_COMMENT, `\n${comment}\n`); | ||
| } | ||
|
IchordeDionysos marked this conversation as resolved.
|
||
|
|
||
| if (SOURCE_MAPPING_URL_COMMENT.test(text)) { | ||
| return text.replace(SOURCE_MAPPING_URL_COMMENT, (match) => `\n${comment}${match}`); | ||
| } | ||
|
|
||
| // No source map reference; append at the very end on its own line. | ||
| return text.endsWith('\n') ? `${text}${comment}\n` : `${text}\n${comment}\n`; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the top-level `debugId` field on a JSON source map. | ||
| * | ||
| * Per ECMA-426, source maps embed the same Debug ID under a `debugId` key so | ||
| * that consumers can pair a generated file with its source map without relying | ||
| * on URL/path conventions. | ||
| */ | ||
| export function injectDebugIdIntoSourceMap(json: string, id: string): string { | ||
| let parsed: Record<string, unknown>; | ||
| try { | ||
| parsed = JSON.parse(json) as Record<string, unknown>; | ||
| } catch { | ||
| // Source map is malformed; do not corrupt it further. | ||
| return json; | ||
| } | ||
|
|
||
| if (parsed['debugId'] === id) { | ||
| return json; | ||
| } | ||
|
|
||
| parsed['debugId'] = id; | ||
|
|
||
| // Preserve existing pretty-print indentation when the source map is formatted. | ||
| const indent = json.match(/^[^{]*{\r?\n([ \t]+)/)?.[1]; | ||
|
|
||
| return JSON.stringify(parsed, null, indent); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import { generateDebugId, injectDebugIdIntoJs, injectDebugIdIntoSourceMap } from './debug-id'; | ||
|
|
||
| const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/; | ||
|
|
||
| describe('debug-id', () => { | ||
| describe('generateDebugId', () => { | ||
| it('produces a canonical UUIDv5 string', () => { | ||
| expect(generateDebugId('hello')).toMatch(UUID); | ||
| expect(generateDebugId(new TextEncoder().encode('hello'))).toMatch(UUID); | ||
| }); | ||
|
|
||
| it('is deterministic for identical inputs', () => { | ||
| expect(generateDebugId('same content')).toBe(generateDebugId('same content')); | ||
| }); | ||
|
|
||
| it('differs for different inputs', () => { | ||
| expect(generateDebugId('one')).not.toBe(generateDebugId('two')); | ||
| }); | ||
| }); | ||
|
|
||
| describe('injectDebugIdIntoJs', () => { | ||
| const id = '11111111-2222-5333-9444-555555555555'; | ||
|
|
||
| it('inserts the debugId comment immediately above sourceMappingURL', () => { | ||
| const text = 'console.log(1);\n//# sourceMappingURL=foo.js.map\n'; | ||
| const result = injectDebugIdIntoJs(text, id); | ||
| expect(result).toBe( | ||
| 'console.log(1);\n//# debugId=11111111-2222-5333-9444-555555555555\n//# sourceMappingURL=foo.js.map\n', | ||
| ); | ||
| }); | ||
|
|
||
| it('appends the comment when no sourceMappingURL is present', () => { | ||
| const text = 'console.log(1);\n'; | ||
| const result = injectDebugIdIntoJs(text, id); | ||
| expect(result).toBe('console.log(1);\n//# debugId=11111111-2222-5333-9444-555555555555\n'); | ||
| }); | ||
|
|
||
| it('appends a leading newline when input does not end with one', () => { | ||
| const text = 'console.log(1);'; | ||
| const result = injectDebugIdIntoJs(text, id); | ||
| expect(result).toBe('console.log(1);\n//# debugId=11111111-2222-5333-9444-555555555555\n'); | ||
| }); | ||
|
|
||
| it('replaces an existing debugId comment (idempotent)', () => { | ||
| const original = | ||
| 'console.log(1);\n//# debugId=00000000-0000-5000-8000-000000000000\n//# sourceMappingURL=foo.js.map\n'; | ||
| const result = injectDebugIdIntoJs(original, id); | ||
| expect(result).toBe( | ||
| 'console.log(1);\n//# debugId=11111111-2222-5333-9444-555555555555\n//# sourceMappingURL=foo.js.map\n', | ||
| ); | ||
| // Re-running with the same id is a no-op. | ||
| expect(injectDebugIdIntoJs(result, id)).toBe(result); | ||
| }); | ||
| }); | ||
|
|
||
| describe('injectDebugIdIntoSourceMap', () => { | ||
| const id = '11111111-2222-5333-9444-555555555555'; | ||
|
|
||
| it('adds a top-level debugId field', () => { | ||
| const map = JSON.stringify({ version: 3, sources: ['a.ts'], mappings: '' }); | ||
| const updated = JSON.parse(injectDebugIdIntoSourceMap(map, id)); | ||
| expect(updated.debugId).toBe(id); | ||
| expect(updated.version).toBe(3); | ||
| }); | ||
|
|
||
| it('overwrites an existing debugId field', () => { | ||
| const map = JSON.stringify({ version: 3, debugId: 'old', mappings: '' }); | ||
| const updated = JSON.parse(injectDebugIdIntoSourceMap(map, id)); | ||
| expect(updated.debugId).toBe(id); | ||
| }); | ||
|
|
||
| it('returns the original input when debugId already matches', () => { | ||
| const map = | ||
| '{\n "version": 3,\n "debugId": "11111111-2222-5333-9444-555555555555",\n "mappings": ""\n}'; | ||
| expect(injectDebugIdIntoSourceMap(map, id)).toBe(map); | ||
| }); | ||
|
|
||
| it('preserves indentation when updating a pretty-printed source map', () => { | ||
| const map = '{\n\t"version": 3,\n\t"mappings": ""\n}'; | ||
| expect(injectDebugIdIntoSourceMap(map, id)).toBe( | ||
| '{\n\t"version": 3,\n\t"mappings": "",\n\t"debugId": "11111111-2222-5333-9444-555555555555"\n}', | ||
| ); | ||
| }); | ||
|
|
||
| it('returns the original input when JSON is malformed', () => { | ||
| const malformed = '{ this is not json'; | ||
| expect(injectDebugIdIntoSourceMap(malformed, id)).toBe(malformed); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There would also be the possibility of using an existing implementation, e.g. of
uuid.Happy to make this change.