Skip to content

Commit dca9029

Browse files
nits
1 parent 057c177 commit dca9029

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

lib/internal/policy/manifest.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,45 @@ function REACTION_LOG(error) {
5151
}
5252

5353
class Manifest {
54+
/**
55+
* Url string => true | string | SRI[]
56+
*
57+
* Used to compare a resource to the content body at the resource.
58+
* `true` is used to signify that all integrities are allowed, otherwise,
59+
* SRI strings are parsed to compare with the body.
60+
*
61+
* This stores strings instead of eagerly parsing SRI strings
62+
* and only converts them to SRI data structures when needed.
63+
* This avoids needing to parse all SRI strings at startup even
64+
* if some never end up being used.
65+
*/
5466
#integrities = new SafeMap();
67+
/**
68+
* Url string => (string) => true | URL
69+
*
70+
* Used to find where a dependency is located.
71+
*
72+
* This stores functions to lazily calculate locations as needed.
73+
* `true` is used to signify that the location is not specified
74+
* by the manifest and default resolution should be allowed.
75+
*/
5576
#dependencies = new SafeMap();
77+
/**
78+
* (Error) => undefined
79+
*
80+
* Performs default action for what happens when a manifest encounters
81+
* a violation such as abort()ing or exiting the process, throwing the error,
82+
* or logging the error.
83+
*/
5684
#reaction = null;
85+
86+
/**
87+
* `obj` should match the policy file format described in the docs
88+
* it is expected to not have prototype pollution issues either by reassigning
89+
* the prototype to `null` for values or by running prior to any user code.
90+
*
91+
* `manifestURL` is a URL to resolve relative locations against.
92+
*/
5793
constructor(obj, manifestURL) {
5894
const integrities = this.#integrities;
5995
const dependencies = this.#dependencies;

lib/internal/policy/sri.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
2-
// Value of https://w3c.github.io/webappsec-subresource-integrity/#the-integrity-attribute
2+
// Utility to parse the value of
3+
// https://w3c.github.io/webappsec-subresource-integrity/#the-integrity-attribute
34

45
const {
56
Object: {
@@ -10,7 +11,6 @@ const {
1011
StringPrototype
1112
} = primordials;
1213

13-
// Returns [{algorithm, value (in base64 string), options,}]
1414
const {
1515
ERR_SRI_PARSE
1616
} = require('internal/errors').codes;
@@ -29,21 +29,22 @@ freeze(kSRIPattern);
2929
const BufferFrom = require('buffer').Buffer.from;
3030
const RealArrayPrototype = getPrototypeOf([]);
3131

32+
// Returns {algorithm, value (in base64 string), options,}[]
3233
const parse = (str) => {
3334
let prevIndex = 0;
35+
// Avoid setters being fired
3436
const entries = setPrototypeOf([], null);
35-
for (const match of StringPrototype.matchAll(
37+
const matches = StringPrototype.matchAll(
3638
StringPrototype.trimRight(str),
37-
kSRIPattern)
38-
) {
39+
kSRIPattern
40+
);
41+
for (const match of matches) {
3942
if (match.index !== prevIndex) {
4043
throw new ERR_SRI_PARSE(str, str.charAt(prevIndex), prevIndex);
4144
}
4245
if (entries.length > 0 && match[1] === '') {
4346
throw new ERR_SRI_PARSE(str, str.charAt(prevIndex), prevIndex);
4447
}
45-
46-
// Avoid setters being fired
4748
entries[entries.length] = freeze({
4849
__proto__: null,
4950
algorithm: match[2],

0 commit comments

Comments
 (0)