Skip to content

Commit 0388fa4

Browse files
vuanhphungIsaac
andauthored
fix: apply TLS options to Thrift connections made through a proxy (#595)
proxy-agent only uses the httpsAgent we pass it for URLs that bypass the proxy. Tunnelled TLS connections take their settings from the ProxyAgent's top-level options, so ca, cert, key, rejectUnauthorized and minVersion were dropped and Node's process-wide trust store was used instead. Pass the same TLS options at the top level. Signed-off-by: Vu Anh Phung <vu.phung@databricks.com> Co-authored-by: Isaac <no-reply@databricks.com>
1 parent a953b7f commit 0388fa4

3 files changed

Lines changed: 243 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
- Thrift connections made through `proxy` now apply `checkServerCertificate`, `customCaCert` and `clientCert`/`clientKey`. Previously the proxied path ignored them and verified against Node's process-wide trust store, so a certificate outside the configured trust set could be accepted. **Compatibility:** proxied connections now verify unless `checkServerCertificate: false` is set; as on direct connections, `NODE_TLS_REJECT_UNAUTHORIZED=0` no longer turns verification off. For an `https` proxy these options also apply to the proxy's own certificate. (#485)
56
- Honor the client `useCloudFetch: false` default on the kernel backend so complete results can be fetched without cloud-storage downloads. (#574)
67
- Thrift HTTP errors (`THTTPException`) now append server detail from the error headers (at most 500 characters). Kernel-backend errors now carry a non-enumerable `cause` summarizing the kernel error code and message (also at most 500 characters). Reyden fallback preserves KP001 after any existing kernel cause. (#467)
78
- Add `Symbol.asyncDispose` to clients, sessions, and operations, delegating to `close()`. Use `asyncDisposable()` with interface-typed results in TypeScript; existing interfaces and factory return types remain unchanged. (#331)

‎lib/connection/connections/HttpConnection.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,8 @@ export default class HttpConnection implements IConnectionProvider {
131131
return new http.Agent(httpAgentOptions);
132132
}
133133

134-
private createHttpsAgent(): https.Agent {
135-
const httpsAgentOptions: https.AgentOptions = {
136-
...this.getAgentDefaultOptions(),
134+
private getTlsOptions(): https.AgentOptions {
135+
return {
137136
minVersion: 'TLSv1.2',
138137
// Validate the server certificate by default; only skip verification when
139138
// the caller explicitly opts out via `rejectUnauthorized: false`.
@@ -142,6 +141,13 @@ export default class HttpConnection implements IConnectionProvider {
142141
cert: this.options.cert,
143142
key: this.options.key,
144143
};
144+
}
145+
146+
private createHttpsAgent(): https.Agent {
147+
const httpsAgentOptions: https.AgentOptions = {
148+
...this.getAgentDefaultOptions(),
149+
...this.getTlsOptions(),
150+
};
145151
return new https.Agent(httpsAgentOptions);
146152
}
147153

@@ -153,6 +159,10 @@ export default class HttpConnection implements IConnectionProvider {
153159

154160
return new ProxyAgent({
155161
...this.getAgentDefaultOptions(),
162+
// `httpsAgent` below only serves URLs that bypass the proxy. Tunnelled TLS
163+
// connections take their settings from these top-level options, which
164+
// proxy-agent also applies to the connection to an `https:` proxy.
165+
...this.getTlsOptions(),
156166
getProxyForUrl: () => proxyUrl,
157167
httpsAgent: this.createHttpsAgent(),
158168
httpAgent: this.createHttpAgent(),

‎tests/unit/connection/connections/HttpConnection.test.ts‎

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
import { execFileSync } from 'child_process';
2+
import fs from 'fs';
13
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';
29
import { expect } from 'chai';
10+
import { ProxyAgent } from 'proxy-agent';
311
import HttpConnection from '../../../../lib/connection/connections/HttpConnection';
412
import ThriftHttpConnection from '../../../../lib/connection/connections/ThriftHttpConnection';
513
import IConnectionOptions from '../../../../lib/connection/contracts/IConnectionOptions';
@@ -211,4 +219,225 @@ describe('HttpConnection.connect', () => {
211219
expect(thriftConnection.url).to.be.equal(testCase.expected);
212220
}
213221
});
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+
});
214443
});

0 commit comments

Comments
 (0)