From 7089cde4675de469f32c7222bccb7c41c1810537 Mon Sep 17 00:00:00 2001 From: Vu Anh Phung Date: Wed, 11 Nov 2026 23:05:00 +0000 Subject: [PATCH 1/8] feat: add `https` connection option to allow plain HTTP transport DBSQLClient hardcoded `https: true`, so callers behind a trusted local reverse proxy had to monkey-patch the private getConnectionOptions(). `https: false` now selects plain HTTP (port 80 by default) on the Thrift transport, and the kernel backend receives an explicit http://host[:port] host. HTTPS stays the default; mTLS client identity is rejected over HTTP. Fixes #503 Signed-off-by: Vu Anh Phung Co-authored-by: Isaac Signed-off-by: Vu Anh Phung --- CHANGELOG.md | 1 + CONNECTION_PARAMETERS.md | 31 ++++- lib/DBSQLClient.ts | 13 +- lib/contracts/IDBSQLClient.ts | 14 ++ lib/kernel/KernelAuth.ts | 5 +- tests/e2e/http_transport.test.ts | 81 +++++++++++ tests/e2e/proxy.test.ts | 14 +- tests/unit/DBSQLClient.test.ts | 51 +++++++ .../connections/PlainHttpTransport.test.ts | 128 ++++++++++++++++++ tests/unit/kernel/connectionOptions.test.ts | 21 +++ 10 files changed, 343 insertions(+), 16 deletions(-) create mode 100644 tests/e2e/http_transport.test.ts create mode 100644 tests/unit/connection/connections/PlainHttpTransport.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 2858c667..bf9c1376 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - 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) - 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) - Thrift `ExecuteStatement` requests are now retried when the connection fails before the request is sent (`ECONNREFUSED`, `ENOTFOUND`, `EAI_AGAIN`), within the existing `retryMaxAttempts` / `retriesTimeout` budget; once retries run out, the original connection error is thrown. Failures after the request may have reached the server are still not retried, so a statement can't run twice. (#458) +- Add the `https` connection option: set `https: false` to connect to `host` over plain HTTP (port 80 by default), for example through a trusted local reverse proxy. HTTPS remains the default, and plain HTTP should only be used on a trusted local or private link. (#503) - CloudFetch retry-exhaustion errors now identify the download and the last failure: the `RetryError` message reads `CloudFetch result download failed: (last error: )`. `` is `HTTP : ` (or `HTTP ` when the status text is empty) for HTTP failures, and `network error `, `network error ()` (e.g. `aborted`, when there is no `code`) or a bare `network error` for network failures (the raw network-error message, which can contain the presigned URL, is not copied). If no cause can be determined, the ` (last error: …)` suffix is omitted. The `RetryError` instance, `errorCode` and `payload` (including the node-fetch `Request` / `Response`) are unchanged. (#471) - Report untyped NULL columns as STRING in kernel result metadata, matching Thrift. (#470) - `IOperation.close()` no longer throws when the close request fails after the operation has finished (for example, once its results were read, or when the execute response already reported completion): the failure is logged as a warning and reported in the returned status's `info`, and the operation is still closed locally. Closing an operation that hasn't finished still throws on failure. (#480) diff --git a/CONNECTION_PARAMETERS.md b/CONNECTION_PARAMETERS.md index 0ee313ce..e0503021 100644 --- a/CONNECTION_PARAMETERS.md +++ b/CONNECTION_PARAMETERS.md @@ -42,7 +42,8 @@ backend (the default), the **SEA / Kernel** backend, or both. | ----------------------- | --------- | :----: | :----: | ------------- | -------------------------------------------------------------------------------------- | | `host` | `string` | ✅ | ✅ | — (required) | Required on both. | | `path` | `string` | ✅ | ✅ | — (required) | Kernel also derives `x-databricks-org-id` from a `?o=` query parameter. | -| `port` | `number` | ✅ | ❌ | `443` / — | Kernel ignores this field. Include a non-default port in `host` for Kernel. | +| `port` | `number` | ✅ | ⚠️ | `443` / — | Kernel uses it only with `https: false`; otherwise put a non-default port in `host`. | +| `https` | `boolean` | ✅ | ✅ | `true` | `false` selects plain HTTP; see [Plain HTTP transport](#plain-http-transport). | | `userAgentEntry` | `string` | ✅ | ✅ | — | Folded into the connector `User-Agent` on both. | | `useKernel` (internal) | `boolean` | — | — | `false` | Selects the backend: `false`/omitted → Thrift, `true` → Kernel. | | `clientId` (deprecated) | `string` | ✅ | ✅ | — | Runtime-only alias for `userAgentEntry`. `userAgentEntry` wins when both are supplied. | @@ -101,6 +102,34 @@ disabling verification. | `clientKey` | `Buffer \| string` | ✅ | ✅ | — | Public mTLS private key. Must be paired with `clientCert`; PKCS#8 is recommended for Kernel portability. | | `clientCertPem` / `clientKeyPem` (internal) | `Buffer \| string` | ❌ | ✅ | — | Runtime-only Kernel aliases for the public mTLS pair. They take precedence over `clientCert`/`clientKey` when both pairs are supplied. | +## Plain HTTP transport + +Set `https: false` to connect to `host` over plain HTTP, for example through a +trusted local reverse proxy that forwards to Databricks over HTTPS (and may +inject credentials, with `authType: 'custom'` returning no headers): + +```ts +await client.connect({ + host: 'localhost', + port: 3015, + path: '/sql/1.0/warehouses/', + https: false, + authType: 'custom', + provider: { authenticate: async () => ({}) }, +}); +``` + +Plain HTTP is unencrypted: SQL text, results, and any credentials the driver +sends can be read or altered on the network. Use it only on a trusted local or +private link. With `https: false`, `port` defaults to `80`, the TLS options +above have no effect, and `clientCert`/`clientKey` are rejected. + +On Thrift, `https: false` applies to the SQL connection only. OAuth +(`databricks-oauth`), token federation, telemetry, and feature-flag requests +still use HTTPS to `host`. Kernel uses `http://host[:port]` for all of its +requests, including its own OAuth and telemetry calls. CloudFetch downloads use +the presigned URLs returned by the server on both backends. + ## Results and type rendering | Option | Type | Thrift | Kernel | Default Value | Note | diff --git a/lib/DBSQLClient.ts b/lib/DBSQLClient.ts index 3f3f84d2..4256873a 100644 --- a/lib/DBSQLClient.ts +++ b/lib/DBSQLClient.ts @@ -231,6 +231,15 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient, I ); } + // Only an explicit `false` opts out of TLS. + const https = options.https !== false; + // http.Agent would silently drop the client identity; the kernel rejects it too. + if (!https && hasClientCert) { + throw new HiveDriverError( + 'DBSQLClient: mutual TLS (`clientCert`/`clientKey`) requires HTTPS; remove them or do not set `https: false`.', + ); + } + // Validate the PEM inputs up front with the same ordered BEGIN…END check the // kernel path uses (normalizePemBytes), so a truncated/headerless/DER blob is // rejected here with a named, actionable error instead of surfacing as an @@ -246,9 +255,9 @@ export default class DBSQLClient extends EventEmitter implements IDBSQLClient, I return { host: options.host, - port: options.port || 443, + port: options.port || (https ? 443 : 80), path: prependSlash(options.path), - https: true, + https, socketTimeout: options.socketTimeout, proxy: options.proxy, // `customCaCert` is ADDITIVE: Node's `ca` option replaces the system trust diff --git a/lib/contracts/IDBSQLClient.ts b/lib/contracts/IDBSQLClient.ts index 2220ca12..ea9c3e60 100644 --- a/lib/contracts/IDBSQLClient.ts +++ b/lib/contracts/IDBSQLClient.ts @@ -81,6 +81,20 @@ export type ConnectionOptions = { */ escapeMetadataCatalogName?: boolean; + /** + * Connect to `host` over HTTPS (`true`, the default) or plain HTTP (`false`). + * + * Plain HTTP is unencrypted: SQL, results, and any credentials the driver + * sends can be read and modified on the network. Use `false` only for a + * trusted local or private link, such as a reverse proxy that forwards to + * Databricks over HTTPS. With `false`, `port` defaults to 80, the TLS options + * have no effect, and `clientCert`/`clientKey` are rejected. + * + * On the Thrift backend this applies to the SQL connection only; OAuth, token + * federation, telemetry, and feature-flag requests still use HTTPS. + */ + https?: boolean; + /** * Verify the server's TLS certificate on the primary Thrift transport. * Secure-by-default: omitting this leaves full chain + hostname verification diff --git a/lib/kernel/KernelAuth.ts b/lib/kernel/KernelAuth.ts index 7bcb7816..88cb0d75 100644 --- a/lib/kernel/KernelAuth.ts +++ b/lib/kernel/KernelAuth.ts @@ -843,7 +843,10 @@ export function buildKernelConnectionOptions(options: ConnectionOptions): Kernel KernelHttpOptions & KernelProxyOptions & KernelFederationOptions = { - hostName: options.host, + // The kernel prepends `https://` to a bare host but keeps an explicit `http://`. + // It otherwise ignores `port`, so carry it into the URL as the Thrift path does. + hostName: + options.https === false ? `http://${options.host}${options.port ? `:${options.port}` : ''}` : options.host, httpPath: prependSlash(options.path), // Match the NodeJS Thrift driver, which surfaces INTERVAL columns as // strings. The kernel defaults to native Arrow interval/duration types; diff --git a/tests/e2e/http_transport.test.ts b/tests/e2e/http_transport.test.ts new file mode 100644 index 00000000..d3a3dfb0 --- /dev/null +++ b/tests/e2e/http_transport.test.ts @@ -0,0 +1,81 @@ +import { expect } from 'chai'; +import http from 'http'; +import { AddressInfo, Socket } from 'net'; +import httpProxy from 'http-proxy'; +import { DBSQLClient } from '../../lib'; +import { ConnectionOptions } from '../../lib/contracts/IDBSQLClient'; +import { InternalConnectionOptions } from '../../lib/contracts/InternalConnectionOptions'; + +import config from './utils/config'; + +// Application --HTTP--> local reverse proxy --HTTPS--> Databricks +describe('Plain HTTP transport through a local reverse proxy', () => { + const requests: Array<{ method?: string; url?: string }> = []; + const sockets = new Set(); + let server: http.Server; + let port: number; + + before(async () => { + const proxy = httpProxy.createProxyServer({ target: `https://${config.host}`, changeOrigin: true, secure: true }); + server = http.createServer((req, res) => { + requests.push({ method: req.method, url: req.url }); + proxy.web(req, res, {}, (error) => { + res.writeHead(502); + res.end(String(error)); + }); + }); + server.on('connection', (socket: Socket) => { + sockets.add(socket); + socket.on('close', () => sockets.delete(socket)); + }); + await new Promise((resolve) => { + server.listen(0, '127.0.0.1', resolve); + }); + port = (server.address() as AddressInfo).port; + }); + + after(async () => { + const closed = new Promise((resolve) => { + server.close(() => resolve()); + }); + sockets.forEach((socket) => socket.destroy()); + await closed; + }); + + beforeEach(() => { + requests.length = 0; + }); + + [false, true].forEach((useKernel) => { + it(`runs a query with https: false (${useKernel ? 'kernel' : 'thrift'} backend)`, async () => { + const client = new DBSQLClient(); + await client.connect({ + host: '127.0.0.1', + port, + path: config.path, + token: config.token, + https: false, + useKernel, + } as ConnectionOptions & InternalConnectionOptions); + + const session = await client.openSession({ initialCatalog: config.catalog, initialSchema: config.schema }); + const operation = await session.executeStatement('SELECT 1 AS one'); + const rows = (await operation.fetchAll()) as Array>; + await operation.close(); + await session.close(); + await client.close(); + + expect(rows).to.have.length(1); + expect(Number(rows[0].one)).to.equal(1); + // The SQL traffic went through the plain-HTTP proxy: Thrift to the + // warehouse path, the kernel to the SQL Statement Execution API. + if (useKernel) { + expect(requests.some((request) => request.url?.startsWith('/api/2.0/sql/'))).to.be.true; + } else { + expect(requests.length).to.be.gte(1); + const path = config.path.startsWith('/') ? config.path : `/${config.path}`; + expect(requests.every((request) => request.url === path)).to.be.true; + } + }); + }); +}); diff --git a/tests/e2e/proxy.test.ts b/tests/e2e/proxy.test.ts index cd6e8ae4..de807d5a 100644 --- a/tests/e2e/proxy.test.ts +++ b/tests/e2e/proxy.test.ts @@ -65,26 +65,16 @@ describe('Proxy', () => { try { const client = new DBSQLClient(); - // Our proxy mock is HTTP -> HTTPS, but DBSQLClient is hard-coded to use HTTPS. - // Here we override default behavior to make DBSQLClient work with HTTP proxy - // @ts-expect-error TS2341: Property getConnectionOptions is private - const originalGetConnectionOptions = client.getConnectionOptions; - // @ts-expect-error TS2341: Property getConnectionOptions is private - client.getConnectionOptions = (...args) => { - const result = originalGetConnectionOptions.apply(client, args); - result.https = false; - result.port = 80; - return result; - }; - const clientConfig = client.getConfig(); sinon.stub(client, 'getConfig').returns(clientConfig); + // Our proxy mock is HTTP -> HTTPS, so the client speaks plain HTTP to it. const connection = await client.connect({ host: config.host, path: config.path, token: config.token, proxy: proxy.config, + https: false, }); await connection.openSession({ diff --git a/tests/unit/DBSQLClient.test.ts b/tests/unit/DBSQLClient.test.ts index f955e57d..386f268f 100644 --- a/tests/unit/DBSQLClient.test.ts +++ b/tests/unit/DBSQLClient.test.ts @@ -21,6 +21,7 @@ import IThriftClient from '../../lib/contracts/IThriftClient'; import IAuthentication from '../../lib/connection/contracts/IAuthentication'; import AuthProviderStub from './.stubs/AuthProviderStub'; import ConnectionProviderStub from './.stubs/ConnectionProviderStub'; +import HiveDriverError from '../../lib/errors/HiveDriverError'; import { TProtocolVersion } from '../../thrift/TCLIService_types'; import TelemetryClientProvider from '../../lib/telemetry/TelemetryClientProvider'; import FeatureFlagCache from '../../lib/FeatureFlagCache'; @@ -199,6 +200,56 @@ describe('DBSQLClient.connect', () => { ); }); + it('should use HTTPS on port 443 by default', async () => { + const client = new DBSQLClient(); + + const connectionOptions = client['getConnectionOptions']({ host: 'h.databricks.com', path: '', token: 'x' }); + + expect(connectionOptions.https).to.be.true; + expect(connectionOptions.port).to.equal(443); + }); + + it('should use plain HTTP on port 80 when https is false', async () => { + const client = new DBSQLClient(); + + const connectionOptions = client['getConnectionOptions']({ + host: 'localhost', + path: '', + token: 'x', + https: false, + }); + + expect(connectionOptions.https).to.be.false; + expect(connectionOptions.port).to.equal(80); + }); + + it('should keep an explicit port when https is false', async () => { + const client = new DBSQLClient(); + + const connectionOptions = client['getConnectionOptions']({ ...connectOptions, port: 3015, https: false }); + + expect(connectionOptions.https).to.be.false; + expect(connectionOptions.port).to.equal(3015); + }); + + it('should only switch to plain HTTP for an explicit https: false', async () => { + const client = new DBSQLClient(); + + expect(client['getConnectionOptions']({ ...connectOptions, https: true }).https).to.be.true; + expect(client['getConnectionOptions']({ ...connectOptions, https: undefined }).https).to.be.true; + // Untyped JS callers: anything but the boolean `false` keeps HTTPS. + expect(client['getConnectionOptions']({ ...connectOptions, https: 'false' } as any).https).to.be.true; + }); + + it('should reject a mutual TLS client identity when https is false', async () => { + const client = new DBSQLClient(); + + // Rejected before PEM parsing, so placeholder values are enough. + expect(() => + client['getConnectionOptions']({ ...connectOptions, https: false, clientCert: 'cert', clientKey: 'key' }), + ).to.throw(HiveDriverError, /requires HTTPS/); + }); + it('should initialize connection state', async () => { const client = new DBSQLClient(); diff --git a/tests/unit/connection/connections/PlainHttpTransport.test.ts b/tests/unit/connection/connections/PlainHttpTransport.test.ts new file mode 100644 index 00000000..07d150bf --- /dev/null +++ b/tests/unit/connection/connections/PlainHttpTransport.test.ts @@ -0,0 +1,128 @@ +import { expect, AssertionError } from 'chai'; +import http, { IncomingHttpHeaders } from 'http'; +import { AddressInfo, Socket } from 'net'; +import thrift from 'thrift'; +import DBSQLClient from '../../../../lib/DBSQLClient'; +import TCLIService from '../../../../thrift/TCLIService'; +import { TStatusCode, TProtocolVersion } from '../../../../thrift/TCLIService_types'; + +const warehousePath = '/sql/1.0/warehouses/abc'; + +// Minimal plain-HTTP Thrift endpoint standing in for a trusted local reverse proxy. +class LocalThriftServer { + public readonly requests: Array<{ method?: string; url?: string; encrypted: boolean; headers: IncomingHttpHeaders }> = + []; + + private readonly server: http.Server; + + // The driver's keep-alive agent holds sockets open; `server.close()` waits for them. + private readonly sockets = new Set(); + + constructor() { + const handle = { guid: Buffer.from('6ba7b8109dad11d180b400c04fd430c8', 'hex'), secret: Buffer.alloc(16, 2) }; + this.server = thrift.createWebServer({ + services: { + [warehousePath]: { + processor: TCLIService, + // Single-argument handlers select the generated processor's promise style. + handler: { + OpenSession: async (_req: unknown) => ({ + status: { statusCode: TStatusCode.SUCCESS_STATUS }, + serverProtocolVersion: TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V8, + sessionHandle: { sessionId: handle }, + }), + CloseSession: async (_req: unknown) => ({ status: { statusCode: TStatusCode.SUCCESS_STATUS } }), + }, + }, + }, + } as any) as unknown as http.Server; + this.server.prependListener('request', (req: http.IncomingMessage) => { + this.requests.push({ + method: req.method, + url: req.url, + encrypted: Boolean((req.socket as any).encrypted), + headers: req.headers, + }); + }); + this.server.on('connection', (socket: Socket) => { + this.sockets.add(socket); + socket.on('close', () => this.sockets.delete(socket)); + }); + } + + public async start(): Promise { + await new Promise((resolve) => { + this.server.listen(0, '127.0.0.1', resolve); + }); + return (this.server.address() as AddressInfo).port; + } + + public async stop(): Promise { + const closed = new Promise((resolve) => { + this.server.close(() => resolve()); + }); + this.sockets.forEach((socket) => socket.destroy()); + await closed; + } +} + +describe('DBSQLClient plain HTTP transport', () => { + let server: LocalThriftServer; + let port: number; + + beforeEach(async () => { + server = new LocalThriftServer(); + port = await server.start(); + }); + + afterEach(async () => { + await server.stop(); + }); + + const baseOptions = () => ({ + host: '127.0.0.1', + port, + path: warehousePath, + telemetryEnabled: false, + retryMaxAttempts: 1, + authType: 'custom' as const, + // The reverse proxy injects credentials, so the driver sends none. + provider: { authenticate: async () => ({}) }, + }); + + it('opens and closes a session over plain HTTP with https: false', async () => { + const client = new DBSQLClient(); + await client.connect({ ...baseOptions(), https: false }); + + const session = await client.openSession(); + await session.close(); + await client.close(); + + expect(server.requests).to.have.length(2); + for (const request of server.requests) { + expect(request.method).to.equal('POST'); + expect(request.url).to.equal(warehousePath); + expect(request.encrypted).to.be.false; + expect(request.headers.authorization).to.be.undefined; + } + }); + + it('keeps using HTTPS when https is omitted', async () => { + const client = new DBSQLClient(); + await client.connect(baseOptions()); + + try { + await client.openSession(); + expect.fail('a TLS handshake against a plain-HTTP server should fail'); + } catch (error) { + if (error instanceof AssertionError) { + throw error; + } + } finally { + await client.close(); + } + + // The TLS ClientHello never parses as an HTTP request. + expect(server.requests).to.have.length(0); + }); +}); diff --git a/tests/unit/kernel/connectionOptions.test.ts b/tests/unit/kernel/connectionOptions.test.ts index c29ff749..4253a768 100644 --- a/tests/unit/kernel/connectionOptions.test.ts +++ b/tests/unit/kernel/connectionOptions.test.ts @@ -63,6 +63,27 @@ describe('KernelAuth connection options — geospatial result representation', ( }); }); +describe('KernelAuth connection options — https transport', () => { + it('forwards host unchanged by default (kernel prepends https://)', () => { + expect(buildKernelConnectionOptions(opts({})).hostName).to.equal('h.databricks.com'); + expect(buildKernelConnectionOptions(opts({ https: true })).hostName).to.equal('h.databricks.com'); + }); + + it('selects plain HTTP via an explicit http:// host when https is false', () => { + expect(buildKernelConnectionOptions(opts({ https: false })).hostName).to.equal('http://h.databricks.com'); + }); + + it('carries the port into the http:// host (matching the Thrift URL)', () => { + expect(buildKernelConnectionOptions(opts({ host: 'localhost', port: 3015, https: false })).hostName).to.equal( + 'http://localhost:3015', + ); + // A port already embedded in `host` (the kernel convention) is preserved. + expect(buildKernelConnectionOptions(opts({ host: 'localhost:3015', https: false })).hostName).to.equal( + 'http://localhost:3015', + ); + }); +}); + describe('KernelAuth connection options — maxConnections', () => { it('forwards a valid positive integer', () => { const native = buildKernelConnectionOptions(opts({ maxConnections: 10 })) as { maxConnections?: number }; From b51037b6cd075e3330ffc26e43e4432ed35e02de Mon Sep 17 00:00:00 2001 From: Vu Anh Phung Date: Wed, 11 Nov 2026 23:14:07 +0000 Subject: [PATCH 2/8] fix: avoid doubling the kernel port when host already has one With `https: false`, the kernel host name appended `port` even when `host` already carried a `:port` suffix (the kernel's documented convention), producing an invalid `http://localhost:3015:3015`. Signed-off-by: Vu Anh Phung Co-authored-by: Isaac Signed-off-by: Vu Anh Phung --- lib/kernel/KernelAuth.ts | 15 +++++++++++---- tests/unit/kernel/connectionOptions.test.ts | 9 +++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/kernel/KernelAuth.ts b/lib/kernel/KernelAuth.ts index 88cb0d75..00d5e214 100644 --- a/lib/kernel/KernelAuth.ts +++ b/lib/kernel/KernelAuth.ts @@ -830,6 +830,16 @@ export function buildKernelProxyOptions(options: ConnectionOptions): KernelProxy return { proxy: out }; } +// The kernel prepends `https://` to a bare host but keeps an explicit `http://`. +// It otherwise ignores `port`, so carry it into the URL unless `host` already has one. +function buildKernelHostName(options: ConnectionOptions): string { + if (options.https !== false) { + return options.host; + } + const port = options.port && !/:\d+$/.test(options.host) ? `:${options.port}` : ''; + return `http://${options.host}${port}`; +} + export function buildKernelConnectionOptions(options: ConnectionOptions): KernelNativeConnectionOptions { const { authType } = options as { authType?: string }; @@ -843,10 +853,7 @@ export function buildKernelConnectionOptions(options: ConnectionOptions): Kernel KernelHttpOptions & KernelProxyOptions & KernelFederationOptions = { - // The kernel prepends `https://` to a bare host but keeps an explicit `http://`. - // It otherwise ignores `port`, so carry it into the URL as the Thrift path does. - hostName: - options.https === false ? `http://${options.host}${options.port ? `:${options.port}` : ''}` : options.host, + hostName: buildKernelHostName(options), httpPath: prependSlash(options.path), // Match the NodeJS Thrift driver, which surfaces INTERVAL columns as // strings. The kernel defaults to native Arrow interval/duration types; diff --git a/tests/unit/kernel/connectionOptions.test.ts b/tests/unit/kernel/connectionOptions.test.ts index 4253a768..01551ff8 100644 --- a/tests/unit/kernel/connectionOptions.test.ts +++ b/tests/unit/kernel/connectionOptions.test.ts @@ -82,6 +82,15 @@ describe('KernelAuth connection options — https transport', () => { 'http://localhost:3015', ); }); + + it('does not double the port when it is set in both host and port', () => { + expect(buildKernelConnectionOptions(opts({ host: 'localhost:3015', port: 3015, https: false })).hostName).to.equal( + 'http://localhost:3015', + ); + expect(buildKernelConnectionOptions(opts({ host: '[::1]:3015', port: 3015, https: false })).hostName).to.equal( + 'http://[::1]:3015', + ); + }); }); describe('KernelAuth connection options — maxConnections', () => { From 587552d4692d430867bc2d4eed20daefa01f3ec6 Mon Sep 17 00:00:00 2001 From: Vu Anh Phung Date: Wed, 11 Nov 2026 23:25:03 +0000 Subject: [PATCH 3/8] fix: do not read a bare IPv6 host as carrying a port `/:\d+$/` matched literals like `::1`, so the kernel dropped `port`. Only `name:port` and `[ipv6]:port` now count as an embedded port, giving the same URL shape as the Thrift transport. Signed-off-by: Vu Anh Phung Co-authored-by: Isaac Signed-off-by: Vu Anh Phung --- lib/kernel/KernelAuth.ts | 4 +++- tests/unit/kernel/connectionOptions.test.ts | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/kernel/KernelAuth.ts b/lib/kernel/KernelAuth.ts index 00d5e214..27e16921 100644 --- a/lib/kernel/KernelAuth.ts +++ b/lib/kernel/KernelAuth.ts @@ -836,7 +836,9 @@ function buildKernelHostName(options: ConnectionOptions): string { if (options.https !== false) { return options.host; } - const port = options.port && !/:\d+$/.test(options.host) ? `:${options.port}` : ''; + // `name:port` or `[ipv6]:port`; a bare IPv6 literal like `::1` has no port. + const hasPort = /^(\[[^\]]+\]|[^:]+):\d+$/.test(options.host); + const port = options.port && !hasPort ? `:${options.port}` : ''; return `http://${options.host}${port}`; } diff --git a/tests/unit/kernel/connectionOptions.test.ts b/tests/unit/kernel/connectionOptions.test.ts index 01551ff8..6da07173 100644 --- a/tests/unit/kernel/connectionOptions.test.ts +++ b/tests/unit/kernel/connectionOptions.test.ts @@ -91,6 +91,16 @@ describe('KernelAuth connection options — https transport', () => { 'http://[::1]:3015', ); }); + + it('does not mistake an IPv6 literal for an embedded port', () => { + expect(buildKernelConnectionOptions(opts({ host: '[::1]', port: 3015, https: false })).hostName).to.equal( + 'http://[::1]:3015', + ); + // Same URL shape as the Thrift transport, which also requires brackets. + expect(buildKernelConnectionOptions(opts({ host: '::1', port: 3015, https: false })).hostName).to.equal( + 'http://::1:3015', + ); + }); }); describe('KernelAuth connection options — maxConnections', () => { From 3a38fda3f636e3dffc4404396bb3918626b19c15 Mon Sep 17 00:00:00 2001 From: Vu Anh Phung Date: Wed, 11 Nov 2026 23:34:48 +0000 Subject: [PATCH 4/8] fix: strip a trailing slash from the kernel http:// host Thrift strips a trailing `/` from `host` before adding the port; the kernel mapping did not, producing `http://localhost/:3015`. Signed-off-by: Vu Anh Phung Co-authored-by: Isaac Signed-off-by: Vu Anh Phung --- lib/kernel/KernelAuth.ts | 6 ++++-- tests/unit/kernel/connectionOptions.test.ts | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/kernel/KernelAuth.ts b/lib/kernel/KernelAuth.ts index 27e16921..20746d76 100644 --- a/lib/kernel/KernelAuth.ts +++ b/lib/kernel/KernelAuth.ts @@ -836,10 +836,12 @@ function buildKernelHostName(options: ConnectionOptions): string { if (options.https !== false) { return options.host; } + // Strip a trailing `/` like the Thrift URL builder does. + const host = options.host.replace(/\/$/, ''); // `name:port` or `[ipv6]:port`; a bare IPv6 literal like `::1` has no port. - const hasPort = /^(\[[^\]]+\]|[^:]+):\d+$/.test(options.host); + const hasPort = /^(\[[^\]]+\]|[^:]+):\d+$/.test(host); const port = options.port && !hasPort ? `:${options.port}` : ''; - return `http://${options.host}${port}`; + return `http://${host}${port}`; } export function buildKernelConnectionOptions(options: ConnectionOptions): KernelNativeConnectionOptions { diff --git a/tests/unit/kernel/connectionOptions.test.ts b/tests/unit/kernel/connectionOptions.test.ts index 6da07173..9f7bec81 100644 --- a/tests/unit/kernel/connectionOptions.test.ts +++ b/tests/unit/kernel/connectionOptions.test.ts @@ -92,6 +92,15 @@ describe('KernelAuth connection options — https transport', () => { ); }); + it('strips a trailing slash from host before adding the port (matching Thrift)', () => { + expect(buildKernelConnectionOptions(opts({ host: 'localhost/', port: 3015, https: false })).hostName).to.equal( + 'http://localhost:3015', + ); + expect(buildKernelConnectionOptions(opts({ host: 'localhost:3015/', port: 3015, https: false })).hostName).to.equal( + 'http://localhost:3015', + ); + }); + it('does not mistake an IPv6 literal for an embedded port', () => { expect(buildKernelConnectionOptions(opts({ host: '[::1]', port: 3015, https: false })).hostName).to.equal( 'http://[::1]:3015', From e62cc20a449d744a8c5760a2ac330af487633717 Mon Sep 17 00:00:00 2001 From: "peco-engineer-bot[bot]" Date: Fri, 13 Nov 2026 06:10:15 +0000 Subject: [PATCH 5/8] engineer-bot: address @mention on PR #588 Signed-off-by: peco-engineer-bot[bot] Signed-off-by: Vu Anh Phung --- lib/DBSQLSession.ts | 6 +- lib/FeatureFlagCache.ts | 2 +- .../auth/DatabricksOAuth/OAuthManager.ts | 11 ++- lib/connection/connections/HttpConnection.ts | 45 ++++++++-- .../contracts/IConnectionProvider.ts | 6 +- lib/result/CloudFetchResultHandler.ts | 2 +- lib/telemetry/DatabricksTelemetryExporter.ts | 2 +- .../connections/HttpConnection.test.ts | 68 +++++++++++++++ .../connections/PlainHttpTransport.test.ts | 82 +++++++++++++++++-- tests/unit/tmp_debug.test.ts | 0 10 files changed, 199 insertions(+), 25 deletions(-) create mode 100644 tests/unit/tmp_debug.test.ts diff --git a/lib/DBSQLSession.ts b/lib/DBSQLSession.ts index cc567e64..64c24b3e 100644 --- a/lib/DBSQLSession.ts +++ b/lib/DBSQLSession.ts @@ -171,7 +171,7 @@ export default class DBSQLSession implements IDBSQLSession { } const connectionProvider = await this.context.getConnectionProvider(); - const agent = await connectionProvider.getAgent(); + const agent = await connectionProvider.getAgent(presignedUrl); const response = await fetch(presignedUrl, { method: 'GET', headers, agent }); if (!response.ok) { @@ -184,7 +184,7 @@ export default class DBSQLSession implements IDBSQLSession { private async handleStagingRemove(presignedUrl: string, headers: HeadersInit): Promise { const connectionProvider = await this.context.getConnectionProvider(); - const agent = await connectionProvider.getAgent(); + const agent = await connectionProvider.getAgent(presignedUrl); const response = await fetch(presignedUrl, { method: 'DELETE', headers, agent }); // Looks that AWS and Azure have a different behavior of HTTP `DELETE` for non-existing files. @@ -209,7 +209,7 @@ export default class DBSQLSession implements IDBSQLSession { } const connectionProvider = await this.context.getConnectionProvider(); - const agent = await connectionProvider.getAgent(); + const agent = await connectionProvider.getAgent(presignedUrl); const fileStream = fs.createReadStream(localFile); const fileInfo = fs.statSync(localFile, { bigint: true }); diff --git a/lib/FeatureFlagCache.ts b/lib/FeatureFlagCache.ts index 9420f527..8e7f323c 100644 --- a/lib/FeatureFlagCache.ts +++ b/lib/FeatureFlagCache.ts @@ -234,7 +234,7 @@ export default class FeatureFlagCache { /** Retries transient failures using the connection's retry policy. */ private async fetchWithRetry(url: string, init: RequestInit): Promise { const connectionProvider = await this.context.getConnectionProvider(); - const agent = await connectionProvider.getAgent(); + const agent = await connectionProvider.getAgent(url); const retryPolicy = await connectionProvider.getRetryPolicy(); const requestConfig: RequestInit = { agent, ...init }; const result = await retryPolicy.invokeWithRetry(() => { diff --git a/lib/connection/auth/DatabricksOAuth/OAuthManager.ts b/lib/connection/auth/DatabricksOAuth/OAuthManager.ts index d5c931b6..40ff80ae 100644 --- a/lib/connection/auth/DatabricksOAuth/OAuthManager.ts +++ b/lib/connection/auth/DatabricksOAuth/OAuthManager.ts @@ -36,6 +36,8 @@ export default abstract class OAuthManager { protected agent?: http.Agent; + protected httpAgent?: http.Agent; + protected issuer?: Issuer; protected client?: BaseClient; @@ -60,10 +62,13 @@ export default abstract class OAuthManager { // Obtain http agent each time when we need an OAuth client // to ensure that we always use a valid agent instance const connectionProvider = await this.context.getConnectionProvider(); - this.agent = await connectionProvider.getAgent(); + // openid-client picks `http`/`https` by each request's URL, so the agent must + // match that URL's protocol rather than the SQL transport's. + this.agent = await connectionProvider.getAgent('https:'); + this.httpAgent = await connectionProvider.getAgent('http:'); - const getHttpOptions = () => ({ - agent: this.agent, + const getHttpOptions = (url?: { protocol: string }) => ({ + agent: url?.protocol === 'http:' ? this.httpAgent : this.agent, }); if (!this.issuer) { diff --git a/lib/connection/connections/HttpConnection.ts b/lib/connection/connections/HttpConnection.ts index 8253a69a..6472265a 100644 --- a/lib/connection/connections/HttpConnection.ts +++ b/lib/connection/connections/HttpConnection.ts @@ -32,6 +32,14 @@ const DRIVER_MANAGED_HEADERS = new Set([ const VALID_HEADER_NAME = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/; const INVALID_HEADER_VALUE_CHAR = /[^\t\x20-\x7e\x80-\xff]/; +function getProtocol(url: string | URL): string { + if (typeof url !== 'string') { + return url.protocol; + } + const match = /^([a-z][a-z0-9+.-]*:)/i.exec(url); + return match ? match[1].toLowerCase() : ''; +} + export default class HttpConnection implements IConnectionProvider { private readonly options: IConnectionOptions; @@ -44,7 +52,11 @@ export default class HttpConnection implements IConnectionProvider { private connection?: ThriftHttpConnection; - private agent?: http.Agent; + private proxyAgent?: http.Agent; + + private httpAgent?: http.Agent; + + private httpsAgent?: https.Agent; constructor(options: IConnectionOptions, context: IClientContext) { this.options = options; @@ -104,16 +116,33 @@ export default class HttpConnection implements IConnectionProvider { return result; } - public async getAgent(): Promise { - if (!this.agent) { - if (this.options.proxy !== undefined) { - this.agent = this.createProxyAgent(this.options.proxy); - } else { - this.agent = this.options.https ? this.createHttpsAgent() : this.createHttpAgent(); + /** + * Returns the agent for a request to `url` (an absolute URL or a bare + * protocol such as `'https:'`). Node rejects an agent whose protocol differs + * from the request's, so the agent follows the URL rather than `https`, which + * only selects the SQL transport. Without `url`, returns the SQL transport's agent. + */ + public async getAgent(url?: string | URL): Promise { + if (this.options.proxy !== undefined) { + // ProxyAgent handles both protocols itself. + if (!this.proxyAgent) { + this.proxyAgent = this.createProxyAgent(this.options.proxy); } + return this.proxyAgent; } - return this.agent; + const useHttps = url === undefined ? Boolean(this.options.https) : getProtocol(url) !== 'http:'; + if (useHttps) { + if (!this.httpsAgent) { + this.httpsAgent = this.createHttpsAgent(); + } + return this.httpsAgent; + } + + if (!this.httpAgent) { + this.httpAgent = this.createHttpAgent(); + } + return this.httpAgent; } private getAgentDefaultOptions(): http.AgentOptions { diff --git a/lib/connection/contracts/IConnectionProvider.ts b/lib/connection/contracts/IConnectionProvider.ts index 76406473..a81bbe82 100644 --- a/lib/connection/contracts/IConnectionProvider.ts +++ b/lib/connection/contracts/IConnectionProvider.ts @@ -10,7 +10,11 @@ export interface HttpTransactionDetails { export default interface IConnectionProvider { getThriftConnection(): Promise; - getAgent(): Promise; + /** + * Returns the agent for a request to `url`, whose protocol it must match. + * Without `url`, returns the agent for the SQL transport. + */ + getAgent(url?: string | URL): Promise; setHeaders(headers: HeadersInit): void; diff --git a/lib/result/CloudFetchResultHandler.ts b/lib/result/CloudFetchResultHandler.ts index a64bb4bb..773b9f38 100644 --- a/lib/result/CloudFetchResultHandler.ts +++ b/lib/result/CloudFetchResultHandler.ts @@ -148,7 +148,7 @@ export default class CloudFetchResultHandler implements IResultsProvider { const connectionProvider = await this.context.getConnectionProvider(); - const agent = await connectionProvider.getAgent(); + const agent = await connectionProvider.getAgent(typeof url === 'string' ? url : 'href' in url ? url.href : url.url); const retryPolicy = await connectionProvider.getRetryPolicy(); const requestConfig: RequestInit = { agent, ...init }; diff --git a/lib/telemetry/DatabricksTelemetryExporter.ts b/lib/telemetry/DatabricksTelemetryExporter.ts index 4978a969..be17e304 100644 --- a/lib/telemetry/DatabricksTelemetryExporter.ts +++ b/lib/telemetry/DatabricksTelemetryExporter.ts @@ -332,7 +332,7 @@ export default class DatabricksTelemetryExporter { private async sendRequest(url: string, init: RequestInit): Promise { const connectionProvider = await this.context.getConnectionProvider(); - const agent = await connectionProvider.getAgent(); + const agent = await connectionProvider.getAgent(url); // Do NOT route through `connectionProvider.getRetryPolicy()` here. That // policy is bounded by the customer's `retriesTimeout` (15 min default) // and `retryMaxAttempts` (5), so each call to `sendRequest` could spend diff --git a/tests/unit/connection/connections/HttpConnection.test.ts b/tests/unit/connection/connections/HttpConnection.test.ts index 18bed8aa..5dbfa5aa 100644 --- a/tests/unit/connection/connections/HttpConnection.test.ts +++ b/tests/unit/connection/connections/HttpConnection.test.ts @@ -106,6 +106,74 @@ describe('HttpConnection.connect', () => { expect(thriftConnection.config.agent).to.be.instanceOf(http.Agent); }); + it('should select the agent by the request protocol, not the SQL transport', async () => { + const connection = new HttpConnection( + { + host: 'localhost', + port: 10001, + https: false, + path: '/hive', + rejectUnauthorized: false, + ca: 'ca', + }, + new ClientContextStub(), + ); + + const sqlAgent = await connection.getAgent(); + const thriftConnection = await connection.getThriftConnection(); + expect(thriftConnection.config.agent).to.equal(sqlAgent); + expect(sqlAgent).to.be.instanceOf(http.Agent); + expect(sqlAgent).to.not.be.instanceOf(https.Agent); + + // CloudFetch and OAuth use HTTPS URLs even when the SQL transport is plain HTTP. + const httpsAgent = await connection.getAgent('https://bucket.s3.amazonaws.com/chunk?X-Amz-Signature=x'); + expect(httpsAgent).to.be.instanceOf(https.Agent); + expect((httpsAgent as https.Agent).options.rejectUnauthorized).to.be.false; + expect((httpsAgent as https.Agent).options.ca).to.equal('ca'); + expect(await connection.getAgent(new URL('HTTPS://example.com'))).to.equal(httpsAgent); + expect(await connection.getAgent('https:')).to.equal(httpsAgent); + + expect(await connection.getAgent('http://localhost:10001/hive')).to.equal(sqlAgent); + expect(await connection.getAgent('http:')).to.equal(sqlAgent); + }); + + it('should use the HTTPS agent for the SQL transport and an HTTP agent for http:// URLs', async () => { + const connection = new HttpConnection( + { + host: 'localhost', + port: 10001, + https: true, + path: '/hive', + }, + new ClientContextStub(), + ); + + const sqlAgent = await connection.getAgent(); + expect(sqlAgent).to.be.instanceOf(https.Agent); + expect(await connection.getAgent('https://example.com')).to.equal(sqlAgent); + + const httpAgent = await connection.getAgent('http://example.com'); + expect(httpAgent).to.be.instanceOf(http.Agent); + expect(httpAgent).to.not.be.instanceOf(https.Agent); + }); + + it('should use the proxy agent for every protocol', async () => { + const connection = new HttpConnection( + { + host: 'localhost', + port: 10001, + https: false, + path: '/hive', + proxy: { protocol: 'http', host: 'proxy.local', port: 8080 }, + }, + new ClientContextStub(), + ); + + const sqlAgent = await connection.getAgent(); + expect(await connection.getAgent('https://example.com')).to.equal(sqlAgent); + expect(await connection.getAgent('http://example.com')).to.equal(sqlAgent); + }); + it('should update headers (case 1: Thrift connection not initialized)', async () => { const initialHeaders = { a: 'test header A', diff --git a/tests/unit/connection/connections/PlainHttpTransport.test.ts b/tests/unit/connection/connections/PlainHttpTransport.test.ts index 07d150bf..425387d1 100644 --- a/tests/unit/connection/connections/PlainHttpTransport.test.ts +++ b/tests/unit/connection/connections/PlainHttpTransport.test.ts @@ -3,6 +3,9 @@ import http, { IncomingHttpHeaders } from 'http'; import { AddressInfo, Socket } from 'net'; import thrift from 'thrift'; import DBSQLClient from '../../../../lib/DBSQLClient'; +import CloudFetchResultHandler from '../../../../lib/result/CloudFetchResultHandler'; +import { DatabricksOAuthManager, OAuthFlow } from '../../../../lib/connection/auth/DatabricksOAuth/OAuthManager'; +import ResultsProviderStub from '../../.stubs/ResultsProviderStub'; import TCLIService from '../../../../thrift/TCLIService'; import { TStatusCode, TProtocolVersion } from '../../../../thrift/TCLIService_types'; @@ -13,6 +16,8 @@ class LocalThriftServer { public readonly requests: Array<{ method?: string; url?: string; encrypted: boolean; headers: IncomingHttpHeaders }> = []; + public connectionCount = 0; + private readonly server: http.Server; // The driver's keep-alive agent holds sockets open; `server.close()` waits for them. @@ -24,14 +29,17 @@ class LocalThriftServer { services: { [warehousePath]: { processor: TCLIService, - // Single-argument handlers select the generated processor's promise style. + // Callback-style handlers: the generated processor's promise style + // (single-argument handlers) needs `Q`, which `thrift` no longer provides. handler: { - OpenSession: async (_req: unknown) => ({ - status: { statusCode: TStatusCode.SUCCESS_STATUS }, - serverProtocolVersion: TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V8, - sessionHandle: { sessionId: handle }, - }), - CloseSession: async (_req: unknown) => ({ status: { statusCode: TStatusCode.SUCCESS_STATUS } }), + OpenSession: (_req: unknown, result: (error: unknown, response: unknown) => void) => + result(null, { + status: { statusCode: TStatusCode.SUCCESS_STATUS }, + serverProtocolVersion: TProtocolVersion.SPARK_CLI_SERVICE_PROTOCOL_V8, + sessionHandle: { sessionId: handle }, + }), + CloseSession: (_req: unknown, result: (error: unknown, response: unknown) => void) => + result(null, { status: { statusCode: TStatusCode.SUCCESS_STATUS } }), }, }, }, @@ -45,6 +53,7 @@ class LocalThriftServer { }); }); this.server.on('connection', (socket: Socket) => { + this.connectionCount += 1; this.sockets.add(socket); socket.on('close', () => this.sockets.delete(socket)); }); @@ -107,6 +116,65 @@ describe('DBSQLClient plain HTTP transport', () => { } }); + // CloudFetch links and OAuth endpoints stay HTTPS with `https: false`. A + // plain-HTTP agent would make Node throw ERR_INVALID_PROTOCOL before + // connecting; the right agent opens a socket and attempts a TLS handshake, + // which this plain-HTTP server rejects without parsing an HTTP request. + async function expectTlsAttempt(request: () => Promise) { + const connectionsBefore = server.connectionCount; + try { + await request(); + expect.fail('a TLS handshake against a plain-HTTP server should fail'); + } catch (error) { + if (error instanceof AssertionError) { + throw error; + } + expect(String((error as any)?.stack ?? error)).to.not.include('ERR_INVALID_PROTOCOL'); + expect((error as any)?.code).to.not.equal('ERR_INVALID_PROTOCOL'); + } + expect(server.connectionCount).to.be.greaterThan(connectionsBefore); + } + + it('runs SQL over HTTP and CloudFetch downloads over HTTPS with https: false', async () => { + const client = new DBSQLClient(); + await client.connect({ ...baseOptions(), https: false }); + + const session = await client.openSession(); + await session.close(); + expect(server.requests).to.have.length(2); + + const handler = new CloudFetchResultHandler(client, new ResultsProviderStub([], undefined), { + status: { statusCode: TStatusCode.SUCCESS_STATUS }, + }); + await expectTlsAttempt(() => handler['fetch'](`https://127.0.0.1:${port}/chunk?X-Amz-Signature=secret`)); + + await client.close(); + // The HTTPS attempt never reached the server as an HTTP request. + expect(server.requests).to.have.length(2); + }); + + it('runs SQL over HTTP and OAuth requests over HTTPS with https: false', async () => { + const client = new DBSQLClient(); + await client.connect({ ...baseOptions(), https: false }); + + const session = await client.openSession(); + await session.close(); + expect(server.requests).to.have.length(2); + + const oauthManager = new DatabricksOAuthManager({ + host: `127.0.0.1:${port}`, + flow: OAuthFlow.M2M, + clientId: 'id', + clientSecret: 'secret', + context: client, + }); + // OIDC discovery goes to https://127.0.0.1:/oidc/... . + await expectTlsAttempt(() => oauthManager['getClient']()); + + await client.close(); + expect(server.requests).to.have.length(2); + }); + it('keeps using HTTPS when https is omitted', async () => { const client = new DBSQLClient(); await client.connect(baseOptions()); diff --git a/tests/unit/tmp_debug.test.ts b/tests/unit/tmp_debug.test.ts new file mode 100644 index 00000000..e69de29b From 47c527ae8a454be1b1e6cae2afd821091f233781 Mon Sep 17 00:00:00 2001 From: Vu Anh Phung Date: Fri, 13 Nov 2026 06:15:43 +0000 Subject: [PATCH 6/8] fix: satisfy lint in CloudFetch agent selection Signed-off-by: Vu Anh Phung --- lib/result/CloudFetchResultHandler.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/result/CloudFetchResultHandler.ts b/lib/result/CloudFetchResultHandler.ts index 773b9f38..fc9d662d 100644 --- a/lib/result/CloudFetchResultHandler.ts +++ b/lib/result/CloudFetchResultHandler.ts @@ -148,7 +148,11 @@ export default class CloudFetchResultHandler implements IResultsProvider { const connectionProvider = await this.context.getConnectionProvider(); - const agent = await connectionProvider.getAgent(typeof url === 'string' ? url : 'href' in url ? url.href : url.url); + let requestUrl = url; + if (typeof requestUrl !== 'string') { + requestUrl = 'href' in requestUrl ? requestUrl.href : requestUrl.url; + } + const agent = await connectionProvider.getAgent(requestUrl); const retryPolicy = await connectionProvider.getRetryPolicy(); const requestConfig: RequestInit = { agent, ...init }; From e3a6fd6a0d2582b749e8f5d0c622eb6c7a9e41a1 Mon Sep 17 00:00:00 2001 From: "peco-engineer-bot[bot]" Date: Fri, 13 Nov 2026 06:28:51 +0000 Subject: [PATCH 7/8] engineer-bot: address @mention on PR #588 Signed-off-by: peco-engineer-bot[bot] Signed-off-by: Vu Anh Phung --- lib/kernel/KernelAuth.ts | 8 +++++--- tests/unit/kernel/connectionOptions.test.ts | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/kernel/KernelAuth.ts b/lib/kernel/KernelAuth.ts index 20746d76..8d900485 100644 --- a/lib/kernel/KernelAuth.ts +++ b/lib/kernel/KernelAuth.ts @@ -830,14 +830,16 @@ export function buildKernelProxyOptions(options: ConnectionOptions): KernelProxy return { proxy: out }; } -// The kernel prepends `https://` to a bare host but keeps an explicit `http://`. +// The kernel prepends `https://` to a bare host but keeps an explicit scheme. // It otherwise ignores `port`, so carry it into the URL unless `host` already has one. function buildKernelHostName(options: ConnectionOptions): string { if (options.https !== false) { return options.host; } - // Strip a trailing `/` like the Thrift URL builder does. - const host = options.host.replace(/\/$/, ''); + // The kernel accepts a full URL in `host`, so drop an existing `http://` or + // `https://` scheme before applying HTTP (`https: false` selects the scheme). + // Then strip a trailing `/` like the Thrift URL builder does. + const host = options.host.replace(/^https?:\/\//i, '').replace(/\/$/, ''); // `name:port` or `[ipv6]:port`; a bare IPv6 literal like `::1` has no port. const hasPort = /^(\[[^\]]+\]|[^:]+):\d+$/.test(host); const port = options.port && !hasPort ? `:${options.port}` : ''; diff --git a/tests/unit/kernel/connectionOptions.test.ts b/tests/unit/kernel/connectionOptions.test.ts index 9f7bec81..153e388f 100644 --- a/tests/unit/kernel/connectionOptions.test.ts +++ b/tests/unit/kernel/connectionOptions.test.ts @@ -101,6 +101,26 @@ describe('KernelAuth connection options — https transport', () => { ); }); + const hostName = (extra: Record) => buildKernelConnectionOptions(opts(extra)).hostName; + + it('normalizes an existing http:// scheme in host instead of prefixing it again', () => { + expect(hostName({ host: 'http://localhost:3015', https: false })).to.equal('http://localhost:3015'); + expect(hostName({ host: 'http://localhost:3015', port: 3015, https: false })).to.equal('http://localhost:3015'); + expect(hostName({ host: 'HTTP://localhost', port: 3015, https: false })).to.equal('http://localhost:3015'); + expect(hostName({ host: 'http://localhost:3015/', https: false })).to.equal('http://localhost:3015'); + }); + + it('replaces an existing https:// scheme in host with http:// when https is false', () => { + expect(hostName({ host: 'https://localhost:3015', https: false })).to.equal('http://localhost:3015'); + expect(hostName({ host: 'https://localhost', port: 3015, https: false })).to.equal('http://localhost:3015'); + expect(hostName({ host: 'https://[::1]:3015', port: 3015, https: false })).to.equal('http://[::1]:3015'); + }); + + it('forwards a full-URL host unchanged when https is not false', () => { + expect(hostName({ host: 'https://h.databricks.com' })).to.equal('https://h.databricks.com'); + expect(hostName({ host: 'http://localhost:3015', https: true })).to.equal('http://localhost:3015'); + }); + it('does not mistake an IPv6 literal for an embedded port', () => { expect(buildKernelConnectionOptions(opts({ host: '[::1]', port: 3015, https: false })).hostName).to.equal( 'http://[::1]:3015', From 192a662e0fe84647c749e3523536daa7a80e8883 Mon Sep 17 00:00:00 2001 From: Vu Anh Phung Date: Fri, 13 Nov 2026 06:34:19 +0000 Subject: [PATCH 8/8] test: adapt retry fixture to protocol-specific agents Signed-off-by: Vu Anh Phung --- .../connection/connections/ThriftHttpConnectionRetry.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/connection/connections/ThriftHttpConnectionRetry.test.ts b/tests/unit/connection/connections/ThriftHttpConnectionRetry.test.ts index 523b6245..4492f69b 100644 --- a/tests/unit/connection/connections/ThriftHttpConnectionRetry.test.ts +++ b/tests/unit/connection/connections/ThriftHttpConnectionRetry.test.ts @@ -98,7 +98,7 @@ class RefusingAgent extends http.Agent { async function createThriftConnection(port: number, agent: http.Agent): Promise { const context = new ClientContextStub({ retryDelayMin: 1, retryDelayMax: 2, retryMaxAttempts: 3 }); const httpConnection = new HttpConnection({ host: '127.0.0.1', port, path: '/' }, context); - httpConnection['agent'] = agent; + httpConnection['httpAgent'] = agent; return httpConnection.getThriftConnection(); }