1'use strict'; 2// Flags: --expose-internals 3 4const common = require('../common'); 5 6if (!common.hasCrypto) common.skip('missing crypto'); 7common.requireNoPackageJSONAbove(); 8 9const Manifest = require('internal/policy/manifest').Manifest; 10const assert = require('assert'); 11 12// #region files 13{ 14 const baseURLs = [ 15 // Localhost is special cased in spec 16 'file://localhost/root', 17 'file:///root', 18 'file:///', 19 'file:///root/dir1', 20 'file:///root/dir1/', 21 'file:///root/dir1/dir2', 22 'file:///root/dir1/dir2/', 23 ]; 24 25 { 26 const manifest = new Manifest({ 27 scopes: { 28 'file:///': { 29 dependencies: true 30 } 31 } 32 }); 33 34 for (const href of baseURLs) { 35 assert.strictEqual( 36 manifest.getDependencyMapper(href).resolve('fs'), 37 true 38 ); 39 } 40 } 41 { 42 const manifest = new Manifest({ 43 scopes: { 44 '': { 45 dependencies: true 46 } 47 } 48 }); 49 50 for (const href of baseURLs) { 51 assert.strictEqual( 52 manifest.getDependencyMapper(href).resolve('fs'), 53 true 54 ); 55 } 56 } 57 { 58 const manifest = new Manifest({ 59 scopes: { 60 '': { 61 dependencies: true 62 }, 63 'file:': { 64 cascade: true 65 } 66 } 67 }); 68 69 for (const href of baseURLs) { 70 assert.strictEqual( 71 manifest.getDependencyMapper(href).resolve('fs'), 72 true 73 ); 74 } 75 } 76 { 77 const manifest = new Manifest({ 78 scopes: { 79 'file:': { 80 dependencies: true 81 } 82 } 83 }); 84 85 for (const href of baseURLs) { 86 assert.strictEqual( 87 manifest 88 .getDependencyMapper(href) 89 .resolve('fs'), 90 true); 91 } 92 93 assert.strictEqual( 94 manifest 95 .getDependencyMapper('file://host/') 96 .resolve('fs'), 97 true); 98 } 99 { 100 const manifest = new Manifest({ 101 resources: { 102 'file:///root/dir1': { 103 dependencies: { 104 fs: 'test:fs1' 105 } 106 }, 107 'file:///root/dir1/isolated': {}, 108 'file:///root/dir1/cascade': { 109 cascade: true 110 } 111 }, 112 scopes: { 113 'file:///root/dir1/': { 114 dependencies: { 115 fs: 'test:fs2' 116 } 117 }, 118 'file:///root/dir1/censor/': { 119 }, 120 } 121 }); 122 123 for (const href of baseURLs) { 124 const redirector = manifest.getDependencyMapper(href); 125 if (href.startsWith('file:///root/dir1/')) { 126 assert.strictEqual( 127 redirector.resolve('fs').href, 128 'test:fs2' 129 ); 130 } else if (href === 'file:///root/dir1') { 131 assert.strictEqual( 132 redirector.resolve('fs').href, 133 'test:fs1' 134 ); 135 } else { 136 assert.strictEqual(redirector.resolve('fs'), null); 137 } 138 } 139 140 assert.strictEqual( 141 manifest 142 .getDependencyMapper('file:///root/dir1/isolated') 143 .resolve('fs'), 144 null 145 ); 146 assert.strictEqual( 147 manifest 148 .getDependencyMapper('file:///root/dir1/cascade') 149 .resolve('fs').href, 150 'test:fs2' 151 ); 152 assert.strictEqual( 153 manifest 154 .getDependencyMapper('file:///root/dir1/censor/foo') 155 .resolve('fs'), 156 null 157 ); 158 } 159} 160// #endregion 161// #region data 162{ 163 const baseURLs = [ 164 'data:text/javascript,0', 165 'data:text/javascript,0/1', 166 ]; 167 168 { 169 const manifest = new Manifest({ 170 scopes: { 171 'data:text/': { 172 dependencies: { 173 fs: true 174 } 175 } 176 } 177 }); 178 179 for (const href of baseURLs) { 180 assert.strictEqual( 181 manifest.getDependencyMapper(href).resolve('fs'), 182 null); 183 } 184 } 185 { 186 const manifest = new Manifest({ 187 scopes: { 188 'data:/': { 189 dependencies: { 190 fs: true 191 } 192 } 193 } 194 }); 195 196 for (const href of baseURLs) { 197 assert.strictEqual( 198 manifest.getDependencyMapper(href).resolve('fs'), 199 null); 200 } 201 } 202 { 203 const manifest = new Manifest({ 204 scopes: { 205 'data:': { 206 dependencies: true 207 } 208 } 209 }); 210 211 for (const href of baseURLs) { 212 assert.strictEqual( 213 manifest.getDependencyMapper(href).resolve('fs'), 214 true 215 ); 216 } 217 } 218 { 219 const manifest = new Manifest({ 220 scopes: { 221 'data:text/javascript,0/': { 222 dependencies: { 223 fs: 'test:fs1' 224 } 225 }, 226 } 227 }); 228 229 for (const href of baseURLs) { 230 assert.strictEqual( 231 manifest.getDependencyMapper(href).resolve('fs'), 232 null); 233 } 234 } 235} 236// #endregion 237// #region blob 238{ 239 { 240 const manifest = new Manifest({ 241 scopes: { 242 'https://example.com/': { 243 dependencies: true 244 } 245 } 246 }); 247 248 assert.strictEqual( 249 manifest 250 .getDependencyMapper('blob:https://example.com/has-origin') 251 .resolve('fs'), 252 true 253 ); 254 } 255 { 256 const manifest = new Manifest({ 257 scopes: { 258 'https://example.com': { 259 dependencies: true 260 } 261 } 262 }); 263 264 assert.strictEqual( 265 manifest 266 .getDependencyMapper('blob:https://example.com/has-origin') 267 .resolve('fs'), 268 true 269 ); 270 } 271 { 272 const manifest = new Manifest({ 273 scopes: { 274 } 275 }); 276 277 assert.strictEqual( 278 manifest 279 .getDependencyMapper('blob:https://example.com/has-origin') 280 .resolve('fs'), 281 null); 282 } 283 { 284 const manifest = new Manifest({ 285 scopes: { 286 'blob:https://example.com/has-origin': { 287 cascade: true 288 } 289 } 290 }); 291 292 assert.strictEqual( 293 manifest 294 .getDependencyMapper('blob:https://example.com/has-origin') 295 .resolve('fs'), 296 null); 297 } 298 { 299 const manifest = new Manifest({ 300 scopes: { 301 // FIXME 302 'https://example.com/': { 303 dependencies: true 304 }, 305 'blob:https://example.com/has-origin': { 306 cascade: true 307 } 308 } 309 }); 310 311 assert.strictEqual( 312 manifest 313 .getDependencyMapper('blob:https://example.com/has-origin') 314 .resolve('fs'), 315 true 316 ); 317 } 318 { 319 const manifest = new Manifest({ 320 scopes: { 321 'blob:': { 322 dependencies: true 323 }, 324 'blob:https://example.com/has-origin': { 325 cascade: true 326 } 327 } 328 }); 329 330 assert.strictEqual( 331 manifest 332 .getDependencyMapper('blob:https://example.com/has-origin') 333 .resolve('fs'), 334 null); 335 assert.strictEqual( 336 manifest 337 .getDependencyMapper('blob:foo').resolve('fs'), 338 true 339 ); 340 } 341} 342// #endregion 343