From f7cc33165494b45579c9d8536d3b7c1b1bcfc64c Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 3 Mar 2019 11:40:56 +0100 Subject: [PATCH] Add type validation to the `default_preferences` generation (PR 10548 follow-up) The generated `default_preferences.json` file is necessary when initializing the Firefox preferences, which only supports certain types, hence this patch adds additional validation to help prevent run-time errors in Firefox. Given that these changes add a code-path to `AppOptions.getAll` which could throw, the `OptionKind.PREFERENCE` branch is also modified to require *exact* matching to prevent (future) errors in the viewer. Finally the conditionally defined `defaultOptions` will no longer (potentially) be considered during the `gulp default_preferences` task, to make it more difficult for them to be accidentally included. --- gulpfile.js | 2 +- web/app.js | 4 +++- web/app_options.js | 14 ++++++++++---- web/preferences.js | 2 -- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index fe03105b6..ffc65f1f6 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -535,7 +535,7 @@ gulp.task('default_preferences-pre', function() { ], { base: 'src/', }), gulp.src([ 'web/*.js', - '!web/{pdfjs,preferences,viewer}.js', + '!web/{app,pdfjs,preferences,viewer}.js', ], { base: '.', }), ]).pipe(transform('utf8', preprocess)) .pipe(gulp.dest(DEFAULT_PREFERENCES_DIR + 'lib/')); diff --git a/web/app.js b/web/app.js index 65e31def5..da99a55f2 100644 --- a/web/app.js +++ b/web/app.js @@ -187,7 +187,9 @@ let PDFViewerApplication = { for (const name in prefs) { AppOptions.set(name, prefs[name]); } - } catch (reason) { } + } catch (reason) { + console.error(`_readPreferences: "${reason.message}".`); + } }, /** diff --git a/web/app_options.js b/web/app_options.js index ef5216ffe..ea9878314 100644 --- a/web/app_options.js +++ b/web/app_options.js @@ -222,7 +222,7 @@ const defaultOptions = { }, }; if (typeof PDFJSDev === 'undefined' || - PDFJSDev.test('!PRODUCTION || GENERIC')) { + PDFJSDev.test('!PRODUCTION || (GENERIC && !LIB)')) { defaultOptions.disablePreferences = { /** @type {boolean} */ value: false, @@ -262,9 +262,15 @@ class AppOptions { if ((kind & defaultOption.kind) === 0) { continue; } - if ((kind & OptionKind.PREFERENCE) !== 0) { - options[name] = defaultOption.value; - continue; + if (kind === OptionKind.PREFERENCE) { + const value = defaultOption.value, valueType = typeof value; + + if (valueType === 'boolean' || valueType === 'string' || + (valueType === 'number' && Number.isInteger(value))) { + options[name] = value; + continue; + } + throw new Error(`Invalid type for preference: ${name}`); } } const userOption = userOptions[name]; diff --git a/web/preferences.js b/web/preferences.js index d2fd8bd2b..ba3b1b53e 100644 --- a/web/preferences.js +++ b/web/preferences.js @@ -35,8 +35,6 @@ function getDefaultPreferences() { } }).then(function({ AppOptions, OptionKind, }) { return AppOptions.getAll(OptionKind.PREFERENCE); - }, function(reason) { - console.error(reason); }); } }