mirror of
https://github.com/nodejs/node.git
synced 2025-05-22 22:11:41 +00:00

Store all primordials as properties of the primordials object. Static functions are prefixed by the constructor's name and prototype methods are prefixed by the constructor's name followed by "Prototype". For example: primordials.Object.keys becomes primordials.ObjectKeys. PR-URL: https://github.com/nodejs/node/pull/30610 Refs: https://github.com/nodejs/node/issues/29766 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
70 lines
1.9 KiB
JavaScript
70 lines
1.9 KiB
JavaScript
'use strict';
|
|
// Value of https://w3c.github.io/webappsec-subresource-integrity/#the-integrity-attribute
|
|
|
|
const {
|
|
ObjectDefineProperty,
|
|
ObjectFreeze,
|
|
ObjectSeal,
|
|
RegExpPrototypeExec,
|
|
RegExpPrototypeTest,
|
|
StringPrototypeSlice,
|
|
} = primordials;
|
|
|
|
// Returns [{algorithm, value (in base64 string), options,}]
|
|
const {
|
|
ERR_SRI_PARSE
|
|
} = require('internal/errors').codes;
|
|
const kWSP = '[\\x20\\x09]';
|
|
const kVCHAR = '[\\x21-\\x7E]';
|
|
const kHASH_ALGO = 'sha(?:256|384|512)';
|
|
// Base64
|
|
const kHASH_VALUE = '[A-Za-z0-9+/]+[=]{0,2}';
|
|
const kHASH_EXPRESSION = `(${kHASH_ALGO})-(${kHASH_VALUE})`;
|
|
const kOPTION_EXPRESSION = `(${kVCHAR}*)`;
|
|
const kHASH_WITH_OPTIONS = `${kHASH_EXPRESSION}(?:[?](${kOPTION_EXPRESSION}))?`;
|
|
const kSRIPattern = RegExp(`(${kWSP}*)(?:${kHASH_WITH_OPTIONS})`, 'g');
|
|
ObjectSeal(kSRIPattern);
|
|
const kAllWSP = RegExp(`^${kWSP}*$`);
|
|
ObjectSeal(kAllWSP);
|
|
|
|
const BufferFrom = require('buffer').Buffer.from;
|
|
|
|
const parse = (str) => {
|
|
kSRIPattern.lastIndex = 0;
|
|
let prevIndex = 0;
|
|
let match;
|
|
const entries = [];
|
|
while (match = RegExpPrototypeExec(kSRIPattern, str)) {
|
|
if (match.index !== prevIndex) {
|
|
throw new ERR_SRI_PARSE(str, str.charAt(prevIndex), prevIndex);
|
|
}
|
|
if (entries.length > 0 && match[1] === '') {
|
|
throw new ERR_SRI_PARSE(str, str.charAt(prevIndex), prevIndex);
|
|
}
|
|
|
|
// Avoid setters being fired
|
|
ObjectDefineProperty(entries, entries.length, {
|
|
enumerable: true,
|
|
configurable: true,
|
|
value: ObjectFreeze({
|
|
__proto__: null,
|
|
algorithm: match[2],
|
|
value: BufferFrom(match[3], 'base64'),
|
|
options: match[4] === undefined ? null : match[4],
|
|
})
|
|
});
|
|
prevIndex = prevIndex + match[0].length;
|
|
}
|
|
|
|
if (prevIndex !== str.length) {
|
|
if (!RegExpPrototypeTest(kAllWSP, StringPrototypeSlice(str, prevIndex))) {
|
|
throw new ERR_SRI_PARSE(str, str.charAt(prevIndex), prevIndex);
|
|
}
|
|
}
|
|
return entries;
|
|
};
|
|
|
|
module.exports = {
|
|
parse,
|
|
};
|