From c1fef7d2f2e213a460165e050032fbf479dcc719 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 27 Jul 2023 12:46:00 +0200 Subject: [PATCH] Guard `navigator`-object accesses in `src/`-files (issue 15728) 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). --- src/display/font_loader.js | 1 + src/shared/util.js | 13 +++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/display/font_loader.js b/src/display/font_loader.js index 23f4a6a1c0e4e..7ca4ebbf5da6d 100644 --- a/src/display/font_loader.js +++ b/src/display/font_loader.js @@ -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) diff --git a/src/shared/util.js b/src/shared/util.js index ed1ec6886265b..5a4874ffeb6cd 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -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() {