Repository navigation
Description
Summary
POST /api/enterprise/client-portal/auth/exchange — the ent#163 trusted-issuer
token exchange — returns HTTP 500 for every caller it exists for.
src/backend/client_portal/router.py:350 calls settings_service.get_portal_session_policy(),
and settings_service is never imported in that module. grep -n settings_service src/backend/client_portal/router.py returns exactly one line: the call itself.
File "/app/client_portal/router.py", line 350, in portal_auth_exchange
idle_s, _ = settings_service.get_portal_session_policy()
NameError: name 'settings_service' is not defined
This is the seam a licensee uses to run their own customer portal against Trinity
while keeping their own IdP. It is 100% broken: not intermittent, not
configuration-dependent.
Reproduction
# 1. admin mints a portal_delegate key
curl -sX POST $API/api/mcp/keys -H "Authorization: Bearer $ADMIN_JWT" \
-H 'Content-Type: application/json' \
-d '{"name":"repro","scope":"portal_delegate"}'
# 2. exchange it for a portal session for any email with portal access
curl -sX POST $API/api/enterprise/client-portal/auth/exchange \
-H "Authorization: Bearer $DELEGATE_KEY" -H 'Content-Type: application/json' \
-d '{"email":"[email protected]"}'
# -> 500 Internal Server Error, NameError in the backend logVerified on the running dev stack. Note the failure is only reachable with a
valid portal_delegate key — any other principal is refused 403 by the scope
fence before reaching line 350. So it is not externally probeable, and it is
broken for exactly the callers the route was built for.
Scope
- Affected:
/auth/exchangeonly. - Not affected: platform login, platform JWT, the portal OTP flow
(/auth/request+/auth/verify), portal session tokens, MCP keys, agent auth.
All verified working on the same stack in the same minute.
Where it came from
Introduced by ent#375 (#2099), "the session slides instead of expiring at 12 hours",
which added the idle-window read to the response.
dependencies.py performs the same read correctly — a deliberate
function-local import, because settings_service is imported by nearly everything
and a module-level import here would create a cycle — wrapped in a try/except
that degrades to the shipped policy rather than 500ing an auth path:
try:
from services.settings_service import settings_service
return settings_service.get_portal_session_policy()
except Exception:
... # degrade to the shipped policyThe router copied the call and not the import.
Why the tests did not catch it
tests/unit/test_163_portal_delegate_scope.py and
tests/unit/test_ent163_portal_delegated_identity.py carry 8 tests between them,
including test_exchange_mints_a_portal_session_for_an_asserted_email. None of
them exercises the route through the app, so a NameError in the handler body is
invisible to all of them.
Proposed fix
- Function-local import inside
portal_auth_exchange, matchingdependencies.py
— including thetry/exceptdegrade, since this is an auth path and a settings
read should not be able to 500 it. - One test that actually calls
POST /auth/exchangethrough the app with a valid
portal_delegatekey and asserts a 200 with a usable token. The existing tests
assert correct things about a code path they never execute; that is the gap
that let this ship.
Present on
origin/dev and origin/main (line 308 there) — released, not a dev-only regression.
Activity
dolho commentedon Sep 10, 2026
PR: #2690 (→
dev)Two lines of code. Reuses
dependencies._portal_session_policy()instead of re-importingsettings_servicein the router — that reader already carries both properties this route needs: the function-local import (module-level would cycle, sincesettings_serviceimportsdb) and the degrade to the shipped policy, because a settings hiccup must not 500 an auth path. A second copy of the call would be a second chance to omit the degrade.The test is the actual deliverable.
test_2689_portal_exchange_route_executes.pydrives the handler rather than the service — which is the gap that let aNameErrorreachmain. Proven by reinstating the defect:Only the three new tests fail; all 26 pre-existing ent#163 tests still pass.
An AST scan across
src/backendfor the same shape — a module calling a method on a service singleton it never binds — found no other instance.fix(review): the module-scope import was a second capture bug (#2689)
fix(portal): /auth/exchange runs again — settings_service was used, n…