Skip to content

Commit a9ac86d

Browse files
aduh95danielleadams
authored andcommitted
policy: refactor to use more primordials
PR-URL: #36210 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
1 parent 74adc44 commit a9ac86d

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

lib/internal/policy/manifest.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
const {
44
ArrayIsArray,
5-
Map,
6-
MapPrototypeSet,
75
ObjectCreate,
86
ObjectEntries,
97
ObjectFreeze,
@@ -12,6 +10,8 @@ const {
1210
RegExpPrototypeTest,
1311
SafeMap,
1412
SafeSet,
13+
StringPrototypeEndsWith,
14+
StringPrototypeReplace,
1515
Symbol,
1616
uncurryThis,
1717
} = primordials;
@@ -328,14 +328,15 @@ class Manifest {
328328
* @returns {string}
329329
*/
330330
const protocolOrResolve = (resourceHREF) => {
331-
if (resourceHREF.endsWith(':')) {
331+
if (StringPrototypeEndsWith(resourceHREF, ':')) {
332332
// URL parse will trim these anyway, save the compute
333-
resourceHREF = resourceHREF.replace(
333+
resourceHREF = StringPrototypeReplace(
334+
resourceHREF,
334335
// eslint-disable-next-line
335336
/^[\x00-\x1F\x20]|\x09\x0A\x0D|[\x00-\x1F\x20]$/g,
336337
''
337338
);
338-
if (/^[a-zA-Z][a-zA-Z+\-.]*:$/.test(resourceHREF)) {
339+
if (RegExpPrototypeTest(/^[a-zA-Z][a-zA-Z+\-.]*:$/, resourceHREF)) {
339340
return resourceHREF;
340341
}
341342
}
@@ -418,7 +419,7 @@ class Manifest {
418419
// Only a few schemes are hierarchical
419420
if (SPECIAL_SCHEMES.has(currentURL.protocol)) {
420421
// Make first '..' act like '.'
421-
if (currentURL.pathname.slice(-1) !== '/') {
422+
if (!StringPrototypeEndsWith(currentURL.pathname, '/')) {
422423
currentURL.pathname += '/';
423424
}
424425
let lastHREF;
@@ -470,7 +471,7 @@ class Manifest {
470471
assertIntegrity(url, content) {
471472
const href = `${url}`;
472473
debug('Checking integrity of %s', href);
473-
const realIntegrities = new Map();
474+
const realIntegrities = new SafeMap();
474475
const integrities = this.#resourceIntegrities;
475476
function processEntry(href) {
476477
let integrityEntries = integrities.get(href);
@@ -499,8 +500,7 @@ class Manifest {
499500
timingSafeEqual(digest, expected)) {
500501
return true;
501502
}
502-
MapPrototypeSet(
503-
realIntegrities,
503+
realIntegrities.set(
504504
algorithm,
505505
BufferToString(digest, 'base64')
506506
);

lib/internal/policy/sri.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
// https://w3c.github.io/webappsec-subresource-integrity/#the-integrity-attribute
44

55
const {
6+
ArrayPrototype,
67
ObjectDefineProperty,
78
ObjectFreeze,
8-
ObjectGetPrototypeOf,
99
ObjectSeal,
1010
ObjectSetPrototypeOf,
1111
RegExp,
@@ -32,7 +32,6 @@ const kAllWSP = RegExp(`^${kWSP}*$`);
3232
ObjectSeal(kAllWSP);
3333

3434
const BufferFrom = require('buffer').Buffer.from;
35-
const RealArrayPrototype = ObjectGetPrototypeOf([]);
3635

3736
// Returns {algorithm, value (in base64 string), options,}[]
3837
const parse = (str) => {
@@ -41,10 +40,10 @@ const parse = (str) => {
4140
const entries = [];
4241
while (match = RegExpPrototypeExec(kSRIPattern, str)) {
4342
if (match.index !== prevIndex) {
44-
throw new ERR_SRI_PARSE(str, str.charAt(prevIndex), prevIndex);
43+
throw new ERR_SRI_PARSE(str, str[prevIndex], prevIndex);
4544
}
4645
if (entries.length > 0 && match[1] === '') {
47-
throw new ERR_SRI_PARSE(str, str.charAt(prevIndex), prevIndex);
46+
throw new ERR_SRI_PARSE(str, str[prevIndex], prevIndex);
4847
}
4948

5049
// Avoid setters being fired
@@ -63,10 +62,10 @@ const parse = (str) => {
6362

6463
if (prevIndex !== str.length) {
6564
if (!RegExpPrototypeTest(kAllWSP, StringPrototypeSlice(str, prevIndex))) {
66-
throw new ERR_SRI_PARSE(str, str.charAt(prevIndex), prevIndex);
65+
throw new ERR_SRI_PARSE(str, str[prevIndex], prevIndex);
6766
}
6867
}
69-
return ObjectSetPrototypeOf(entries, RealArrayPrototype);
68+
return ObjectSetPrototypeOf(entries, ArrayPrototype);
7069
};
7170

7271
module.exports = {

0 commit comments

Comments
 (0)