Skip to content

feat(auth): accept a list of candidate redirect ports for U2M browser flow (PECOBLR-4144) - #257

Merged
eric-wang-1990 merged 20 commits into
mainfrom
eric-wang/pecoblr-4144-u2m-port-range
Aug 18, 2026
Merged

eric-wang-1990 merged 20 commits into
mainfrom
eric-wang/pecoblr-4144-u2m-port-range

Conversation

@eric-wang-1990

@eric-wang-1990 eric-wang-1990 commented Aug 18, 2026 •

Copy link
Copy Markdown
Collaborator

What

Let the U2M browser flow accept an ordered list of candidate redirect ports and bind the first free one, instead of a single fixed port. (The ports are a discrete list, not a contiguous range — e.g. the databricks-sql-python app registers http://localhost:8020 and http://localhost:8025.)

Why

The U2M flow bound exactly one callback port (CallbackServer::new). If that port was occupied — a stale prior listener, or a second concurrent connection — the browser flow failed outright. The standalone Thrift connector tolerates this by trying the localhost redirect ports registered for its OAuth app and binding the first free one; the kernel's single-port API could not, so the connector (PECOBLR-4040) had to pin a single port (8020) and lose the fallback. Surfaced by the review of connector PR databricks/databricks-sql-python#914.

How

  • src/auth/oauth/callback.rs — new CallbackServer::new_in_range(ports): tries the candidate ports in order, skips AddrInUse, binds the first that succeeds, and returns a clear error if all are busy. redirect_uri() already derives from the bound port, so the OAuth redirect_uri matches whichever port won. new(port) (single-port) is removed — it was dead once the flow moved to this.
  • Single-field model: AuthConfig::OAuthU2M.redirect_port: Option<u16> becomes redirect_ports: Option<Vec<u16>> (one field, no precedence rule), and oauth_u2m() takes the list. resolve_u2m_bundle uses a non-empty list verbatim, else the 8030 default. AuthorizationCodeProvider carries callback_ports: Vec<u16>.
  • Single-port callers stay backward-compatible by wrapping their one port as a one-element list — no external API break:
    • c_abi / napi keep their u16 FFI and forward Some(vec![port]).
    • pyo3 exposes only redirect_ports (a Python list[int]); a single port is [port]. The redundant singular redirect_port kwarg was removed per review.

Candidate ports must be caller-supplied — each must be a registered redirect URI of the OAuth app. An automatic "+N" would be wrong: the default databricks-sql-connector app registers only 8030, and the databricks-sql-python app registers a specific discrete set (8020, 8025), not a contiguous range.

Testing

TDD. Unit tests: callback (skip-busy binds next, all-busy errors, single-port/empty cases), session::resolve_u2m_bundle (list verbatim; empty/None → default), config (constructor carries the list), pyo3 auth (list forwarded; single port → one-element list; empty/zero rejected), and napi (single port → Some(vec![port])).

  • cargo test --lib — 799 passed; pyo3 --lib — 16 passed; napi cargo check --tests — clean; fmt --check + clippy --all-targets clean (root + pyo3 + napi); doc tests pass.
  • Live E2E (tests/v0_session_e2e.rs::open_with_oauth_u2m_falls_back_past_busy_port, #[ignore], gated by DATABRICKS_RUN_U2M=1): occupies 8020, opens U2M as databricks-sql-python with redirect_ports=[8020, 8025], and the browser redirects to :8025. Verified passing against a live Azure warehouse — session opened + closed with 8020 blocked, proving the fallback end-to-end.

Scope / follow-up

Kernel-only. The connector-side change (databricks-sql-python forwarding its registered port list on the use_kernel path) is a separate PR. Related: the connector's PYSQL_OAUTH_REDIRECT_PORT_RANGE constant is misaligned with the app's actual registered ports — tracked in PECOBLR-4154.

This pull request and its description were written by Isaac.

…LR-4144)

The U2M browser flow bound exactly one callback port
(CallbackServer::new), so if that port was occupied — a stale prior
listener, or a second concurrent connection — the flow failed. The
Thrift connector tolerates this by iterating its registered redirect-URI
range (databricks-sql-python -> 8020-8024) and binding the first free
port; the kernel's single-port API could not.

Add CallbackServer::new_in_range: try candidate ports in order, skip
AddrInUse, bind the first that succeeds, and surface a clear error if all
are busy. redirect_uri() already derives from the bound port, so the
OAuth redirect_uri matches automatically.

Thread a caller-supplied port list through additively: a new
AuthConfig::OAuthU2M.redirect_ports (Option<Vec<u16>>) plus a
with_redirect_ports() builder — no signature change to oauth_u2m(), so
c_abi and napi keep their single-port APIs untouched. resolve_u2m_bundle
picks a non-empty redirect_ports over the single redirect_port over the
default. Only pyo3 exposes the list (redirect_ports on Session.__new__),
the one consumer registered for a multi-port app; empty / zero-containing
lists are rejected like redirect_port=0.

Candidate ports must be caller-supplied because they must be registered
redirect URIs of the OAuth app — an automatic +N would not be registered
for the default databricks-sql-connector app (8030 only).

Co-authored-by: Isaac
Signed-off-by: eric-wang-1990 <e.wang@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No issues identified by the review bot.

Address review preference: instead of the additive redirect_port +
redirect_ports pair, collapse to one field. AuthConfig::OAuthU2M now
carries redirect_ports: Option<Vec<u16>> only, oauth_u2m() takes the
list, and the with_redirect_ports() builder is removed.

Single-port callers stay fully backward-compatible by wrapping their one
port as a one-element list: c_abi and napi keep their u16 FFI and forward
Some(vec![port]); pyo3 keeps the redirect_port Python kwarg (folded into
the list) alongside the new redirect_ports. resolve_u2m_bundle takes just
the list (non-empty → used verbatim; else the 8030 default).

No behavior change vs the previous commit — single ports still resolve to
exactly that port; only the internal shape is simplified.

Co-authored-by: Isaac
Signed-off-by: eric-wang-1990 <e.wang@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low

Looks good — clean, backward-compatible port-range support with thorough test coverage across kernel, pyo3, napi, and C ABI. new_in_range correctly skips AddrInUse, surfaces other bind errors immediately, and redirect_uri() reflects the bound port. One low note about silent precedence when both redirect_port and redirect_ports are set.

Comment thread pyo3/src/auth.rs Resolved
@eric-wang-1990 eric-wang-1990 added the engineer-bot Apply to an issue to trigger the engineer-bot; on a PR to enable engineer follow-up label Aug 18, 2026
The redirect_port -> redirect_ports: Option<Vec<u16>> change also touches
the integration-test targets under tests/, which cargo test --lib does
not compile (only the CI unit+doc/e2e/tls jobs do):

- tests/oauth_u2m_integration.rs: new_with_full_config now takes
  Vec<u16>; wrap each single port as vec![port] (4 sites).
- tests/v0_session_e2e.rs: AuthConfig::OAuthU2M literal field
  redirect_port -> redirect_ports.

Fixes the failing Build (tls-rustls) / Clippy / Unit+doc / Coverage /
v0 E2E checks, which all stemmed from these test targets failing to
compile.

Co-authored-by: Isaac
Signed-off-by: eric-wang-1990 <e.wang@databricks.com>
peco-engineer-bot[bot] and others added 3 commits August 18, 2026 02:11
Addresses:
  - #3800378252 at pyo3/src/auth.rs:55

Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>
Address review comments on PR #257:

- pyo3: remove the singular `redirect_port` from OAuthParams and the
  Session.__new__ kwarg. It duplicated `redirect_ports` and created the
  "both set → which wins" ambiguity the review bot flagged. pyo3 now
  exposes only `redirect_ports`; a single port is `[port]`. The
  databricks-sql-python default-port pinning (8020 under the default
  client) now yields a one-element list, and the redirect_port=0 guard is
  subsumed by the existing empty/zero redirect_ports checks.

- callback: remove CallbackServer::new (single port). It was dead after
  the U2M flow moved to new_in_range; the doc example and the six new(0)
  tests now use new_in_range(&[0]) (a one-element range is equivalent).

No behavior change: a single port still resolves to exactly that port.

Co-authored-by: Isaac
Signed-off-by: eric-wang-1990 <e.wang@databricks.com>
Interactive #[ignore] test proving the fix end-to-end: occupies port
8020, opens a U2M session as databricks-sql-python with
redirect_ports=[8020, 8025], and expects the browser to redirect to
http://localhost:8025 (the callback server skips the busy 8020 and binds
8025). Before this change the single-port API would have failed when
8020 was occupied.

