Skip to content
This repository was archived by the owner on Jul 14, 2023. It is now read-only.

Commit 09896a2

Browse files
Fix: Invalid proxy settings should be handled and not crash (#11)
1 parent b86d038 commit 09896a2

4 files changed

Lines changed: 85 additions & 10 deletions

File tree

.travis.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ node_js:
33
- 9
44
- 8
55

6-
branches:
7-
only:
8-
- master
9-
106
install:
117
- npm install
128
script:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "get-proxy-settings",
3-
"version": "0.1.8",
3+
"version": "0.1.9",
44
"description": "Retrieve proxy settings specified by the system",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/proxy.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { expect } from "chai";
2+
import { parseWindowsProxySetting } from "./proxy";
3+
describe("proxy", () => {
4+
describe("parseWindowsProxySetting", () => {
5+
it("should return null if null or undefined", () => {
6+
expect(parseWindowsProxySetting(null)).to.be.null;
7+
expect(parseWindowsProxySetting(undefined)).to.be.null;
8+
});
9+
10+
it("should return same url for http and https if just a url is provided", () => {
11+
const value = parseWindowsProxySetting("http://localhost:8888");
12+
expect(value).not.to.be.null;
13+
expect(value.http.host).to.eql("localhost");
14+
expect(value.http.port).to.eql("8888");
15+
expect(value.https.host).to.eql("localhost");
16+
expect(value.https.port).to.eql("8888");
17+
});
18+
19+
it("should coresponding http and https urls", () => {
20+
const value = parseWindowsProxySetting("http=http://localhost:8888;https=http://localhost:9999");
21+
expect(value).not.to.be.null;
22+
expect(value.http.host).to.eql("localhost");
23+
expect(value.http.port).to.eql("8888");
24+
expect(value.https.host).to.eql("localhost");
25+
expect(value.https.port).to.eql("9999");
26+
});
27+
28+
it("should assign https automatically if missing but http provided", () => {
29+
const value = parseWindowsProxySetting("http=http://localhost:8888");
30+
expect(value).not.to.be.null;
31+
expect(value.http.host).to.eql("localhost");
32+
expect(value.http.port).to.eql("8888");
33+
expect(value.https.host).to.eql("localhost");
34+
expect(value.https.port).to.eql("8888");
35+
});
36+
37+
it("should assign http automatically if missing but https provided", () => {
38+
const value = parseWindowsProxySetting("https=http://localhost:8888");
39+
expect(value).not.to.be.null;
40+
expect(value.http.host).to.eql("localhost");
41+
expect(value.http.port).to.eql("8888");
42+
expect(value.https.host).to.eql("localhost");
43+
expect(value.https.port).to.eql("8888");
44+
});
45+
46+
it("should return null if invalid url is provided", () => {
47+
expect(parseWindowsProxySetting("invalid-url")).to.be.null;
48+
});
49+
});
50+
});

src/proxy.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as npmConfLoader from "npm-conf";
2+
import * as url from "url";
23
import { ProxyAuthenticationRequiredError } from "./proxy-errors";
34
import { ProxyCredentials, ProxySetting, ProxySettings } from "./proxy-settings";
45
import { validateProxySetting } from "./validate";
56
import { Hive, openKey } from "./winreg";
6-
77
const npmConf = npmConfLoader();
88

99
export async function getProxySettings(): Promise<ProxySettings> {
@@ -71,11 +71,40 @@ export function getEnvProxy(): ProxySettings {
7171
return { http: new ProxySetting(httpProxy), https: new ProxySetting(httpsProxy) };
7272
}
7373

74-
function parseWindowsProxySetting(proxySetting: string): ProxySettings {
74+
export function parseWindowsProxySetting(proxySetting: string): ProxySettings {
75+
if (!proxySetting) { return null; }
76+
if (isValidUrl(proxySetting)) {
77+
const setting = new ProxySetting(proxySetting);
78+
return {
79+
http: setting,
80+
https: setting,
81+
};
82+
}
7583
const settings = proxySetting.split(";").map(x => x.split("=", 2));
76-
const result: ProxySettings = {};
84+
const result = {};
7785
for (const [key, value] of settings) {
78-
result[key] = new ProxySetting(value);
86+
if (value) {
87+
result[key] = new ProxySetting(value);
88+
}
89+
}
90+
91+
return processResults(result);
92+
}
93+
94+
function isValidUrl(value: string) {
95+
const obj = url.parse(value);
96+
return Boolean(obj.hostname);
97+
}
98+
99+
function processResults(results: { [key: string]: ProxySetting }) {
100+
const { http, https } = results;
101+
if (http && https) {
102+
return { http, https };
103+
} else if (http) {
104+
return { http, https: http };
105+
} else if (https) {
106+
return { http: https, https };
107+
} else {
108+
return null;
79109
}
80-
return result;
81110
}

0 commit comments

Comments
 (0)