-
Notifications
You must be signed in to change notification settings - Fork 30k
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
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
c1af667
bootstrap: move policy setup code to internal/process/policy.js
joyeecheung 7f2050c
fixup! bootstrap: move policy setup code to internal/process/policy.js
joyeecheung 3616a03
fixup! bootstrap: move policy setup code to internal/process/policy.js
joyeecheung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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}`); | ||||||||||
} else { | ||||||||||
const cwdURL = pathToFileURL(process.cwd()); | ||||||||||
cwdURL.pathname += '/'; | ||||||||||
manifestURLObject = new URL(policyPath, cwdURL); | ||||||||||
Comment on lines
+33
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, I think it should be:
Suggested change
|
||||||||||
} | ||||||||||
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 | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
} = 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() { | ||||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:(non-blocking for this PR)
There was a problem hiding this comment.
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).