Skip to content

Commit

Permalink
fix(publish): validate dist-tag (#7459)
Browse files Browse the repository at this point in the history
  • Loading branch information
reggi committed May 1, 2024
1 parent f6fff32 commit 4ab6cf4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ class Publish extends BaseCommand {
}

const resolved = npa.resolve(manifest.name, manifest.version)

// make sure tag is valid, this will throw if invalid
npa(`${manifest.name}@${defaultTag}`)

const registry = npmFetch.pickRegistry(resolved, opts)
const creds = this.npm.config.getCredentialsByURI(registry)
const noCreds = !(creds.token || creds.username || creds.certfile && creds.keyfile)
Expand Down
20 changes: 19 additions & 1 deletion test/lib/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ t.test('shows usage with wrong set of arguments', async t => {
await t.rejects(publish.exec(['a', 'b', 'c']), publish.usage)
})

t.test('throws when invalid tag', async t => {
t.test('throws when invalid tag is semver', async t => {
const { npm } = await loadMockNpm(t, {
config: {
tag: '0.0.13',
Expand All @@ -306,6 +306,24 @@ t.test('throws when invalid tag', async t => {
)
})

t.test('throws when invalid tag when not url encodable', async t => {
const { npm } = await loadMockNpm(t, {
config: {
tag: '@test',
},
prefixDir: {
'package.json': JSON.stringify(pkgJson, null, 2),
},
})
await t.rejects(
npm.exec('publish', []),
{
/* eslint-disable-next-line max-len */
message: 'Invalid tag name "@test" of package "test-package@@test": Tags may not have any characters that encodeURIComponent encodes.',
}
)
})

t.test('tarball', async t => {
const { npm, joinedOutput, logs, home } = await loadMockNpm(t, {
config: {
Expand Down

0 comments on commit 4ab6cf4

Please sign in to comment.