Gated by DATABRICKS_RUN_U2M=1 like the existing U2M E2E test, so CI
without a browser/credentials skips it.

Co-authored-by: Isaac
Signed-off-by: eric-wang-1990 <e.wang@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Medium

Solid, well-tested refactor from a single redirect_port to a candidate redirect_ports list — new_in_range skip-busy/all-busy logic is correct, redirect_uri() derives from the bound port, and C ABI / napi keep their u16 FFI backward-compatible by wrapping in a one-element vec. One medium concern: the pyo3 Python redirect_port kwarg was renamed (not aliased), contradicting the PR description's backward-compat claim and breaking existing Python callers.

Comment thread pyo3/src/session.rs Resolved
peco-engineer-bot[bot] and others added 2 commits August 18, 2026 02:29
Addresses:
  - #3800442505 at pyo3/src/session.rs:123

Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low

Looks good — solid implementation of the busy-port fallback with clean error handling (skip AddrInUse, surface other bind errors immediately, error when all busy), consistent one-element-list backward compat across pyo3/napi/c_abi, and thorough test coverage. One low doc-accuracy nit: three comments describe the app's redirect ports as a contiguous 8020‑8024 range, contradicting the PR's own discrete-set premise (8020, 8025). Minor aside: the PR description says the singular redirect_port pyo3 kwarg was removed, but pyo3/src/session.rs retains it (reconciled via fold_redirect_ports) — the code behavior is fine, just note the description mismatch.

