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 integrity: true 30 } 31 } 32 }); 33 34 for (const href of baseURLs) { 35 assert.strictEqual( 36 manifest.assertIntegrity(href), 37 true 38 ); 39 assert.strictEqual( 40 manifest.assertIntegrity(href, null), 41 true 42 ); 43 assert.strictEqual( 44 manifest.assertIntegrity(href, ''), 45 true 46 ); 47 } 48 } 49 { 50 const manifest = new Manifest({ 51 scopes: { 52 'file:': { 53 integrity: true 54 } 55 } 56 }); 57 58 for (const href of baseURLs) { 59 assert.strictEqual( 60 manifest.assertIntegrity(href), 61 true 62 ); 63 assert.strictEqual( 64 manifest.assertIntegrity(href, null), 65 true 66 ); 67 assert.strictEqual( 68 manifest.assertIntegrity(href, ''), 69 true 70 ); 71 } 72 } 73 { 74 const manifest = new Manifest({ 75 resources: { 76 'file:///root/dir1/isolated': {}, 77 'file:///root/dir1/cascade': { 78 cascade: true 79 } 80 }, 81 scopes: { 82 'file:///root/dir1/': { 83 integrity: true, 84 }, 85 'file:///root/dir1/dir2/': { 86 cascade: true, 87 }, 88 'file:///root/dir1/censor/': { 89 }, 90 } 91 }); 92 assert.throws( 93 () => { 94 manifest.assertIntegrity('file:///root/dir1/isolated'); 95 }, 96 /ERR_MANIFEST_ASSERT_INTEGRITY/ 97 ); 98 assert.strictEqual( 99 manifest.assertIntegrity('file:///root/dir1/cascade'), 100 true 101 ); 102 assert.strictEqual( 103 manifest.assertIntegrity('file:///root/dir1/enoent'), 104 true 105 ); 106 assert.strictEqual( 107 manifest.assertIntegrity('file:///root/dir1/dir2/enoent'), 108 true 109 ); 110 assert.throws( 111 () => { 112 manifest.assertIntegrity('file:///root/dir1/censor/enoent'); 113 }, 114 /ERR_MANIFEST_ASSERT_INTEGRITY/ 115 ); 116 } 117} 118// #endregion 119// #region data 120{ 121 const baseURLs = [ 122 'data:text/javascript,0', 123 'data:text/javascript,0/1', 124 ]; 125 126 { 127 const manifest = new Manifest({ 128 scopes: { 129 'data:text/': { 130 integrity: true 131 } 132 } 133 }); 134 135 for (const href of baseURLs) { 136 assert.throws( 137 () => { 138 manifest.assertIntegrity(href); 139 }, 140 /ERR_MANIFEST_ASSERT_INTEGRITY/ 141 ); 142 } 143 } 144 { 145 const manifest = new Manifest({ 146 scopes: { 147 'data:/': { 148 integrity: true 149 } 150 } 151 }); 152 153 for (const href of baseURLs) { 154 assert.throws( 155 () => { 156 manifest.assertIntegrity(href); 157 }, 158 /ERR_MANIFEST_ASSERT_INTEGRITY/ 159 ); 160 } 161 } 162 { 163 const manifest = new Manifest({ 164 scopes: { 165 'data:': { 166 integrity: true 167 } 168 } 169 }); 170 171 for (const href of baseURLs) { 172 assert.strictEqual(manifest.assertIntegrity(href), true); 173 } 174 } 175 { 176 const manifest = new Manifest({ 177 scopes: { 178 'data:text/javascript,0/': { 179 integrity: true 180 }, 181 } 182 }); 183 184 for (const href of baseURLs) { 185 assert.throws( 186 () => { 187 manifest.assertIntegrity(href); 188 }, 189 /ERR_MANIFEST_ASSERT_INTEGRITY/ 190 ); 191 } 192 } 193} 194// #endregion 195// #region blob 196{ 197 { 198 const manifest = new Manifest({ 199 scopes: { 200 'https://example.com/': { 201 integrity: true 202 } 203 } 204 }); 205 206 assert.strictEqual( 207 manifest.assertIntegrity('blob:https://example.com/has-origin'), 208 true 209 ); 210 } 211 { 212 const manifest = new Manifest({ 213 scopes: { 214 } 215 }); 216 217 assert.throws( 218 () => { 219 manifest.assertIntegrity('blob:https://example.com/has-origin'); 220 }, 221 /ERR_MANIFEST_ASSERT_INTEGRITY/ 222 ); 223 } 224 { 225 const manifest = new Manifest({ 226 scopes: { 227 'blob:https://example.com/has-origin': { 228 cascade: true 229 } 230 } 231 }); 232 233 assert.throws( 234 () => { 235 manifest.assertIntegrity('blob:https://example.com/has-origin'); 236 }, 237 /ERR_MANIFEST_ASSERT_INTEGRITY/ 238 ); 239 } 240 { 241 const manifest = new Manifest({ 242 resources: { 243 'blob:https://example.com/has-origin': { 244 cascade: true 245 } 246 }, 247 scopes: { 248 'https://example.com': { 249 integrity: true 250 } 251 } 252 }); 253 254 assert.strictEqual( 255 manifest.assertIntegrity('blob:https://example.com/has-origin'), 256 true 257 ); 258 } 259 { 260 const manifest = new Manifest({ 261 scopes: { 262 'blob:': { 263 integrity: true 264 }, 265 'https://example.com': { 266 cascade: true 267 } 268 } 269 }); 270 271 assert.throws( 272 () => { 273 manifest.assertIntegrity('blob:https://example.com/has-origin'); 274 }, 275 /ERR_MANIFEST_ASSERT_INTEGRITY/ 276 ); 277 assert.strictEqual( 278 manifest.assertIntegrity('blob:foo'), 279 true 280 ); 281 } 282} 283// #endregion 284// #startonerror 285{ 286 const manifest = new Manifest({ 287 scopes: { 288 'file:///': { 289 integrity: true 290 } 291 }, 292 onerror: 'throw' 293 }); 294 assert.throws( 295 () => { 296 manifest.assertIntegrity('http://example.com'); 297 }, 298 /ERR_MANIFEST_ASSERT_INTEGRITY/ 299 ); 300} 301{ 302 assert.throws( 303 () => { 304 new Manifest({ 305 scopes: { 306 'file:///': { 307 integrity: true 308 } 309 }, 310 onerror: 'unknown' 311 }); 312 }, 313 /ERR_MANIFEST_UNKNOWN_ONERROR/ 314 ); 315} 316// #endonerror 317