Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

policy: refactor to use more primordials #36210

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
policy: refactor to use more primordials
  • Loading branch information
aduh95 committed Nov 21, 2020
commit 888eef75aa56e1998f903d1779b024c3d186319d
18 changes: 9 additions & 9 deletions lib/internal/policy/manifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

const {
ArrayIsArray,
Map,
MapPrototypeSet,
ObjectCreate,
ObjectEntries,
ObjectFreeze,
Expand All @@ -12,6 +10,8 @@ const {
RegExpPrototypeTest,
SafeMap,
SafeSet,
StringPrototypeEndsWith,
StringPrototypeReplace,
Symbol,
uncurryThis,
} = primordials;
Expand Down Expand Up @@ -328,14 +328,15 @@ class Manifest {
* @returns {string}
*/
const protocolOrResolve = (resourceHREF) => {
if (resourceHREF.endsWith(':')) {
if (StringPrototypeEndsWith(resourceHREF, ':')) {
// URL parse will trim these anyway, save the compute
resourceHREF = resourceHREF.replace(
resourceHREF = StringPrototypeReplace(
resourceHREF,
// eslint-disable-next-line
/^[\x00-\x1F\x20]|\x09\x0A\x0D|[\x00-\x1F\x20]$/g,
''
);
if (/^[a-zA-Z][a-zA-Z+\-.]*:$/.test(resourceHREF)) {
if (RegExpPrototypeTest(/^[a-zA-Z][a-zA-Z+\-.]*:$/, resourceHREF)) {
return resourceHREF;
}
}
Expand Down Expand Up @@ -418,7 +419,7 @@ class Manifest {
// Only a few schemes are hierarchical
if (SPECIAL_SCHEMES.has(currentURL.protocol)) {
// Make first '..' act like '.'
if (currentURL.pathname.slice(-1) !== '/') {
if (!StringPrototypeEndsWith(currentURL.pathname, '/')) {
currentURL.pathname += '/';
}
let lastHREF;
Expand Down Expand Up @@ -470,7 +471,7 @@ class Manifest {
assertIntegrity(url, content) {
const href = `${url}`;
debug('Checking integrity of %s', href);
const realIntegrities = new Map();
const realIntegrities = new SafeMap();
const integrities = this.#resourceIntegrities;
function processEntry(href) {
let integrityEntries = integrities.get(href);
Expand Down Expand Up @@ -499,8 +500,7 @@ class Manifest {
timingSafeEqual(digest, expected)) {
return true;
}
MapPrototypeSet(
realIntegrities,
realIntegrities.set(
bmeck marked this conversation as resolved.
Show resolved Hide resolved
algorithm,
BufferToString(digest, 'base64')
);
Expand Down
11 changes: 5 additions & 6 deletions lib/internal/policy/sri.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// https://w3c.github.io/webappsec-subresource-integrity/#the-integrity-attribute

const {
ArrayPrototype,
ObjectDefineProperty,
ObjectFreeze,
ObjectGetPrototypeOf,
ObjectSeal,
ObjectSetPrototypeOf,
RegExp,
Expand All @@ -32,7 +32,6 @@ const kAllWSP = RegExp(`^${kWSP}*$`);
ObjectSeal(kAllWSP);

const BufferFrom = require('buffer').Buffer.from;
const RealArrayPrototype = ObjectGetPrototypeOf([]);

// Returns {algorithm, value (in base64 string), options,}[]
const parse = (str) => {
Expand All @@ -41,10 +40,10 @@ const parse = (str) => {
const entries = [];
while (match = RegExpPrototypeExec(kSRIPattern, str)) {
if (match.index !== prevIndex) {
throw new ERR_SRI_PARSE(str, str.charAt(prevIndex), prevIndex);
throw new ERR_SRI_PARSE(str, str[prevIndex], prevIndex);
}
if (entries.length > 0 && match[1] === '') {
throw new ERR_SRI_PARSE(str, str.charAt(prevIndex), prevIndex);
throw new ERR_SRI_PARSE(str, str[prevIndex], prevIndex);
}

// Avoid setters being fired
Expand All @@ -63,10 +62,10 @@ const parse = (str) => {

if (prevIndex !== str.length) {
if (!RegExpPrototypeTest(kAllWSP, StringPrototypeSlice(str, prevIndex))) {
throw new ERR_SRI_PARSE(str, str.charAt(prevIndex), prevIndex);
throw new ERR_SRI_PARSE(str, str[prevIndex], prevIndex);
}
}
return ObjectSetPrototypeOf(entries, RealArrayPrototype);
return ObjectSetPrototypeOf(entries, ArrayPrototype);
};

module.exports = {
Expand Down