11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// Flags: --expose-internals
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciif (!common.hasCrypto) common.skip('missing crypto');
71cb0ef41Sopenharmony_cicommon.requireNoPackageJSONAbove();
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst Manifest = require('internal/policy/manifest').Manifest;
101cb0ef41Sopenharmony_ciconst assert = require('assert');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci// #region files
131cb0ef41Sopenharmony_ci{
141cb0ef41Sopenharmony_ci  const baseURLs = [
151cb0ef41Sopenharmony_ci    // Localhost is special cased in spec
161cb0ef41Sopenharmony_ci    'file://localhost/root',
171cb0ef41Sopenharmony_ci    'file:///root',
181cb0ef41Sopenharmony_ci    'file:///',
191cb0ef41Sopenharmony_ci    'file:///root/dir1',
201cb0ef41Sopenharmony_ci    'file:///root/dir1/',
211cb0ef41Sopenharmony_ci    'file:///root/dir1/dir2',
221cb0ef41Sopenharmony_ci    'file:///root/dir1/dir2/',
231cb0ef41Sopenharmony_ci  ];
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  {
261cb0ef41Sopenharmony_ci    const manifest = new Manifest({
271cb0ef41Sopenharmony_ci      scopes: {
281cb0ef41Sopenharmony_ci        'file:///': {
291cb0ef41Sopenharmony_ci          integrity: true
301cb0ef41Sopenharmony_ci        }
311cb0ef41Sopenharmony_ci      }
321cb0ef41Sopenharmony_ci    });
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci    for (const href of baseURLs) {
351cb0ef41Sopenharmony_ci      assert.strictEqual(
361cb0ef41Sopenharmony_ci        manifest.assertIntegrity(href),
371cb0ef41Sopenharmony_ci        true
381cb0ef41Sopenharmony_ci      );
391cb0ef41Sopenharmony_ci      assert.strictEqual(
401cb0ef41Sopenharmony_ci        manifest.assertIntegrity(href, null),
411cb0ef41Sopenharmony_ci        true
421cb0ef41Sopenharmony_ci      );
431cb0ef41Sopenharmony_ci      assert.strictEqual(
441cb0ef41Sopenharmony_ci        manifest.assertIntegrity(href, ''),
451cb0ef41Sopenharmony_ci        true
461cb0ef41Sopenharmony_ci      );
471cb0ef41Sopenharmony_ci    }
481cb0ef41Sopenharmony_ci  }
491cb0ef41Sopenharmony_ci  {
501cb0ef41Sopenharmony_ci    const manifest = new Manifest({
511cb0ef41Sopenharmony_ci      scopes: {
521cb0ef41Sopenharmony_ci        'file:': {
531cb0ef41Sopenharmony_ci          integrity: true
541cb0ef41Sopenharmony_ci        }
551cb0ef41Sopenharmony_ci      }
561cb0ef41Sopenharmony_ci    });
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci    for (const href of baseURLs) {
591cb0ef41Sopenharmony_ci      assert.strictEqual(
601cb0ef41Sopenharmony_ci        manifest.assertIntegrity(href),
611cb0ef41Sopenharmony_ci        true
621cb0ef41Sopenharmony_ci      );
631cb0ef41Sopenharmony_ci      assert.strictEqual(
641cb0ef41Sopenharmony_ci        manifest.assertIntegrity(href, null),
651cb0ef41Sopenharmony_ci        true
661cb0ef41Sopenharmony_ci      );
671cb0ef41Sopenharmony_ci      assert.strictEqual(
681cb0ef41Sopenharmony_ci        manifest.assertIntegrity(href, ''),
691cb0ef41Sopenharmony_ci        true
701cb0ef41Sopenharmony_ci      );
711cb0ef41Sopenharmony_ci    }
721cb0ef41Sopenharmony_ci  }
731cb0ef41Sopenharmony_ci  {
741cb0ef41Sopenharmony_ci    const manifest = new Manifest({
751cb0ef41Sopenharmony_ci      resources: {
761cb0ef41Sopenharmony_ci        'file:///root/dir1/isolated': {},
771cb0ef41Sopenharmony_ci        'file:///root/dir1/cascade': {
781cb0ef41Sopenharmony_ci          cascade: true
791cb0ef41Sopenharmony_ci        }
801cb0ef41Sopenharmony_ci      },
811cb0ef41Sopenharmony_ci      scopes: {
821cb0ef41Sopenharmony_ci        'file:///root/dir1/': {
831cb0ef41Sopenharmony_ci          integrity: true,
841cb0ef41Sopenharmony_ci        },
851cb0ef41Sopenharmony_ci        'file:///root/dir1/dir2/': {
861cb0ef41Sopenharmony_ci          cascade: true,
871cb0ef41Sopenharmony_ci        },
881cb0ef41Sopenharmony_ci        'file:///root/dir1/censor/': {
891cb0ef41Sopenharmony_ci        },
901cb0ef41Sopenharmony_ci      }
911cb0ef41Sopenharmony_ci    });
921cb0ef41Sopenharmony_ci    assert.throws(
931cb0ef41Sopenharmony_ci      () => {
941cb0ef41Sopenharmony_ci        manifest.assertIntegrity('file:///root/dir1/isolated');
951cb0ef41Sopenharmony_ci      },
961cb0ef41Sopenharmony_ci      /ERR_MANIFEST_ASSERT_INTEGRITY/
971cb0ef41Sopenharmony_ci    );
981cb0ef41Sopenharmony_ci    assert.strictEqual(
991cb0ef41Sopenharmony_ci      manifest.assertIntegrity('file:///root/dir1/cascade'),
1001cb0ef41Sopenharmony_ci      true
1011cb0ef41Sopenharmony_ci    );
1021cb0ef41Sopenharmony_ci    assert.strictEqual(
1031cb0ef41Sopenharmony_ci      manifest.assertIntegrity('file:///root/dir1/enoent'),
1041cb0ef41Sopenharmony_ci      true
1051cb0ef41Sopenharmony_ci    );
1061cb0ef41Sopenharmony_ci    assert.strictEqual(
1071cb0ef41Sopenharmony_ci      manifest.assertIntegrity('file:///root/dir1/dir2/enoent'),
1081cb0ef41Sopenharmony_ci      true
1091cb0ef41Sopenharmony_ci    );
1101cb0ef41Sopenharmony_ci    assert.throws(
1111cb0ef41Sopenharmony_ci      () => {
1121cb0ef41Sopenharmony_ci        manifest.assertIntegrity('file:///root/dir1/censor/enoent');
1131cb0ef41Sopenharmony_ci      },
1141cb0ef41Sopenharmony_ci      /ERR_MANIFEST_ASSERT_INTEGRITY/
1151cb0ef41Sopenharmony_ci    );
1161cb0ef41Sopenharmony_ci  }
1171cb0ef41Sopenharmony_ci}
1181cb0ef41Sopenharmony_ci// #endregion
1191cb0ef41Sopenharmony_ci// #region data
1201cb0ef41Sopenharmony_ci{
1211cb0ef41Sopenharmony_ci  const baseURLs = [
1221cb0ef41Sopenharmony_ci    'data:text/javascript,0',
1231cb0ef41Sopenharmony_ci    'data:text/javascript,0/1',
1241cb0ef41Sopenharmony_ci  ];
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ci  {
1271cb0ef41Sopenharmony_ci    const manifest = new Manifest({
1281cb0ef41Sopenharmony_ci      scopes: {
1291cb0ef41Sopenharmony_ci        'data:text/': {
1301cb0ef41Sopenharmony_ci          integrity: true
1311cb0ef41Sopenharmony_ci        }
1321cb0ef41Sopenharmony_ci      }
1331cb0ef41Sopenharmony_ci    });
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci    for (const href of baseURLs) {
1361cb0ef41Sopenharmony_ci      assert.throws(
1371cb0ef41Sopenharmony_ci        () => {
1381cb0ef41Sopenharmony_ci          manifest.assertIntegrity(href);
1391cb0ef41Sopenharmony_ci        },
1401cb0ef41Sopenharmony_ci        /ERR_MANIFEST_ASSERT_INTEGRITY/
1411cb0ef41Sopenharmony_ci      );
1421cb0ef41Sopenharmony_ci    }
1431cb0ef41Sopenharmony_ci  }
1441cb0ef41Sopenharmony_ci  {
1451cb0ef41Sopenharmony_ci    const manifest = new Manifest({
1461cb0ef41Sopenharmony_ci      scopes: {
1471cb0ef41Sopenharmony_ci        'data:/': {
1481cb0ef41Sopenharmony_ci          integrity: true
1491cb0ef41Sopenharmony_ci        }
1501cb0ef41Sopenharmony_ci      }
1511cb0ef41Sopenharmony_ci    });
1521cb0ef41Sopenharmony_ci
1531cb0ef41Sopenharmony_ci    for (const href of baseURLs) {
1541cb0ef41Sopenharmony_ci      assert.throws(
1551cb0ef41Sopenharmony_ci        () => {
1561cb0ef41Sopenharmony_ci          manifest.assertIntegrity(href);
1571cb0ef41Sopenharmony_ci        },
1581cb0ef41Sopenharmony_ci        /ERR_MANIFEST_ASSERT_INTEGRITY/
1591cb0ef41Sopenharmony_ci      );
1601cb0ef41Sopenharmony_ci    }
1611cb0ef41Sopenharmony_ci  }
1621cb0ef41Sopenharmony_ci  {
1631cb0ef41Sopenharmony_ci    const manifest = new Manifest({
1641cb0ef41Sopenharmony_ci      scopes: {
1651cb0ef41Sopenharmony_ci        'data:': {
1661cb0ef41Sopenharmony_ci          integrity: true
1671cb0ef41Sopenharmony_ci        }
1681cb0ef41Sopenharmony_ci      }
1691cb0ef41Sopenharmony_ci    });
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ci    for (const href of baseURLs) {
1721cb0ef41Sopenharmony_ci      assert.strictEqual(manifest.assertIntegrity(href), true);
1731cb0ef41Sopenharmony_ci    }
1741cb0ef41Sopenharmony_ci  }
1751cb0ef41Sopenharmony_ci  {
1761cb0ef41Sopenharmony_ci    const manifest = new Manifest({
1771cb0ef41Sopenharmony_ci      scopes: {
1781cb0ef41Sopenharmony_ci        'data:text/javascript,0/': {
1791cb0ef41Sopenharmony_ci          integrity: true
1801cb0ef41Sopenharmony_ci        },
1811cb0ef41Sopenharmony_ci      }
1821cb0ef41Sopenharmony_ci    });
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci    for (const href of baseURLs) {
1851cb0ef41Sopenharmony_ci      assert.throws(
1861cb0ef41Sopenharmony_ci        () => {
1871cb0ef41Sopenharmony_ci          manifest.assertIntegrity(href);
1881cb0ef41Sopenharmony_ci        },
1891cb0ef41Sopenharmony_ci        /ERR_MANIFEST_ASSERT_INTEGRITY/
1901cb0ef41Sopenharmony_ci      );
1911cb0ef41Sopenharmony_ci    }
1921cb0ef41Sopenharmony_ci  }
1931cb0ef41Sopenharmony_ci}
1941cb0ef41Sopenharmony_ci// #endregion
1951cb0ef41Sopenharmony_ci// #region blob
1961cb0ef41Sopenharmony_ci{
1971cb0ef41Sopenharmony_ci  {
1981cb0ef41Sopenharmony_ci    const manifest = new Manifest({
1991cb0ef41Sopenharmony_ci      scopes: {
2001cb0ef41Sopenharmony_ci        'https://example.com/': {
2011cb0ef41Sopenharmony_ci          integrity: true
2021cb0ef41Sopenharmony_ci        }
2031cb0ef41Sopenharmony_ci      }
2041cb0ef41Sopenharmony_ci    });
2051cb0ef41Sopenharmony_ci
2061cb0ef41Sopenharmony_ci    assert.strictEqual(
2071cb0ef41Sopenharmony_ci      manifest.assertIntegrity('blob:https://example.com/has-origin'),
2081cb0ef41Sopenharmony_ci      true
2091cb0ef41Sopenharmony_ci    );
2101cb0ef41Sopenharmony_ci  }
2111cb0ef41Sopenharmony_ci  {
2121cb0ef41Sopenharmony_ci    const manifest = new Manifest({
2131cb0ef41Sopenharmony_ci      scopes: {
2141cb0ef41Sopenharmony_ci      }
2151cb0ef41Sopenharmony_ci    });
2161cb0ef41Sopenharmony_ci
2171cb0ef41Sopenharmony_ci    assert.throws(
2181cb0ef41Sopenharmony_ci      () => {
2191cb0ef41Sopenharmony_ci        manifest.assertIntegrity('blob:https://example.com/has-origin');
2201cb0ef41Sopenharmony_ci      },
2211cb0ef41Sopenharmony_ci      /ERR_MANIFEST_ASSERT_INTEGRITY/
2221cb0ef41Sopenharmony_ci    );
2231cb0ef41Sopenharmony_ci  }
2241cb0ef41Sopenharmony_ci  {
2251cb0ef41Sopenharmony_ci    const manifest = new Manifest({
2261cb0ef41Sopenharmony_ci      scopes: {
2271cb0ef41Sopenharmony_ci        'blob:https://example.com/has-origin': {
2281cb0ef41Sopenharmony_ci          cascade: true
2291cb0ef41Sopenharmony_ci        }
2301cb0ef41Sopenharmony_ci      }
2311cb0ef41Sopenharmony_ci    });
2321cb0ef41Sopenharmony_ci
2331cb0ef41Sopenharmony_ci    assert.throws(
2341cb0ef41Sopenharmony_ci      () => {
2351cb0ef41Sopenharmony_ci        manifest.assertIntegrity('blob:https://example.com/has-origin');
2361cb0ef41Sopenharmony_ci      },
2371cb0ef41Sopenharmony_ci      /ERR_MANIFEST_ASSERT_INTEGRITY/
2381cb0ef41Sopenharmony_ci    );
2391cb0ef41Sopenharmony_ci  }
2401cb0ef41Sopenharmony_ci  {
2411cb0ef41Sopenharmony_ci    const manifest = new Manifest({
2421cb0ef41Sopenharmony_ci      resources: {
2431cb0ef41Sopenharmony_ci        'blob:https://example.com/has-origin': {
2441cb0ef41Sopenharmony_ci          cascade: true
2451cb0ef41Sopenharmony_ci        }
2461cb0ef41Sopenharmony_ci      },
2471cb0ef41Sopenharmony_ci      scopes: {
2481cb0ef41Sopenharmony_ci        'https://example.com': {
2491cb0ef41Sopenharmony_ci          integrity: true
2501cb0ef41Sopenharmony_ci        }
2511cb0ef41Sopenharmony_ci      }
2521cb0ef41Sopenharmony_ci    });
2531cb0ef41Sopenharmony_ci
2541cb0ef41Sopenharmony_ci    assert.strictEqual(
2551cb0ef41Sopenharmony_ci      manifest.assertIntegrity('blob:https://example.com/has-origin'),
2561cb0ef41Sopenharmony_ci      true
2571cb0ef41Sopenharmony_ci    );
2581cb0ef41Sopenharmony_ci  }
2591cb0ef41Sopenharmony_ci  {
2601cb0ef41Sopenharmony_ci    const manifest = new Manifest({
2611cb0ef41Sopenharmony_ci      scopes: {
2621cb0ef41Sopenharmony_ci        'blob:': {
2631cb0ef41Sopenharmony_ci          integrity: true
2641cb0ef41Sopenharmony_ci        },
2651cb0ef41Sopenharmony_ci        'https://example.com': {
2661cb0ef41Sopenharmony_ci          cascade: true
2671cb0ef41Sopenharmony_ci        }
2681cb0ef41Sopenharmony_ci      }
2691cb0ef41Sopenharmony_ci    });
2701cb0ef41Sopenharmony_ci
2711cb0ef41Sopenharmony_ci    assert.throws(
2721cb0ef41Sopenharmony_ci      () => {
2731cb0ef41Sopenharmony_ci        manifest.assertIntegrity('blob:https://example.com/has-origin');
2741cb0ef41Sopenharmony_ci      },
2751cb0ef41Sopenharmony_ci      /ERR_MANIFEST_ASSERT_INTEGRITY/
2761cb0ef41Sopenharmony_ci    );
2771cb0ef41Sopenharmony_ci    assert.strictEqual(
2781cb0ef41Sopenharmony_ci      manifest.assertIntegrity('blob:foo'),
2791cb0ef41Sopenharmony_ci      true
2801cb0ef41Sopenharmony_ci    );
2811cb0ef41Sopenharmony_ci  }
2821cb0ef41Sopenharmony_ci}
2831cb0ef41Sopenharmony_ci// #endregion
2841cb0ef41Sopenharmony_ci// #startonerror
2851cb0ef41Sopenharmony_ci{
2861cb0ef41Sopenharmony_ci  const manifest = new Manifest({
2871cb0ef41Sopenharmony_ci    scopes: {
2881cb0ef41Sopenharmony_ci      'file:///': {
2891cb0ef41Sopenharmony_ci        integrity: true
2901cb0ef41Sopenharmony_ci      }
2911cb0ef41Sopenharmony_ci    },
2921cb0ef41Sopenharmony_ci    onerror: 'throw'
2931cb0ef41Sopenharmony_ci  });
2941cb0ef41Sopenharmony_ci  assert.throws(
2951cb0ef41Sopenharmony_ci    () => {
2961cb0ef41Sopenharmony_ci      manifest.assertIntegrity('http://example.com');
2971cb0ef41Sopenharmony_ci    },
2981cb0ef41Sopenharmony_ci    /ERR_MANIFEST_ASSERT_INTEGRITY/
2991cb0ef41Sopenharmony_ci  );
3001cb0ef41Sopenharmony_ci}
3011cb0ef41Sopenharmony_ci{
3021cb0ef41Sopenharmony_ci  assert.throws(
3031cb0ef41Sopenharmony_ci    () => {
3041cb0ef41Sopenharmony_ci      new Manifest({
3051cb0ef41Sopenharmony_ci        scopes: {
3061cb0ef41Sopenharmony_ci          'file:///': {
3071cb0ef41Sopenharmony_ci            integrity: true
3081cb0ef41Sopenharmony_ci          }
3091cb0ef41Sopenharmony_ci        },
3101cb0ef41Sopenharmony_ci        onerror: 'unknown'
3111cb0ef41Sopenharmony_ci      });
3121cb0ef41Sopenharmony_ci    },
3131cb0ef41Sopenharmony_ci    /ERR_MANIFEST_UNKNOWN_ONERROR/
3141cb0ef41Sopenharmony_ci  );
3151cb0ef41Sopenharmony_ci}
3161cb0ef41Sopenharmony_ci// #endonerror
317