Skip to content

Commit c10ef6a

Browse files
committed
Forces using https for the regular registries (#7393)
* Forces using https for the regular registries * Fixes linting * Updates the changelog * Adds npmjs.com to the list
1 parent 77c2630 commit c10ef6a

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Please add one entry in this file for each change in Yarn's behavior. Use the sa
44

55
## Master
66

7+
- Enforces https for the Yarn and npm registries.
8+
9+
[#7393](https://github.com/yarnpkg/yarn/pull/7393) - [**Maël Nison**](https://twitter.com/arcanis)
10+
711
- Adds support for reading `yarnPath` from v2-produced `.yarnrc.yml` files.
812

913
[#7350](https://github.com/yarnpkg/yarn/pull/7350) - [**Maël Nison**](https://twitter.com/arcanis)

__tests__/registries/npm-registry.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,30 @@ describe('getRequestUrl functional test', () => {
781781

782782
expect(npmRegistry.getRequestUrl(registry, pathname)).toEqual('https://my.registry.co/registry/foo/bar/baz');
783783
});
784+
785+
for (const host of [`registry.yarnpkg.com`, `registry.npmjs.org`, `registry.npmjs.com`]) {
786+
test(`enforces loading packages through https when they come from ${host}`, () => {
787+
const testCwd = '.';
788+
const {mockRequestManager, mockRegistries, mockReporter} = createMocks();
789+
const npmRegistry = new NpmRegistry(testCwd, mockRegistries, mockRequestManager, mockReporter, true, []);
790+
const registry = `http://${host}/registry`;
791+
const pathname = 'foo/bar/baz';
792+
793+
expect(npmRegistry.getRequestUrl(registry, pathname)).toEqual(`https://${host}/registry/foo/bar/baz`);
794+
});
795+
}
796+
797+
test("doesn't change the protocol for packages from other registries", () => {
798+
const testCwd = '.';
799+
const {mockRequestManager, mockRegistries, mockReporter} = createMocks();
800+
const npmRegistry = new NpmRegistry(testCwd, mockRegistries, mockRequestManager, mockReporter, true, []);
801+
const registry = 'http://registry.mylittlepony.org/registry';
802+
const pathname = 'foo/bar/baz';
803+
804+
expect(npmRegistry.getRequestUrl(registry, pathname)).toEqual(
805+
'http://registry.mylittlepony.org/registry/foo/bar/baz',
806+
);
807+
});
784808
});
785809

786810
describe('getScope functional test', () => {

src/registries/npm-registry.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import url from 'url';
2222
import ini from 'ini';
2323

2424
const DEFAULT_REGISTRY = 'https://registry.npmjs.org/';
25+
const REGEX_REGISTRY_ENFORCED_HTTPS = /^https?:\/\/([^\/]+\.)?(yarnpkg\.com|npmjs\.(org|com))(\/|$)/;
2526
const REGEX_REGISTRY_HTTP_PROTOCOL = /^https?:/i;
2627
const REGEX_REGISTRY_PREFIX = /^(https?:)?\/\//i;
2728
const REGEX_REGISTRY_SUFFIX = /registry\/?$/;
@@ -112,13 +113,17 @@ export default class NpmRegistry extends Registry {
112113
}
113114

114115
getRequestUrl(registry: string, pathname: string): string {
115-
const isUrl = REGEX_REGISTRY_PREFIX.test(pathname);
116+
let resolved = pathname;
116117

117-
if (isUrl) {
118-
return pathname;
119-
} else {
120-
return url.resolve(addSuffix(registry, '/'), pathname);
118+
if (!REGEX_REGISTRY_PREFIX.test(pathname)) {
119+
resolved = url.resolve(addSuffix(registry, '/'), pathname);
121120
}
121+
122+
if (REGEX_REGISTRY_ENFORCED_HTTPS.test(resolved)) {
123+
resolved = resolved.replace(/^http:\/\//, 'https://');
124+
}
125+
126+
return resolved;
122127
}
123128

124129
isRequestToRegistry(requestUrl: string, registryUrl: string): boolean {

0 commit comments

Comments
 (0)