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: remove strict 8909 mode #135

Merged
merged 1 commit into from
Aug 15, 2023
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
47 changes: 15 additions & 32 deletions lib/npa.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,40 +257,23 @@ function fromFile (res, where) {
})
}

// environment switch for testing
if (process.env.NPM_PACKAGE_ARG_8909_STRICT !== '1') {
// XXX backwards compatibility lack of compliance with 8909
// Remove when we want a breaking change to come into RFC compliance.
if (resolvedUrl.host && resolvedUrl.host !== 'localhost') {
const rawSpec = res.rawSpec.replace(/^file:\/\//, 'file:///')
resolvedUrl = new url.URL(rawSpec, `file://${path.resolve(where)}/`)
specUrl = new url.URL(rawSpec)
rawNoPrefix = rawSpec.replace(/^file:/, '')
}
// turn file:/../foo into file:../foo
// for 1, 2 or 3 leading slashes since we attempted
// in the previous step to make it a file protocol url with a leading slash
if (/^\/{1,3}\.\.?(\/|$)/.test(rawNoPrefix)) {
const rawSpec = res.rawSpec.replace(/^file:\/{1,3}/, 'file:')
resolvedUrl = new url.URL(rawSpec, `file://${path.resolve(where)}/`)
specUrl = new url.URL(rawSpec)
rawNoPrefix = rawSpec.replace(/^file:/, '')
}
// XXX end 8909 violation backwards compatibility section
}

// file:foo - relative url to ./foo
// file:/foo - absolute path /foo
// file:///foo - absolute path to /foo, no authority host
// file://localhost/foo - absolute path to /foo, on localhost
// file://foo - absolute path to / on foo host (error!)
// XXX backwards compatibility lack of compliance with RFC 8909
if (resolvedUrl.host && resolvedUrl.host !== 'localhost') {
const msg = `Invalid file: URL, must be absolute if // present`
throw Object.assign(new Error(msg), {
raw: res.rawSpec,
parsed: resolvedUrl,
})
const rawSpec = res.rawSpec.replace(/^file:\/\//, 'file:///')
resolvedUrl = new url.URL(rawSpec, `file://${path.resolve(where)}/`)
specUrl = new url.URL(rawSpec)
rawNoPrefix = rawSpec.replace(/^file:/, '')
}
// turn file:/../foo into file:../foo
// for 1, 2 or 3 leading slashes since we attempted
// in the previous step to make it a file protocol url with a leading slash
if (/^\/{1,3}\.\.?(\/|$)/.test(rawNoPrefix)) {
const rawSpec = res.rawSpec.replace(/^file:\/{1,3}/, 'file:')
resolvedUrl = new url.URL(rawSpec, `file://${path.resolve(where)}/`)
specUrl = new url.URL(rawSpec)
rawNoPrefix = rawSpec.replace(/^file:/, '')
}
// XXX end RFC 8909 violation backwards compatibility section

// turn /C:/blah into just C:/blah on windows
let specPath = decodeURIComponent(specUrl.pathname)
Expand Down
41 changes: 11 additions & 30 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -731,38 +731,19 @@ t.test('basic', function (t) {
t.end()
})

t.test('strict 8909 compliance mode', t => {
t.teardown(() => process.env.NPM_PACKAGE_ARG_8909_STRICT = '0')
process.env.NPM_PACKAGE_ARG_8909_STRICT = '1'

t.throws(() => npa('file://.'), {
message: 'Invalid file: URL, must be absolute if // present',
raw: 'file://.',
})

t.throws(() => npa('file://some/relative/path'), {
message: 'Invalid file: URL, must be absolute if // present',
raw: 'file://some/relative/path',
})

// I cannot for the life of me figure out how to make new URL('file:...')
// actually fail to parse. it seems like it accepts any garbage you can
// throw at it. However, because it theoretically CAN throw, here's a test.
t.throws(() => {
const broken = t.mock('..', {
url: {
URL: class {
constructor () {
throw new Error('thansk i haet it')
}
},
t.test('invalid url', t => {
const broken = t.mock('..', {
url: {
URL: class {
constructor () {
throw new Error('something went wrong')
}
},
})
broken('file:thansk i haet it')
}, {
message: 'Invalid file: URL, must comply with RFC 8909',
},
})

t.throws(() => broken('file:./test'),
{ message: 'Invalid file: URL' }
)
t.end()
})

Expand Down