Skip to content

Commit 0640b09

Browse files
committed
lib: refactor policy code for readability
Simplify a few particularly quirky bits of code, and add whitespace for readability. PR-URL: #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>
1 parent 1905f8e commit 0640b09

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

lib/internal/policy/manifest.js

+13
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,23 @@ const kReactions = new SafeWeakMap();
2222
const kRelativeURLStringPattern = /^\.{0,2}\//;
2323
const { shouldAbortOnUncaughtException } = internalBinding('config');
2424
const { abort, exit, _rawDebug } = process;
25+
2526
function REACTION_THROW(error) {
2627
throw error;
2728
}
29+
2830
function REACTION_EXIT(error) {
2931
REACTION_LOG(error);
3032
if (shouldAbortOnUncaughtException) {
3133
abort();
3234
}
3335
exit(1);
3436
}
37+
3538
function REACTION_LOG(error) {
3639
_rawDebug(error.stack);
3740
}
41+
3842
class Manifest {
3943
constructor(obj, manifestURL) {
4044
const integrities = {
@@ -44,6 +48,7 @@ class Manifest {
4448
__proto__: null,
4549
integrity: REACTION_THROW,
4650
};
51+
4752
if (obj.onerror) {
4853
const behavior = obj.onerror;
4954
if (behavior === 'throw') {
@@ -55,8 +60,10 @@ class Manifest {
5560
throw new ERR_MANIFEST_UNKNOWN_ONERROR(behavior);
5661
}
5762
}
63+
5864
kReactions.set(this, Object.freeze(reactions));
5965
const manifestEntries = entries(obj.resources);
66+
6067
for (var i = 0; i < manifestEntries.length; i++) {
6168
let url = manifestEntries[i][0];
6269
const integrity = manifestEntries[i][1].integrity;
@@ -65,10 +72,12 @@ class Manifest {
6572
if (RegExpTest(kRelativeURLStringPattern, url)) {
6673
url = new URL(url, manifestURL).href;
6774
}
75+
6876
const sri = Object.freeze(SRI.parse(integrity));
6977
if (url in integrities) {
7078
const old = integrities[url];
7179
let mismatch = false;
80+
7281
if (old.length !== sri.length) {
7382
mismatch = true;
7483
} else {
@@ -85,6 +94,7 @@ class Manifest {
8594
break compare;
8695
}
8796
}
97+
8898
if (mismatch) {
8999
throw new ERR_MANIFEST_INTEGRITY_MISMATCH(url);
90100
}
@@ -96,10 +106,12 @@ class Manifest {
96106
kIntegrities.set(this, integrities);
97107
Object.freeze(this);
98108
}
109+
99110
assertIntegrity(url, content) {
100111
debug(`Checking integrity of ${url}`);
101112
const integrities = kIntegrities.get(this);
102113
const realIntegrities = new Map();
114+
103115
if (integrities && url in integrities) {
104116
const integrityEntries = integrities[url];
105117
// Avoid clobbered Symbol.iterator
@@ -122,6 +134,7 @@ class Manifest {
122134
kReactions.get(this).integrity(error);
123135
}
124136
}
137+
125138
// Lock everything down to avoid problems even if reference is leaked somehow
126139
Object.setPrototypeOf(Manifest, null);
127140
Object.setPrototypeOf(Manifest.prototype, null);

lib/internal/policy/sri.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,27 @@ const { freeze } = Object;
1818
Object.seal(kSRIPattern);
1919
const kAllWSP = new RegExp(`^${kWSP}*$`);
2020
Object.seal(kAllWSP);
21+
2122
const RegExpExec = Function.call.bind(RegExp.prototype.exec);
2223
const RegExpTest = Function.call.bind(RegExp.prototype.test);
2324
const StringSlice = Function.call.bind(String.prototype.slice);
24-
const {
25-
Buffer: {
26-
from: BufferFrom
27-
}
28-
} = require('buffer');
25+
26+
const BufferFrom = require('buffer').Buffer.from;
2927
const { defineProperty } = Object;
28+
3029
const parse = (str) => {
3130
kSRIPattern.lastIndex = 0;
3231
let prevIndex = 0;
33-
let match = RegExpExec(kSRIPattern, str);
32+
let match;
3433
const entries = [];
35-
while (match) {
34+
while (match = RegExpExec(kSRIPattern, str)) {
3635
if (match.index !== prevIndex) {
3736
throw new ERR_SRI_PARSE(str, prevIndex);
3837
}
39-
if (entries.length > 0) {
40-
if (match[1] === '') {
41-
throw new ERR_SRI_PARSE(str, prevIndex);
42-
}
38+
if (entries.length > 0 && match[1] === '') {
39+
throw new ERR_SRI_PARSE(str, prevIndex);
4340
}
41+
4442
// Avoid setters being fired
4543
defineProperty(entries, entries.length, {
4644
enumerable: true,
@@ -53,8 +51,8 @@ const parse = (str) => {
5351
})
5452
});
5553
prevIndex = prevIndex + match[0].length;
56-
match = RegExpExec(kSRIPattern, str);
5754
}
55+
5856
if (prevIndex !== str.length) {
5957
if (!RegExpTest(kAllWSP, StringSlice(str, prevIndex))) {
6058
throw new ERR_SRI_PARSE(str, prevIndex);

lib/internal/process/policy.js

+4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ const {
55
} = require('internal/errors').codes;
66
const { Manifest } = require('internal/policy/manifest');
77
let manifest;
8+
89
module.exports = Object.freeze({
910
__proto__: null,
1011
setup(src, url) {
1112
if (src === null) {
1213
manifest = null;
1314
return;
1415
}
16+
1517
const json = JSON.parse(src, (_, o) => {
1618
if (o && typeof o === 'object') {
1719
Reflect.setPrototypeOf(o, null);
@@ -21,12 +23,14 @@ module.exports = Object.freeze({
2123
});
2224
manifest = new Manifest(json, url);
2325
},
26+
2427
get manifest() {
2528
if (typeof manifest === 'undefined') {
2629
throw new ERR_MANIFEST_TDZ();
2730
}
2831
return manifest;
2932
},
33+
3034
assertIntegrity(moduleURL, content) {
3135
this.manifest.matchesIntegrity(moduleURL, content);
3236
}

0 commit comments

Comments
 (0)