Merge pull request #21718 from calixteman/fix/file-param-encoding

Don't re-encode the `file` parameter in the viewer
This commit is contained in:
Tim van der Meij 2026-08-06 21:59:41 +02:00 committed by GitHub
commit 93c2ae4896
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 5 deletions

View File

@ -1359,6 +1359,36 @@ describe("PDF viewer", () => {
}); });
}); });
describe("File param with a relative URL and a query string (issue 20137)", () => {
let pages;
beforeEach(async () => {
// The `file` parameter is `/test/pdfs/basicapi.pdf?token=%2Ffoo`.
pages = await loadAndWait(
"basicapi.pdf%3Ftoken%3D%252Ffoo",
".textLayer .endOfContent"
);
});
afterEach(async () => {
await closePages(pages);
});
it("must not re-encode the file param", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
const pdfUrl = await page.evaluate(
() => window.PDFViewerApplication.url
);
expect(pdfUrl)
.withContext(`In ${browserName}`)
.toBe("/test/pdfs/basicapi.pdf?token=%2Ffoo");
})
);
});
});
describe("Keyboard scrolling on startup (bug 843653)", () => { describe("Keyboard scrolling on startup (bug 843653)", () => {
let pages; let pages;

View File

@ -888,11 +888,13 @@ const PDFViewerApplication = {
const queryString = document.location.search.substring(1); const queryString = document.location.search.substring(1);
const params = parseQueryString(queryString); const params = parseQueryString(queryString);
file = params.get("file") ?? AppOptions.get("defaultUrl"); file = params.get("file") ?? AppOptions.get("defaultUrl");
try { // Note that `parseQueryString` has already percent-decoded the parameter,
file = new URL(file).href; // hence it's used as-is below: re-encoding it would break URLs with e.g.
} catch { // a query string or a percent-encoded path (issue 20137).
file = encodeURIComponent(file).replaceAll("%2F", "/"); // In a relative URL a "#" is assumed to be part of the filename, rather
} // than a fragment separator, since the viewer takes its own hash
// parameters from the *viewer* URL (issue 19990).
file = URL.parse(file)?.href ?? file.replaceAll("#", "%23");
validateFileURL(file); validateFileURL(file);
} else if (PDFJSDev.test("MOZCENTRAL")) { } else if (PDFJSDev.test("MOZCENTRAL")) {
file = window.location.href; file = window.location.href;