lib: refactor policy code for readability
Simplify a few particularly quirky bits of code, and add whitespace for readability. PR-URL: https://github.com/nodejs/node/pull/25629 Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
This commit is contained in:
parent
399ef4f536
commit
0f5c14c43e
@ -22,9 +22,11 @@ const kReactions = new SafeWeakMap();
|
|||||||
const kRelativeURLStringPattern = /^\.{0,2}\//;
|
const kRelativeURLStringPattern = /^\.{0,2}\//;
|
||||||
const { shouldAbortOnUncaughtException } = internalBinding('config');
|
const { shouldAbortOnUncaughtException } = internalBinding('config');
|
||||||
const { abort, exit, _rawDebug } = process;
|
const { abort, exit, _rawDebug } = process;
|
||||||
|
|
||||||
function REACTION_THROW(error) {
|
function REACTION_THROW(error) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
function REACTION_EXIT(error) {
|
function REACTION_EXIT(error) {
|
||||||
REACTION_LOG(error);
|
REACTION_LOG(error);
|
||||||
if (shouldAbortOnUncaughtException) {
|
if (shouldAbortOnUncaughtException) {
|
||||||
@ -32,9 +34,11 @@ function REACTION_EXIT(error) {
|
|||||||
}
|
}
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
function REACTION_LOG(error) {
|
function REACTION_LOG(error) {
|
||||||
_rawDebug(error.stack);
|
_rawDebug(error.stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
class Manifest {
|
class Manifest {
|
||||||
constructor(obj, manifestURL) {
|
constructor(obj, manifestURL) {
|
||||||
const integrities = {
|
const integrities = {
|
||||||
@ -44,6 +48,7 @@ class Manifest {
|
|||||||
__proto__: null,
|
__proto__: null,
|
||||||
integrity: REACTION_THROW,
|
integrity: REACTION_THROW,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (obj.onerror) {
|
if (obj.onerror) {
|
||||||
const behavior = obj.onerror;
|
const behavior = obj.onerror;
|
||||||
if (behavior === 'throw') {
|
if (behavior === 'throw') {
|
||||||
@ -55,8 +60,10 @@ class Manifest {
|
|||||||
throw new ERR_MANIFEST_UNKNOWN_ONERROR(behavior);
|
throw new ERR_MANIFEST_UNKNOWN_ONERROR(behavior);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
kReactions.set(this, Object.freeze(reactions));
|
kReactions.set(this, Object.freeze(reactions));
|
||||||
const manifestEntries = entries(obj.resources);
|
const manifestEntries = entries(obj.resources);
|
||||||
|
|
||||||
for (var i = 0; i < manifestEntries.length; i++) {
|
for (var i = 0; i < manifestEntries.length; i++) {
|
||||||
let url = manifestEntries[i][0];
|
let url = manifestEntries[i][0];
|
||||||
const integrity = manifestEntries[i][1].integrity;
|
const integrity = manifestEntries[i][1].integrity;
|
||||||
@ -65,10 +72,12 @@ class Manifest {
|
|||||||
if (RegExpTest(kRelativeURLStringPattern, url)) {
|
if (RegExpTest(kRelativeURLStringPattern, url)) {
|
||||||
url = new URL(url, manifestURL).href;
|
url = new URL(url, manifestURL).href;
|
||||||
}
|
}
|
||||||
|
|
||||||
const sri = Object.freeze(SRI.parse(integrity));
|
const sri = Object.freeze(SRI.parse(integrity));
|
||||||
if (url in integrities) {
|
if (url in integrities) {
|
||||||
const old = integrities[url];
|
const old = integrities[url];
|
||||||
let mismatch = false;
|
let mismatch = false;
|
||||||
|
|
||||||
if (old.length !== sri.length) {
|
if (old.length !== sri.length) {
|
||||||
mismatch = true;
|
mismatch = true;
|
||||||
} else {
|
} else {
|
||||||
@ -85,6 +94,7 @@ class Manifest {
|
|||||||
break compare;
|
break compare;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mismatch) {
|
if (mismatch) {
|
||||||
throw new ERR_MANIFEST_INTEGRITY_MISMATCH(url);
|
throw new ERR_MANIFEST_INTEGRITY_MISMATCH(url);
|
||||||
}
|
}
|
||||||
@ -96,10 +106,12 @@ class Manifest {
|
|||||||
kIntegrities.set(this, integrities);
|
kIntegrities.set(this, integrities);
|
||||||
Object.freeze(this);
|
Object.freeze(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
assertIntegrity(url, content) {
|
assertIntegrity(url, content) {
|
||||||
debug(`Checking integrity of ${url}`);
|
debug(`Checking integrity of ${url}`);
|
||||||
const integrities = kIntegrities.get(this);
|
const integrities = kIntegrities.get(this);
|
||||||
const realIntegrities = new Map();
|
const realIntegrities = new Map();
|
||||||
|
|
||||||
if (integrities && url in integrities) {
|
if (integrities && url in integrities) {
|
||||||
const integrityEntries = integrities[url];
|
const integrityEntries = integrities[url];
|
||||||
// Avoid clobbered Symbol.iterator
|
// Avoid clobbered Symbol.iterator
|
||||||
@ -122,6 +134,7 @@ class Manifest {
|
|||||||
kReactions.get(this).integrity(error);
|
kReactions.get(this).integrity(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lock everything down to avoid problems even if reference is leaked somehow
|
// Lock everything down to avoid problems even if reference is leaked somehow
|
||||||
Object.setPrototypeOf(Manifest, null);
|
Object.setPrototypeOf(Manifest, null);
|
||||||
Object.setPrototypeOf(Manifest.prototype, null);
|
Object.setPrototypeOf(Manifest.prototype, null);
|
||||||
|
@ -18,29 +18,27 @@ const { freeze } = Object;
|
|||||||
Object.seal(kSRIPattern);
|
Object.seal(kSRIPattern);
|
||||||
const kAllWSP = new RegExp(`^${kWSP}*$`);
|
const kAllWSP = new RegExp(`^${kWSP}*$`);
|
||||||
Object.seal(kAllWSP);
|
Object.seal(kAllWSP);
|
||||||
|
|
||||||
const RegExpExec = Function.call.bind(RegExp.prototype.exec);
|
const RegExpExec = Function.call.bind(RegExp.prototype.exec);
|
||||||
const RegExpTest = Function.call.bind(RegExp.prototype.test);
|
const RegExpTest = Function.call.bind(RegExp.prototype.test);
|
||||||
const StringSlice = Function.call.bind(String.prototype.slice);
|
const StringSlice = Function.call.bind(String.prototype.slice);
|
||||||
const {
|
|
||||||
Buffer: {
|
const BufferFrom = require('buffer').Buffer.from;
|
||||||
from: BufferFrom
|
|
||||||
}
|
|
||||||
} = require('buffer');
|
|
||||||
const { defineProperty } = Object;
|
const { defineProperty } = Object;
|
||||||
|
|
||||||
const parse = (str) => {
|
const parse = (str) => {
|
||||||
kSRIPattern.lastIndex = 0;
|
kSRIPattern.lastIndex = 0;
|
||||||
let prevIndex = 0;
|
let prevIndex = 0;
|
||||||
let match = RegExpExec(kSRIPattern, str);
|
let match;
|
||||||
const entries = [];
|
const entries = [];
|
||||||
while (match) {
|
while (match = RegExpExec(kSRIPattern, str)) {
|
||||||
if (match.index !== prevIndex) {
|
if (match.index !== prevIndex) {
|
||||||
throw new ERR_SRI_PARSE(str, prevIndex);
|
throw new ERR_SRI_PARSE(str, prevIndex);
|
||||||
}
|
}
|
||||||
if (entries.length > 0) {
|
if (entries.length > 0 && match[1] === '') {
|
||||||
if (match[1] === '') {
|
throw new ERR_SRI_PARSE(str, prevIndex);
|
||||||
throw new ERR_SRI_PARSE(str, prevIndex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Avoid setters being fired
|
// Avoid setters being fired
|
||||||
defineProperty(entries, entries.length, {
|
defineProperty(entries, entries.length, {
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
@ -53,8 +51,8 @@ const parse = (str) => {
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
prevIndex = prevIndex + match[0].length;
|
prevIndex = prevIndex + match[0].length;
|
||||||
match = RegExpExec(kSRIPattern, str);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (prevIndex !== str.length) {
|
if (prevIndex !== str.length) {
|
||||||
if (!RegExpTest(kAllWSP, StringSlice(str, prevIndex))) {
|
if (!RegExpTest(kAllWSP, StringSlice(str, prevIndex))) {
|
||||||
throw new ERR_SRI_PARSE(str, prevIndex);
|
throw new ERR_SRI_PARSE(str, prevIndex);
|
||||||
|
@ -5,6 +5,7 @@ const {
|
|||||||
} = require('internal/errors').codes;
|
} = require('internal/errors').codes;
|
||||||
const { Manifest } = require('internal/policy/manifest');
|
const { Manifest } = require('internal/policy/manifest');
|
||||||
let manifest;
|
let manifest;
|
||||||
|
|
||||||
module.exports = Object.freeze({
|
module.exports = Object.freeze({
|
||||||
__proto__: null,
|
__proto__: null,
|
||||||
setup(src, url) {
|
setup(src, url) {
|
||||||
@ -12,6 +13,7 @@ module.exports = Object.freeze({
|
|||||||
manifest = null;
|
manifest = null;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const json = JSON.parse(src, (_, o) => {
|
const json = JSON.parse(src, (_, o) => {
|
||||||
if (o && typeof o === 'object') {
|
if (o && typeof o === 'object') {
|
||||||
Reflect.setPrototypeOf(o, null);
|
Reflect.setPrototypeOf(o, null);
|
||||||
@ -21,12 +23,14 @@ module.exports = Object.freeze({
|
|||||||
});
|
});
|
||||||
manifest = new Manifest(json, url);
|
manifest = new Manifest(json, url);
|
||||||
},
|
},
|
||||||
|
|
||||||
get manifest() {
|
get manifest() {
|
||||||
if (typeof manifest === 'undefined') {
|
if (typeof manifest === 'undefined') {
|
||||||
throw new ERR_MANIFEST_TDZ();
|
throw new ERR_MANIFEST_TDZ();
|
||||||
}
|
}
|
||||||
return manifest;
|
return manifest;
|
||||||
},
|
},
|
||||||
|
|
||||||
assertIntegrity(moduleURL, content) {
|
assertIntegrity(moduleURL, content) {
|
||||||
this.manifest.matchesIntegrity(moduleURL, content);
|
this.manifest.matchesIntegrity(moduleURL, content);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user