1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

Ensure that a missing/invalid "Content-Range" header is handled in PDFNetworkStream (issue 19075)

In the event that the "Content-Range" header is missing/invalid, loading will now be aborted rather than hanging indefinitely.
This commit is contained in:
Jonas Jenwald 2024-11-27 12:33:39 +01:00
parent e04b62ba2d
commit 2661d0623b
3 changed files with 64 additions and 7 deletions

View file

@ -13,7 +13,10 @@
* limitations under the License.
*/
import { AbortException } from "../../src/shared/util.js";
import {
AbortException,
UnexpectedResponseException,
} from "../../src/shared/util.js";
import { PDFNetworkStream } from "../../src/display/network.js";
import { testCrossOriginRedirects } from "./common_pdfstream_tests.js";
import { TestPdfsServer } from "./test_utils.js";
@ -118,6 +121,44 @@ describe("network", function () {
expect(fullReaderCancelled).toEqual(true);
});
it(`handle reading ranges with missing/invalid "Content-Range" header`, async function () {
async function readRanges(mode) {
const rangeSize = 32768;
const stream = new PDFNetworkStream({
url: `${pdf1}?test-network-break-ranges=${mode}`,
length: pdf1Length,
rangeChunkSize: rangeSize,
disableStream: true,
disableRange: false,
});
const fullReader = stream.getFullReader();
await fullReader.headersReady;
// Ensure that range requests are supported.
expect(fullReader.isRangeSupported).toEqual(true);
// We shall be able to close the full reader without issues.
fullReader.cancel(new AbortException("Don't need fullReader."));
const rangeReader = stream.getRangeReader(
pdf1Length - rangeSize,
pdf1Length
);
try {
await rangeReader.read();
// Shouldn't get here.
expect(false).toEqual(true);
} catch (ex) {
expect(ex instanceof UnexpectedResponseException).toEqual(true);
expect(ex.status).toEqual(0);
}
}
await Promise.all([readRanges("missing"), readRanges("invalid")]);
});
describe("Redirects", function () {
beforeAll(async function () {
await TestPdfsServer.ensureStarted();

View file

@ -177,6 +177,7 @@ class WebServer {
this.#serveFileRange(
response,
localURL,
url.searchParams,
fileSize,
start,
isNaN(end) ? fileSize : end + 1
@ -307,7 +308,7 @@ class WebServer {
stream.pipe(response);
}
#serveFileRange(response, fileURL, fileSize, start, end) {
#serveFileRange(response, fileURL, searchParams, fileSize, start, end) {
if (end > fileSize || start > end) {
response.writeHead(416);
response.end();
@ -330,6 +331,16 @@ class WebServer {
"Content-Range",
`bytes ${start}-${end - 1}/${fileSize}`
);
// Support test in `test/unit/network_spec.js`.
switch (searchParams.get("test-network-break-ranges")) {
case "missing":
response.removeHeader("Content-Range");
break;
case "invalid":
response.setHeader("Content-Range", "bytes abc-def/qwerty");
break;
}
response.writeHead(206);
stream.pipe(response);
}