Skip to content

Commit

Permalink
fix: don't try to open file:/// urls
Browse files Browse the repository at this point in the history
These are never valid in the contexts from which this lib is called.
Namely these are the bugs, docs, fund, help, and repo commands, and for
oauth logins.

PR-URL: #4025
Credit: @wraithgar
Close: #4025
Reviewed-by: @isaacs
  • Loading branch information
wraithgar committed Nov 9, 2021
1 parent b8d6089 commit 7887fb3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/utils/open-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const open = async (npm, url, errMsg) => {
}

try {
if (!/^(https?|file):$/.test(new URL(url).protocol)) {
if (!/^https?:$/.test(new URL(url).protocol)) {
throw new Error()
}
} catch (_) {
Expand Down
18 changes: 17 additions & 1 deletion test/lib/utils/open-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ t.test('opens a url', async t => {
t.same(OUTPUT, [], 'printed no output')
})

t.test('returns error for non-https and non-file url', async t => {
t.test('returns error for non-https url', async t => {
t.teardown(() => {
openerUrl = null
openerOpts = null
Expand All @@ -57,6 +57,22 @@ t.test('returns error for non-https and non-file url', async t => {
t.same(OUTPUT, [], 'printed no output')
})

t.test('returns error for file url', async t => {
t.teardown(() => {
openerUrl = null
openerOpts = null
OUTPUT.length = 0
})
await t.rejects(
openUrl(npm, 'file:///usr/local/bin/ls', 'npm home'),
/Invalid URL/,
'got the correct error'
)
t.equal(openerUrl, null, 'did not open')
t.same(openerOpts, null, 'did not open')
t.same(OUTPUT, [], 'printed no output')
})

t.test('returns error for non-parseable url', async t => {
t.teardown(() => {
openerUrl = null
Expand Down

0 comments on commit 7887fb3

Please sign in to comment.