From b0a3ba0b99ce5920722244f1ccb8b830826abef4 Mon Sep 17 00:00:00 2001
From: Gar <gar+gh@danger.computer>
Date: Thu, 1 Feb 2024 11:35:44 -0800
Subject: [PATCH] fix: prevent adding invalid dist-tag (#7195)

---
 lib/commands/dist-tag.js      | 3 +++
 test/lib/commands/dist-tag.js | 9 +++++++++
 2 files changed, 12 insertions(+)

diff --git a/lib/commands/dist-tag.js b/lib/commands/dist-tag.js
index 15f9622d14906..ff49bc8e307cb 100644
--- a/lib/commands/dist-tag.js
+++ b/lib/commands/dist-tag.js
@@ -89,6 +89,9 @@ class DistTag extends BaseCommand {
 
     log.verbose('dist-tag add', defaultTag, 'to', spec.name + '@' + version)
 
+    // make sure new spec with tag is valid, this will throw if invalid
+    npa(`${spec.name}@${defaultTag}`)
+
     if (!spec.name || !version || !defaultTag) {
       throw this.usageError('must provide a spec with a name and version, and a tag to add')
     }
diff --git a/test/lib/commands/dist-tag.js b/test/lib/commands/dist-tag.js
index 1c63ce497d3fb..918f658c6462a 100644
--- a/test/lib/commands/dist-tag.js
+++ b/test/lib/commands/dist-tag.js
@@ -318,6 +318,15 @@ t.test('add missing pkg name', async t => {
   )
 })
 
+t.test('add invalid tag', async t => {
+  const { distTag } = await mockDist(t)
+  await t.rejects(
+    distTag.exec(['add', '@tag']),
+    { code: 'EINVALIDTAGNAME' },
+    'should exit with invalid tag name error'
+  )
+})
+
 t.test('set existing version', async t => {
   const { distTag, logs } = await mockDist(t)
   await distTag.exec(['set', '@scoped/another@0.6.0', 'b'])