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: don't try to open file:/// urls #4025

Merged
merged 1 commit into from
Nov 9, 2021
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
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