Skip to content

Commit bf9aa42

Browse files
aduh95targos
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 acc9fad commit bf9aa42

2 files changed

Lines changed: 14 additions & 15 deletions

File tree

‎lib/internal/policy/manifest.js‎

Lines changed: 9 additions & 9 deletions
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
constprotocolOrResolve=(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
returnresourceHREF;
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
letlastHREF;
@@ -476,7 +477,7 @@ class Manifest {
476477
assertIntegrity(url,content){
477478
consthref=`${url}`;
478479
debug('Checking integrity of %s',href);
479-
constrealIntegrities=newMap();
480+
constrealIntegrities=newSafeMap();
480481
constintegrities=this.#resourceIntegrities;
481482
functionprocessEntry(href){
482483
letintegrityEntries=integrities.get(href);
@@ -505,8 +506,7 @@ class Manifest {
505506
timingSafeEqual(digest,expected)){
506507
returntrue;
507508
}
508-
MapPrototypeSet(
509-
realIntegrities,
509+
realIntegrities.set(
510510
algorithm,
511511
BufferToString(digest,'base64')
512512
);

‎lib/internal/policy/sri.js‎

Lines changed: 5 additions & 6 deletions
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
constBufferFrom=require('buffer').Buffer.from;
35-
constRealArrayPrototype=ObjectGetPrototypeOf([]);
3635

3736
// Returns {algorithm, value (in base64 string), options,}[]
3837
constparse=(str)=>{
@@ -41,10 +40,10 @@ const parse = (str) => {
4140
constentries=[];
4241
while(match=RegExpPrototypeExec(kSRIPattern,str)){
4342
if(match.index!==prevIndex){
44-
thrownewERR_SRI_PARSE(str,str.charAt(prevIndex),prevIndex);
43+
thrownewERR_SRI_PARSE(str,str[prevIndex],prevIndex);
4544
}
4645
if(entries.length>0&&match[1]===''){
47-
thrownewERR_SRI_PARSE(str,str.charAt(prevIndex),prevIndex);
46+
thrownewERR_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-
thrownewERR_SRI_PARSE(str,str.charAt(prevIndex),prevIndex);
65+
thrownewERR_SRI_PARSE(str,str[prevIndex],prevIndex);
6766
}
6867
}
69-
returnObjectSetPrototypeOf(entries,RealArrayPrototype);
68+
returnObjectSetPrototypeOf(entries,ArrayPrototype);
7069
};
7170

7271
module.exports={

0 commit comments

Comments
 (0)