Repository navigation
fix(cache): validate addAll requests before starting fetches - #6254
ferhatelmas wants to merge 2 commits into
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
mcollina
left a comment
There was a problem hiding this comment.
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:
- 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 likeftp://example.com/to cover the scheme check path? - 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>
|
Improved the test and added a comment for the intention, PTAL |
|
@KhafraDev ptal |
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. |
|
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. Looks like a spec issue, as that loop already has problems: w3c/ServiceWorker#1543. |
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
Breaking Changes and Deprecations
N/A
Status