On this page
- Status: Accepted (P1-P4 shipped and deployed 2026-08; P5 live-DB verify done at deploy; no org enabled as of 2026-09-03)
- Owners: platform / security
- Extends: §66 (RLS runtime), inv 4 (envelope encryption), ADR 0016 (tenant domains).
Per-org LDAP/AD sign-in for enterprise tenants that require directory auth. Delivered in phases; this ADR records the foundation (schema + dependency + RLS) and the decisions the later phases execute against. Config lives in org_auth_ldap (one row per org); the directory client is ldapts (pure-JS, server-side).
Decisions#
D1 — Login entry is org-subdomain-first
The LDAP sign-in surface resolves its org from the request host via getRequestTenant() (ADR 0016) — the tenant is already pinned by the subdomain / white-label domain, so no org picker is shown and no untrusted org field is read at the login boundary. Apex sign-in with an explicit org field is a later extension, not part of this design.
D2 — Enablement is a per-org entitlement, not a plugin
org_auth_ldap.enabled is a dedicated per-org flag set only by a super-admin path. LDAP is NOT modelled as a plugin: it has no nav surface, and plugin config forbids secret-bearing fields. enabled = false means the login path ignores the org's config entirely (fail-closed).
D3 — Encrypted config read only after org context is pinned → no RLS exception
The bind password is envelope-encrypted (AES-256-GCM, inv 4) in the same column shape as ai_connections. The row is read ONLY inside withOrgContext / runWithRlsContext (§66), after the org is pinned — never on an unpinned pre-authorize path. Therefore org_auth_ldap rides the plain org_only RLS cohort (sql/rls-policies.sql) and carries NO secret-bearing entry in rls-exceptions.json.
D4 — Match by email; sync name only; never auto-provision
Identity matches an existing user by email — the cross-org identity key, which is NEVER overwritten from the directory. Only name is synced onto the matched membership. Login requires an existing org membership: a successful bind for an unknown user does NOT auto-provision an account.
Identity-confusion containment (the directory is org-admin-controlled). The org admin controls the directory host, the attribute map, and the server, so it can make a bind succeed and return ANY email. Two hard controls bound the blast radius of that trust:
- No super-admin via LDAP. If the email resolves to a platform super-admin (
users.is_super_admin), login is REFUSED. Super-admins are routinely added to customer orgs for support; without this a malicious org admin could return a super-admin's email and mint a platform-takeover session. - Single-org members only. Login is REFUSED unless the resolved user is a member of THIS org and of NO other org. A tenant directory may only ever unlock the one org it belongs to; a multi-org user must sign in with password/passkey/magic-link instead.
Enforced in resolveLdapUser (apps/web/src/auth/ldap.ts): super-admin check before the membership read, then orgIds.length === 1 && orgIds[0] === thisOrg.
D5 — Bind is fail-closed and default-secure
The directory bind runs with a hard timeout and fails closed. TLS defaults to LDAPS with certificate validation (use_ldaps true, allow_insecure_tls false); allow_insecure_tls is a per-org opt-in for on-prem self-signed certs only. Bind attempts are rate-limited under auth:ldap:* and fail closed in production.
SSRF egress guard (the host is org-admin-controlled). The server dials host from inside the production network and returns a coarse open/closed/timeout signal, so an unguarded host is a port scanner and a cloud-metadata exfil primitive. A NARROW LDAP-specific DENYLIST guards it — deliberately NOT the backup path's all-private block, because legitimate on-prem AD lives in RFC-1918:
- Denied: loopback (
127.0.0.0/8,::1,localhost/*.localhost), link-local incl. metadata (169.254.0.0/16,fe80::/10), unspecified (0.0.0.0/8,::), IPv6 unique-local (fc00::/7), and the platform's OWN infrastructure derived with no configuration (2026-09-03): the IPv4 subnets of the process's network interfaces, every hostname/IP literal named by a URL-shaped environment value (database, cache, object store, PDF browser, AI endpoints…) and keys ending in_HOST, plus the compose service names.LDAP_HOST_DENYLIST(comma-separated names/IPv4 CIDRs) remains an optional extra, never a prerequisite. - IPv6 is classified from CANONICAL 16-bit groups, never a text regex. An IPv4-mapped/compatible address is denied by its EMBEDDED IPv4 (through the v4 denylist) in every spelling — dotted (
::ffff:127.0.0.1), hex-compressed (::ffff:7f00:1), fully expanded (0:0:0:0:0:ffff:a9fe:a9fe→ metadata), and bracketed. An unparseable IPv6 literal fails CLOSED (denied). This closed a proven live-socket bypass where hex-form mapped addresses reached127.0.0.1and169.254.169.254on Linux dual-stack. - Allowed: RFC-1918 private ranges — customer AD.
- Enforced twice (
apps/web/src/auth/ldap-host-guard.ts): (a) a synchronous literal check inldapConfigInputSchemaat write time; (b) the AUTHORITATIVE async check at dial time inmakeClient— resolve the name, refuse if ANY resolved IP is denied, then PIN the socket to that exact IP (via ldapts'createConnection/createSecureConnectionhooks) to defeat DNS rebinding, while keepingtlsOptions.servername= the original host so certificate validation still holds. Both the login andtestConnectiondial paths go throughmakeClient.
Residual risks (accepted)
- A malicious org admin can still impersonate a REGULAR, single-org member of their OWN org (the directory attests that member's email without their real password). Accepted: an org admin already fully controls that org's data; D4's super-admin refusal and single-org restriction cap the reach to exactly that org.
- The dial-time guard resolves-then-pins, so there is no TOCTOU window on the pinned socket. The one residual is an on-prem AD on the same subnet as the platform's own interface, which is refused; that deployment moves the AD binding off the shared subnet.
Phased implementation#
- P1 (this ADR) — schema (
org_auth_ldap), dependency (ldapts), RLS cohort membership. Inert: no login path reads it. - P2 — admin router + super-admin
enabledtoggle and config CRUD. - P3 — NextAuth provider wiring the bind + email-match +
namesync (D4/D5). - P4 — org-admin configuration UI.
- P5 — verify (
verify:rlson a live DB) + deploy. Done. Operational prerequisite before enabling an org: an owner passkey registered as break-glass. (The infra denylist is derived automatically since 2026-09-03; no env is required.) - P6 (open) — directory-driven access for multi-company orgs: group→role and group→brand mapping with nested-group resolution (
LDAP_MATCHING_RULE_IN_CHAIN), periodic attribute/membership sync and deprovision on directory disable, login-attempt audit rows, explicit JIT-provisioning switch (default off).
Amendment 2026-09-03#
The group→role/group→brand mapping half of P6 now exists as a standalone module,
apps/web/src/auth/directory-group-mapping.ts — built first for OIDC (ADR 0045
D8: pure, directory-agnostic, highest-privilege-first precedence, owner never an automatic mapping
target) precisely so LDAP's own P6 phase can adopt it rather than fork it. P6's remaining scope
(nested-group resolution over memberOf, periodic sync/deprovision, login-attempt audit rows) is
still open.