Repository navigation
fix(security): upgrade tar and preserve Node 18 startup #1806
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
Merged
Merged
Changes from all commits
Commits
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 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,117 @@ | ||
| diff --git a/lib/managers/docker.js b/lib/managers/docker.js | ||
| --- a/lib/managers/docker.js | ||
| +++ b/lib/managers/docker.js | ||
| @@ -1,6 +1,5 @@ | ||
| import { Buffer } from "node:buffer"; | ||
| import { | ||
| - createReadStream, | ||
| lstatSync, | ||
| readdirSync, | ||
| readFileSync, | ||
| @@ -8,7 +7,6 @@ | ||
| import { platform as _platform, userInfo as _userInfo, homedir } from "node:os"; | ||
| import { basename, join, resolve, win32 } from "node:path"; | ||
| import process from "node:process"; | ||
| -import stream from "node:stream/promises"; | ||
| import { URL } from "node:url"; | ||
|
|
||
| import { x } from "tar"; | ||
| @@ -1481,23 +1479,18 @@ | ||
| return await safeExtractArchive( | ||
| fullImageName, | ||
| dir, | ||
| - async () => | ||
| - await stream.pipeline( | ||
| - createReadStream(fullImageName), | ||
| - x({ | ||
| - sync: false, | ||
| - preserveOwner: false, | ||
| - noMtime: true, | ||
| - noChmod: true, | ||
| - strict: !NON_STRICT_TAR_EXTRACT, | ||
| - C: dir, | ||
| - portable: true, | ||
| - unlink: true, | ||
| - onwarn: handleTarWarning, | ||
| - onReadEntry: handleAbsolutePath, | ||
| - filter: tarFilter, | ||
| - }), | ||
| - ), | ||
| + () => | ||
| + x({ | ||
| + file: fullImageName, | ||
| + preserveOwner: false, | ||
| + noMtime: true, | ||
| + strict: !NON_STRICT_TAR_EXTRACT, | ||
| + cwd: dir, | ||
| + unlink: true, | ||
| + onwarn: handleTarWarning, | ||
| + onEntry: handleAbsolutePath, | ||
| + filter: tarFilter, | ||
| + }), | ||
| "untar", | ||
| { | ||
| blockedReason: | ||
| @@ -1956,41 +1949,31 @@ | ||
| console.log(`About to export image ${fullImageName} to ${tempDir}`); | ||
| } | ||
| } | ||
| - await stream.pipeline( | ||
| - client.stream(`images/${fullImageName}/get`), | ||
| - x({ | ||
| - sync: false, | ||
| - preserveOwner: false, | ||
| - noMtime: true, | ||
| - noChmod: true, | ||
| - strict: !NON_STRICT_TAR_EXTRACT, | ||
| - C: tempDir, | ||
| - portable: true, | ||
| - unlink: true, | ||
| - onwarn: handleTarWarning, | ||
| - onReadEntry: handleAbsolutePath, | ||
| - filter: tarFilter, | ||
| - }), | ||
| - ); | ||
| + await x({ | ||
| + input: client.stream(`images/${fullImageName}/get`), | ||
| + preserveOwner: false, | ||
| + noMtime: true, | ||
| + strict: !NON_STRICT_TAR_EXTRACT, | ||
| + cwd: tempDir, | ||
| + unlink: true, | ||
| + onwarn: handleTarWarning, | ||
| + onEntry: handleAbsolutePath, | ||
| + filter: tarFilter, | ||
| + }); | ||
| } catch (_err) { | ||
| if (localData?.Id) { | ||
| console.log(`Retrying with ${localData.Id}`); | ||
| try { | ||
| - await stream.pipeline( | ||
| - client.stream(`images/${localData.Id}/get`), | ||
| - x({ | ||
| - sync: true, | ||
| - preserveOwner: false, | ||
| - noMtime: true, | ||
| - noChmod: true, | ||
| - strict: !NON_STRICT_TAR_EXTRACT, | ||
| - C: tempDir, | ||
| - portable: true, | ||
| - onwarn: handleTarWarning, | ||
| - onReadEntry: handleAbsolutePath, | ||
| - filter: tarFilter, | ||
| - }), | ||
| - ); | ||
| + await x({ | ||
| + input: client.stream(`images/${localData.Id}/get`), | ||
| + preserveOwner: false, | ||
| + noMtime: true, | ||
| + strict: !NON_STRICT_TAR_EXTRACT, | ||
| + cwd: tempDir, | ||
| + onwarn: handleTarWarning, | ||
| + onEntry: handleAbsolutePath, | ||
| + filter: tarFilter, | ||
| + }); | ||
| } catch (_err) { | ||
| // ignore | ||
| } |
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,33 @@ | ||
| diff --git a/dist/index.js b/dist/index.js | ||
| --- a/dist/index.js | ||
| +++ b/dist/index.js | ||
| @@ -1046,7 +1046,7 @@ | ||
| const ArrayPrototypeSplice = require_primordials_uncurry.uncurryThis(Array.prototype.splice); | ||
| const ArrayPrototypeToLocaleString = require_primordials_uncurry.uncurryThis(Array.prototype.toLocaleString); | ||
| const ArrayPrototypeToReversed = require_primordials_uncurry.uncurryThis(Array.prototype.toReversed); | ||
| - const ArrayPrototypeToSorted = require_primordials_uncurry.uncurryThis(Array.prototype.toSorted); | ||
| + const ArrayPrototypeToSorted = typeof Array.prototype.toSorted === 'function' ? require_primordials_uncurry.uncurryThis(Array.prototype.toSorted) : ((values, compareFn) => ArrayPrototypeSort(ArrayFrom(values), compareFn)); | ||
| const ArrayPrototypeToSpliced = require_primordials_uncurry.uncurryThis(Array.prototype.toSpliced); | ||
| const ArrayPrototypeToString = require_primordials_uncurry.uncurryThis(Array.prototype.toString); | ||
| const ArrayPrototypeUnshift = require_primordials_uncurry.uncurryThis(Array.prototype.unshift); | ||
| diff --git a/dist/exists.js b/dist/exists.js | ||
| --- a/dist/exists.js | ||
| +++ b/dist/exists.js | ||
| @@ -1845,7 +1845,7 @@ | ||
| const ArrayPrototypeSplice = require_primordials_uncurry.uncurryThis(Array.prototype.splice); | ||
| const ArrayPrototypeToLocaleString = require_primordials_uncurry.uncurryThis(Array.prototype.toLocaleString); | ||
| const ArrayPrototypeToReversed = require_primordials_uncurry.uncurryThis(Array.prototype.toReversed); | ||
| - const ArrayPrototypeToSorted = require_primordials_uncurry.uncurryThis(Array.prototype.toSorted); | ||
| + const ArrayPrototypeToSorted = typeof Array.prototype.toSorted === 'function' ? require_primordials_uncurry.uncurryThis(Array.prototype.toSorted) : ((values, compareFn) => ArrayPrototypeSort(ArrayFrom(values), compareFn)); | ||
| const ArrayPrototypeToSpliced = require_primordials_uncurry.uncurryThis(Array.prototype.toSpliced); | ||
| const ArrayPrototypeToString = require_primordials_uncurry.uncurryThis(Array.prototype.toString); | ||
| const ArrayPrototypeUnshift = require_primordials_uncurry.uncurryThis(Array.prototype.unshift); | ||
| @@ -14080,7 +14080,7 @@ | ||
| const ArrayPrototypeSplice = require_primordials_uncurry.uncurryThis(Array.prototype.splice); | ||
| const ArrayPrototypeToLocaleString = require_primordials_uncurry.uncurryThis(Array.prototype.toLocaleString); | ||
| const ArrayPrototypeToReversed = require_primordials_uncurry.uncurryThis(Array.prototype.toReversed); | ||
| - const ArrayPrototypeToSorted = require_primordials_uncurry.uncurryThis(Array.prototype.toSorted); | ||
| + const ArrayPrototypeToSorted = typeof Array.prototype.toSorted === 'function' ? require_primordials_uncurry.uncurryThis(Array.prototype.toSorted) : ((values, compareFn) => ArrayPrototypeSort(ArrayFrom(values), compareFn)); | ||
| const ArrayPrototypeToSpliced = require_primordials_uncurry.uncurryThis(Array.prototype.toSpliced); | ||
| const ArrayPrototypeToString = require_primordials_uncurry.uncurryThis(Array.prototype.toString); | ||
| const ArrayPrototypeUnshift = require_primordials_uncurry.uncurryThis(Array.prototype.unshift); |
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,21 @@ | ||
| diff --git a/dist/index.js b/dist/index.js | ||
| --- a/dist/index.js | ||
| +++ b/dist/index.js | ||
| @@ -1937,7 +1937,7 @@ | ||
| const ArrayPrototypeSplice = require_primordials_uncurry.uncurryThis(Array.prototype.splice); | ||
| const ArrayPrototypeToLocaleString = require_primordials_uncurry.uncurryThis(Array.prototype.toLocaleString); | ||
| const ArrayPrototypeToReversed = require_primordials_uncurry.uncurryThis(Array.prototype.toReversed); | ||
| - const ArrayPrototypeToSorted = require_primordials_uncurry.uncurryThis(Array.prototype.toSorted); | ||
| + const ArrayPrototypeToSorted = typeof Array.prototype.toSorted === 'function' ? require_primordials_uncurry.uncurryThis(Array.prototype.toSorted) : ((values, compareFn) => ArrayPrototypeSort(ArrayFrom(values), compareFn)); | ||
| const ArrayPrototypeToSpliced = require_primordials_uncurry.uncurryThis(Array.prototype.toSpliced); | ||
| const ArrayPrototypeToString = require_primordials_uncurry.uncurryThis(Array.prototype.toString); | ||
| const ArrayPrototypeUnshift = require_primordials_uncurry.uncurryThis(Array.prototype.unshift); | ||
| @@ -3981,7 +3981,7 @@ | ||
| const ArrayPrototypeSplice = require_primordials_uncurry.uncurryThis(Array.prototype.splice); | ||
| const ArrayPrototypeToLocaleString = require_primordials_uncurry.uncurryThis(Array.prototype.toLocaleString); | ||
| const ArrayPrototypeToReversed = require_primordials_uncurry.uncurryThis(Array.prototype.toReversed); | ||
| - const ArrayPrototypeToSorted = require_primordials_uncurry.uncurryThis(Array.prototype.toSorted); | ||
| + const ArrayPrototypeToSorted = typeof Array.prototype.toSorted === 'function' ? require_primordials_uncurry.uncurryThis(Array.prototype.toSorted) : ((values, compareFn) => ArrayPrototypeSort(ArrayFrom(values), compareFn)); | ||
| const ArrayPrototypeToSpliced = require_primordials_uncurry.uncurryThis(Array.prototype.toSpliced); | ||
| const ArrayPrototypeToString = require_primordials_uncurry.uncurryThis(Array.prototype.toString); | ||
| const ArrayPrototypeUnshift = require_primordials_uncurry.uncurryThis(Array.prototype.unshift); |
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,13 @@ | ||
| diff --git a/lib/install.js b/lib/install.js | ||
| --- a/lib/install.js | ||
| +++ b/lib/install.js | ||
| @@ -231,7 +231,8 @@ | ||
| contentShasums[filename] = checksum | ||
| log.verbose('content checksum', filename, checksum) | ||
| }), | ||
| - tar.extract({ | ||
| + async input => tar.extract({ | ||
| + input, | ||
| strip: 1, | ||
| cwd: tarExtractDir, | ||
| filter: isValid, |
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,132 @@ | ||
| diff --git a/lib/dir.js b/lib/dir.js | ||
| --- a/lib/dir.js | ||
| +++ b/lib/dir.js | ||
| @@ -1,6 +1,5 @@ | ||
| -const { resolve, join } = require('node:path') | ||
| -const { mkdtemp, writeFile, rm } = require('node:fs/promises') | ||
| -const { tmpdir } = require('node:os') | ||
| +const { resolve } = require('node:path') | ||
| +const { Readable } = require('node:stream') | ||
| const packlist = require('npm-packlist') | ||
| const runScript = require('@npmcli/run-script') | ||
| const tar = require('tar') | ||
| @@ -82,11 +81,8 @@ | ||
| return packlist(this.tree, { path: this.resolved, prefix, workspaces, globalIgnoreFile }) | ||
| }) | ||
| .then(async files => { | ||
| - const { options, cleanup } = await this.#tarOptions() | ||
| - const source = tar.c(options, files) | ||
| - // the strip temp file must outlive content consumption, so clean up once the stream is done | ||
| - source.once('end', cleanup) | ||
| - source.once('error', cleanup) | ||
| + const options = await this.#tarOptions() | ||
| + const source = Readable.fromWeb(tar.c(options, files)) | ||
| return source.on('error', er => stream.emit('error', er)).pipe(stream) | ||
| }) | ||
| .catch(er => stream.emit('error', er)) | ||
| @@ -94,7 +90,7 @@ | ||
| } | ||
|
|
||
| // Build the tar create options. | ||
| - // When the packed package.json declares patchedDependencies, redirect it to a stripped copy so project-local patches never ship. | ||
| + // When the packed package.json declares patchedDependencies, replace its archive body with a stripped copy so project-local patches never ship. | ||
| // Non-patched packs are unchanged. | ||
| async #tarOptions () { | ||
| const options = tarCreateOptions(this.package) | ||
| @@ -102,7 +98,7 @@ | ||
| // read package.json from disk after prepare so the strip reflects the actually-packed manifest. | ||
| const pkgJson = await PackageJson.load(this.resolved) | ||
| if (!('patchedDependencies' in pkgJson.content)) { | ||
| - return { options, cleanup: () => {} } | ||
| + return options | ||
| } | ||
|
|
||
| // serialize the package.json minus patchedDependencies, preserving its indent and newline. | ||
| @@ -113,30 +109,22 @@ | ||
| const newline = content[Symbol.for('newline')] | ||
| const stripped = `${JSON.stringify(content, null, indent)}\n`.replace(/\n/g, newline) | ||
|
|
||
| - // write the stripped copy to a temp dir, removing it if the write itself fails. | ||
| - const dir = await mkdtemp(join(tmpdir(), 'pacote-pack-')) | ||
| - const strippedPath = join(dir, 'package.json') | ||
| - try { | ||
| - await writeFile(strippedPath, stripped) | ||
| - } catch (er) { | ||
| - /* istanbul ignore next - writing to a freshly created temp dir is not deterministically failable */ | ||
| - await rm(dir, { recursive: true, force: true }) | ||
| - /* istanbul ignore next */ | ||
| - throw er | ||
| + const body = Buffer.from(stripped) | ||
| + const filter = options.filter | ||
| + options.filter = (path, stat) => { | ||
| + // Keep hard links to the original manifest independent of the stripped entry. | ||
| + if (path === 'package.json') { | ||
| + stat.nlink = 1 | ||
| + } | ||
| + return filter(path, stat) | ||
| } | ||
| - const size = Buffer.byteLength(stripped) | ||
| - | ||
| - // point only the top-level package.json entry at the stripped copy; every other file is untouched. | ||
| - // onWriteEntry runs before the tar header and the file's hardlink check, so size and nlink here are honored. | ||
| - options.onWriteEntry = (entry) => { | ||
| + options.onWriteEntry = entry => { | ||
| if (entry.path === 'package.json') { | ||
| - entry.absolute = strippedPath | ||
| - entry.stat.size = size | ||
| - entry.stat.nlink = 1 | ||
| + entry.body = body | ||
| + entry.size = body.length | ||
| } | ||
| } | ||
| - | ||
| - return { options, cleanup: () => rm(dir, { recursive: true, force: true }) } | ||
| + return options | ||
| } | ||
|
|
||
| manifest () { | ||
| diff --git a/lib/fetcher.js b/lib/fetcher.js | ||
| --- a/lib/fetcher.js | ||
| +++ b/lib/fetcher.js | ||
| @@ -385,28 +385,19 @@ | ||
| return this.#toFile(dest) | ||
| } | ||
|
|
||
| - #extract (dest, tarball) { | ||
| - const extractor = tar.x(this.#tarxOptions({ cwd: dest })) | ||
| - const p = new Promise((resolve, reject) => { | ||
| - extractor.on('end', () => { | ||
| - resolve({ | ||
| - resolved: this.resolved, | ||
| - integrity: this.integrity && String(this.integrity), | ||
| - from: this.from, | ||
| - }) | ||
| - }) | ||
| - | ||
| - extractor.on('error', er => { | ||
| - log.warn('tar', er.message) | ||
| - log.silly('tar', er) | ||
| - reject(er) | ||
| - }) | ||
| - | ||
| - tarball.on('error', er => reject(er)) | ||
| - }) | ||
| - | ||
| - tarball.pipe(extractor) | ||
| - return p | ||
| + async #extract (dest, tarball) { | ||
| + try { | ||
| + await tar.x({ ...this.#tarxOptions({ cwd: dest }), input: tarball }) | ||
| + } catch (er) { | ||
| + log.warn('tar', er.message) | ||
| + log.silly('tar', er) | ||
| + throw er | ||
| + } | ||
| + return { | ||
| + resolved: this.resolved, | ||
| + integrity: this.integrity && String(this.integrity), | ||
| + from: this.from, | ||
| + } | ||
| } | ||
|
|
||
| // always ensure that entries are at least as permissive as our configured |
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.
Uh oh!
There was an error while loading. Please reload this page.