Skip to content

Commit 7887fb3

Browse files
committed
fix: don't try to open file:/// urls
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
1 parent b8d6089 commit 7887fb3

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/utils/open-url.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const open = async (npm, url, errMsg) => {
2525
}
2626

2727
try {
28-
if (!/^(https?|file):$/.test(new URL(url).protocol)) {
28+
if (!/^https?:$/.test(new URL(url).protocol)) {
2929
throw new Error()
3030
}
3131
} catch (_) {

test/lib/utils/open-url.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ t.test('opens a url', async t => {
4141
t.same(OUTPUT, [], 'printed no output')
4242
})
4343

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

60+
t.test('returns error for file url', async t => {
61+
t.teardown(() => {
62+
openerUrl = null
63+
openerOpts = null
64+
OUTPUT.length = 0
65+
})
66+
await t.rejects(
67+
openUrl(npm, 'file:///usr/local/bin/ls', 'npm home'),
68+
/Invalid URL/,
69+
'got the correct error'
70+
)
71+
t.equal(openerUrl, null, 'did not open')
72+
t.same(openerOpts, null, 'did not open')
73+
t.same(OUTPUT, [], 'printed no output')
74+
})
75+
6076
t.test('returns error for non-parseable url', async t => {
6177
t.teardown(() => {
6278
openerUrl = null

0 commit comments

Comments
 (0)