From 1569f4ca28124a655b5244f07e6edba98e22bcc7 Mon Sep 17 00:00:00 2001 From: Balthazar Gronon Date: Sun, 5 Feb 2017 02:22:08 +0100 Subject: [PATCH] Fix private urls using colon separator (#2519) * Fix private urls using colon separator Closes #573, closes #2416. Related to #2384, #573. * Remove unused suppression * Move to dedicated method & add tests --- __tests__/resolvers/exotics/git-resolver.js | 45 +++++++++++++++++++++ src/resolvers/exotics/git-resolver.js | 22 +++++++--- 2 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 __tests__/resolvers/exotics/git-resolver.js diff --git a/__tests__/resolvers/exotics/git-resolver.js b/__tests__/resolvers/exotics/git-resolver.js new file mode 100644 index 0000000000..e0ff5c1b8a --- /dev/null +++ b/__tests__/resolvers/exotics/git-resolver.js @@ -0,0 +1,45 @@ +/* @flow */ + +import {parse} from 'url'; + +import GitResolver from '../../../src/resolvers/exotics/git-resolver.js'; + +test('GitResolver transformUrl method is defined', () => { + expect(GitResolver.transformUrl).toBeDefined(); +}); + +test('GitResolver transformUrl does not affect normal urls', () => { + + const urls = [ + 'git+https://github.com/npm-ml/ocaml.git#npm-4.02.3', + 'git+ssh://git@gitlab.com/user/repo.git', + 'git+ssh://git@github.com/user/repo.git', + 'git+ssh://username@private.url/sub/right-pad', + 'https://github.com/npm-ml/re', + 'https://github.com/npm-ml/ocaml.git#npm-4.02.3', + 'https://git@github.com/stevemao/left-pad.git', + 'https://bitbucket.org/hgarcia/node-bitbucket-api.git', + 'https://github.com/yarnpkg/yarn/releases/download/v0.18.1/yarn-v0.18.1.tar.gz', + 'https://github.com/babel/babel-loader.git#greenkeeper/cross-env-3.1.4', + 'package@git@bitbucket.org:team/repo.git', + ]; + + urls.forEach((url) => { + expect(GitResolver.transformUrl(parse(url))).toBe(url); + }); + +}); + +test('GitResolver transformUrl affect host colon separated urls', () => { + + const urls = [ + 'git+ssh://username@private.url:sub/right-pad', + 'git+ssh://private.url:sub/right-pad', + 'https://private.url:sub/right-pad', + ]; + + urls.forEach((url) => { + expect(GitResolver.transformUrl(parse(url))).toBe(url.replace(':sub', '/sub')); + }); + +}); diff --git a/src/resolvers/exotics/git-resolver.js b/src/resolvers/exotics/git-resolver.js index bc2e32877a..ba91a4b8ff 100644 --- a/src/resolvers/exotics/git-resolver.js +++ b/src/resolvers/exotics/git-resolver.js @@ -11,6 +11,7 @@ import ExoticResolver from './exotic-resolver.js'; import Git from '../../util/git.js'; const urlParse = require('url').parse; +const urlFormat = require('url').format; // we purposefully omit https and http as those are only valid if they end in the .git extension const GIT_PROTOCOLS = ['git:', 'git+ssh:', 'git+https:', 'ssh:']; @@ -60,6 +61,14 @@ export default class GitResolver extends ExoticResolver { return false; } + // This transformUrl util is here to replace colon separators in the pathname + // from private urls. It takes the url parts retrieved using urlFormat and + // returns the associated url. Related to #573, introduced in #2519. + static transformUrl(parts) : string { + const pathname = parts.pathname ? parts.pathname.replace(/^\/:/, '/') : ''; + return urlFormat({...parts, pathname}); + } + async resolve(forked?: true): Promise { const {url} = this; @@ -92,7 +101,10 @@ export default class GitResolver extends ExoticResolver { } const {config} = this; - const client = new Git(config, url, this.hash); + + const transformedUrl = GitResolver.transformUrl(parts); + + const client = new Git(config, transformedUrl, this.hash); const commit = await client.init(); async function tryRegistry(registry): Promise { @@ -103,12 +115,12 @@ export default class GitResolver extends ExoticResolver { return null; } - const json = await config.readJson(`${url}/${filename}`, () => JSON.parse(file)); + const json = await config.readJson(`${transformedUrl}/${filename}`, () => JSON.parse(file)); json._uid = commit; json._remote = { - resolved: `${url}#${commit}`, + resolved: `${transformedUrl}#${commit}`, type: 'git', - reference: url, + reference: transformedUrl, hash: commit, registry, }; @@ -131,6 +143,6 @@ export default class GitResolver extends ExoticResolver { } } - throw new MessageError(this.reporter.lang('couldntFindManifestIn', url)); + throw new MessageError(this.reporter.lang('couldntFindManifestIn', transformedUrl)); } }