Skip to content

Commit e3ea926

Browse files
Merge pull request #19493 from Snuffleupagus/URL-parse
Introduce some `URL.parse()` usage in the code-base
2 parents 34ef74c + c2e3330 commit e3ea926

File tree

5 files changed

+52
-72
lines changed

5 files changed

+52
-72
lines changed

src/display/api.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -525,18 +525,20 @@ function getUrlProp(val) {
525525
if (val instanceof URL) {
526526
return val.href;
527527
}
528-
try {
529-
// The full path is required in the 'url' field.
530-
return new URL(val, window.location).href;
531-
} catch {
528+
if (typeof val === "string") {
532529
if (
533530
typeof PDFJSDev !== "undefined" &&
534531
PDFJSDev.test("GENERIC") &&
535-
isNodeJS &&
536-
typeof val === "string"
532+
isNodeJS
537533
) {
538534
return val; // Use the url as-is in Node.js environments.
539535
}
536+
537+
// The full path is required in the 'url' field.
538+
const url = URL.parse(val, window.location);
539+
if (url) {
540+
return url.href;
541+
}
540542
}
541543
throw new Error(
542544
"Invalid PDF url data: " +
@@ -2082,14 +2084,9 @@ class PDFWorker {
20822084
// Check if URLs have the same origin. For non-HTTP based URLs, returns
20832085
// false.
20842086
this._isSameOrigin = (baseUrl, otherUrl) => {
2085-
let base;
2086-
try {
2087-
base = new URL(baseUrl);
2088-
if (!base.origin || base.origin === "null") {
2089-
return false; // non-HTTP url
2090-
}
2091-
} catch {
2092-
return false;
2087+
const base = URL.parse(baseUrl);
2088+
if (!base?.origin || base.origin === "null") {
2089+
return false; // non-HTTP url
20932090
}
20942091
const other = new URL(otherUrl, base);
20952092
return base.origin === other.origin;
@@ -2202,7 +2199,7 @@ class PDFWorker {
22022199
if (
22032200
typeof PDFJSDev !== "undefined" &&
22042201
PDFJSDev.test("GENERIC") &&
2205-
!PDFWorker._isSameOrigin(window.location.href, workerSrc)
2202+
!PDFWorker._isSameOrigin(window.location, workerSrc)
22062203
) {
22072204
workerSrc = PDFWorker._createCDNWrapper(
22082205
new URL(workerSrc, window.location).href

src/display/display_utils.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -402,13 +402,9 @@ function isValidFetchUrl(url, baseUrl) {
402402
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
403403
throw new Error("Not implemented: isValidFetchUrl");
404404
}
405-
try {
406-
const { protocol } = baseUrl ? new URL(url, baseUrl) : new URL(url);
407-
// The Fetch API only supports the http/https protocols, and not file/ftp.
408-
return protocol === "http:" || protocol === "https:";
409-
} catch {
410-
return false; // `new URL()` will throw on incorrect data.
411-
}
405+
const res = baseUrl ? URL.parse(url, baseUrl) : URL.parse(url);
406+
// The Fetch API only supports the http/https protocols, and not file/ftp.
407+
return res?.protocol === "http:" || res?.protocol === "https:";
412408
}
413409

414410
/**

src/display/network_utils.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,8 @@ function createHeaders(isHttp, httpHeaders) {
3333
}
3434

3535
function getResponseOrigin(url) {
36-
try {
37-
return new URL(url).origin;
38-
} catch {
39-
// `new URL()` will throw on incorrect data.
40-
}
4136
// Notably, null is distinct from "null" string (e.g. from file:-URLs).
42-
return null;
37+
return URL.parse(url)?.origin ?? null;
4338
}
4439

4540
function validateRangeRequestCapabilities({

src/shared/util.js

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -412,35 +412,28 @@ function createValidAbsoluteUrl(url, baseUrl = null, options = null) {
412412
if (!url) {
413413
return null;
414414
}
415-
try {
416-
if (options && typeof url === "string") {
417-
// Let URLs beginning with "www." default to using the "http://" protocol.
418-
if (options.addDefaultProtocol && url.startsWith("www.")) {
419-
const dots = url.match(/\./g);
420-
// Avoid accidentally matching a *relative* URL pointing to a file named
421-
// e.g. "www.pdf" or similar.
422-
if (dots?.length >= 2) {
423-
url = `http://${url}`;
424-
}
425-
}
426-
427-
// According to ISO 32000-1:2008, section 12.6.4.7, URIs should be encoded
428-
// in 7-bit ASCII. Some bad PDFs use UTF-8 encoding; see bug 1122280.
429-
if (options.tryConvertEncoding) {
430-
try {
431-
url = stringToUTF8String(url);
432-
} catch {}
415+
if (options && typeof url === "string") {
416+
// Let URLs beginning with "www." default to using the "http://" protocol.
417+
if (options.addDefaultProtocol && url.startsWith("www.")) {
418+
const dots = url.match(/\./g);
419+
// Avoid accidentally matching a *relative* URL pointing to a file named
420+
// e.g. "www.pdf" or similar.
421+
if (dots?.length >= 2) {
422+
url = `http://${url}`;
433423
}
434424
}
435425

436-
const absoluteUrl = baseUrl ? new URL(url, baseUrl) : new URL(url);
437-
if (_isValidProtocol(absoluteUrl)) {
438-
return absoluteUrl;
426+
// According to ISO 32000-1:2008, section 12.6.4.7, URIs should be encoded
427+
// in 7-bit ASCII. Some bad PDFs use UTF-8 encoding; see bug 1122280.
428+
if (options.tryConvertEncoding) {
429+
try {
430+
url = stringToUTF8String(url);
431+
} catch {}
439432
}
440-
} catch {
441-
/* `new URL()` will throw on incorrect data. */
442433
}
443-
return null;
434+
435+
const absoluteUrl = baseUrl ? URL.parse(url, baseUrl) : URL.parse(url);
436+
return _isValidProtocol(absoluteUrl) ? absoluteUrl : null;
444437
}
445438

446439
function shadow(obj, prop, value, nonSerializable = false) {

web/app.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,35 +2274,34 @@ if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
22742274
}
22752275

22762276
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
2277-
const HOSTED_VIEWER_ORIGINS = [
2277+
const HOSTED_VIEWER_ORIGINS = new Set([
22782278
"null",
22792279
"http://mozilla.github.io",
22802280
"https://mozilla.github.io",
2281-
];
2281+
]);
22822282
// eslint-disable-next-line no-var
22832283
var validateFileURL = function (file) {
22842284
if (!file) {
22852285
return;
22862286
}
2287-
try {
2288-
const viewerOrigin = new URL(window.location.href).origin || "null";
2289-
if (HOSTED_VIEWER_ORIGINS.includes(viewerOrigin)) {
2290-
// Hosted or local viewer, allow for any file locations
2291-
return;
2292-
}
2293-
const fileOrigin = new URL(file, window.location.href).origin;
2294-
// Removing of the following line will not guarantee that the viewer will
2295-
// start accepting URLs from foreign origin -- CORS headers on the remote
2296-
// server must be properly configured.
2297-
if (fileOrigin !== viewerOrigin) {
2298-
throw new Error("file origin does not match viewer's");
2299-
}
2300-
} catch (ex) {
2301-
PDFViewerApplication._documentError("pdfjs-loading-error", {
2302-
message: ex.message,
2303-
});
2304-
throw ex;
2287+
const viewerOrigin = URL.parse(window.location)?.origin || "null";
2288+
if (HOSTED_VIEWER_ORIGINS.has(viewerOrigin)) {
2289+
// Hosted or local viewer, allow for any file locations
2290+
return;
2291+
}
2292+
const fileOrigin = URL.parse(file, window.location)?.origin;
2293+
if (fileOrigin === viewerOrigin) {
2294+
return;
23052295
}
2296+
const ex = new Error("file origin does not match viewer's");
2297+
2298+
PDFViewerApplication._documentError("pdfjs-loading-error", {
2299+
message: ex.message,
2300+
});
2301+
// Removing of the following line will not guarantee that the viewer will
2302+
// start accepting URLs from foreign origin -- CORS headers on the remote
2303+
// server must be properly configured.
2304+
throw ex;
23062305
};
23072306

23082307
// eslint-disable-next-line no-var

0 commit comments

Comments
 (0)