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

bootstrap: move policy setup code to internal/process/policy.js #43282

Closed
wants to merge 3 commits 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
51 changes: 4 additions & 47 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const {
ObjectDefineProperties,
ObjectDefineProperty,
ObjectGetOwnPropertyDescriptor,
SafeMap,
SafeWeakMap,
StringPrototypeStartsWith,
globalThis,
Expand All @@ -24,9 +23,6 @@ const {
} = require('internal/util');

const { Buffer } = require('buffer');
const {
ERR_MANIFEST_ASSERT_INTEGRITY,
} = require('internal/errors').codes;
const assert = require('internal/assert');

function prepareMainThreadExecution(expandArgv1 = false,
Expand Down Expand Up @@ -462,51 +458,12 @@ function initializeClusterIPC() {
}

function initializePolicy() {
const experimentalPolicy = getOptionValue('--experimental-policy');
if (experimentalPolicy) {
const policyPath = getOptionValue('--experimental-policy');
if (policyPath) {
process.emitWarning('Policies are experimental.',
'ExperimentalWarning');
const { pathToFileURL, URL } = require('internal/url');
// URL here as it is slightly different parsing
// no bare specifiers for now
let manifestURL;
if (require('path').isAbsolute(experimentalPolicy)) {
manifestURL = new URL(`file://${experimentalPolicy}`);
} else {
const cwdURL = pathToFileURL(process.cwd());
cwdURL.pathname += '/';
manifestURL = new URL(experimentalPolicy, cwdURL);
}
const fs = require('fs');
const src = fs.readFileSync(manifestURL, 'utf8');
const experimentalPolicyIntegrity = getOptionValue('--policy-integrity');
if (experimentalPolicyIntegrity) {
const SRI = require('internal/policy/sri');
const { createHash, timingSafeEqual } = require('crypto');
const realIntegrities = new SafeMap();
const integrityEntries = SRI.parse(experimentalPolicyIntegrity);
let foundMatch = false;
for (let i = 0; i < integrityEntries.length; i++) {
const {
algorithm,
value: expected
} = integrityEntries[i];
const hash = createHash(algorithm);
hash.update(src);
const digest = hash.digest();
if (digest.length === expected.length &&
timingSafeEqual(digest, expected)) {
foundMatch = true;
break;
}
realIntegrities.set(algorithm, digest.toString('base64'));
}
if (!foundMatch) {
throw new ERR_MANIFEST_ASSERT_INTEGRITY(manifestURL, realIntegrities);
}
}
require('internal/process/policy')
.setup(src, manifestURL.href);
const policyIntegrity = getOptionValue('--policy-integrity');
require('internal/process/policy').setup(policyPath, policyIntegrity);
}
}

Expand Down
56 changes: 50 additions & 6 deletions lib/internal/process/policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,78 @@ const {
JSONParse,
ObjectFreeze,
ReflectSetPrototypeOf,
SafeMap,
} = primordials;

const {
ERR_MANIFEST_TDZ,
ERR_MANIFEST_ASSERT_INTEGRITY,
} = require('internal/errors').codes;
const { Manifest } = require('internal/policy/manifest');
const { pathToFileURL, URL } = require('internal/url');
const { readFileSync } = require('fs');
const { isAbsolute } = require('path');
const { parse } = require('internal/policy/sri');

let manifest;
let manifestSrc;
let manifestURL;

module.exports = ObjectFreeze({
__proto__: null,
setup(src, url) {
manifestSrc = src;
manifestURL = url;
if (src === null) {
setup(policyPath, policyIntegrity) {
// URL here as it is slightly different parsing
// no bare specifiers for now
let manifestURLObject;
if (isAbsolute(policyPath)) {
manifestURLObject = new URL(`file://${policyPath}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the path contains a char that has a special meaning in URL (e.g. ?)? @watilde since you're the one who committed this line, do you know if that should be that instead:

Suggested change
manifestURLObject = new URL(`file://${policyPath}`);
manifestURLObject = pathToFileUrl(policyPath);

(non-blocking for this PR)

Copy link
Member Author

@joyeecheung joyeecheung Jun 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather leave this to other PRs as this one only tries to move things to a different palce (same with other suggestions below, but I can apply the first ones since they are only added in this PR).

} else {
const cwdURL = pathToFileURL(process.cwd());
cwdURL.pathname += '/';
manifestURLObject = new URL(policyPath, cwdURL);
Comment on lines +33 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, I think it should be:

Suggested change
const cwdURL = pathToFileURL(process.cwd());
cwdURL.pathname += '/';
manifestURLObject = new URL(policyPath, cwdURL);
manifestURLObject = pathToFileURL(join(process.cwd(), policyPath));

}
manifestSrc = readFileSync(manifestURLObject, 'utf8');

if (policyIntegrity) {
// These should be loaded lazily in case the build does not have crypto.
const { createHash, timingSafeEqual } = require('crypto');
const realIntegrities = new SafeMap();
const integrityEntries = parse(policyIntegrity);
let foundMatch = false;
for (let i = 0; i < integrityEntries.length; i++) {
const {
algorithm,
value: expected
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
value: expected
value: expected,

} = integrityEntries[i];
const hash = createHash(algorithm);
hash.update(manifestSrc);
const digest = hash.digest();
if (digest.length === expected.length &&
timingSafeEqual(digest, expected)) {
foundMatch = true;
break;
}
realIntegrities.set(algorithm, digest.toString('base64'));
}
if (!foundMatch) {
throw new ERR_MANIFEST_ASSERT_INTEGRITY(manifestURL, realIntegrities);
}
}
manifestURL = manifestURLObject.href;

if (manifestSrc === null) {
manifest = null;
return;
}

const json = JSONParse(src, (_, o) => {
const json = JSONParse(manifestSrc, (_, o) => {
if (o && typeof o === 'object') {
ReflectSetPrototypeOf(o, null);
ObjectFreeze(o);
}
return o;
});
manifest = new Manifest(json, url);
manifest = new Manifest(json, manifestURL);
},

get manifest() {
Expand Down