forked from felicienfrancois/node-electron-proxy-agent
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathproxy.test.ts
More file actions
285 lines (269 loc) · 10.2 KB
/
proxy.test.ts
File metadata and controls
285 lines (269 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import * as https from 'https';
import * as tls from 'tls';
import * as assert from 'assert';
import * as fs from 'fs';
import * as path from 'path';
import * as vpa from '../../..';
import { createPacProxyAgent } from '../../../src/agent';
import { testRequest, ca, unusedCa, proxiedProxyAgentParamsV1, tlsProxiedProxyAgentParamsV1, log } from './utils';
describe('Proxied client', function () {
it('should use HTTP proxy for HTTPS connection', function () {
return testRequest(https, {
hostname: 'test-https-server',
path: '/test-path',
agent: createPacProxyAgent(async () => 'PROXY test-http-proxy:3128'),
ca,
});
});
it('should use HTTPS proxy for HTTPS connection', function () {
const { resolveProxyWithRequest: resolveProxy } = vpa.createProxyResolver(tlsProxiedProxyAgentParamsV1);
const patchedHttps: typeof https = {
...https,
...vpa.createHttpPatch(tlsProxiedProxyAgentParamsV1, https, resolveProxy),
} as any;
return testRequest(patchedHttps, {
hostname: 'test-https-server',
path: '/test-path',
_vscodeTestReplaceCaCerts: true,
});
});
it('should use HTTPS proxy for HTTPS connection (fetch)', async function () {
const { resolveProxyURL } = vpa.createProxyResolver(tlsProxiedProxyAgentParamsV1);
const patchedFetch = vpa.createFetchPatch(tlsProxiedProxyAgentParamsV1, globalThis.fetch, resolveProxyURL);
const res = await patchedFetch('https://test-https-server/test-path');
assert.strictEqual(res.status, 200);
assert.strictEqual((await res.json()).status, 'OK!');
});
it('should support basic auth', function () {
return testRequest(https, {
hostname: 'test-https-server',
path: '/test-path',
agent: createPacProxyAgent(async () => 'PROXY foo:bar@test-http-auth-proxy:3128'),
ca,
});
});
it('should fail with 407 when auth is missing', async function () {
try {
await testRequest(https, {
hostname: 'test-https-server',
path: '/test-path',
agent: createPacProxyAgent(async () => 'PROXY test-http-auth-proxy:3128'),
ca,
});
} catch (err) {
assert.strictEqual((err as any).statusCode, 407);
return;
}
assert.fail('Should have failed');
});
it('should call auth callback after 407', function () {
return testRequest(https, {
hostname: 'test-https-server',
path: '/test-path',
agent: createPacProxyAgent(async () => 'PROXY test-http-auth-proxy:3128', {
async lookupProxyAuthorization(proxyURL, proxyAuthenticate) {
assert.strictEqual(proxyURL, 'http://test-http-auth-proxy:3128/');
if (!proxyAuthenticate) {
return;
}
assert.strictEqual(proxyAuthenticate, 'Basic realm="Squid Basic Authentication"');
return `Basic ${Buffer.from('foo:bar').toString('base64')}`;
},
}),
ca,
});
});
it('should call auth callback before request', function () {
return testRequest(https, {
hostname: 'test-https-server',
path: '/test-path',
agent: createPacProxyAgent(async () => 'PROXY test-http-auth-proxy:3128', {
async lookupProxyAuthorization(proxyURL, proxyAuthenticate) {
assert.strictEqual(proxyURL, 'http://test-http-auth-proxy:3128/');
assert.strictEqual(proxyAuthenticate, undefined);
return `Basic ${Buffer.from('foo:bar').toString('base64')}`;
},
}),
ca,
});
});
it('should pass state around', async function () {
let count = 0;
await testRequest(https, {
hostname: 'test-https-server',
path: '/test-path',
agent: createPacProxyAgent(async () => 'PROXY test-http-auth-proxy:3128', {
async lookupProxyAuthorization(proxyURL, proxyAuthenticate, state: { count?: number }) {
assert.strictEqual(proxyURL, 'http://test-http-auth-proxy:3128/');
assert.strictEqual(proxyAuthenticate, state.count ? 'Basic realm="Squid Basic Authentication"' : undefined);
const credentials = state.count === 2 ? 'foo:bar' : 'foo:wrong';
count = state.count = (state.count || 0) + 1;
return `Basic ${Buffer.from(credentials).toString('base64')}`;
},
}),
ca,
});
assert.strictEqual(count, 3);
});
it('should work with kerberos', function () {
this.timeout(10000);
const proxyAuthenticateCache = {};
return testRequest(https, {
hostname: 'test-https-server',
path: '/test-path',
agent: createPacProxyAgent(async () => 'PROXY test-http-kerberos-proxy:80', {
async lookupProxyAuthorization(proxyURL, proxyAuthenticate, state) {
assert.strictEqual(proxyURL, 'http://test-http-kerberos-proxy/');
if (proxyAuthenticate) {
assert.strictEqual(proxyAuthenticate, 'Negotiate');
}
return lookupProxyAuthorization(log, log, proxyAuthenticateCache, true, proxyURL, proxyAuthenticate, state);
},
}),
ca,
});
});
it('should use system certificates', async function () {
const { resolveProxyWithRequest: resolveProxy } = vpa.createProxyResolver(proxiedProxyAgentParamsV1);
const patchedHttps: typeof https = {
...https,
...vpa.createHttpPatch(proxiedProxyAgentParamsV1, https, resolveProxy),
} as any;
await testRequest(patchedHttps, {
hostname: 'test-https-server',
path: '/test-path',
_vscodeTestReplaceCaCerts: true,
});
});
it('should use ca request option', async function () {
const { resolveProxyWithRequest: resolveProxy } = vpa.createProxyResolver(proxiedProxyAgentParamsV1);
const patchedHttps: typeof https = {
...https,
...vpa.createHttpPatch(proxiedProxyAgentParamsV1, https, resolveProxy),
} as any;
try {
await testRequest(patchedHttps, {
hostname: 'test-https-server',
path: '/test-path',
_vscodeTestReplaceCaCerts: true,
ca: unusedCa,
});
assert.fail('Expected to fail with self-signed certificate');
} catch (err: any) {
assert.strictEqual(err?.message, 'self-signed certificate');
}
});
it('should use ca agent option 1', async function () {
const { resolveProxyWithRequest: resolveProxy } = vpa.createProxyResolver(proxiedProxyAgentParamsV1);
const patchedHttps: typeof https = {
...https,
...vpa.createHttpPatch(proxiedProxyAgentParamsV1, https, resolveProxy),
} as any;
try {
await testRequest(patchedHttps, {
hostname: 'test-https-server',
path: '/test-path',
_vscodeTestReplaceCaCerts: true,
agent: new https.Agent({ ca: unusedCa }),
});
assert.fail('Expected to fail with self-signed certificate');
} catch (err: any) {
assert.strictEqual(err?.message, 'self-signed certificate');
}
});
it('should use ca agent option 2', async function () {
try {
vpa.resetCaches(); // Allows loadAdditionalCertificates to run again.
const params = {
...proxiedProxyAgentParamsV1,
loadAdditionalCertificates: async () => [
...await vpa.loadSystemCertificates({ log }),
],
};
const { resolveProxyWithRequest: resolveProxy } = vpa.createProxyResolver(params);
const patchedHttps: typeof https = {
...https,
...vpa.createHttpPatch(params, https, resolveProxy),
} as any;
await testRequest(patchedHttps, {
hostname: 'test-https-server',
path: '/test-path',
_vscodeTestReplaceCaCerts: true,
agent: new https.Agent({ ca }),
});
} finally {
vpa.resetCaches(); // Allows loadAdditionalCertificates to run again.
}
});
it('should prefer ca agent option', async function () {
const { resolveProxyWithRequest: resolveProxy } = vpa.createProxyResolver(proxiedProxyAgentParamsV1);
const patchedHttps: typeof https = {
...https,
...vpa.createHttpPatch(proxiedProxyAgentParamsV1, https, resolveProxy),
} as any;
await testRequest(patchedHttps, {
hostname: 'test-https-server',
path: '/test-path',
_vscodeTestReplaceCaCerts: true,
ca: unusedCa,
agent: new https.Agent({ ca: undefined }),
});
});
});
// From microsoft/vscode's proxyResolver.ts:
async function lookupProxyAuthorization(
extHostLogService: Console,
mainThreadTelemetry: Console,
// configProvider: ExtHostConfigProvider,
proxyAuthenticateCache: Record<string, string | string[] | undefined>,
isRemote: boolean,
proxyURL: string,
proxyAuthenticate: string | string[] | undefined,
state: { kerberosRequested?: boolean }
): Promise<string | undefined> {
const cached = proxyAuthenticateCache[proxyURL];
if (proxyAuthenticate) {
proxyAuthenticateCache[proxyURL] = proxyAuthenticate;
}
extHostLogService.trace('ProxyResolver#lookupProxyAuthorization callback', `proxyURL:${proxyURL}`, `proxyAuthenticate:${proxyAuthenticate}`, `proxyAuthenticateCache:${cached}`);
const header = proxyAuthenticate || cached;
const authenticate = Array.isArray(header) ? header : typeof header === 'string' ? [header] : [];
sendTelemetry(mainThreadTelemetry, authenticate, isRemote);
if (authenticate.some(a => /^(Negotiate|Kerberos)( |$)/i.test(a)) && !state.kerberosRequested) {
try {
state.kerberosRequested = true;
const kerberos = await import('kerberos');
const url = new URL(proxyURL);
const spn = /* configProvider.getConfiguration('http').get<string>('proxyKerberosServicePrincipal')
|| */ (process.platform === 'win32' ? `HTTP/${url.hostname}` : `HTTP@${url.hostname}`);
extHostLogService.debug('ProxyResolver#lookupProxyAuthorization Kerberos authentication lookup', `proxyURL:${proxyURL}`, `spn:${spn}`);
const client = await kerberos.initializeClient(spn);
const response = await client.step('');
return 'Negotiate ' + response;
} catch (err) {
extHostLogService.error('ProxyResolver#lookupProxyAuthorization Kerberos authentication failed', err);
}
}
return undefined;
}
type ProxyAuthenticationClassification = {
owner: 'chrmarti';
comment: 'Data about proxy authentication requests';
authenticationType: { classification: 'PublicNonPersonalData'; purpose: 'FeatureInsight'; comment: 'Type of the authentication requested' };
extensionHostType: { classification: 'SystemMetaData'; purpose: 'FeatureInsight'; comment: 'Type of the extension host' };
};
type ProxyAuthenticationEvent = {
authenticationType: string;
extensionHostType: string;
};
let telemetrySent = false;
function sendTelemetry(mainThreadTelemetry: Console, authenticate: string[], isRemote: boolean) {
if (telemetrySent || !authenticate.length) {
return;
}
telemetrySent = true;
mainThreadTelemetry.debug('proxyAuthenticationRequest', {
authenticationType: authenticate.map(a => a.split(' ')[0]).join(','),
extensionHostType: isRemote ? 'remote' : 'local',
});
}