Skip to content

Hardening Multi-Tenant SaaS Auth: Building a 4,365 req/s Go OAuth 2.1 + PostgreSQL 16 FORCE RLS Server

Repositories: github.com/cuhawk/BastionAuth · github.com/cuhawk/AegisMesh


The Bug Class Behind Real-World Multi-Tenant & OAuth Breaches

Across enterprise web assessments and bug-bounty programs, the highest-severity SaaS vulnerabilities rarely come from exotic cryptography breaks—they come from protocol composition gaps and application-layer tenant checks that silently fail open:

  • Authorization requests tampered in transit or replayed across redirect URIs with permissive query matching.
  • Bearer tokens stolen from logs, proxies, or XSS and replayed from a different network/client without proof of possession.
  • Delegated service-to-service calls widening scopes or authorization_details during token exchange.
  • A single missing WHERE tenant_id = $1 clause in a new SQL query leaking cross-tenant rows.

BastionAuth is a Go 1.25 authorization server engineered from the ground up around modern OAuth 2.1 RFCs, MCP Client ID Metadata Documents (CIMD), and PostgreSQL 16 FORCE ROW LEVEL SECURITY so missing tenant context fails closed inside the database engine itself.


Architecture

flowchart LR
    Browser[User Browser<br/>__Host-bastion_session] -->|Consent & Grant Revoke| Auth[BastionAuth Go Server]
    Client[OAuth / MCP Client] -->|PAR + Signed JAR + PKCE S256 + DPoP| Auth
    Delegate[Confidential Delegate] -->|RFC 8693 Subject + Actor + DPoP| Auth
    Auth -->|Verify kid via Trusted JWKS| JWKS[Issuer JWKS]
    Auth -->|Transaction-Scoped SET LOCAL tenant_id| DB[(PostgreSQL 16<br/>NOSUPERUSER · NOBYPASSRLS<br/>FORCE RLS)]
    Auth -->|Audience-Bound DPoP Token cnf.jkt| Client
    Client -->|Token + Fresh DPoP ath Proof| Resource[Resource Server<br/>RFC 7662 / JWKS]

Protocol Surface Implemented

Standard Endpoint / Mechanism Security Invariant Enforced
OAuth 2.1 + PKCE S256 GET/POST /oauth/authorize, POST /oauth/token Exact redirect_uri match (rejects fragments, malformed queries, reserved params), optional dpop_jkt code binding, single-use codes, rotating refresh tokens
RFC 9126 (PAR) POST /oauth/par Stores auth parameters server-side; returns single-use request_uri (<= 60s TTL) consumed atomically at consent
RFC 9396 (RAR) authorization_details Structured JSON grant constraints carried into access tokens & refresh rotations; token exchange can only narrow—never widen—actions, locations, datatypes, or privileges
RFC 9101 (Signed JAR) request / request_uri PS256 (>= 2048-bit), ES256, EdDSA verification; SSRF-hardened request_uri fetch (public IPs only, verified HTTPS, 5s timeout, 64 KiB cap, redirects disabled); PostgreSQL-backed jti replay protection
RFC 7523 (private_key_jwt) /oauth/token, /par, /revoke, /introspect Eliminates shared client secrets; verifies RFC 7638 SHA-256 JWK thumbprint kid, iss, sub, aud, nbf, exp, and single-use jti
RFC 8693 (Token Exchange) POST /oauth/token Requires may_act authorization on subject token; enforces scope intersection of (subject ∩ actor ∩ target policy); rejects escalation with invalid_scope
DPoP + Revocation / Introspection RFC 7009, RFC 7662, RFC 8414, RFC 9728 Sender-constrained cnf.jkt + ath binding; grant-generation revocation kills codes, rotated refresh tokens, and denylists tracked access-token jtis until expiry
MCP Client Metadata (CIMD) HTTPS client_id metadata URLs Public-IP-only HTTPS fetch, 64 KiB limit, bounded ETag cache (<= 5 min), loopback IP or HTTPS callbacks only

Database Isolation: Fail-Closed PostgreSQL 16 FORCE RLS

Rather than trusting every Go handler to filter by tenant, BastionAuth connects with a dedicated non-owner role (NOSUPERUSER NOBYPASSRLS) and enforces FORCE ROW LEVEL SECURITY on tenant tables. Every transaction sets the tenant context explicitly; if a code path ever forgets to bind tenant state, PostgreSQL returns zero rows / denies writes by default. Schema migrations are validated with a custom cmd/migrationlint linter and compatible with transaction-mode PgBouncer (avoiding named prepared statements).


Measured Benchmarks & Verification

Verified against PostgreSQL 16.16 with go test -race -v ./... and the loopback HTTP benchmark (benchmarks/run_bench.go, real Ed25519 signatures, served JWKS, unique DPoP proofs per request):

Metric Measured Result (Wave 6) Notes
Throughput (16 concurrent workers) 4,365.89 req/s 4,000 loopback HTTP requests, 0 failures
Latency (p50 / p95 / p99) 3.04 ms / 7.33 ms / 9.39 ms Includes full JWT signing, JWKS verification & DPoP proof check
Allocations per operation 802 allocs/op Measured via Go testing.Benchmark across both HTTP peers
Concurrency & Integration Tests 19 suites / 99 -race subtests Run against live PostgreSQL 16.16, 0 skips, 0 data races

Extending Zero-Trust Down to the Cluster Layer (AegisMesh)

Auth tokens alone don't stop lateral movement if a compromised pod can hit cloud metadata or bypass an API gateway. As a companion layer, AegisMesh compiles a single multi-tenant workload intent into Cilium L7/DNS, Istio STRICT mTLS, SPIRE federation, Tetragon eBPF (bprm_check_security + security_socket_connect blocking direct agent TCP 443), Kyverno 1.16, and AWS/GCP IAM + VPC Service Controls—formally proving shortest-path reachability first:

  • Rejected 115 / 115 hostile intent mutations with counterexample witnesses (32 / 32 valid topologies accepted, 0 false rejections).
  • 10.83 ms p95 proof time at 1,000 workload nodes (5.89 MiB heap allocation).

Reproduce in 60 Seconds

git clone https://github.com/cuhawk/BastionAuth.git && cd BastionAuth
go test -race -v ./...
go run ./cmd/migrationlint ./migrations
go run ./benchmarks/run_bench.go -requests 4000 -workers 16

View BastionAuth on GitHub View AegisMesh on GitHub