Skip to content

fix(cache): validate addAll requests before starting fetches - #6254

Open
ferhatelmas wants to merge 2 commits into
nodejs:mainfrom
ferhatelmas:cache-addall
Open

ferhatelmas wants to merge 2 commits into
nodejs:mainfrom
ferhatelmas:cache-addall

Conversation

@ferhatelmas

@ferhatelmas ferhatelmas commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

This relates to...

Rationale

When Cache.addAll() receives a valid URL followed by an invalid URL,
it can start fetching the first URL before rejecting the invalid one.
If that fetch subsequently fails, its rejection is unhandled.

Changes

Features

N/A

Bug Fixes

  • Prepare and validate all requests before starting any fetches.
  • Add a test for a valid URL followed by an invalid URL.

Breaking Changes and Deprecations

N/A

Status

Prepare all requests before fetching so an invalid URL cannot leave
an earlier fetch with an unhandled rejection.

Signed-off-by: Ferhat Elmas <elmas.ferhat@gmail.com>
@codecov-commenter

codecov-commenter commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.77%. Comparing base (161e78e) to head (32f8e55).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6254      +/-   ##
==========================================
- Coverage   91.77%   91.77%   -0.01%     
==========================================
  Files         113      113              
  Lines       44875    44878       +3     
==========================================
+ Hits        41186    41188       +2     
- Misses       3689     3690       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mcollina mcollina 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.

Thanks! The fix looks right to me. On main, addAll([valid, invalid]) starts the first fetch before the second entry throws, so that fetch's promise never reaches Promise.all and, if it fails, ends up as an unhandled rejection that can crash the process even when the caller handled the addAll rejection.

On the spec side: the SW spec interleaves construction and fetching in step 5, so this isn't a literal match, but the literal text has a gap (a constructor throw at 5.1 doesn't abort earlier fetches; see w3c/ServiceWorker#1543). Chromium and Firefox both build and validate the whole list before fetching anything, which is exactly what this PR does. No WPT covers this case and the cache-storage WPT results are unchanged.

Two requests:

  1. The new test doesn't actually catch the regression: against main's cache.js, strictEqual(requests, 0) still passes because the first request hasn't reached the server yet when it's checked; the file only fails because of the unhandled rejection afterwards. Could you wait briefly (e.g. await sleep(100)) before asserting, and add a case with a non-HTTP(S) URL like ftp://example.com/ to cover the scheme check path?
  2. Nit: a short comment above the second loop explaining that fetches are intentionally deferred until every request is validated (matching browsers), so nobody "fixes" it back to the spec's step order later.

LGTM with the test fix.

Signed-off-by: Ferhat Elmas <elmas.ferhat@gmail.com>
@ferhatelmas

Copy link
Copy Markdown
Contributor Author

Improved the test and added a comment for the intention, PTAL

@mcollina

Copy link
Copy Markdown
Member

@KhafraDev ptal

@KhafraDev

Copy link
Copy Markdown
Member

When Cache.addAll() receives a valid URL followed by an invalid URL,
it can start fetching the first URL before rejecting the invalid one.
If that fetch subsequently fails, its rejection is unhandled.

AI does not know the correct behavior and therefore cannot be trusted with reasoning. I fail to see how, under any circumstance, we're handling this wrong. I hate that Opus 5.5/Neon/Astra suck at writing code in undici because they're generally good everywhere else, giving people false confidence in their low-effort, low-quality PRs.

@mcollina

Copy link
Copy Markdown
Member

The question here is more on the line of what is correct behavior. I had AI check Chromium and Firefox, and they match this behavior, while the spec matches what we do.
We are more compliant than them, and this is not covered by wpts. This does what I would expect from the code.

Looks like a spec issue, as that loop already has problems: w3c/ServiceWorker#1543.

@mcollina

Copy link
Copy Markdown
Member

This branch has not been deployed

No deployments
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.

4 participants