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

fix(pack): refuse to pack invalid packument #3115

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 17 additions & 5 deletions lib/pack.js
Expand Up @@ -45,22 +45,34 @@ class Pack extends BaseCommand {
args = ['.']

const unicode = this.npm.config.get('unicode')
const dryRun = this.npm.config.get('dry-run')

// clone the opts because pacote mutates it with resolved/integrity
const tarballs = await Promise.all(args.map(async (arg) => {
// Get the manifests and filenames first so we can bail early on manifest
// errors before making any tarballs
const manifests = []
for (const arg of args) {
const spec = npa(arg)
const dryRun = this.npm.config.get('dry-run')
const manifest = await pacote.manifest(spec, this.npm.flatOptions)
if (!manifest._id)
throw new Error('Invalid package, must have name and version')
ruyadorno marked this conversation as resolved.
Show resolved Hide resolved

const filename = `${manifest.name}-${manifest.version}.tgz`
.replace(/^@/, '').replace(/\//, '-')
manifests.push({ arg, filename, manifest })
}

// Load tarball names up for printing afterward to isolate from the
// noise generated during packing
const tarballs = []
for (const { arg, filename, manifest } of manifests) {
const tarballData = await libpack(arg, this.npm.flatOptions)
const pkgContents = await getContents(manifest, tarballData)

if (!dryRun)
await writeFile(filename, tarballData)

return pkgContents
}))
tarballs.push(pkgContents)
}

for (const tar of tarballs) {
logTar(tar, { log, unicode })
Expand Down
76 changes: 51 additions & 25 deletions test/lib/pack.js
Expand Up @@ -15,10 +15,12 @@ const mockPacote = {
manifest: (spec) => {
if (spec.type === 'directory')
return pacote.manifest(spec)
return {
const m = {
name: spec.name || 'test-package',
version: spec.version || '1.0.0-test',
}
m._id = `${m.name}@${m.version}`
return m
},
}

Expand All @@ -43,9 +45,8 @@ t.test('should pack current directory with no arguments', (t) => {
})
const pack = new Pack(npm)

pack.exec([], er => {
if (er)
throw er
pack.exec([], err => {
t.error(err, { bail: true })

const filename = `npm-${require('../../package.json').version}.tgz`
t.strictSame(OUTPUT, [[filename]])
Expand Down Expand Up @@ -79,9 +80,8 @@ t.test('should pack given directory', (t) => {
})
const pack = new Pack(npm)

pack.exec([testDir], er => {
if (er)
throw er
pack.exec([testDir], err => {
t.error(err, { bail: true })

const filename = 'my-cool-pkg-1.0.0.tgz'
t.strictSame(OUTPUT, [[filename]])
Expand Down Expand Up @@ -115,9 +115,8 @@ t.test('should pack given directory for scoped package', (t) => {
})
const pack = new Pack(npm)

return pack.exec([testDir], er => {
if (er)
throw er
return pack.exec([testDir], err => {
t.error(err, { bail: true })

const filename = 'cool-my-pkg-1.0.0.tgz'
t.strictSame(OUTPUT, [[filename]])
Expand Down Expand Up @@ -150,16 +149,47 @@ t.test('should log pack contents', (t) => {
})
const pack = new Pack(npm)

pack.exec([], er => {
if (er)
throw er
pack.exec([], err => {
t.error(err, { bail: true })

const filename = `npm-${require('../../package.json').version}.tgz`
t.strictSame(OUTPUT, [[filename]])
t.end()
})
})

t.test('invalid packument', (t) => {
const mockPacote = {
manifest: () => {
return {}
},
}
const Pack = t.mock('../../lib/pack.js', {
libnpmpack,
pacote: mockPacote,
npmlog: {
notice: () => {},
showProgress: () => {},
clearProgress: () => {},
},
})
const npm = mockNpm({
config: {
unicode: true,
json: true,
'dry-run': true,
},
output,
})
const pack = new Pack(npm)
pack.exec([], err => {
t.match(err, { message: 'Invalid package, must have name and version' })

t.strictSame(OUTPUT, [])
t.end()
})
})

t.test('workspaces', (t) => {
const testDir = t.testdir({
'package.json': JSON.stringify({
Expand Down Expand Up @@ -201,9 +231,8 @@ t.test('workspaces', (t) => {
const pack = new Pack(npm)

t.test('all workspaces', (t) => {
pack.execWorkspaces([], [], er => {
if (er)
throw er
pack.execWorkspaces([], [], err => {
t.error(err, { bail: true })

t.strictSame(OUTPUT, [
['workspace-a-1.0.0.tgz'],
Expand All @@ -214,9 +243,8 @@ t.test('workspaces', (t) => {
})

t.test('all workspaces, `.` first arg', (t) => {
pack.execWorkspaces(['.'], [], er => {
if (er)
throw er
pack.execWorkspaces(['.'], [], err => {
t.error(err, { bail: true })

t.strictSame(OUTPUT, [
['workspace-a-1.0.0.tgz'],
Expand All @@ -227,9 +255,8 @@ t.test('workspaces', (t) => {
})

t.test('one workspace', (t) => {
pack.execWorkspaces([], ['workspace-a'], er => {
if (er)
throw er
pack.execWorkspaces([], ['workspace-a'], err => {
t.error(err, { bail: true })

t.strictSame(OUTPUT, [
['workspace-a-1.0.0.tgz'],
Expand All @@ -239,9 +266,8 @@ t.test('workspaces', (t) => {
})

t.test('specific package', (t) => {
pack.execWorkspaces(['abbrev'], [], er => {
if (er)
throw er
pack.execWorkspaces(['abbrev'], [], err => {
t.error(err, { bail: true })

t.strictSame(OUTPUT, [
['abbrev-1.0.0-test.tgz'],
Expand Down