Merge pull request #19493 from Snuffleupagus/URL-parse
Introduce some `URL.parse()` usage in the code-base
This commit is contained in:
commit
e3ea92603d
@ -525,18 +525,20 @@ function getUrlProp(val) {
|
|||||||
if (val instanceof URL) {
|
if (val instanceof URL) {
|
||||||
return val.href;
|
return val.href;
|
||||||
}
|
}
|
||||||
try {
|
if (typeof val === "string") {
|
||||||
// The full path is required in the 'url' field.
|
|
||||||
return new URL(val, window.location).href;
|
|
||||||
} catch {
|
|
||||||
if (
|
if (
|
||||||
typeof PDFJSDev !== "undefined" &&
|
typeof PDFJSDev !== "undefined" &&
|
||||||
PDFJSDev.test("GENERIC") &&
|
PDFJSDev.test("GENERIC") &&
|
||||||
isNodeJS &&
|
isNodeJS
|
||||||
typeof val === "string"
|
|
||||||
) {
|
) {
|
||||||
return val; // Use the url as-is in Node.js environments.
|
return val; // Use the url as-is in Node.js environments.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The full path is required in the 'url' field.
|
||||||
|
const url = URL.parse(val, window.location);
|
||||||
|
if (url) {
|
||||||
|
return url.href;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"Invalid PDF url data: " +
|
"Invalid PDF url data: " +
|
||||||
@ -2082,15 +2084,10 @@ class PDFWorker {
|
|||||||
// Check if URLs have the same origin. For non-HTTP based URLs, returns
|
// Check if URLs have the same origin. For non-HTTP based URLs, returns
|
||||||
// false.
|
// false.
|
||||||
this._isSameOrigin = (baseUrl, otherUrl) => {
|
this._isSameOrigin = (baseUrl, otherUrl) => {
|
||||||
let base;
|
const base = URL.parse(baseUrl);
|
||||||
try {
|
if (!base?.origin || base.origin === "null") {
|
||||||
base = new URL(baseUrl);
|
|
||||||
if (!base.origin || base.origin === "null") {
|
|
||||||
return false; // non-HTTP url
|
return false; // non-HTTP url
|
||||||
}
|
}
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const other = new URL(otherUrl, base);
|
const other = new URL(otherUrl, base);
|
||||||
return base.origin === other.origin;
|
return base.origin === other.origin;
|
||||||
};
|
};
|
||||||
@ -2202,7 +2199,7 @@ class PDFWorker {
|
|||||||
if (
|
if (
|
||||||
typeof PDFJSDev !== "undefined" &&
|
typeof PDFJSDev !== "undefined" &&
|
||||||
PDFJSDev.test("GENERIC") &&
|
PDFJSDev.test("GENERIC") &&
|
||||||
!PDFWorker._isSameOrigin(window.location.href, workerSrc)
|
!PDFWorker._isSameOrigin(window.location, workerSrc)
|
||||||
) {
|
) {
|
||||||
workerSrc = PDFWorker._createCDNWrapper(
|
workerSrc = PDFWorker._createCDNWrapper(
|
||||||
new URL(workerSrc, window.location).href
|
new URL(workerSrc, window.location).href
|
||||||
|
|||||||
@ -402,13 +402,9 @@ function isValidFetchUrl(url, baseUrl) {
|
|||||||
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
||||||
throw new Error("Not implemented: isValidFetchUrl");
|
throw new Error("Not implemented: isValidFetchUrl");
|
||||||
}
|
}
|
||||||
try {
|
const res = baseUrl ? URL.parse(url, baseUrl) : URL.parse(url);
|
||||||
const { protocol } = baseUrl ? new URL(url, baseUrl) : new URL(url);
|
|
||||||
// The Fetch API only supports the http/https protocols, and not file/ftp.
|
// The Fetch API only supports the http/https protocols, and not file/ftp.
|
||||||
return protocol === "http:" || protocol === "https:";
|
return res?.protocol === "http:" || res?.protocol === "https:";
|
||||||
} catch {
|
|
||||||
return false; // `new URL()` will throw on incorrect data.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -33,13 +33,8 @@ function createHeaders(isHttp, httpHeaders) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getResponseOrigin(url) {
|
function getResponseOrigin(url) {
|
||||||
try {
|
|
||||||
return new URL(url).origin;
|
|
||||||
} catch {
|
|
||||||
// `new URL()` will throw on incorrect data.
|
|
||||||
}
|
|
||||||
// Notably, null is distinct from "null" string (e.g. from file:-URLs).
|
// Notably, null is distinct from "null" string (e.g. from file:-URLs).
|
||||||
return null;
|
return URL.parse(url)?.origin ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateRangeRequestCapabilities({
|
function validateRangeRequestCapabilities({
|
||||||
|
|||||||
@ -412,7 +412,6 @@ function createValidAbsoluteUrl(url, baseUrl = null, options = null) {
|
|||||||
if (!url) {
|
if (!url) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
if (options && typeof url === "string") {
|
if (options && typeof url === "string") {
|
||||||
// Let URLs beginning with "www." default to using the "http://" protocol.
|
// Let URLs beginning with "www." default to using the "http://" protocol.
|
||||||
if (options.addDefaultProtocol && url.startsWith("www.")) {
|
if (options.addDefaultProtocol && url.startsWith("www.")) {
|
||||||
@ -433,14 +432,8 @@ function createValidAbsoluteUrl(url, baseUrl = null, options = null) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const absoluteUrl = baseUrl ? new URL(url, baseUrl) : new URL(url);
|
const absoluteUrl = baseUrl ? URL.parse(url, baseUrl) : URL.parse(url);
|
||||||
if (_isValidProtocol(absoluteUrl)) {
|
return _isValidProtocol(absoluteUrl) ? absoluteUrl : null;
|
||||||
return absoluteUrl;
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
/* `new URL()` will throw on incorrect data. */
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function shadow(obj, prop, value, nonSerializable = false) {
|
function shadow(obj, prop, value, nonSerializable = false) {
|
||||||
|
|||||||
25
web/app.js
25
web/app.js
@ -2274,35 +2274,34 @@ if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
|
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
|
||||||
const HOSTED_VIEWER_ORIGINS = [
|
const HOSTED_VIEWER_ORIGINS = new Set([
|
||||||
"null",
|
"null",
|
||||||
"http://mozilla.github.io",
|
"http://mozilla.github.io",
|
||||||
"https://mozilla.github.io",
|
"https://mozilla.github.io",
|
||||||
];
|
]);
|
||||||
// eslint-disable-next-line no-var
|
// eslint-disable-next-line no-var
|
||||||
var validateFileURL = function (file) {
|
var validateFileURL = function (file) {
|
||||||
if (!file) {
|
if (!file) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
const viewerOrigin = URL.parse(window.location)?.origin || "null";
|
||||||
const viewerOrigin = new URL(window.location.href).origin || "null";
|
if (HOSTED_VIEWER_ORIGINS.has(viewerOrigin)) {
|
||||||
if (HOSTED_VIEWER_ORIGINS.includes(viewerOrigin)) {
|
|
||||||
// Hosted or local viewer, allow for any file locations
|
// Hosted or local viewer, allow for any file locations
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const fileOrigin = new URL(file, window.location.href).origin;
|
const fileOrigin = URL.parse(file, window.location)?.origin;
|
||||||
// Removing of the following line will not guarantee that the viewer will
|
if (fileOrigin === viewerOrigin) {
|
||||||
// start accepting URLs from foreign origin -- CORS headers on the remote
|
return;
|
||||||
// server must be properly configured.
|
|
||||||
if (fileOrigin !== viewerOrigin) {
|
|
||||||
throw new Error("file origin does not match viewer's");
|
|
||||||
}
|
}
|
||||||
} catch (ex) {
|
const ex = new Error("file origin does not match viewer's");
|
||||||
|
|
||||||
PDFViewerApplication._documentError("pdfjs-loading-error", {
|
PDFViewerApplication._documentError("pdfjs-loading-error", {
|
||||||
message: ex.message,
|
message: ex.message,
|
||||||
});
|
});
|
||||||
|
// Removing of the following line will not guarantee that the viewer will
|
||||||
|
// start accepting URLs from foreign origin -- CORS headers on the remote
|
||||||
|
// server must be properly configured.
|
||||||
throw ex;
|
throw ex;
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// eslint-disable-next-line no-var
|
// eslint-disable-next-line no-var
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user