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

bugfix: Prevent arborist from changing protocols as part of resolve step #5256

Closed
Closed
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 tap-snapshots/test/lib/commands/ls.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ print-deduped-symlinks@1.0.0 {CWD}/tap-testdir-ls-ls-print-deduped-symlinks

exports[`test/lib/commands/ls.js TAP ls resolved points to git ref > should output tree containing git refs 1`] = `
test-npm-ls@1.0.0 {CWD}/tap-testdir-ls-ls-resolved-points-to-git-ref
\`-- abbrev@1.1.1 (git+ssh://git@github.com/isaacs/abbrev-js.git#b8f3a2fc0c3bb8ffd8b0d0072cc6b5a3667e963c)
\`-- abbrev@1.1.1 (git+https://github.com/isaacs/abbrev-js.git#b8f3a2fc0c3bb8ffd8b0d0072cc6b5a3667e963c)

`

Expand Down
6 changes: 3 additions & 3 deletions test/lib/commands/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -3684,7 +3684,7 @@ t.test('ls --json', t => {
abbrev: {
version: '1.1.1',
/* eslint-disable-next-line max-len */
resolved: 'git+ssh://git@github.com/isaacs/abbrev-js.git#b8f3a2fc0c3bb8ffd8b0d0072cc6b5a3667e963c',
resolved: 'git+https://github.com/isaacs/abbrev-js.git#b8f3a2fc0c3bb8ffd8b0d0072cc6b5a3667e963c',
},
},
},
Expand Down Expand Up @@ -4618,7 +4618,7 @@ t.test('ls --package-lock-only', t => {
dependencies: {
abbrev: {
/* eslint-disable-next-line max-len */
version: 'git+ssh://git@github.com/isaacs/abbrev-js.git#b8f3a2fc0c3bb8ffd8b0d0072cc6b5a3667e963c',
version: 'git+https://github.com/isaacs/abbrev-js.git#b8f3a2fc0c3bb8ffd8b0d0072cc6b5a3667e963c',
from: 'abbrev@git+https://github.com/isaacs/abbrev-js.git',
},
},
Expand All @@ -4633,7 +4633,7 @@ t.test('ls --package-lock-only', t => {
dependencies: {
abbrev: {
/* eslint-disable-next-line max-len */
resolved: 'git+ssh://git@github.com/isaacs/abbrev-js.git#b8f3a2fc0c3bb8ffd8b0d0072cc6b5a3667e963c',
resolved: 'git+https://github.com/isaacs/abbrev-js.git#b8f3a2fc0c3bb8ffd8b0d0072cc6b5a3667e963c',
},
},
},
Expand Down
41 changes: 32 additions & 9 deletions workspaces/arborist/lib/consistent-resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// lockfiles, and for converting hosted git repos to a consistent url type.
const npa = require('npm-package-arg')
const relpath = require('./relpath.js')

const consistentResolve = (resolved, fromPath, toPath, relPaths = false) => {
if (!resolved) {
return null
Expand All @@ -20,16 +21,38 @@ const consistentResolve = (resolved, fromPath, toPath, relPaths = false) => {
raw,
} = npa(resolved, fromPath)
const isPath = type === 'file' || type === 'directory'
return isPath && !relPaths ? `file:${fetchSpec.replace(/#/g, '%23')}`
: isPath ? 'file:' + (toPath ? relpath(toPath, fetchSpec.replace(/#/g, '%23')) : fetchSpec.replace(/#/g, '%23'))
: hosted ? `git+${
hosted.auth ? hosted.https(hostedOpt) : hosted.sshurl(hostedOpt)
}`
: type === 'git' ? saveSpec

if (isPath && !relPaths) {
return `file:${fetchSpec.replace(/#/g, '%23')}`
}

if (isPath) {
if (toPath) {
return 'file:' + relpath(toPath, fetchSpec.replace(/#/g, '%23'))
} else {
return 'file:' + fetchSpec.replace(/#/g, '%23')
}
}

if (hosted) {
if (hosted.default === 'https') {
return `git+${hosted.https(hostedOpt)}`
} else {
return `git+${hosted.sshurl(hostedOpt)}`
}
}

if (type === 'git') {
return saveSpec
}

if (rawSpec === '' && raw.slice(-1) !== '@') {
// always return something. 'foo' is interpreted as 'foo@' otherwise.
: rawSpec === '' && raw.slice(-1) !== '@' ? raw
// just strip off the name, but otherwise return as-is
: rawSpec
return raw
}

// just strip off the name, but otherwise return as-is
return rawSpec
} catch (_) {
// whatever we passed in was not acceptable to npa.
// leave it 100% untouched.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1594,7 +1594,7 @@ ArboristNode {
"name": "full-git-url",
"packageName": "abbrev",
"path": "install-types/node_modules/full-git-url",
"resolved": "git+ssh://git@github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb",
"resolved": "git+https://github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb",
"version": "1.1.1",
},
"ghshort" => ArboristNode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13530,7 +13530,7 @@ ArboristNode {
"location": "node_modules/full-git-url",
"name": "full-git-url",
"path": "{CWD}/test/fixtures/install-types/node_modules/full-git-url",
"resolved": "git+ssh://git@github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb",
"resolved": "git+https://github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb",
},
"ghshort" => ArboristNode {
"edgesIn": Set {
Expand Down Expand Up @@ -14219,7 +14219,7 @@ ArboristNode {
"location": "node_modules/full-git-url",
"name": "full-git-url",
"path": "{CWD}/test/fixtures/install-types/node_modules/full-git-url",
"resolved": "git+ssh://git@github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb",
"resolved": "git+https://github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb",
},
"ghshort" => ArboristNode {
"edgesIn": Set {
Expand Down Expand Up @@ -14908,7 +14908,7 @@ ArboristNode {
"location": "node_modules/full-git-url",
"name": "full-git-url",
"path": "{CWD}/test/arborist/tap-testdir-load-virtual-load-from-npm-shrinkwrap.json/node_modules/full-git-url",
"resolved": "git+ssh://git@github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb",
"resolved": "git+https://github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb",
},
"ghshort" => ArboristNode {
"edgesIn": Set {
Expand Down Expand Up @@ -15561,7 +15561,7 @@ ArboristNode {
"location": "node_modules/full-git-url",
"name": "full-git-url",
"path": "{CWD}/test/fixtures/install-types-sw-only/node_modules/full-git-url",
"resolved": "git+ssh://git@github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb",
"resolved": "git+https://github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb",
},
"ghshort" => ArboristNode {
"location": "node_modules/ghshort",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ Object {
"version": "1.0.0",
},
"node_modules/full-git-url": Object {
"resolved": "git+ssh://git@github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb",
"resolved": "git+https://github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb",
},
"node_modules/ghshort": Object {
"resolved": "git+ssh://git@github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb",
Expand Down Expand Up @@ -1883,7 +1883,7 @@ Object {
},
"full-git-url": Object {
"from": "full-git-url@git+https://github.com/isaacs/abbrev-js.git",
"version": "git+ssh://git@github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb",
"version": "git+https://github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb",
},
"ghshort": Object {
"from": "ghshort@github:isaacs/abbrev-js",
Expand Down Expand Up @@ -2094,7 +2094,7 @@ Object {
"node_modules/full-git-url": Object {
"license": "ISC",
"name": "abbrev",
"resolved": "git+ssh://git@github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb",
"resolved": "git+https://github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb",
"version": "1.1.1",
},
"node_modules/ghshort": Object {
Expand Down Expand Up @@ -14192,7 +14192,7 @@ Object {

exports[`test/shrinkwrap.js TAP look up from locks and such lockfile > full git 1`] = `
Object {
"resolved": "git+ssh://git@github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb",
"resolved": "git+https://github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb",
}
`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ exports[`test/yarn-lock.js TAP load a yarn lock from an actual tree install-type
"version" "1.0.0"

"full-git-url@git+https://github.com/isaacs/abbrev-js.git":
"resolved" "git+ssh://git@github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb"
"resolved" "git+https://github.com/isaacs/abbrev-js.git#a9ee72ebc8fe3975f1b0c7aeb3a8f2a806a432eb"
"version" "1.1.1"

"ghshort@github:isaacs/abbrev-js":
Expand Down
25 changes: 17 additions & 8 deletions workspaces/arborist/test/consistent-resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,37 +56,46 @@ t.test('file and directories made consistent if toPath not set', t => {

t.test('consistent hosted git info urls', t => {
const expect = 'git+ssh://git@github.com/a/b.git'
const expectAuth = 'git+https://user:pass@github.com/a/b.git'
t.equal(cr('a/b'), expect)
t.equal(cr('github:a/b'), expect)
t.equal(cr('git+https://github.com/a/b'), expect)
t.equal(cr('git://github.com/a/b'), expect)
t.equal(cr('git+ssh://git@github.com/a/b'), expect)
t.equal(cr('git+https://github.com/a/b.git'), expect)
t.equal(cr('git://github.com/a/b.git'), expect)
t.equal(cr('git+ssh://git@github.com/a/b.git'), expect)
t.equal(cr('git+https://user:pass@github.com/a/b.git'), expectAuth)

const hash = '#0000000000000000000000000000000000000000'
t.equal(cr('a/b' + hash), expect + hash)
t.equal(cr('github:a/b' + hash), expect + hash)
t.equal(cr('git+https://github.com/a/b' + hash), expect + hash)
t.equal(cr('git://github.com/a/b' + hash), expect + hash)
t.equal(cr('git+ssh://git@github.com/a/b' + hash), expect + hash)
t.equal(cr('git+https://github.com/a/b.git' + hash), expect + hash)
t.equal(cr('git://github.com/a/b.git' + hash), expect + hash)
t.equal(cr('git+ssh://git@github.com/a/b.git' + hash), expect + hash)
t.equal(cr('xyz@a/b' + hash), expect + hash)
t.equal(cr('xyz@github:a/b' + hash), expect + hash)
t.equal(cr('xyz@git+https://github.com/a/b' + hash), expect + hash)
t.equal(cr('xyz@git://github.com/a/b' + hash), expect + hash)
t.equal(cr('xyz@git+ssh://git@github.com/a/b' + hash), expect + hash)
t.equal(cr('xyz@git+https://github.com/a/b.git' + hash), expect + hash)
t.equal(cr('xyz@git://github.com/a/b.git' + hash), expect + hash)
t.equal(cr('xyz@git+ssh://git@github.com/a/b.git' + hash), expect + hash)
t.end()
})

t.test('consistent resolve hosted git to https urls', t => {
const expect = 'git+https://github.com/a/b.git'
const expectAuth = 'git+https://user:pass@github.com/a/b.git'
const expectUserOnlyAuth = 'git+https://user@github.com/a/b.git'
t.equal(cr('git+https://github.com/a/b'), expect)
t.equal(cr('git+https://github.com/a/b.git'), expect)
t.equal(cr('git+https://user:pass@github.com/a/b.git'), expectAuth)
t.equal(cr('git+https://user@github.com/a/b.git'), expectUserOnlyAuth)

const hash = '#0000000000000000000000000000000000000000'
t.equal(cr('git+https://github.com/a/b' + hash), expect + hash)
t.equal(cr('git+https://github.com/a/b.git' + hash), expect + hash)
t.equal(cr('xyz@git+https://github.com/a/b' + hash), expect + hash)
t.equal(cr('xyz@git+https://github.com/a/b.git' + hash), expect + hash)
t.end()
})

t.test('unhosted git returns saveSpec', t => {
const r = 'git+https://x.com/y.git#0000000000000000000000000000000000000000'
t.equal(cr(r), r)
Expand Down