|
| 1 | +import { execFileSync } from 'child_process'; |
| 2 | +import fs from 'fs'; |
1 | 3 | import http from 'http'; |
| 4 | +import https from 'https'; |
| 5 | +import net from 'net'; |
| 6 | +import os from 'os'; |
| 7 | +import path from 'path'; |
| 8 | +import tls from 'tls'; |
2 | 9 | import { expect } from 'chai'; |
| 10 | +import { ProxyAgent } from 'proxy-agent'; |
3 | 11 | import HttpConnection from '../../../../lib/connection/connections/HttpConnection'; |
4 | 12 | import ThriftHttpConnection from '../../../../lib/connection/connections/ThriftHttpConnection'; |
5 | 13 | import IConnectionOptions from '../../../../lib/connection/contracts/IConnectionOptions'; |
@@ -211,4 +219,225 @@ describe('HttpConnection.connect', () => { |
211 | 219 | expect(thriftConnection.url).to.be.equal(testCase.expected); |
212 | 220 | } |
213 | 221 | }); |
| 222 | + |
| 223 | + it('should pass TLS options to the proxy agent', async () => { |
| 224 | + const connection = new HttpConnection( |
| 225 | + { |
| 226 | + host: 'localhost', |
| 227 | + port: 443, |
| 228 | + https: true, |
| 229 | + proxy: { protocol: 'http', host: 'localhost', port: 8080 }, |
| 230 | + ca: 'ca', |
| 231 | + cert: 'cert', |
| 232 | + key: 'key', |
| 233 | + }, |
| 234 | + new ClientContextStub(), |
| 235 | + ); |
| 236 | + |
| 237 | + const agent = (await connection.getAgent()) as ProxyAgent; |
| 238 | + |
| 239 | + expect(agent).to.be.instanceOf(ProxyAgent); |
| 240 | + expect(agent.connectOpts).to.include({ |
| 241 | + minVersion: 'TLSv1.2', |
| 242 | + rejectUnauthorized: true, |
| 243 | + ca: 'ca', |
| 244 | + cert: 'cert', |
| 245 | + key: 'key', |
| 246 | + }); |
| 247 | + }); |
| 248 | +}); |
| 249 | + |
| 250 | +// `tls.getCACertificates` / `tls.setDefaultCACertificates` exist on every supported Node line |
| 251 | +// but are newer than the pinned @types/node. |
| 252 | +const tlsDefaultCa = tls as unknown as { |
| 253 | + getCACertificates?: (type: 'default') => string[]; |
| 254 | + setDefaultCACertificates?: (certs: string[]) => void; |
| 255 | +}; |
| 256 | + |
| 257 | +describe('HttpConnection through an HTTP CONNECT proxy', () => { |
| 258 | + // Self-signed `localhost` certificate, generated per run so no private key is committed. |
| 259 | + let cert = ''; |
| 260 | + let key = ''; |
| 261 | + |
| 262 | + let server: https.Server; |
| 263 | + let serverRequests = 0; |
| 264 | + let clientCertCN: string | undefined; |
| 265 | + |
| 266 | + let proxy: http.Server; |
| 267 | + let proxyConnects = 0; |
| 268 | + const tunnels = new Set<net.Socket>(); |
| 269 | + |
| 270 | + const agents: Array<http.Agent | undefined> = []; |
| 271 | + |
| 272 | + before(async function () { |
| 273 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'dbsql-proxy-tls-')); |
| 274 | + try { |
| 275 | + execFileSync( |
| 276 | + 'openssl', |
| 277 | + [ |
| 278 | + 'req', |
| 279 | + '-x509', |
| 280 | + '-newkey', |
| 281 | + 'ec', |
| 282 | + '-pkeyopt', |
| 283 | + 'ec_paramgen_curve:prime256v1', |
| 284 | + '-nodes', |
| 285 | + '-days', |
| 286 | + '1', |
| 287 | + '-subj', |
| 288 | + '/CN=localhost', |
| 289 | + '-addext', |
| 290 | + 'subjectAltName=DNS:localhost,IP:127.0.0.1', |
| 291 | + '-keyout', |
| 292 | + path.join(dir, 'key.pem'), |
| 293 | + '-out', |
| 294 | + path.join(dir, 'cert.pem'), |
| 295 | + ], |
| 296 | + { stdio: 'ignore' }, |
| 297 | + ); |
| 298 | + cert = fs.readFileSync(path.join(dir, 'cert.pem'), 'utf8'); |
| 299 | + key = fs.readFileSync(path.join(dir, 'key.pem'), 'utf8'); |
| 300 | + } catch { |
| 301 | + this.skip(); // openssl is not available |
| 302 | + } finally { |
| 303 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 304 | + } |
| 305 | + |
| 306 | + server = https.createServer({ cert, key, requestCert: true, rejectUnauthorized: false }, (req, res) => { |
| 307 | + serverRequests += 1; |
| 308 | + clientCertCN = (req.socket as tls.TLSSocket).getPeerCertificate().subject?.CN; |
| 309 | + req.resume(); |
| 310 | + res.end('ok'); |
| 311 | + }); |
| 312 | + |
| 313 | + proxy = http.createServer(); |
| 314 | + proxy.on('connect', (req: http.IncomingMessage, clientSocket: net.Socket, head: Buffer) => { |
| 315 | + proxyConnects += 1; |
| 316 | + const upstream = net.connect(Number(String(req.url).split(':')[1]), '127.0.0.1', () => { |
| 317 | + clientSocket.write('HTTP/1.1 200 Connection Established\r\n\r\n'); |
| 318 | + upstream.write(head); |
| 319 | + upstream.pipe(clientSocket); |
| 320 | + clientSocket.pipe(upstream); |
| 321 | + }); |
| 322 | + tunnels.add(clientSocket).add(upstream); |
| 323 | + upstream.on('error', () => clientSocket.destroy()); |
| 324 | + clientSocket.on('error', () => upstream.destroy()); |
| 325 | + }); |
| 326 | + |
| 327 | + await new Promise<void>((resolve) => { |
| 328 | + server.listen(0, '127.0.0.1', resolve); |
| 329 | + }); |
| 330 | + await new Promise<void>((resolve) => { |
| 331 | + proxy.listen(0, '127.0.0.1', resolve); |
| 332 | + }); |
| 333 | + }); |
| 334 | + |
| 335 | + beforeEach(() => { |
| 336 | + serverRequests = 0; |
| 337 | + clientCertCN = undefined; |
| 338 | + proxyConnects = 0; |
| 339 | + }); |
| 340 | + |
| 341 | + afterEach(() => { |
| 342 | + agents.splice(0).forEach((agent) => agent?.destroy()); |
| 343 | + }); |
| 344 | + |
| 345 | + after(async () => { |
| 346 | + tunnels.forEach((socket) => socket.destroy()); |
| 347 | + server?.closeAllConnections(); |
| 348 | + await Promise.all( |
| 349 | + [server, proxy].map( |
| 350 | + (s) => |
| 351 | + new Promise<void>((resolve) => { |
| 352 | + if (!s?.listening) { |
| 353 | + resolve(); |
| 354 | + return; |
| 355 | + } |
| 356 | + s.close(() => resolve()); |
| 357 | + }), |
| 358 | + ), |
| 359 | + ); |
| 360 | + }); |
| 361 | + |
| 362 | + async function post(options: Partial<IConnectionOptions>): Promise<void> { |
| 363 | + const connection = new HttpConnection( |
| 364 | + { |
| 365 | + host: 'localhost', |
| 366 | + port: (server.address() as net.AddressInfo).port, |
| 367 | + https: true, |
| 368 | + proxy: { protocol: 'http', host: '127.0.0.1', port: (proxy.address() as net.AddressInfo).port }, |
| 369 | + ...options, |
| 370 | + }, |
| 371 | + new ClientContextStub(), |
| 372 | + ); |
| 373 | + const agent = await connection.getAgent(); |
| 374 | + agents.push(agent); |
| 375 | + |
| 376 | + await new Promise<void>((resolve, reject) => { |
| 377 | + const req = https.request( |
| 378 | + { |
| 379 | + host: 'localhost', |
| 380 | + port: (server.address() as net.AddressInfo).port, |
| 381 | + method: 'POST', |
| 382 | + path: '/sql/1.0/warehouses/test', |
| 383 | + headers: { Authorization: 'Bearer test-token' }, |
| 384 | + agent, |
| 385 | + }, |
| 386 | + (res) => { |
| 387 | + res.resume(); |
| 388 | + res.on('end', resolve); |
| 389 | + }, |
| 390 | + ); |
| 391 | + req.on('error', reject); |
| 392 | + req.end(); |
| 393 | + }); |
| 394 | + } |
| 395 | + |
| 396 | + async function expectRejected(options: Partial<IConnectionOptions>): Promise<NodeJS.ErrnoException> { |
| 397 | + let error: NodeJS.ErrnoException | undefined; |
| 398 | + try { |
| 399 | + await post(options); |
| 400 | + } catch (e) { |
| 401 | + error = e as NodeJS.ErrnoException; |
| 402 | + } |
| 403 | + expect(error, 'expected the TLS handshake to fail').to.not.be.undefined; |
| 404 | + expect(proxyConnects).to.equal(1); |
| 405 | + // The handshake fails before the request (and its bearer token) is written. |
| 406 | + expect(serverRequests).to.equal(0); |
| 407 | + return error as NodeJS.ErrnoException; |
| 408 | + } |
| 409 | + |
| 410 | + it('should verify the server certificate by default', async () => { |
| 411 | + const error = await expectRejected({}); |
| 412 | + expect(error.code).to.equal('DEPTH_ZERO_SELF_SIGNED_CERT'); |
| 413 | + }); |
| 414 | + |
| 415 | + it('should verify against the configured CA set rather than the process default trust store', async function () { |
| 416 | + if (!tlsDefaultCa.getCACertificates || !tlsDefaultCa.setDefaultCACertificates) { |
| 417 | + this.skip(); |
| 418 | + } |
| 419 | + // Trust the server's certificate process-wide (as NODE_EXTRA_CA_CERTS would), while the |
| 420 | + // connection's own CA set (the built-in roots) does not include it. |
| 421 | + const defaults = tlsDefaultCa.getCACertificates('default'); |
| 422 | + tlsDefaultCa.setDefaultCACertificates([...defaults, cert]); |
| 423 | + try { |
| 424 | + const error = await expectRejected({ ca: [...tls.rootCertificates] }); |
| 425 | + expect(error.code).to.equal('DEPTH_ZERO_SELF_SIGNED_CERT'); |
| 426 | + } finally { |
| 427 | + tlsDefaultCa.setDefaultCACertificates(defaults); |
| 428 | + } |
| 429 | + }); |
| 430 | + |
| 431 | + it('should allow disabling server certificate validation via rejectUnauthorized', async () => { |
| 432 | + await post({ rejectUnauthorized: false }); |
| 433 | + expect(proxyConnects).to.equal(1); |
| 434 | + expect(serverRequests).to.equal(1); |
| 435 | + }); |
| 436 | + |
| 437 | + it('should apply the CA and the client certificate', async () => { |
| 438 | + await post({ ca: cert, cert, key }); |
| 439 | + expect(proxyConnects).to.equal(1); |
| 440 | + expect(serverRequests).to.equal(1); |
| 441 | + expect(clientCertCN).to.equal('localhost'); |
| 442 | + }); |
214 | 443 | }); |
0 commit comments