Skip to content

Commit a29235c

Browse files
cathleenyIsaac
andauthored
feat(client): support SOCKS4/4a/5/5h proxies (#518)
## Summary Adds SOCKS proxy support to the kernel's shared HTTP client, so SQL requests, CloudFetch downloads and OAuth use the same tunnel. - `ProxyConfig.url`, the Rust ADBC driver's `adbc.databricks.proxy.url`, and `HTTPS_PROXY`/`ALL_PROXY` accept `socks4://`, `socks4a://`, `socks5://` and `socks5h://` next to `http(s)://`. `socks5`/`socks4` resolve the target host locally; `socks5h`/`socks4a` let the proxy resolve it. - An explicit URL with any other scheme, including bare `socks://`, fails with `InvalidArgument`. In environment variables, an unsupported scheme is ignored, so reqwest falls back to `ALL_PROXY` or connects directly. - SOCKS5 credentials use RFC 1929. When credentials are set, only username/password authentication is offered, and a rejected login fails at once without resending the credentials. - SOCKS4 ignores credentials: it has no password, and the user ID isn't sent yet, because hyper-util always sends an empty one. - An unreachable proxy or rejected credentials fail the request, with no direct fallback. TLS is verified against the target host through the tunnel. - Proxy credentials are redacted from proxy URLs in errors, logs and `Debug` output. `ProxyConfig` fields, defaults and the C ABI are unchanged. ## Dependencies - reqwest's `socks` feature is implemented by hyper-util, which is already a dependency, so there's no new crate. - hyper-util goes from 0.1.20 to 0.1.21 in the root, `napi/` and `pyo3/` lockfiles, because 0.1.20 writes a malformed SOCKS4/4a CONNECT request. `Cargo.toml` now requires `hyper-util >= 0.1.21`, so downstream lockfiles get the fix too. - The bump adds `base64 v0.23.1` (MIT OR Apache-2.0) next to 0.22.1. 0.1.21 also validates HTTP proxy CONNECT responses more strictly; the existing HTTP-proxy tests pass. - The Node.js, Go, Python and ODBC changes depend on this one and on its release: databricks/databricks-sql-nodejs#653, databricks/databricks-sql-go#607, databricks/databricks-sql-python#1082 and databricks/databricks-odbc#525. ## Validation - Tests on both TLS backends against a loopback SOCKS4/4a/5 fixture that records each handshake: - the scheme allow-list and the DNS mode per scheme; - RFC 1929 credentials; - dead proxies and refused CONNECTs failing closed; - bypass hosts, and environment proxies tested in a child process; - CloudFetch downloads through the tunnel, and TLS hostname verification through the tunnel; - redaction in errors and logs. - ADBC config tests cover the new schemes, and the C ABI smoke test covers SOCKS URLs. - `cargo fmt` and `clippy -D warnings` pass on both backends, as do the workspace, `napi/`, `pyo3/` and `adbc/` tests and `cargo deny`. - Live against staging DBSQL and Reyden warehouses through the fixture: - all four schemes, credentials, a dead proxy, `NO_PROXY` and `bypass_hosts`, and a 200,000-row CloudFetch query; - an HTTP CONNECT proxy still works; - `SELECT 1` through `socks5h` was within 50 ms of direct. This pull request and its description were written by Isaac. Signed-off-by: cat23123 <58714163+cat23123@users.noreply.github.com> Co-authored-by: cat23123 <58714163+cat23123@users.noreply.github.com> Co-authored-by: Isaac <no-reply@databricks.com>
1 parent a941656 commit a29235c

21 files changed

Lines changed: 1382 additions & 78 deletions

‎CONNECTION_PARAMETERS.md‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ by the struct it lives on. `ConnectionConfig` and `AuthConfig` are
1212
| [`AuthConfig`](#authconfig--authentication-required) | PAT / OAuth M2M / OAuth U2M *(required)* |
1313
| [`DefaultOpts`](#defaultopts--session-defaults-optional) | default catalog / schema, transactions |
1414
| [`HttpConfig`](#httpconfig--http-client-optional) | timeouts, connection pool, retries, headers |
15-
| [`ProxyConfig`](#proxyconfig--httphttps-proxy-optional) | HTTP/HTTPS proxy |
15+
| [`ProxyConfig`](#proxyconfig--httphttps-proxy-optional) | HTTP/HTTPS/SOCKS proxy |
1616
| [`TlsConfig`](#tlsconfig--tls--ssl-optional) | cert validation, custom CA, mTLS |
1717
| [`ResultConfig`](#resultconfig--result-handling-optional) | CloudFetch tuning, type rendering |
1818
| [`geospatial_result_representation`](#geospatial_result_representation--geospatial-result-encoding-optional) | GEOMETRY / GEOGRAPHY result encoding |
@@ -178,13 +178,30 @@ until implemented) and `External` (a caller-supplied `TokenProvider`).
178178
## `ProxyConfig` — HTTP/HTTPS proxy (optional)
179179

180180
When `url` is `None`, the kernel does not configure a proxy and
181-
`reqwest`'s standard `HTTP_PROXY` / `HTTPS_PROXY` / `NO_PROXY` env-var
182-
behaviour applies. Setting `url` **overrides** those env vars.
181+
`reqwest`'s standard `HTTP_PROXY` / `HTTPS_PROXY` / `ALL_PROXY` / `NO_PROXY`
182+
env-var behaviour applies. Those variables are used only with the `http`,
183+
`https`, `socks4`, `socks4a`, `socks5` and `socks5h` schemes. A value with
184+
any other scheme, such as bare `socks://`, is ignored, so the kernel falls
185+
back to `ALL_PROXY` or connects directly. Setting `url` **overrides** those
186+
env vars, and an unsupported scheme in `url` fails with `InvalidArgument`.
187+
188+
SOCKS: `socks5://` and `socks4://` resolve the target host locally;
189+
`socks5h://` and `socks4a://` let the proxy resolve it. SOCKS5 credentials
190+
use RFC 1929 username/password authentication. When credentials are set,
191+
that is the only method offered, so a proxy that allows only
192+
unauthenticated access rejects the connection. SOCKS4 has no password, and
193+
the kernel does not yet send a SOCKS4 user ID, so a SOCKS4 proxy that checks
194+
the user ID refuses the connection. An unreachable proxy fails the request
195+
after the retry policy's attempts; a rejected SOCKS5 authentication fails it
196+
at once, without resending the credentials. The kernel never falls back to a
197+
direct connection. TLS is still negotiated with, and verified against, the target
198+
host through the tunnel. Proxy URLs are logged and reported in errors with
199+
userinfo replaced by `***`.
183200

184201
| Field | Description | Default |
185202
|-------|-------------|---------|
186-
| `url` | Proxy endpoint, e.g. `http://proxy.corp.example.com:8080`. Must be `http://` or `https://`. | `None` (env-var behaviour) |
187-
| `username` | Proxy basic-auth username. Overrides credentials embedded in `url`. | `None` |
203+
| `url` | Proxy endpoint, e.g. `http://proxy.corp.example.com:8080` or `socks5h://proxy.corp.example.com:1080`. Scheme must be `http`, `https`, `socks4`, `socks4a`, `socks5` or `socks5h`. | `None` (env-var behaviour) |
204+
| `username` | Proxy basic-auth (SOCKS5: RFC 1929) username. Overrides credentials embedded in `url`. | `None` |
188205
| `password` | Proxy basic-auth password. Ignored unless `username` is set. Redacted in `Debug`. | `None` |
189206
| `bypass_hosts` | Comma-separated hosts/domains to bypass the proxy, e.g. `localhost,*.internal.corp,.example.com`. | `None` |
190207

‎Cargo.lock‎

Lines changed: 18 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,13 @@ arrow-ipc = "59"
7373
# (which `native-tls` pulls in on Linux) collides with Node's symbols at
7474
# module load time and segfaults the process before any Rust code runs.
7575
# `rustls` is pure Rust + ring and avoids the conflict entirely.
76-
reqwest = { version = "0.12", default-features = false, features = ["json", "gzip", "stream"] }
76+
#
77+
# `socks` adds SOCKS4/4a/5/5h proxies. From 0.12.28 it is implemented by
78+
# hyper-util (already a dependency) and pulls in no extra crate.
79+
reqwest = { version = "0.12.28", default-features = false, features = ["json", "gzip", "stream", "socks"] }
80+
# Enforce the SOCKS4/4a fix for consumers with their own lockfiles: hyper-util
81+
# 0.1.20 writes a malformed SOCKS4 CONNECT request.
82+
hyper-util = { version = "0.1.21", default-features = false }
7783
native-tls = { version = "0.2", optional = true }
7884
rustls = { version = "0.23.41", optional = true, default-features = false, features = ["ring", "std", "tls12"] }
7985
# Enforce TLS dependency minimums for consumers with their own lockfiles.

‎NEXT_CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ changes go in their respective binding's NEXT_CHANGELOG.
2020
`kernel_session_config_set_port` connect to `host:port`, so bindings no longer
2121
build it into the host. A non-default port is rejected with `InvalidArgument`
2222
if the host has credentials, a path, query or fragment.
23+
- `ProxyConfig.url`, the Rust ADBC driver's `adbc.databricks.proxy.url`,
24+
`HTTPS_PROXY` and `ALL_PROXY` accept SOCKS4 and SOCKS5 proxy URLs.
2325

2426
### Changed
2527

28+
- Proxy credentials are redacted from proxy URLs in errors, logs and `Debug`
29+
output.
30+
2631
### Updated
2732

2833
### Fixed

‎adbc/Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎adbc/src/config.rs‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,9 +640,13 @@ impl DatabaseConfig {
640640
));
641641
}
642642
if let Some(url) = &self.proxy.url {
643-
if !(url.starts_with("http://") || url.starts_with("https://")) {
643+
const SCHEMES: [&str; 6] = ["http", "https", "socks4", "socks4a", "socks5", "socks5h"];
644+
if !url
645+
.split_once("://")
646+
.is_some_and(|(scheme, _)| SCHEMES.contains(&scheme))
647+
{
644648
return Err(adbc_error(
645-
"proxy URL must use http or https",
649+
"proxy URL must use http, https, socks4, socks4a, socks5 or socks5h",
646650
Status::InvalidArguments,
647651
));
648652
}
@@ -1370,6 +1374,35 @@ mod tests {
13701374
assert!(config.validate().is_ok());
13711375
}
13721376

1377+
#[test]
1378+
fn proxy_url_accepts_http_and_socks_schemes() {
1379+
for (url, accepted) in [
1380+
("http://proxy.example.com:8080", true),
1381+
("https://proxy.example.com:8443", true),
1382+
("socks4://proxy.example.com:1080", true),
1383+
("socks4a://proxy.example.com:1080", true),
1384+
("socks5://proxy.example.com:1080", true),
1385+
("socks5h://proxy.example.com:1080", true),
1386+
("socks://proxy.example.com:1080", false),
1387+
("SOCKS5://proxy.example.com:1080", false),
1388+
("ftp://proxy.example.com:21", false),
1389+
("proxy.example.com:1080", false),
1390+
// SOCKS credentials still go through the username/password options.
1391+
("socks5h://user:secret@proxy.example.com:1080", false),
1392+
] {
1393+
let mut config = DatabaseConfig::default();
1394+
set(&mut config, "adbc.databricks.proxy.url", url);
1395+
match config.validate() {
1396+
Ok(_) => assert!(accepted, "{url} must be rejected"),
1397+
Err(error) => {
1398+
assert!(!accepted, "{url} must be accepted: {}", error.message);
1399+
assert_eq!(error.status, Status::InvalidArguments, "{url}");
1400+
assert!(!error.message.contains("secret"), "{url}");
1401+
}
1402+
}
1403+
}
1404+
}
1405+
13731406
#[test]
13741407
fn proxy_basic_auth_reaches_kernel_config() {
13751408
let mut config = DatabaseConfig::default();

‎include/databricks_kernel.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,10 @@ KernelStatusCode kernel_init_logging_callback(const char* level,
453453
* override.
454454
*/
455455

456-
/* Route traffic through an HTTP/HTTPS proxy. `url` is required (e.g.
457-
* "http://proxy.corp:8080"); `username` / `password` / `bypass_hosts`
458-
* (comma-separated) are optional — pass NULL to omit. */
456+
/* Route traffic through an HTTP/HTTPS or SOCKS proxy. `url` is required
457+
* (e.g. "http://proxy.corp:8080" or "socks5h://proxy.corp:1080"; schemes
458+
* http, https, socks4, socks4a, socks5, socks5h); `username` / `password` /
459+
* `bypass_hosts` (comma-separated) are optional — pass NULL to omit. */
459460
KernelStatusCode kernel_session_config_set_proxy(KernelSessionConfig* config, const char* url,
460461
const char* username, const char* password,
461462
const char* bypass_hosts);

‎napi/Cargo.lock‎

Lines changed: 18 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎napi/NEXT_CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ layout, or npm publishing. Pure-kernel changes go in the root
2222

2323
### Updated
2424

25+
- Bundle the kernel's SOCKS proxy support.
26+
2527
### Fixed
2628

2729
---

‎napi/index.d.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,14 @@ export interface HeaderEntry {
156156
value: string
157157
}
158158
/**
159-
* Programmatic HTTP/HTTPS proxy configuration, mirroring the kernel's
159+
* Programmatic HTTP/HTTPS/SOCKS proxy configuration, mirroring the kernel's
160160
* internal [`ProxyConfig`]. Supplied as a structured object rather than a
161161
* flattened URL so credentials never have to be percent-encoded into the URL
162162
* and the bypass-host list can be expressed.
163163
*
164164
* - `url` — proxy endpoint, e.g. `"http://proxy.corp.example.com:8080"`. Must
165-
* use the `http://` or `https://` scheme.
165+
* use the `http://`, `https://`, `socks4://`, `socks4a://`, `socks5://` or
166+
* `socks5h://` scheme.
166167
* - `username` / `password` — optional proxy basic-auth, applied via
167168
* `reqwest`'s `Proxy::basic_auth` (not embedded in the URL).
168169
* - `bypassHosts` — optional comma-separated host/domain list that should

0 commit comments

Comments
 (0)