Skip to content

Commit ac47032

Browse files
aduh95targos
authored andcommitted
policy: refactor to use more primordials
PR-URL: #36210 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Bradley Farias <[email protected]>
1 parent f59ee44 commit ac47032

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;
@@ -334,14 +334,15 @@ class Manifest {
334334
* @returns {string}
335335
*/
336336
const protocolOrResolve = (resourceHREF) => {
337-
if (resourceHREF.endsWith(':')) {
337+
if (StringPrototypeEndsWith(resourceHREF, ':')) {
338338
// URL parse will trim these anyway, save the compute
339-
resourceHREF = resourceHREF.replace(
339+
resourceHREF = StringPrototypeReplace(
340+
resourceHREF,
340341
// eslint-disable-next-line
341342
/^[\x00-\x1F\x20]|\x09\x0A\x0D|[\x00-\x1F\x20]$/g,
342343
''
343344
);
344-
if (/^[a-zA-Z][a-zA-Z+\-.]*:$/.test(resourceHREF)) {
345+
if (RegExpPrototypeTest(/^[a-zA-Z][a-zA-Z+\-.]*:$/, resourceHREF)) {
345346
return resourceHREF;
346347
}
347348
}
@@ -424,7 +425,7 @@ class Manifest {
424425
// Only a few schemes are hierarchical
425426
if (kSpecialSchemes.has(currentURL.protocol)) {
426427
// Make first '..' act like '.'
427-
if (currentURL.pathname.slice(-1) !== '/') {
428+
if (!StringPrototypeEndsWith(currentURL.pathname, '/')) {
428429
currentURL.pathname += '/';
429430
}
430431
let lastHREF;
@@ -476,7 +477,7 @@ class Manifest {
476477
assertIntegrity(url, content) {
477478
const href = `${url}`;
478479
debug('Checking integrity of %s', href);
479-
const realIntegrities = new Map();
480+
const realIntegrities = new SafeMap();
480481
const integrities = this.#resourceIntegrities;
481482
function processEntry(href) {
482483
let integrityEntries = integrities.get(href);
@@ -505,8 +506,7 @@ class Manifest {
505506
timingSafeEqual(digest, expected)) {
506507
return true;
507508
}
508-
MapPrototypeSet(
509-
realIntegrities,
509+
realIntegrities.set(
510510
algorithm,
511511
BufferToString(digest, 'base64')
512512
);

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)