Skip to content

Commit 506f1d4

Browse files
bmeckjasnell
authored andcommitted
policy: fix cascade getting scope
PR-URL: #37298 Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Co-authored-by: James M Snell <jasnell@gmail.com>
1 parent 2a3feff commit 506f1d4

4 files changed

Lines changed: 49 additions & 12 deletions

File tree

‎lib/internal/policy/manifest.js‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ const shouldAbortOnUncaughtException =
4141
getOptionValue('--abort-on-uncaught-exception');
4242
const{ abort, exit, _rawDebug }=process;
4343

44+
constkTerminate=()=>null;
45+
4446
// From https://url.spec.whatwg.org/#special-scheme
45-
constSPECIAL_SCHEMES=newSafeSet([
47+
constkSpecialSchemes=newSafeSet([
4648
'file:',
4749
'ftp:',
4850
'http:',
@@ -76,7 +78,7 @@ function REACTION_LOG(error) {
7678

7779
classManifest{
7880
/**
79-
* @type {Map<string, DependencyMapper>}
81+
* @type {Map<string | null | undefined, DependencyMapper>}
8082
*
8183
* Used to compare a resource to the content body at the resource.
8284
* `true` is used to signify that all integrities are allowed, otherwise,
@@ -139,6 +141,8 @@ class Manifest {
139141
*/
140142
constructor(obj,manifestURL){
141143
constscopes=this.#scopeDependencies;
144+
scopes.set(null,kTerminate);
145+
scopes.set(undefined,kTerminate);
142146
constintegrities=this.#resourceIntegrities;
143147
constdependencies=this.#resourceDependencies;
144148
letreaction=REACTION_THROW;
@@ -205,18 +209,20 @@ class Manifest {
205209
return(toSpecifier,conditions)=>{
206210
if(toSpecifierindependencyMap!==true){
207211
if(cascade===true){
208-
letscopeHREF;
212+
/** @type {string | null} */
213+
letscopeHREF=resourceHREF;
209214
if(typeofparentDeps==='undefined'){
210215
do{
211-
scopeHREF=this.#findScopeHREF(resourceHREF);
216+
scopeHREF=this.#findScopeHREF(scopeHREF);
217+
if(scopeHREF===resourceHREF){
218+
scopeHREF=null;
219+
}
220+
if(scopes.has(scopeHREF)){
221+
break;
222+
}
212223
}while(
213-
scopeHREF!==null&&
214-
scopes.has(scopeHREF)!==true
224+
scopeHREF!==null
215225
);
216-
}
217-
if(scopeHREF===null){
218-
parentDeps=()=>null;
219-
}else{
220226
parentDeps=scopes.get(scopeHREF);
221227
}
222228
returnparentDeps(toSpecifier);
@@ -416,7 +422,7 @@ class Manifest {
416422
protocol=currentURL.protocol;
417423
}
418424
// Only a few schemes are hierarchical
419-
if(SPECIAL_SCHEMES.has(currentURL.protocol)){
425+
if(kSpecialSchemes.has(currentURL.protocol)){
420426
// Make first '..' act like '.'
421427
if(currentURL.pathname.slice(-1)!=='/'){
422428
currentURL.pathname+='/';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"resources": {
3+
"../multi-deps.js": {
4+
"integrity": true,
5+
"cascade": true
6+
}
7+
},
8+
"scopes": {
9+
"../": {
10+
"integrity": true,
11+
"dependencies": true
12+
}
13+
}
14+
}

‎test/fixtures/policy/multi-deps.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
require('fs');
3+
require('process');

‎test/parallel/test-policy-scopes.js‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ const fixtures = require('../common/fixtures');
1010
constassert=require('assert');
1111
const{ spawnSync }=require('child_process');
1212

13-
constdep=fixtures.path('policy','main.mjs');
1413
{
14+
constdep=fixtures.path('policy','main.mjs');
1515
constdepPolicy=fixtures.path(
1616
'policy',
1717
'dependencies',
@@ -24,3 +24,17 @@ const dep = fixtures.path('policy', 'main.mjs');
2424
);
2525
assert.strictEqual(status,0);
2626
}
27+
{
28+
constdep=fixtures.path('policy','multi-deps.js');
29+
constdepPolicy=fixtures.path(
30+
'policy',
31+
'dependencies',
32+
'dependencies-scopes-and-resources-policy.json');
33+
const{ status }=spawnSync(
34+
process.execPath,
35+
[
36+
'--experimental-policy',depPolicy,dep,
37+
]
38+
);
39+
assert.strictEqual(status,0);
40+
}

0 commit comments

Comments
 (0)