From ee316007cfbcfbd26227b450a02bcfd54b899889 Mon Sep 17 00:00:00 2001 From: 0z5a Date: Sun, 15 Nov 2026 11:06:12 +0100 Subject: [PATCH] fix(range-requests): return 416 when the range starts at or past the end of the body calculateEffectiveBoundaries() rejected an end past the blob and a negative start, but not a start at or past the end of the blob, so "bytes=256-" on a 256-byte response produced a 206 with an empty body and "Content-Range: bytes 256-255/256". Treat such ranges as unsatisfiable, like the other out-of-range cases. --- .../src/utils/calculateEffectiveBoundaries.ts | 7 ++++++- .../sw/test-createPartialResponse.mjs | 14 ++++++++++++++ .../sw/utils/test-calculateEffectiveBoundaries.mjs | 9 +++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/workbox-range-requests/src/utils/calculateEffectiveBoundaries.ts b/packages/workbox-range-requests/src/utils/calculateEffectiveBoundaries.ts index d4cb72478..5ea657426 100644 --- a/packages/workbox-range-requests/src/utils/calculateEffectiveBoundaries.ts +++ b/packages/workbox-range-requests/src/utils/calculateEffectiveBoundaries.ts @@ -35,7 +35,12 @@ function calculateEffectiveBoundaries( const blobSize = blob.size; - if ((end && end > blobSize) || (start && start < 0)) { + if ( + (end && end > blobSize) || + (start && start < 0) || + // A range that starts at or past the end of the blob can't be satisfied. + (start !== undefined && start >= blobSize) + ) { throw new WorkboxError('range-not-satisfiable', { size: blobSize, end, diff --git a/test/workbox-range-requests/sw/test-createPartialResponse.mjs b/test/workbox-range-requests/sw/test-createPartialResponse.mjs index 927b83930..755914dd3 100644 --- a/test/workbox-range-requests/sw/test-createPartialResponse.mjs +++ b/test/workbox-range-requests/sw/test-createPartialResponse.mjs @@ -51,6 +51,20 @@ describe(`createPartialResponse()`, function () { expect(response.status).to.equal(416); }); + it(`should return a Response with status 416 when the range starts at or past the end of the body`, async function () { + for (const range of [ + `bytes=${SOURCE_BLOB_SIZE}-`, + `bytes=${SOURCE_BLOB_SIZE + 1}-`, + ]) { + const request = new Request('/', {headers: {range}}); + const response = await createPartialResponse( + request, + new Response(SOURCE_BLOB), + ); + expect(response.status).to.equal(416, `for '${range}'`); + } + }); + it(`should return the expected Response when it's called with valid parameters`, async function () { const response = await createPartialResponse( VALID_REQUEST, diff --git a/test/workbox-range-requests/sw/utils/test-calculateEffectiveBoundaries.mjs b/test/workbox-range-requests/sw/utils/test-calculateEffectiveBoundaries.mjs index c0c8525c2..9c4745bd7 100644 --- a/test/workbox-range-requests/sw/utils/test-calculateEffectiveBoundaries.mjs +++ b/test/workbox-range-requests/sw/utils/test-calculateEffectiveBoundaries.mjs @@ -60,6 +60,15 @@ describe(`calculateEffectiveBoundaries()`, function () { ); }); + it(`should throw when it's called with a 'start' parameter equal to or larger than the blob's size`, async function () { + for (const start of [SOURCE_BLOB_SIZE, SOURCE_BLOB_SIZE + 1]) { + await expectError( + () => calculateEffectiveBoundaries(SOURCE_BLOB, start), + 'range-not-satisfiable', + ); + } + }); + it(`should return the expected boundaries when it's called with valid parameters`, function () { const testCases = [ [