Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 | 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 29x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 14x 14x 14x 31x 1x 1x 1x 1x 1x 31x 3x 3x 3x 31x 31x 31x 31x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2367x 2367x 2367x 2367x 2367x 2367x 2367x 2367x 76x 76x 2343x 2291x 2291x 2291x 1133x 1133x 1x 1x 1x 1x 1x 1x 1x 1133x 2290x 2290x 2290x 2290x 2290x 2366x 2366x 2366x 2367x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 2289x 4458x 4458x 4458x 4458x 4417x 4417x 1136x 1136x 1136x 1136x 1136x 1129x 1129x 1129x 1129x 1130x 1130x 1130x 1130x 1130x 4417x 4417x 4390x 4390x 4390x 4390x 4390x 4390x 4390x 12x 12x 4378x 4390x 2054x 4377x 1x 2324x 2323x 2323x 4378x 4378x 4417x 27x 70x 16x 16x 11x 67x 4x 4x 4x 4x 4x 4x 68x 11x 11x 11x 11x 11x 4458x 2289x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 1195x 1195x 1195x 1195x 1195x 1195x 1195x 1195x 13x 13x 13x 1195x 55x 55x 55x 55x 42x 55x 55x 55x 1127x 1127x 1127x 1127x 1127x 1127x 1127x 1127x 1127x 1127x 1127x 1127x 31x 31x 31x 31x 31x 31x 31x 31x 125x 125x 125x 125x 125x 125x 1x 125x 124x 124x 124x 124x 124x 11x 11x 11x 124x 124x 106x 106x 58x 58x 106x 106x 106x 291x 49x 47x 47x 49x 244x 244x 244x 106x 106x 124x 125x 122x 47x 47x 118x 8x 8x 24x 125x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 4x 4x 1x 4x 1x 3x 1x 1x 4x 1165x 1165x 1165x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1159x 1159x 1159x 1159x 1159x 42x 42x 27x 42x 15x 15x 1159x 1117x 1117x 1159x 1159x 1159x 1159x 1159x 1159x 1159x 1159x 1164x 1164x 1165x 36x 36x 36x 36x 14x 14x 14x 14x 36x 22x 22x 36x 36x 1164x 1164x 1166x 1166x 1166x 1166x 1166x 1166x 1166x 1198x 1198x 1198x 1198x 1198x 1198x 1198x 1198x 53x 1198x 1198x 1198x 4447x 4447x 4447x 4447x 4447x 4447x 4447x 4447x 4447x 4447x 4447x 1198x 1198x 1198x 1198x 1166x 1166x 7x 7x 7x 2x 2x 7x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 7x 1166x 1166x 103x 103x 103x 103x 103x 42x 42x 30x 27x 27x 27x 27x 26x 42x 103x 42x 42x 26x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 21x 21x 2x 2x 2x 5x 5x 3x 3x 3x 42x 64x 66x 55x 55x 55x 52x 55x 1x 1x 55x 2x 53x 1x 1x 1x 1x 12x 12x 103x 1166x 1166x 1166x 1166x 1166x 1166x 57x 57x 57x 57x 57x 57x 57x 57x 44x 57x 1166x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 5576x 5576x 5576x 26x 26x 5550x 5550x 5550x 5550x 5550x 5576x 31x 31x 31x 31x 31x 31x 31x 31x 31x 36x 36x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 26x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 9085x 9085x 1115x 9085x 58x 58x 58x 58x 7912x 7912x 7912x 31x 31x 31x 31x 31x 31x 31x 31x 19499x 19499x 19499x 31x 17206x 17206x 12816x 12816x 15204x 15204x 12816x 12816x 12816x 12816x 12816x 15204x 17206x 4378x 4390x 12x 12x 17206x | 'use strict'; // #region imports const { ArrayIsArray, ArrayPrototypeSort, ObjectCreate, ObjectEntries, ObjectFreeze, ObjectKeys, ObjectSetPrototypeOf, RegExpPrototypeExec, RegExpPrototypeTest, SafeMap, SafeSet, StringPrototypeEndsWith, StringPrototypeStartsWith, StringPrototypeReplace, Symbol, uncurryThis, } = primordials; const { ERR_MANIFEST_ASSERT_INTEGRITY, ERR_MANIFEST_INVALID_RESOURCE_FIELD, ERR_MANIFEST_INVALID_SPECIFIER, ERR_MANIFEST_UNKNOWN_ONERROR, } = require('internal/errors').codes; let debug = require('internal/util/debuglog').debuglog('policy', (fn) => { debug = fn; }); const SRI = require('internal/policy/sri'); const crypto = require('crypto'); const { Buffer } = require('buffer'); const { URL } = require('internal/url'); const { createHash, timingSafeEqual } = crypto; const HashUpdate = uncurryThis(crypto.Hash.prototype.update); const HashDigest = uncurryThis(crypto.Hash.prototype.digest); const BufferToString = uncurryThis(Buffer.prototype.toString); const kRelativeURLStringPattern = /^\.{0,2}\//; const { getOptionValue } = require('internal/options'); const shouldAbortOnUncaughtException = getOptionValue( '--abort-on-uncaught-exception' ); const { abort, exit, _rawDebug } = process; // #endregion // #region constants // From https://url.spec.whatwg.org/#special-scheme const kSpecialSchemes = new SafeSet([ 'file:', 'ftp:', 'http:', 'https:', 'ws:', 'wss:', ]); /** * @type {symbol} */ const kCascade = Symbol('cascade'); /** * @type {symbol} */ const kFallThrough = Symbol('fall through'); function REACTION_THROW(error) { throw error; } function REACTION_EXIT(error) { REACTION_LOG(error); if (shouldAbortOnUncaughtException) { abort(); } exit(1); } function REACTION_LOG(error) { _rawDebug(error.stack); } // #endregion // #region DependencyMapperInstance class DependencyMapperInstance { /** * @type {string} */ href; /** * @type {DependencyMap | undefined} */ #dependencies; /** * @type {PatternDependencyMap | undefined} */ #patternDependencies; /** * @type {DependencyMapperInstance | null | undefined} */ #parentDependencyMapper; /** * @type {boolean} */ #normalized = false; /** * @type {boolean} */ cascade; /** * @type {boolean} */ allowSameHREFScope; /** * @param {string} parentHREF * @param {DependencyMap | undefined} dependencies * @param {boolean} cascade * @param {boolean} allowSameHREFScope */ constructor( parentHREF, dependencies, cascade = false, allowSameHREFScope = false) { this.href = parentHREF; if (dependencies === kFallThrough || dependencies === undefined || dependencies === null) { this.#dependencies = dependencies; this.#patternDependencies = undefined; } else { const patterns = []; const keys = ObjectKeys(dependencies); for (let i = 0; i < keys.length; i++) { const key = keys[i]; if (StringPrototypeEndsWith(key, '*')) { const target = RegExpPrototypeExec(/^([^*]*)\*([^*]*)$/); if (!target) { throw new ERR_MANIFEST_INVALID_SPECIFIER( this.href, `${target}, pattern needs to have a single trailing "*" in target` ); } const prefix = target[1]; const suffix = target[2]; patterns.push([ target.slice(0, -1), [prefix, suffix], ]); } } ArrayPrototypeSort(patterns, (a, b) => { return a[0] < b[0] ? -1 : 1; }); this.#dependencies = dependencies; this.#patternDependencies = patterns; } this.cascade = cascade; this.allowSameHREFScope = allowSameHREFScope; ObjectFreeze(this); } /** * * @param {string} normalizedSpecifier * @param {Set<string>} conditions * @param {Manifest} manifest * @returns {URL | typeof kFallThrough | null} */ _resolveAlreadyNormalized(normalizedSpecifier, conditions, manifest) { let dependencies = this.#dependencies; debug(this.href, 'resolving', normalizedSpecifier); if (dependencies === kFallThrough) return true; if (dependencies !== undefined && typeof dependencies === 'object') { const normalized = this.#normalized; if (normalized !== true) { /** * @type {Record<string, string>} */ const normalizedDependencyMap = ObjectCreate(null); for (let specifier in dependencies) { const target = dependencies[specifier]; specifier = canonicalizeSpecifier(specifier, manifest.href); normalizedDependencyMap[specifier] = target; } ObjectFreeze(normalizedDependencyMap); dependencies = normalizedDependencyMap; this.#dependencies = normalizedDependencyMap; this.#normalized = true; } debug(dependencies); if (normalizedSpecifier in dependencies === true) { const to = searchDependencies( this.href, dependencies[normalizedSpecifier], conditions ); debug({ to }); if (to === true) { return true; } let ret; if (parsedURLs && parsedURLs.has(to)) { ret = parsedURLs.get(to); } else if (RegExpPrototypeTest(kRelativeURLStringPattern, to)) { ret = resolve(to, manifest.href); } else { ret = resolve(to); } return ret; } } const { cascade } = this; if (cascade !== true) { return null; } let parentDependencyMapper = this.#parentDependencyMapper; if (parentDependencyMapper === undefined) { parentDependencyMapper = manifest.getScopeDependencyMapper( this.href, this.allowSameHREFScope ); this.#parentDependencyMapper = parentDependencyMapper; } if (parentDependencyMapper === null) { return null; } return parentDependencyMapper._resolveAlreadyNormalized( normalizedSpecifier, conditions, manifest ); } } const kArbitraryDependencies = new DependencyMapperInstance( 'arbitrary dependencies', kFallThrough, false, true ); const kNoDependencies = new DependencyMapperInstance( 'no dependencies', null, false, true ); /** * @param {string} href * @param {JSONDependencyMap} dependencies * @param {boolean} cascade * @param {boolean} allowSameHREFScope * @param {Map<string | null | undefined, DependencyMapperInstance>} store */ const insertDependencyMap = ( href, dependencies, cascade, allowSameHREFScope, store ) => { if (cascade !== undefined && typeof cascade !== 'boolean') { throw new ERR_MANIFEST_INVALID_RESOURCE_FIELD(href, 'cascade'); } if (dependencies === true) { store.set(href, kArbitraryDependencies); return; } if (dependencies === null || dependencies === undefined) { store.set( href, cascade ? new DependencyMapperInstance(href, null, true, allowSameHREFScope) : kNoDependencies ); return; } if (objectButNotArray(dependencies)) { store.set( href, new DependencyMapperInstance( href, dependencies, cascade, allowSameHREFScope ) ); return; } throw new ERR_MANIFEST_INVALID_RESOURCE_FIELD(href, 'dependencies'); }; /** * Finds the longest key within `this.#scopeDependencies` that covers a * specific HREF * @param {string} href * @param {ScopeStore} scopeStore * @returns {null | string} */ function findScopeHREF(href, scopeStore, allowSame) { let protocol; if (href !== '') { // default URL parser does some stuff to special urls... skip if this is // just the protocol if (RegExpPrototypeTest(/^[^:]*[:]$/, href)) { protocol = href; } else { let currentURL = new URL(href); const normalizedHREF = currentURL.href; protocol = currentURL.protocol; // Non-opaque blobs adopt origins if (protocol === 'blob:' && currentURL.origin !== 'null') { currentURL = new URL(currentURL.origin); protocol = currentURL.protocol; } // Only a few schemes are hierarchical if (kSpecialSchemes.has(currentURL.protocol)) { // Make first '..' act like '.' if (!StringPrototypeEndsWith(currentURL.pathname, '/')) { currentURL.pathname += '/'; } let lastHREF; let currentHREF = currentURL.href; do { if (scopeStore.has(currentHREF)) { if (allowSame || currentHREF !== normalizedHREF) { return currentHREF; } } lastHREF = currentHREF; currentURL = new URL('..', currentURL); currentHREF = currentURL.href; } while (lastHREF !== currentHREF); } } } if (scopeStore.has(protocol)) { if (allowSame || protocol !== href) return protocol; } if (scopeStore.has('')) { if (allowSame || '' !== href) return ''; } return null; } // #endregion /** * @typedef {Record<string, string> | typeof kFallThrough} DependencyMap * @typedef {Array<[string, [string, string]]>} PatternDependencyMap * @typedef {Record<string, string> | null | true} JSONDependencyMap */ /** * @typedef {Map<string, any>} ScopeStore * @typedef {(specifier: string) => true | URL} DependencyMapper * @typedef {boolean | string | SRI[] | typeof kCascade} Integrity */ class Manifest { #defaultDependencies; /** * @type {string} */ href; /** * @type {(err: Error) => void} * * Performs default action for what happens when a manifest encounters * a violation such as abort()ing or exiting the process, throwing the error, * or logging the error. */ #reaction; /** * @type {Map<string, DependencyMapperInstance>} * * Used to find where a dependency is located. * * This stores functions to lazily calculate locations as needed. * `true` is used to signify that the location is not specified * by the manifest and default resolution should be allowed. * * The functions return `null` to signify that a dependency is * not found */ #resourceDependencies = new SafeMap(); /** * @type {Map<string, Integrity>} * * Used to compare a resource to the content body at the resource. * `true` is used to signify that all integrities are allowed, otherwise, * SRI strings are parsed to compare with the body. * * This stores strings instead of eagerly parsing SRI strings * and only converts them to SRI data structures when needed. * This avoids needing to parse all SRI strings at startup even * if some never end up being used. */ #resourceIntegrities = new SafeMap(); /** * @type {ScopeStore} * * Used to compare a resource to the content body at the resource. * `true` is used to signify that all integrities are allowed, otherwise, * SRI strings are parsed to compare with the body. * * Separate from #resourceDependencies due to conflicts with things like * `blob:` being both a scope and a resource potentially as well as * `file:` being parsed to `file:///` instead of remaining host neutral. */ #scopeDependencies = new SafeMap(); /** * @type {Map<string, boolean | null | typeof kCascade>} * * Used to allow arbitrary loading within a scope */ #scopeIntegrities = new SafeMap(); /** * `obj` should match the policy file format described in the docs * it is expected to not have prototype pollution issues either by reassigning * the prototype to `null` for values or by running prior to any user code. * * `manifestURL` is a URL to resolve relative locations against. * * @param {object} obj * @param {string} manifestHREF */ constructor(obj, manifestHREF) { this.href = manifestHREF; const scopes = this.#scopeDependencies; const integrities = this.#resourceIntegrities; const resourceDependencies = this.#resourceDependencies; let reaction = REACTION_THROW; if (objectButNotArray(obj) && 'onerror' in obj) { const behavior = obj.onerror; if (behavior === 'exit') { reaction = REACTION_EXIT; } else if (behavior === 'log') { reaction = REACTION_LOG; } else if (behavior !== 'throw') { throw new ERR_MANIFEST_UNKNOWN_ONERROR(behavior); } } this.#reaction = reaction; const jsonResourcesEntries = ObjectEntries( obj.resources ?? ObjectCreate(null) ); const jsonScopesEntries = ObjectEntries(obj.scopes ?? ObjectCreate(null)); const defaultDependencies = obj.dependencies ?? ObjectCreate(null); this.#defaultDependencies = new DependencyMapperInstance( 'default', defaultDependencies === true ? kFallThrough : defaultDependencies, false ); for (let i = 0; i < jsonResourcesEntries.length; i++) { const { 0: originalHREF, 1: descriptor } = jsonResourcesEntries[i]; const { cascade, dependencies, integrity } = descriptor; const href = resolve(originalHREF, manifestHREF).href; if (typeof integrity !== 'undefined') { debug('Manifest contains integrity for resource %s', originalHREF); if (typeof integrity === 'string') { integrities.set(href, integrity); } else if (integrity === true) { integrities.set(href, true); } else { throw new ERR_MANIFEST_INVALID_RESOURCE_FIELD(href, 'integrity'); } } else { integrities.set(href, cascade === true ? kCascade : false); } insertDependencyMap( href, dependencies, cascade, true, resourceDependencies ); } const scopeIntegrities = this.#scopeIntegrities; for (let i = 0; i < jsonScopesEntries.length; i++) { const { 0: originalHREF, 1: descriptor } = jsonScopesEntries[i]; const { cascade, dependencies, integrity } = descriptor; const href = emptyOrProtocolOrResolve(originalHREF, manifestHREF); if (typeof integrity !== 'undefined') { debug('Manifest contains integrity for scope %s', originalHREF); if (integrity === true) { scopeIntegrities.set(href, true); } else { throw new ERR_MANIFEST_INVALID_RESOURCE_FIELD(href, 'integrity'); } } else { scopeIntegrities.set(href, cascade === true ? kCascade : false); } insertDependencyMap(href, dependencies, cascade, false, scopes); } ObjectFreeze(this); } /** * @param {string} requester * @returns {{resolve: any, reaction: (err: any) => void}} */ getDependencyMapper(requester) { const requesterHREF = `${requester}`; const dependencies = this.#resourceDependencies; /** * @type {DependencyMapperInstance} */ const instance = ( dependencies.has(requesterHREF) ? dependencies.get(requesterHREF) ?? null : this.getScopeDependencyMapper(requesterHREF, true) ) ?? this.#defaultDependencies; return { resolve: (specifier, conditions) => { const normalizedSpecifier = canonicalizeSpecifier( specifier, requesterHREF ); const result = instance._resolveAlreadyNormalized( normalizedSpecifier, conditions, this ); if (result === kFallThrough) return true; return result; }, reaction: this.#reaction, }; } mightAllow(url, onreact) { const href = `${url}`; debug('Checking for entry of %s', href); if (StringPrototypeStartsWith(href, 'node:')) { return true; } if (this.#resourceIntegrities.has(href)) { return true; } let scope = findScopeHREF(href, this.#scopeIntegrities, true); while (scope !== null) { if (this.#scopeIntegrities.has(scope)) { const entry = this.#scopeIntegrities.get(scope); if (entry === true) { return true; } else if (entry !== kCascade) { break; } } const nextScope = findScopeHREF( new URL('..', scope), this.#scopeIntegrities, false, ); if (!nextScope || nextScope === scope) { break; } scope = nextScope; } if (onreact) { this.#reaction(onreact()); } return false; } assertIntegrity(url, content) { const href = `${url}`; debug('Checking integrity of %s', href); const realIntegrities = new SafeMap(); const integrities = this.#resourceIntegrities; function processEntry(href) { let integrityEntries = integrities.get(href); if (integrityEntries === true) return true; if (typeof integrityEntries === 'string') { const sri = ObjectFreeze(SRI.parse(integrityEntries)); integrities.set(href, sri); integrityEntries = sri; } return integrityEntries; } if (integrities.has(href)) { const integrityEntries = processEntry(href); if (integrityEntries === true) return true; if (ArrayIsArray(integrityEntries)) { // Avoid clobbered Symbol.iterator for (let i = 0; i < integrityEntries.length; i++) { const { algorithm, value: expected } = integrityEntries[i]; const hash = createHash(algorithm); // TODO(tniessen): the content should not be passed as a string in the // first place, see https://github.com/nodejs/node/issues/39707 HashUpdate(hash, content, 'utf8'); const digest = HashDigest(hash, 'buffer'); if (digest.length === expected.length && timingSafeEqual(digest, expected)) { return true; } realIntegrities.set(algorithm, BufferToString(digest, 'base64')); } } if (integrityEntries !== kCascade) { const error = new ERR_MANIFEST_ASSERT_INTEGRITY(url, realIntegrities); this.#reaction(error); } } let scope = findScopeHREF(href, this.#scopeIntegrities, true); while (scope !== null) { if (this.#scopeIntegrities.has(scope)) { const entry = this.#scopeIntegrities.get(scope); if (entry === true) { return true; } else if (entry !== kCascade) { break; } } const nextScope = findScopeHREF(scope, this.#scopeDependencies, false); if (!nextScope) { break; } scope = nextScope; } const error = new ERR_MANIFEST_ASSERT_INTEGRITY(url, realIntegrities); this.#reaction(error); } /** * @param {string} href * @param {boolean} allowSameHREFScope * @returns {DependencyMapperInstance | null} */ getScopeDependencyMapper(href, allowSameHREFScope) { if (href === null) { return this.#defaultDependencies; } /** @type {string | null} */ const scopeHREF = findScopeHREF( href, this.#scopeDependencies, allowSameHREFScope ); if (scopeHREF === null) return this.#defaultDependencies; return this.#scopeDependencies.get(scopeHREF); } } // Lock everything down to avoid problems even if reference is leaked somehow ObjectSetPrototypeOf(Manifest, null); ObjectSetPrototypeOf(Manifest.prototype, null); ObjectFreeze(Manifest); ObjectFreeze(Manifest.prototype); module.exports = ObjectFreeze({ Manifest }); // #region URL utils /** * Attempts to canonicalize relative URL strings against a base URL string * Does not perform I/O * If not able to canonicalize, returns the original specifier * * This effectively removes the possibility of the return value being a relative * URL string * @param {string} specifier * @param {string} base * @returns {string} */ function canonicalizeSpecifier(specifier, base) { try { if (RegExpPrototypeTest(kRelativeURLStringPattern, specifier)) { return resolve(specifier, base).href; } return resolve(specifier).href; } catch { // Continue regardless of error. } return specifier; } /** * Does a special allowance for scopes to be non-valid URLs * that are only protocol strings or the empty string * @param {string} resourceHREF * @param {string} [base] * @returns {string} */ const emptyOrProtocolOrResolve = (resourceHREF, base) => { if (resourceHREF === '') return ''; if (StringPrototypeEndsWith(resourceHREF, ':')) { // URL parse will trim these anyway, save the compute resourceHREF = StringPrototypeReplace( resourceHREF, // eslint-disable-next-line /^[\x00-\x1F\x20]|\x09\x0A\x0D|[\x00-\x1F\x20]$/g, '' ); if (RegExpPrototypeTest(/^[a-zA-Z][a-zA-Z+\-.]*:$/, resourceHREF)) { return resourceHREF; } } return resolve(resourceHREF, base).href; }; /** * @type {Map<string, URL>} */ let parsedURLs; /** * Resolves a valid url string and uses the parsed cache to avoid double parsing * costs. * @param {string} originalHREF * @param {string} [base] * @returns {Readonly<URL>} */ const resolve = (originalHREF, base) => { parsedURLs = parsedURLs ?? new SafeMap(); if (parsedURLs.has(originalHREF)) { return parsedURLs.get(originalHREF); } else if (RegExpPrototypeTest(kRelativeURLStringPattern, originalHREF)) { const resourceURL = new URL(originalHREF, base); parsedURLs.set(resourceURL.href, resourceURL); return resourceURL; } const resourceURL = new URL(originalHREF); parsedURLs.set(originalHREF, resourceURL); return resourceURL; }; // #endregion /** * @param {any} o * @returns {o is object} */ function objectButNotArray(o) { return o && typeof o === 'object' && !ArrayIsArray(o); } function searchDependencies(href, target, conditions) { if (objectButNotArray(target)) { const keys = ObjectKeys(target); for (let i = 0; i < keys.length; i++) { const key = keys[i]; if (conditions.has(key)) { const ret = searchDependencies(href, target[key], conditions); if (ret != null) { return ret; } } } } else if (typeof target === 'string') { return target; } else if (target === true) { return target; } else { throw new ERR_MANIFEST_INVALID_RESOURCE_FIELD(href, 'dependencies'); } return null; } |