Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: refactor policy code for readability #25629

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/internal/policy/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,23 @@ const kReactions = new SafeWeakMap();
const kRelativeURLStringPattern = /^\.{0,2}\//;
const { shouldAbortOnUncaughtException } = internalBinding('config');
const { abort, exit, _rawDebug } = process;

function REACTION_THROW(error) {
throw error;
}

function REACTION_EXIT(error) {
REACTION_LOG(error);
if (shouldAbortOnUncaughtException) {
abort();
}
exit(1);
}

function REACTION_LOG(error) {
_rawDebug(error.stack);
}

class Manifest {
constructor(obj, manifestURL) {
const integrities = {
Expand All @@ -44,6 +48,7 @@ class Manifest {
__proto__: null,
integrity: REACTION_THROW,
};

if (obj.onerror) {
const behavior = obj.onerror;
if (behavior === 'throw') {
Expand All @@ -55,8 +60,10 @@ class Manifest {
throw new ERR_MANIFEST_UNKNOWN_ONERROR(behavior);
}
}

kReactions.set(this, Object.freeze(reactions));
const manifestEntries = entries(obj.resources);

for (var i = 0; i < manifestEntries.length; i++) {
let url = manifestEntries[i][0];
const integrity = manifestEntries[i][1].integrity;
Expand All @@ -65,10 +72,12 @@ class Manifest {
if (RegExpTest(kRelativeURLStringPattern, url)) {
url = new URL(url, manifestURL).href;
}

const sri = Object.freeze(SRI.parse(integrity));
if (url in integrities) {
const old = integrities[url];
let mismatch = false;

if (old.length !== sri.length) {
mismatch = true;
} else {
Expand All @@ -85,6 +94,7 @@ class Manifest {
break compare;
}
}

if (mismatch) {
throw new ERR_MANIFEST_INTEGRITY_MISMATCH(url);
}
Expand All @@ -96,10 +106,12 @@ class Manifest {
kIntegrities.set(this, integrities);
Object.freeze(this);
}

assertIntegrity(url, content) {
debug(`Checking integrity of ${url}`);
const integrities = kIntegrities.get(this);
const realIntegrities = new Map();

if (integrities && url in integrities) {
const integrityEntries = integrities[url];
// Avoid clobbered Symbol.iterator
Expand All @@ -122,6 +134,7 @@ class Manifest {
kReactions.get(this).integrity(error);
}
}

// Lock everything down to avoid problems even if reference is leaked somehow
Object.setPrototypeOf(Manifest, null);
Object.setPrototypeOf(Manifest.prototype, null);
Expand Down
22 changes: 10 additions & 12 deletions lib/internal/policy/sri.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,27 @@ const { freeze } = Object;
Object.seal(kSRIPattern);
const kAllWSP = new RegExp(`^${kWSP}*$`);
Object.seal(kAllWSP);

const RegExpExec = Function.call.bind(RegExp.prototype.exec);
const RegExpTest = Function.call.bind(RegExp.prototype.test);
const StringSlice = Function.call.bind(String.prototype.slice);
const {
Buffer: {
from: BufferFrom
}
} = require('buffer');

const BufferFrom = require('buffer').Buffer.from;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aw i liked this before :(

const { defineProperty } = Object;

const parse = (str) => {
kSRIPattern.lastIndex = 0;
let prevIndex = 0;
let match = RegExpExec(kSRIPattern, str);
let match;
const entries = [];
while (match) {
while (match = RegExpExec(kSRIPattern, str)) {
if (match.index !== prevIndex) {
throw new ERR_SRI_PARSE(str, prevIndex);
}
if (entries.length > 0) {
if (match[1] === '') {
throw new ERR_SRI_PARSE(str, prevIndex);
}
if (entries.length > 0 && match[1] === '') {
throw new ERR_SRI_PARSE(str, prevIndex);
}

// Avoid setters being fired
defineProperty(entries, entries.length, {
enumerable: true,
Expand All @@ -53,8 +51,8 @@ const parse = (str) => {
})
});
prevIndex = prevIndex + match[0].length;
match = RegExpExec(kSRIPattern, str);
}

if (prevIndex !== str.length) {
if (!RegExpTest(kAllWSP, StringSlice(str, prevIndex))) {
throw new ERR_SRI_PARSE(str, prevIndex);
Expand Down
4 changes: 4 additions & 0 deletions lib/internal/process/policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ const {
} = require('internal/errors').codes;
const { Manifest } = require('internal/policy/manifest');
let manifest;

module.exports = Object.freeze({
__proto__: null,
setup(src, url) {
if (src === null) {
manifest = null;
return;
}

const json = JSON.parse(src, (_, o) => {
if (o && typeof o === 'object') {
Reflect.setPrototypeOf(o, null);
Expand All @@ -21,12 +23,14 @@ module.exports = Object.freeze({
});
manifest = new Manifest(json, url);
},

get manifest() {
if (typeof manifest === 'undefined') {
throw new ERR_MANIFEST_TDZ();
}
return manifest;
},

assertIntegrity(moduleURL, content) {
this.manifest.matchesIntegrity(moduleURL, content);
}
Expand Down