Skip to content

Commit

Permalink
Guard navigator-object accesses in src/-files (issue 15728)
Browse files Browse the repository at this point in the history
There are environments that include *incomplete* polyfills for the `navigator`-object, which may thus cause the PDF.js library to break.
Despite that clearly not being our fault, it may still result in bug reports filed against the PDF.js project; see e.g. 15728.

Currently this even seem to affect *the latest* version of Node.js; see e.g. [here].

*Please note:* Thanks to the pre-processor none of these changes affect the Firefox PDF Viewer, however it does add "overhead" when working with and reviewing the affected code (which is why I'm not crazy about this).
  • Loading branch information
Snuffleupagus committed Oct 28, 2023
1 parent 238f3e7 commit c1fef7d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/display/font_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ class FontLoader {
supported = true;
} else if (
typeof navigator !== "undefined" &&
typeof navigator?.userAgent === "string" &&
// User agent string sniffing is bad, but there is no reliable way to
// tell if the font is fully loaded and ready to be used with canvas.
/Mozilla\/5.0.*?rv:\d+.*? Gecko/.test(navigator.userAgent)
Expand Down
13 changes: 7 additions & 6 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,14 +616,15 @@ class FeatureTest {

static get platform() {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) &&
typeof navigator === "undefined"
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
(typeof navigator !== "undefined" &&
typeof navigator?.platform === "string")
) {
return shadow(this, "platform", { isMac: false });
return shadow(this, "platform", {
isMac: navigator.platform.includes("Mac"),
});
}
return shadow(this, "platform", {
isMac: navigator.platform.includes("Mac"),
});
return shadow(this, "platform", { isMac: false });
}

static get isCSSRoundSupported() {
Expand Down

0 comments on commit c1fef7d

Please sign in to comment.