diff --git a/.env.example b/.env.example index 696f3219..648713bd 100644 --- a/.env.example +++ b/.env.example @@ -41,6 +41,16 @@ ENCRYPTION_KEY=__SECRET_HEX_32__ # Larger values effectively lengthen every token's exp window — fix # time sync first, this knob second. # JWT_LEEWAY_SECS=10 + +# Extra ID-token audiences to trust, comma-separated. Leave unset unless +# your IdP puts something besides the client ID in `aud` — Zitadel, for +# one, includes the parent project ID. Unset means the stock +# `openidconnect` verifier runs untouched. Set, it widens exactly one +# check: the extra audiences on a multi-audience token are matched +# against this exact-string allowlist (no wildcards, no prefixes). The +# client ID must still appear in `aud` regardless, and any token with +# more than one audience must carry `azp` equal to the client ID. +# OIDC_ADDITIONAL_TRUSTED_AUDIENCES=my-project-id,another-trusted-aud SERVER_HOST=0.0.0.0 GATEWAY_PORT=3000 CONSOLE_PORT=3001 diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 00000000..1db60636 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,37 @@ + + +## What this changes + + + +## Why + + + +## How it was verified + + + +## Notes for review + + diff --git a/.github/workflows/pr-base-guard.yml b/.github/workflows/pr-base-guard.yml new file mode 100644 index 00000000..1c2b8d72 --- /dev/null +++ b/.github/workflows/pr-base-guard.yml @@ -0,0 +1,49 @@ +# Nudge PRs that were opened against `main` toward `dev`. +# +# GitHub pre-fills a new PR's base with the repo's default branch, which +# here is `main` — the release-only line (see docs/operations/release.md). +# A contributor following the UI therefore lands on the wrong branch by +# default, and the first signal they get today is a human asking them to +# retarget. This says it immediately instead. +# +# `pull_request_target` rather than `pull_request` because a PR from a +# fork gets a read-only token under the latter, so it could not comment +# — and a fork PR is exactly the case this exists for. That trigger runs +# with the base repository's token, so this workflow **must never check +# out or execute PR code**. It doesn't: no actions/checkout, no build, +# nothing but one API call against metadata GitHub already gave us. +name: PR base guard + +on: + pull_request_target: + types: [opened, reopened] + branches: [main] + +permissions: + pull-requests: write + +jobs: + nudge: + runs-on: ubuntu-latest + # The two legitimate ways to target `main`: the release PR (`dev` -> + # `main`) and a hotfix branch. Everything else is a mis-targeted + # contribution. + if: >- + github.event.pull_request.head.ref != 'dev' && + !startsWith(github.event.pull_request.head.ref, 'hotfix/') && + !startsWith(github.event.pull_request.head.ref, 'release/') + steps: + - name: Ask for a retarget to dev + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR: ${{ github.event.pull_request.number }} + REPO: ${{ github.repository }} + run: | + gh pr comment "$PR" --repo "$REPO" --body \ + 'Thanks for the PR. One thing before review: **this targets `main`, and it should target `dev`.** + + `main` is a release-only line — branch-protected, linear history, and every commit on it is a tagged release snapshot. Routine work lands on `dev` and reaches `main` with the next release. GitHub pre-filled `main` here because it is the default branch, so this is our defaults being unhelpful rather than anything you did wrong. + + No need to close and reopen: click **Edit** next to the PR title and change the base branch to `dev`. The commits and the discussion carry over. + + See [CONTRIBUTING.md](../blob/main/CONTRIBUTING.md) and [docs/operations/release.md](../blob/main/docs/operations/release.md) for the branch contract.' diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b9b20fb..1d545317 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,87 @@ target. ## [Unreleased] +## [1.0.2] — 2026-09-13 + +The shared gateway layer moves out into its own repository, and OIDC +learns to accept an ID token whose `aud` carries more than the client +ID. Mostly a fix release otherwise — the deploy and auth items below are +the ones worth reading before you upgrade. + +### Added + +- **`OIDC_ADDITIONAL_TRUSTED_AUDIENCES`** — a comma-separated allowlist + of extra ID-token audiences to trust. Some IdPs put something besides + the client ID in `aud`; Zitadel includes the parent project ID, and + `openidconnect`'s stock verifier rejects every non-client-ID audience, + so login failed outright. Leave it unset and verification is + byte-for-byte what it was. Set, it widens exactly one check: the extra + audiences on a multi-audience token are matched against this list as + exact strings — no wildcards, no prefixes. The client ID must still + appear in `aud` regardless. Documented in `.env.example`. Thanks to + @DaniW42 for the report and the patch (#12). +- **Automatic upstream protocol detection** — the gateway learns an + upstream's dialect and relearns it when the upstream changes, instead + of relying on a static guess. Protocol is now a property of the route. +- **Reverse-proxy identification** — the server recognizes its own + reverse proxy, which is what makes the dashboard WebSocket and the API + docs work behind nginx. + +### Changed + +- **`azp` is validated whenever it is present**, per OIDC Core 3.1.3.7 + step 5, which has no audience-count precondition. Previously it was + only checked when a multi-audience token made it mandatory, so a + single-audience token naming this client with `azp` pointing at a + *different* client was accepted — the IdP stating plainly that the + token was authorized for somebody else. **This can reject a token that + 1.0.1 accepted.** If your IdP sets `azp` to something other than your + client ID, logins will start failing and the message will say so; + that is the IdP to fix, not this setting. +- **The shared gateway layer now comes from + [ThinkWatch-Core](https://github.com/ThinkWatchProject/ThinkWatch-Core)** + as a git dependency rather than living in this tree. Six crates — + types, protocol, provider, resilience, crypto, and the JSON secret + envelope — are one implementation shared with the desktop edition + instead of two copies drifting apart. No runtime or API change; it + affects you only if you build from source, where the build now needs + network access to resolve that dependency. The published images are + unaffected. +- **Stored secrets are redacted on read and decrypted on use.** Provider + credentials no longer travel in plaintext through code paths that + merely display or list them. + +### Fixed + +- **Deploy** — nginx broke the API docs three separate ways; the + dashboard WebSocket wasn't proxied; the prod stack's healthchecks were + wrong; and the dev stack could overwrite production's ClickHouse + credentials. The last one is the reason to read this list. +- **Auth** — the console logged itself out after every token refresh. + Rate-limit counters could be created without an expiry and sit in + Redis forever. +- **Models** — the gateway no longer offers models the upstream refuses, + no longer imports models it won't serve, and reports every dialect + that was refused rather than only the last. Provider `base_url` + trailing slashes are normalized. +- **Analytics** — spend is attributed to a person, not a UUID. +- **Audit** — a syslog forwarder formatting fix that had CI red on + clippy 1.98 (#11, thanks @DaniW42). Two more instances of the same + lint, plus a `result_large_err` false positive in the MCP lifecycle + stages, measured rather than boxed: the `Ok` variant is 296 bytes + against the `Err`'s 152, so boxing saves nothing and adds an + allocation. +- **UI** — the brand mark stays inside the collapsed sidebar rail. + +### Contributing + +`CONTRIBUTING.md` and a PR template now state the branch contract that +had only lived in `docs/operations/release.md`: routine work targets +`dev`, and `main` is the release line. A workflow comments on PRs opened +against `main` from anything other than `dev` or a `hotfix/*` branch, +because GitHub pre-fills the base with the default branch and walks +contributors into it. + ### Added - _(nothing yet)_ @@ -139,7 +220,8 @@ unreleased builds should: stop the gateway, run `db/schema.sql` against PostgreSQL, restart against this tag. The schema is idempotent end-to-end, so the apply is safe to repeat. -[Unreleased]: https://github.com/ThinkWatchProject/ThinkWatch/compare/v1.0.1...HEAD +[Unreleased]: https://github.com/ThinkWatchProject/ThinkWatch/compare/v1.0.2...HEAD +[1.0.2]: https://github.com/ThinkWatchProject/ThinkWatch/releases/tag/v1.0.2 [1.0.1]: https://github.com/ThinkWatchProject/ThinkWatch/releases/tag/v1.0.1 [1.0.0]: https://github.com/ThinkWatchProject/ThinkWatch/releases/tag/v1.0.0 [0.5.0]: https://github.com/ThinkWatchProject/ThinkWatch/releases/tag/v0.5.0 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..642e9b59 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,66 @@ +# Contributing to ThinkWatch + +Thanks for taking the time. This file covers the two things that most +often need a second round on a PR: **which branch to target** and **what +"verified" means here**. + +## Open PRs against `dev` + +```bash +gh pr create --base dev --head your-branch +``` + +`main` is a release-only line: branch-protected, linear history, no +force-push, and every commit on it is a release snapshot that gets +tagged immediately. `dev` is where everything routine lands — features, +fixes, refactors, dependency bumps. + +GitHub pre-fills a new PR's base with the repo's default branch, which +is `main`, so **the default is not the one you want**. If you've already +opened against `main`, no need to close anything: click *Edit* next to +the PR title and change the base to `dev`. A bot will remind you. + +Two exceptions, both maintainer-only: the release PR (`dev` → `main`, +titled `release: vX.Y.Z`) and a `hotfix/*` branch when `dev` has +diverged too far to carry a fix cleanly. The full branch contract is in +[docs/operations/release.md](docs/operations/release.md). + +Automation follows the same rule — Renovate is pinned to `dev` via +`baseBranches` in `renovate.json`. + +## Commit messages + +Conventional Commits (`fix(scope): subject`), because `git-cliff` +renders the CHANGELOG from them and `chore(release):` is the prefix it +skips. Write them in **English**; this is a public repository and the +history is documentation. + +Say *why* in the body, not just *what* — the diff already shows what +changed. A commit that explains the reasoning behind a non-obvious +choice saves the next person from re-deriving it or "fixing" it back. + +## What CI will check + +```bash +make precommit +``` + +That is the same gate CI runs, and it exits 0 on a clean tree. Two +required status checks: `Rust Check & Test` and `Frontend Build`. + +Warnings are errors — `clippy -D warnings` across the workspace, all +targets. Note that the toolchain is `stable`, so a newer stable than +your local one can surface lints you can't see; `rustup update stable` +before blaming CI. + +## Security-relevant changes + +If a change touches authentication, the gateway data path, audit rows, +redaction, or anything that handles a key or a token, the PR description +should say how you convinced yourself it doesn't leak a credential or +weaken an existing check. "The tests pass" doesn't answer that — the +tests didn't know about the hole either. + +Please don't open a public PR or issue for a vulnerability. See +[SECURITY.md](SECURITY.md) if present, or contact the maintainer +directly. diff --git a/Cargo.lock b/Cargo.lock index 7f8abce3..73ae3eaf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1779,7 +1779,7 @@ dependencies = [ "libc", "percent-encoding", "pin-project-lite", - "socket2 0.6.3", + "socket2 0.5.10", "system-configuration", "tokio", "tower-service", @@ -2731,7 +2731,7 @@ dependencies = [ "quinn-udp", "rustc-hash", "rustls", - "socket2 0.6.3", + "socket2 0.5.10", "thiserror 2.0.18", "tokio", "tracing", @@ -2769,7 +2769,7 @@ dependencies = [ "cfg_aliases", "libc", "once_cell", - "socket2 0.6.3", + "socket2 0.5.10", "tracing", "windows-sys 0.60.2", ] @@ -4022,7 +4022,7 @@ checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" [[package]] name = "think-watch-auth" -version = "1.0.1" +version = "1.0.2" dependencies = [ "anyhow", "argon2", @@ -4052,7 +4052,7 @@ dependencies = [ [[package]] name = "think-watch-common" -version = "1.0.1" +version = "1.0.2" dependencies = [ "anyhow", "async-trait", @@ -4089,7 +4089,7 @@ dependencies = [ [[package]] name = "think-watch-gateway" -version = "1.0.1" +version = "1.0.2" dependencies = [ "anyhow", "arc-swap", @@ -4131,7 +4131,7 @@ dependencies = [ [[package]] name = "think-watch-mcp-gateway" -version = "1.0.1" +version = "1.0.2" dependencies = [ "anyhow", "arc-swap", @@ -4159,7 +4159,7 @@ dependencies = [ [[package]] name = "think-watch-server" -version = "1.0.1" +version = "1.0.2" dependencies = [ "anyhow", "arc-swap", @@ -4206,7 +4206,7 @@ dependencies = [ [[package]] name = "think-watch-test-support" -version = "1.0.1" +version = "1.0.2" dependencies = [ "anyhow", "async-stream", @@ -4655,7 +4655,7 @@ dependencies = [ [[package]] name = "tw-crypto" version = "0.1.0" -source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?branch=main#3fe60e3f5412b88e46e400c5c3148c669de4898a" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?branch=main#6d27617d95c8f3bdf97976b4db1546987f4dbea4" dependencies = [ "aes-gcm", "anyhow", @@ -4668,7 +4668,7 @@ dependencies = [ [[package]] name = "tw-protocol" version = "0.1.0" -source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?branch=main#3fe60e3f5412b88e46e400c5c3148c669de4898a" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?branch=main#6d27617d95c8f3bdf97976b4db1546987f4dbea4" dependencies = [ "bytes", "futures", @@ -4681,7 +4681,7 @@ dependencies = [ [[package]] name = "tw-provider" version = "0.1.0" -source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?branch=main#3fe60e3f5412b88e46e400c5c3148c669de4898a" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?branch=main#6d27617d95c8f3bdf97976b4db1546987f4dbea4" dependencies = [ "async-stream", "aws-credential-types", @@ -4707,7 +4707,7 @@ dependencies = [ [[package]] name = "tw-resil" version = "0.1.0" -source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?branch=main#3fe60e3f5412b88e46e400c5c3148c669de4898a" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?branch=main#6d27617d95c8f3bdf97976b4db1546987f4dbea4" dependencies = [ "futures", "metrics", @@ -4721,7 +4721,7 @@ dependencies = [ [[package]] name = "tw-types" version = "0.1.0" -source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?branch=main#3fe60e3f5412b88e46e400c5c3148c669de4898a" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?branch=main#6d27617d95c8f3bdf97976b4db1546987f4dbea4" dependencies = [ "serde", "serde_json", @@ -5122,7 +5122,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.48.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 15e1b531..39cb7c0e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ members = [ ] [workspace.package] -version = "1.0.1" +version = "1.0.2" edition = "2024" # Pin the MSRV to the first stable rustc that ships edition 2024 (1.85, # released 2025-02-20). Without this, contributors on older toolchains diff --git a/crates/auth/src/oidc.rs b/crates/auth/src/oidc.rs index c95fb026..0631b077 100644 --- a/crates/auth/src/oidc.rs +++ b/crates/auth/src/oidc.rs @@ -300,25 +300,43 @@ fn parse_additional_trusted_audiences(value: &str) -> HashSet { .collect() } -/// Require `azp == client_id` whenever the token doesn't unambiguously -/// prove it was minted for us: either it carries more than one audience, -/// or our own client ID isn't among the audiences at all (the latter can -/// only happen when the additional-audiences allowlist above accepted a -/// single-audience token whose sole audience is a *trusted* value other -/// than our client ID — e.g. Zitadel's project ID). In both cases `azp` -/// is the only remaining signal that the IdP actually issued this token -/// for this client, per OIDC Core 3.1.3.7. +/// Enforce OIDC Core 3.1.3.7's `azp` steps, which `openidconnect` leaves +/// to the caller — its own implementation of steps 4 and 5 is commented +/// out upstream, with a note preferring that clients supply the check. +/// +/// Step 5 (`azp`, if present, must be our client ID) is checked +/// unconditionally. Step 4 (a multi-audience token should carry `azp`) +/// makes a *missing* `azp` fatal only when the audiences don't already +/// prove sole intent for us. +/// +/// `client_id_in_audiences` is false-only in theory: `IdTokenVerifier` +/// enforces `client_id ∈ aud` in its `aud_match_required` block *before* +/// `other_aud_verifier_fn` is consulted, so a token that omits our client +/// ID never reaches this function no matter what the allowlist says. It +/// stays a parameter as defense in depth against that guarantee changing +/// (an `aud_match_required(false)`, or an upstream rewrite) — it is not +/// load-bearing today, and the test covering it documents a state the +/// crate currently makes unreachable. fn validate_authorized_party( audience_count: usize, client_id_in_audiences: bool, authorized_party: Option<&str>, client_id: &str, ) -> anyhow::Result<()> { - let azp_required = audience_count > 1 || !client_id_in_audiences; - if azp_required && authorized_party != Some(client_id) { - anyhow::bail!("ID token audiences require azp to match the client ID"); + match authorized_party { + // Step 5: present but pointing at somebody else. Nothing about + // the audience count can excuse this — the IdP is telling us the + // token was authorized for a different client. + Some(party) if party != client_id => { + anyhow::bail!("ID token azp does not match the client ID") + } + // Step 4: absent, and the audiences alone don't establish that + // this token was minted for us. + None if audience_count > 1 || !client_id_in_audiences => { + anyhow::bail!("ID token audiences require azp to match the client ID") + } + _ => Ok(()), } - Ok(()) } /// Pull the JSON payload (middle segment) out of a signed ID token. @@ -359,6 +377,22 @@ mod tests { ); } + #[test] + fn mismatched_authorized_party_is_rejected_even_with_a_single_audience() { + // OIDC Core step 5 has no audience-count precondition. aud = + // [thinkwatch-client] with azp = other-client means the IdP + // authorized a different client, and stock `openidconnect` would + // let it through because its azp check is commented out. + assert!( + validate_authorized_party(1, true, Some("other-client"), "thinkwatch-client").is_err() + ); + // The same shape with a matching azp stays fine. + assert!( + validate_authorized_party(1, true, Some("thinkwatch-client"), "thinkwatch-client") + .is_ok() + ); + } + #[test] fn single_audience_matching_client_id_does_not_require_authorized_party() { assert!(validate_authorized_party(1, true, None, "thinkwatch-client").is_ok()); @@ -386,8 +420,9 @@ mod tests { // `client.id_token_verifier()` branch (stock upstream behavior), // never the `set_other_audience_verifier_fn` branch. Enforced by // code review / the `is_empty()` branch above; a full integration - // test would require a signed JWT fixture and is tracked as a - // follow-up (see docs/thinkwatch.md). + // test would require a signed JWT fixture and issuer discovery, + // so it belongs with the other network-bound auth tests rather + // than here. let empty = parse_additional_trusted_audiences(""); assert!(empty.is_empty()); } diff --git a/deploy/helm/think-watch/Chart.yaml b/deploy/helm/think-watch/Chart.yaml index d1526279..3e18286e 100644 --- a/deploy/helm/think-watch/Chart.yaml +++ b/deploy/helm/think-watch/Chart.yaml @@ -2,8 +2,8 @@ apiVersion: v2 name: think-watch description: Enterprise AI API Gateway & MCP Management Platform type: application -version: 1.0.1 -appVersion: "1.0.1" +version: 1.0.2 +appVersion: "1.0.2" keywords: - ai - gateway diff --git a/web/package.json b/web/package.json index c3a3dc18..96a6cf39 100644 --- a/web/package.json +++ b/web/package.json @@ -1,7 +1,7 @@ { "name": "web", "private": true, - "version": "1.0.1", + "version": "1.0.2", "type": "module", "packageManager": "pnpm@11.0.0", "scripts": {