mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-20 15:18:08 +02:00
Add better validation for the "PREFERENCE" kind AppOptions
Given that the "PREFERENCE" kind is used e.g. to generate the preference-list for the Firefox PDF Viewer, those options need to be carefully validated. With this patch we'll now check this unconditionally in development mode, during testing, and when creating the preferences in the gulpfile.
This commit is contained in:
parent
70015ffe6b
commit
90b2664622
6 changed files with 90 additions and 28 deletions
|
@ -404,6 +404,35 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
|
|||
|
||||
const userOptions = Object.create(null);
|
||||
|
||||
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING || LIB")) {
|
||||
// Ensure that the `defaultOptions` are correctly specified.
|
||||
for (const name in defaultOptions) {
|
||||
const { value, kind } = defaultOptions[name];
|
||||
|
||||
if (kind & OptionKind.PREFERENCE) {
|
||||
if (kind === OptionKind.PREFERENCE) {
|
||||
throw new Error(`Cannot use only "PREFERENCE" kind: ${name}`);
|
||||
}
|
||||
if (kind & OptionKind.BROWSER) {
|
||||
throw new Error(`Cannot mix "PREFERENCE" and "BROWSER" kind: ${name}`);
|
||||
}
|
||||
if (compatibilityParams[name] !== undefined) {
|
||||
throw new Error(
|
||||
`Should not have compatibility-value for "PREFERENCE" kind: ${name}`
|
||||
);
|
||||
}
|
||||
// Only "simple" preference-values are allowed.
|
||||
if (
|
||||
typeof value !== "boolean" &&
|
||||
typeof value !== "string" &&
|
||||
!Number.isInteger(value)
|
||||
) {
|
||||
throw new Error(`Invalid value for "PREFERENCE" kind: ${name}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class AppOptions {
|
||||
constructor() {
|
||||
throw new Error("Cannot initialize AppOptions.");
|
||||
|
@ -421,36 +450,20 @@ class AppOptions {
|
|||
return undefined;
|
||||
}
|
||||
|
||||
static getAll(kind = null) {
|
||||
static getAll(kind = null, defaultOnly = false) {
|
||||
const options = Object.create(null);
|
||||
for (const name in defaultOptions) {
|
||||
const defaultOption = defaultOptions[name];
|
||||
if (kind) {
|
||||
if (!(kind & defaultOption.kind)) {
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
(typeof PDFJSDev === "undefined" || PDFJSDev.test("LIB")) &&
|
||||
kind === OptionKind.PREFERENCE
|
||||
) {
|
||||
if (defaultOption.kind & OptionKind.BROWSER) {
|
||||
throw new Error(`Invalid kind for preference: ${name}`);
|
||||
}
|
||||
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}`);
|
||||
}
|
||||
if (kind && !(kind & defaultOption.kind)) {
|
||||
continue;
|
||||
}
|
||||
if (defaultOnly) {
|
||||
options[name] = defaultOption.value;
|
||||
continue;
|
||||
}
|
||||
const userOption = userOptions[name];
|
||||
|
||||
options[name] =
|
||||
userOption !== undefined
|
||||
? userOption
|
||||
|
|
|
@ -23,7 +23,7 @@ import { AppOptions, OptionKind } from "./app_options.js";
|
|||
class BasePreferences {
|
||||
#defaults = Object.freeze(
|
||||
typeof PDFJSDev === "undefined"
|
||||
? AppOptions.getAll(OptionKind.PREFERENCE)
|
||||
? AppOptions.getAll(OptionKind.PREFERENCE, /* defaultOnly = */ true)
|
||||
: PDFJSDev.eval("DEFAULT_PREFERENCES")
|
||||
);
|
||||
|
||||
|
@ -48,7 +48,7 @@ class BasePreferences {
|
|||
({ browserPrefs, prefs }) => {
|
||||
const BROWSER_PREFS =
|
||||
typeof PDFJSDev === "undefined"
|
||||
? AppOptions.getAll(OptionKind.BROWSER)
|
||||
? AppOptions.getAll(OptionKind.BROWSER, /* defaultOnly = */ true)
|
||||
: PDFJSDev.eval("BROWSER_PREFERENCES");
|
||||
const options = Object.create(null);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue