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(publish): validate dist-tag #7459

Merged
merged 2 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`)
wraithgar marked this conversation as resolved.
Show resolved Hide resolved

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
Loading