-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Use SHA-512 integrity in package locks #14701
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
Closed
Sean McManus (sean-mcmanus)
wants to merge
5
commits into
main
from
seanmcm/devbox2-wsl/agent76/sha512-lock-integrity
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4e52e3a
Use SHA-512 integrity in package locks
sean-mcmanus cb2474d
Reject unpinned package lock entries
sean-mcmanus 97153e2
Validate package lock exemptions
sean-mcmanus 493b432
Normalize legacy integrity matching
sean-mcmanus 62cd463
Improve lockfile validation diagnostics
sean-mcmanus 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { hasLockedIntegrity } from './subresourceIntegrity.mjs'; | ||
|
|
||
| const excludedDirectoryNames = new Set(['.git', 'node_modules']); | ||
|
|
||
| function findPackageLockPaths(repositoryRoot) { | ||
| const packageLockPaths = []; | ||
|
|
||
| function visit(directory) { | ||
| const entries = fs.readdirSync(directory, { withFileTypes: true }) | ||
| .sort((left, right) => left.name.localeCompare(right.name)); | ||
| for (const entry of entries) { | ||
| const entryPath = path.join(directory, entry.name); | ||
| if (entry.isDirectory() && !excludedDirectoryNames.has(entry.name)) { | ||
| visit(entryPath); | ||
| } else if (entry.isFile() && entry.name === 'package-lock.json') { | ||
| packageLockPaths.push(entryPath); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| visit(repositoryRoot); | ||
| return packageLockPaths; | ||
| } | ||
|
|
||
| function getWorkspaceTargetPath(packageEntry) { | ||
| if (packageEntry.link !== true || typeof packageEntry.resolved !== 'string') { | ||
| return undefined; | ||
| } | ||
|
|
||
| const workspacePath = packageEntry.resolved.replaceAll('\\', '/'); | ||
| const normalizedPath = path.posix.normalize(workspacePath); | ||
| return workspacePath.length > 0 | ||
| && !/^[A-Za-z][A-Za-z0-9+.-]*:/.test(workspacePath) | ||
| && !path.posix.isAbsolute(workspacePath) | ||
| && normalizedPath === workspacePath | ||
| && normalizedPath !== '.' | ||
| && normalizedPath !== '..' | ||
| && !normalizedPath.startsWith('../') | ||
| ? normalizedPath | ||
| : undefined; | ||
| } | ||
|
|
||
| function getWorkspacePaths(packages) { | ||
| return new Set(Object.values(packages) | ||
| .map(getWorkspaceTargetPath) | ||
| .filter(workspacePath => workspacePath !== undefined) | ||
| .filter(workspacePath => { | ||
| const workspaceEntry = packages[workspacePath]; | ||
| return !workspacePath.split('/').includes('node_modules') | ||
| && workspaceEntry !== undefined | ||
| && workspaceEntry.resolved === undefined | ||
| && workspaceEntry.integrity === undefined; | ||
| })); | ||
| } | ||
|
|
||
| function isBundledPackageEntry(packages, packagePath, packageEntry) { | ||
| if (packageEntry.inBundle !== true || !packagePath.includes('/node_modules/')) { | ||
| return false; | ||
| } | ||
|
|
||
| let ancestorPath = packagePath.slice(0, packagePath.lastIndexOf('/node_modules/')); | ||
| while (ancestorPath) { | ||
| const ancestorEntry = packages[ancestorPath]; | ||
| if (ancestorEntry && hasLockedIntegrity(ancestorEntry.integrity)) { | ||
| const relativePath = packagePath.slice(`${ancestorPath}/node_modules/`.length); | ||
| const relativeSegments = relativePath.split('/'); | ||
| const packageName = relativeSegments[0].startsWith('@') | ||
| ? relativeSegments.slice(0, 2).join('/') | ||
| : relativeSegments[0]; | ||
| return Array.isArray(ancestorEntry.bundleDependencies) && ancestorEntry.bundleDependencies.includes(packageName); | ||
| } | ||
|
|
||
| const nextSeparator = ancestorPath.lastIndexOf('/node_modules/'); | ||
| if (nextSeparator === -1) { | ||
| break; | ||
| } | ||
| ancestorPath = ancestorPath.slice(0, nextSeparator); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| function isExplicitLocalPackageEntry(packages, workspacePaths, packagePath, packageEntry) { | ||
| const normalizedPackagePath = packagePath.replaceAll('\\', '/'); | ||
| return workspacePaths.has(getWorkspaceTargetPath(packageEntry)) | ||
| || workspacePaths.has(normalizedPackagePath) | ||
| || (packageEntry.link !== true && typeof packageEntry.resolved === 'string' && /^(?:file|link|workspace):/i.test(packageEntry.resolved)) | ||
| || isBundledPackageEntry(packages, normalizedPackagePath, packageEntry); | ||
| } | ||
|
|
||
| export { findPackageLockPaths, getWorkspacePaths, isExplicitLocalPackageEntry }; | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { Buffer } from 'node:buffer'; | ||
| import { createHash } from 'node:crypto'; | ||
|
|
||
| const digestLengths = new Map([ | ||
| ['sha1', 20], | ||
| ['sha256', 32], | ||
| ['sha384', 48], | ||
| ['sha512', 64] | ||
| ]); | ||
| const supportedAlgorithms = new Set(['sha256', 'sha384', 'sha512']); | ||
|
|
||
| function calculateIntegrity(algorithm, content) { | ||
| return `${algorithm}-${createHash(algorithm).update(content).digest('base64')}`; | ||
| } | ||
|
|
||
| function parseValidDigest(digest) { | ||
| const metadataSeparator = digest.indexOf('?'); | ||
| const digestWithoutMetadata = metadataSeparator === -1 ? digest : digest.slice(0, metadataSeparator); | ||
| const match = /^(sha1|sha256|sha384|sha512)-([A-Za-z0-9+/]+={0,2})$/.exec(digestWithoutMetadata); | ||
| if (!match) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const [, algorithm, serializedDigest] = match; | ||
| const decodedDigest = Buffer.from(serializedDigest, 'base64'); | ||
| return decodedDigest.length === digestLengths.get(algorithm) | ||
| && decodedDigest.toString('base64').replace(/=+$/, '') === serializedDigest.replace(/=+$/, '') | ||
| ? { algorithm, digest: decodedDigest } | ||
| : undefined; | ||
| } | ||
|
|
||
| function hasLockedIntegrity(integrity) { | ||
| return typeof integrity === 'string' && integrity.split(/\s+/).some(digest => parseValidDigest(digest) !== undefined); | ||
| } | ||
|
|
||
| function hasSupportedIntegrityAlgorithm(integrity) { | ||
| return typeof integrity === 'string' && integrity.split(/\s+/).some(digest => supportedAlgorithms.has(parseValidDigest(digest)?.algorithm)); | ||
| } | ||
|
|
||
| function integrityMatchesContent(integrity, algorithm, content) { | ||
| if (typeof integrity !== 'string') { | ||
| return false; | ||
| } | ||
|
|
||
| const expectedDigest = createHash(algorithm).update(content).digest(); | ||
| return integrity.split(/\s+/).some(serializedDigest => { | ||
| const parsedDigest = parseValidDigest(serializedDigest); | ||
| return parsedDigest?.algorithm === algorithm && parsedDigest.digest.equals(expectedDigest); | ||
| }); | ||
| } | ||
|
|
||
| export { calculateIntegrity, hasLockedIntegrity, hasSupportedIntegrityAlgorithm, integrityMatchesContent }; |
Oops, something went wrong.
Oops, something went wrong.
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.
✨Copilot (agent102): [Minor] This exemption treats a lockfile-controlled
resolvedprefix as authorization to skip integrity, without validating the target — and the same pattern exists on the Yarn side atverifyYarnLock.mjsline 93 (!isExplicitLocalResolution(currentEntry.resolved)). Both were raised as suppressed low-confidence observations in the automated review at this head; I verified them against this revision rather than taking them as given.Calling the exported validators directly at
62cd4633:node_modules/evilwithresolved: "file:../../outside/evil.tgz"node_modules/evilwithresolved: "link:/etc/evil"node_modules/evilwithresolved: "workspace:../../outside"node_modules/evilwithlink: true, resolved: "../../outside"node_modules/evilwithresolved: "https://example.test/evil.tgz"dependency@1.0.0withresolved "file:../../outside/dep.tgz"dependency@1.0.0withresolved "link:/etc/evil"dependency@1.0.0withresolved "https://example.test/dep.tgz"So the
link: truepath that was hardened earlier in this PR rejects traversal, absolute, and protocol targets, while its non-link sibling accepts an arbitrary string after the protocol prefix. On the Yarn side an ordinary registry selector is exempted purely because of itsresolvedline, which does not match the earlier statement that "only selectors whose actual dependency range uses an explicitfile:,link:, orworkspace:protocol are exempt" — the condition is anORof the selector check and the resolution check.Impact is bounded: no entry in any current lockfile lacks integrity, so nothing relies on this exemption today, and it only matters for a future lockfile change. But since the stated goal is repository-wide integrity enforcement, it is worth closing so the validator cannot be opted out of by editing the lockfile it is validating.
Minimal resolution: apply the same target rules the
link: truepath already uses — resolve the local target relative to the lockfile and exempt only normalized, repository-contained paths — or, for Yarn, rely onhasExplicitLocalSelectoralone. Because every current entry carries integrity, either change is inert against the lockfiles in this PR.