summaryrefslogtreecommitdiff
path: root/lib/internal/policy/manifest.js
blob: f6adca125bdcb4db395efade7f751309d87a986d (plain)
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
'use strict';
const {
  ERR_MANIFEST_ASSERT_INTEGRITY,
  ERR_MANIFEST_INTEGRITY_MISMATCH,
  ERR_MANIFEST_UNKNOWN_ONERROR,
} = require('internal/errors').codes;
const debug = require('util').debuglog('policy');
const SRI = require('internal/policy/sri');
const {
  SafeWeakMap,
  FunctionPrototype,
  Object,
  RegExpPrototype
} = primordials;
const crypto = require('crypto');
const { Buffer } = require('buffer');
const { URL } = require('url');
const { createHash, timingSafeEqual } = crypto;
const HashUpdate = FunctionPrototype.call.bind(crypto.Hash.prototype.update);
const HashDigest = FunctionPrototype.call.bind(crypto.Hash.prototype.digest);
const BufferEquals = FunctionPrototype.call.bind(Buffer.prototype.equals);
const BufferToString = FunctionPrototype.call.bind(Buffer.prototype.toString);
const RegExpTest = FunctionPrototype.call.bind(RegExpPrototype.test);
const { entries } = Object;
const kIntegrities = new SafeWeakMap();
const kReactions = new SafeWeakMap();
const kRelativeURLStringPattern = /^\.{0,2}\//;
const { shouldAbortOnUncaughtException } = internalBinding('config');
const { abort, exit, _rawDebug } = process;

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);
}

class Manifest {
  constructor(obj, manifestURL) {
    const integrities = {
      __proto__: null,
    };
    const reactions = {
      __proto__: null,
      integrity: REACTION_THROW,
    };

    if (obj.onerror) {
      const behavior = obj.onerror;
      if (behavior === 'throw') {
      } else if (behavior === 'exit') {
        reactions.integrity = REACTION_EXIT;
      } else if (behavior === 'log') {
        reactions.integrity = REACTION_LOG;
      } else {
        throw new ERR_MANIFEST_UNKNOWN_ONERROR(behavior);
      }
    }

    kReactions.set(this, Object.freeze(reactions));
    const manifestEntries = entries(obj.resources);

    for (var i = 0; i < manifestEntries.length; i++) {
      let url = manifestEntries[i][0];
      const integrity = manifestEntries[i][1].integrity;
      if (integrity != null) {
        debug(`Manifest contains integrity for url ${url}`);
        if (RegExpTest(kRelativeURLStringPattern, url)) {
          url = new URL(url, manifestURL).href;
        }

        const sri = Object.freeze(SRI.parse(integrity));
        if (url in integrities) {
          const old = integrities[url];
          let mismatch = false;

          if (old.length !== sri.length) {
            mismatch = true;
          } else {
            compare:
            for (var sriI = 0; sriI < sri.length; sriI++) {
              for (var oldI = 0; oldI < old.length; oldI++) {
                if (sri[sriI].algorithm === old[oldI].algorithm &&
                  BufferEquals(sri[sriI].value, old[oldI].value) &&
                  sri[sriI].options === old[oldI].options) {
                  continue compare;
                }
              }
              mismatch = true;
              break compare;
            }
          }

          if (mismatch) {
            throw new ERR_MANIFEST_INTEGRITY_MISMATCH(url);
          }
        }
        integrities[url] = sri;
      }
    }
    Object.freeze(integrities);
    kIntegrities.set(this, integrities);
    Object.freeze(this);
  }

  assertIntegrity(url, content) {
    debug(`Checking integrity of ${url}`);
    const integrities = kIntegrities.get(this);
    const realIntegrities = new Map();

    if (integrities && url in integrities) {
      const integrityEntries = integrities[url];
      // Avoid clobbered Symbol.iterator
      for (var i = 0; i < integrityEntries.length; i++) {
        const {
          algorithm,
          value: expected
        } = integrityEntries[i];
        const hash = createHash(algorithm);
        HashUpdate(hash, content);
        const digest = HashDigest(hash);
        if (digest.length === expected.length &&
          timingSafeEqual(digest, expected)) {
          return true;
        }
        realIntegrities.set(algorithm, BufferToString(digest, 'base64'));
      }
    }
    const error = new ERR_MANIFEST_ASSERT_INTEGRITY(url, realIntegrities);
    kReactions.get(this).integrity(error);
  }
}

// Lock everything down to avoid problems even if reference is leaked somehow
Object.setPrototypeOf(Manifest, null);
Object.setPrototypeOf(Manifest.prototype, null);
Object.freeze(Manifest);
Object.freeze(Manifest.prototype);
module.exports = Object.freeze({ Manifest });