Comment thread src/config.rs Resolved
Addresses:
  - #3800506002 at src/config.rs:339

Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Nit

Looks good — a clean, backward-compatible refactor from a single U2M redirect port to an ordered candidate list with busy-port fallback (CallbackServer::new_in_range). The change is well-tested (skip-busy / all-busy / empty / single-port cases, fold_redirect_ports reconciliation, resolve_u2m_bundle list-vs-default, plus an #[ignore] live E2E), and the old redirect_port=0 rejection is correctly preserved via the new ports.contains(&0) check. Only nit: a few doc comments (src/config.rs:293/:324, CONNECTION_PARAMETERS.md:59) still reference the pre-rename singular redirect_port. Note also the PR description says the singular redirect_port pyo3 kwarg "was removed," but pyo3/src/session.rs intentionally retains it for backward compatibility — the code is correct; the description wording is just slightly off.

Other findings

  • ⚪ Nit — Two doc comments in config.rs still reference the singular field name after this PR renamed redirect_port → redirect_ports:
  • src/config.rs:293: /// - U2M \redirect_port` defaults to `8030`.`
  • src/config.rs:324: /// \redirect_port`, opens the user's browser ...`

The struct field is now redirect_ports, so these enum/module-level docs point at a name that no longer exists. CONNECTION_PARAMETERS.md:59 similarly documents only the singular redirect_port param and doesn't mention the new redirect_ports list. These are outside the changed hunks so they aren't inline-anchorable, but a quick doc sweep would keep them consistent with the rename. Non-blocking.

Consolidated cleanup on top of the bot's latest #257 state, per maintainer
decisions:

- Rename CallbackServer::new_in_range -> new_first_free (it takes a discrete
  candidate list, not a contiguous range); updates the u2m call site and
  callback tests.
- Drop the pyo3 redirect_port kwarg the bot re-added; pyo3 exposes only
  redirect_ports (a single port is [port]). Removes fold_redirect_ports and
  its tests.
- E2E fallback test comment: state the real reason the fallback works —
  Databricks in-house OAuth ignores the loopback redirect port per RFC 8252
  (any localhost port is accepted), not that specific ports are "registered".
  (Confirmed live + server-side; PECOBLR-4154 closed not-a-bug.)

Co-authored-by: Isaac
Signed-off-by: eric-wang-1990 <e.wang@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low

Solid, well-tested refactor from a single U2M callback port to an ordered candidate list — new_first_free correctly skips AddrInUse, surfaces other bind errors immediately, and redirect_uri() tracks the bound port; all bindings (c_abi/napi/pyo3/session/u2m) are updated consistently with backward-compatible one-element wrapping. One low-severity doc-drift issue: the renamed public/pyo3 redirect_ports field still shows as the old singular redirect_port in CONNECTION_PARAMETERS.md and two src/config.rs doc comments. (src/auth/config.rs's redirect_port is the unrelated legacy ADBC struct, not a missed call site.)

Comment thread src/config.rs Resolved
Addresses:
  - #3800804591 at src/config.rs:337

Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low

Looks good — a clean single-port → candidate-list generalization with thorough unit/e2e coverage and backward-compatible FFI wrapping. One low-severity doc inconsistency: the pyo3 redirect_ports field comment claims an empty list defaults, but the pyo3 validator rejects empty lists (only None defaults).

Comment thread pyo3/src/auth.rs Resolved
Addresses:
  - #3801068476 at pyo3/src/auth.rs:59

Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low

Looks good — clean, well-tested refactor from a single U2M redirect port to an ordered candidate list with first-free binding. new_first_free correctly skips AddrInUse, surfaces other bind errors immediately, and errors when all candidates are busy; resolve_u2m_bundle and the pyo3/C-ABI/napi boundaries all preserve backward compatibility, with solid unit + E2E coverage. One low-severity consistency note: napi doesn't guard a 0 port the way the C ABI and pyo3 do.

Comment thread napi/src/database.rs Resolved
Addresses:
  - #3801088886 at napi/src/database.rs:373

Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No issues identified by the review bot.

Previously the bare kernel default was a single port (8030), so only
callers that pass an explicit port list (Python) got the busy-port
fallback. Default databricks-sql-connector consumers (Node, Go, ODBC,
bare Rust) failed outright if 8030 was occupied.

Default redirect_ports to [8030, 8031, 8032, 8033, 8034] so they get the
same first-free-port fallback. Safe without per-port app registration:
databricks-sql-connector uses Databricks in-house OAuth, which drops the
loopback port before comparison (RFC 8252), so any http://localhost:{port}
is accepted; 8030 stays first so the common case is unchanged. Comment
added explaining why.

Co-authored-by: Isaac
Signed-off-by: eric-wang-1990 <e.wang@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Medium

Looks good — a clean, thoroughly-tested refactor from a single U2M redirect port to an ordered candidate list, with backward-compatible single-port forwarding at every binding (C ABI, N-API, pyo3). One medium doc/code mismatch: the docs still describe the default as a single 8030 while the kernel default is now the 8030–8034 window.

Comment thread CONNECTION_PARAMETERS.md Resolved
Addresses:
  - #3801541682 at CONNECTION_PARAMETERS.md:59

Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low · 1 Nit

Looks good — a clean, well-tested rename of the single U2M redirect_port to an ordered redirect_ports candidate list with busy-port fallback. FFI callers (c_abi, napi) correctly wrap their single port and map 0 → None; pyo3 validation and unit/e2e coverage are thorough. Two low/nit items: the default [8030..8034] window is applied to any client when ports are omitted even though its RFC-8252 port-drop safety only holds for the default client (F1), and two stale redirect_port doc references (F2).

Other findings

  • ⚪ Nit — Stale doc references to the renamed field: the resolve_u2m_bundle doc comment still says redirect_port (singular) at src/session.rs:1062 and :1093 after the field/param became redirect_ports. Minor — update the prose to match the new plural list API for consistency with the rest of the diff.

Comment thread src/session.rs Resolved
peco-engineer-bot[bot] added 2 commits August 18, 2026 06:36
Addresses:
  - #3801587000 at src/session.rs:1104

Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>
Addresses:
  - #3801587000 at src/session.rs:1105

Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 2 Low

Solid, well-tested rename from single redirect_port to an ordered redirect_ports candidate list with busy-port fallback; FFI/pyo3 back-compat and the new bind-first-free logic look correct, and coverage (unit + wiremock + ignored E2E) is good. Two low-severity notes: (1) the contiguous default window is applied to custom clients too, where the RFC-8252 port-drop assumption justifying it doesn't hold, so a busy first port can silently bind an unregistered port; (2) a couple of stale redirect_port references remain in the resolve_u2m_bundle doc comment.

Comment thread src/session.rs Resolved
Comment thread src/session.rs Resolved
Update the two remaining stale singular references in session.rs (the
DEFAULT_U2M_CLIENT_ID coupled-bundle doc and the resolve_u2m_bundle doc)
to the plural redirect_ports, matching the renamed field/API. Doc-only.

Co-authored-by: Isaac
Signed-off-by: eric-wang-1990 <e.wang@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: 1 Low

Looks good — clean, well-tested migration from a single redirect_port to an ordered redirect_ports candidate list with first-free binding. The field rename is threaded consistently through config, session, callback, pyo3, napi, and C ABI; single-port bindings correctly wrap as one-element lists; degenerate shapes (empty list / 0 entry) are rejected at the pyo3 boundary and mapped to None at the FFI boundaries as documented; and redirect_uri correctly derives from the bound port. One low-severity note about the load-bearing (CI-untestable) RFC 8252 port-normalization assumption behind the default-window expansion.

Comment thread src/session.rs Resolved

This branch was previously deployed

1 inactive deployment
azure-prod — 6b0bc8a3 Deployed Aug 18, 2026 by oss-engineer-bot[bot] via followup #476
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-assisted engineer-bot Apply to an issue to trigger the engineer-bot; on a PR to enable engineer follow-up

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants