Skip to content

core(source-maps): guard against out-of-bounds sourceIndex#16969

Merged
connorjclark merged 1 commit into
GoogleChrome:mainfrom
ducky-duke:fix/sourcemap-optional-chaining
Apr 17, 2026
Merged

core(source-maps): guard against out-of-bounds sourceIndex#16969
connorjclark merged 1 commit into
GoogleChrome:mainfrom
ducky-duke:fix/sourcemap-optional-chaining

Conversation

@ducky-duke

Copy link
Copy Markdown
Contributor

Summary

When a source map has an empty or malformed sources array, this.#sourceInfos[sourceIndex] is undefined, causing:

TypeError: Cannot read properties of undefined (reading 'sourceURL')
    at SourceMap.parseMap (SourceMap.js:384:56)

This crash is caught by the try/catch in #ensureMappingsProcessed, so the scan continues — but it logs a noisy error and prevents the DuplicatedJavaScript insight from running for the affected page.

Changes

  • core/lib/cdt/generated/SourceMap.js: Add optional chaining (?.) to both this.#sourceInfos[sourceIndex].sourceURL access sites in parseMap (lines 384 and 407).
  • build/build-cdt-lib.js:
    • Add a rawCodeToReplace entry so the fix survives CDT source re-rolls.
    • Use replaceAll instead of replace in the raw-code replacement loop to ensure all occurrences of a pattern are substituted (previously only the first match was replaced).

Reproduction

Run Lighthouse on any page that serves a source map with an empty sources array or where the VLQ-decoded source index exceeds the bounds of the sources array.

@ducky-duke ducky-duke requested a review from a team as a code owner April 16, 2026 11:31
@ducky-duke ducky-duke requested review from paulirish and removed request for a team April 16, 2026 11:31
@google-cla

google-cla Bot commented Apr 16, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@ducky-duke ducky-duke force-pushed the fix/sourcemap-optional-chaining branch from 2f5786a to f774bcb Compare April 16, 2026 11:39
@connorjclark connorjclark changed the title fix: guard against out-of-bounds sourceIndex in SourceMap.parseMap core(source-maps): guard against out-of-bounds sourceIndex Apr 17, 2026
@connorjclark

Copy link
Copy Markdown
Collaborator

looks good, thanks! CI failures are unrelated.

@connorjclark connorjclark merged commit 9f1f118 into GoogleChrome:main Apr 17, 2026
25 of 27 checks passed
@paulirish

Copy link
Copy Markdown
Member

Good fix.. Appreciate the contribution @ducky-duke !

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants