Skip to content

bug(portal): /auth/exchange 500s for every caller — settings_service is used and never imported (ent#163 seam is dead) #2689

Closed

Description

@dolho

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 log

Verified 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/exchange only.
  • 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 policy

The 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

  1. Function-local import inside portal_auth_exchange, matching dependencies.py
    — including the try/except degrade, since this is an auth path and a settings
    read should not be able to 500 it.
  2. One test that actually calls POST /auth/exchange through the app with a valid
    portal_delegate key 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

  1. added
    priority-p1
    type-bug
    theme-reliability
    status-in-progress
    on Sep 10, 2026
  2. self-assigned this
    on Sep 10, 2026
  3. dolho commented on Sep 10, 2026

    @dolho
    ContributorAuthor

    PR: #2690 (→ dev)

    Two lines of code. Reuses dependencies._portal_session_policy() instead of re-importing settings_service in the router — that reader already carries both properties this route needs: the function-local import (module-level would cycle, since settings_service imports db) 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.py drives the handler rather than the service — which is the gap that let a NameError reach main. Proven by reinstating the defect:

    3 failed, 29 passed
    

    Only the three new tests fail; all 26 pre-existing ent#163 tests still pass.

    An AST scan across src/backend for the same shape — a module calling a method on a service singleton it never binds — found no other instance.

  4. added
    status-in-dev
    and removed
    status-in-progress
    on Sep 11, 2026
  5. removed
    status-in-dev
    on Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

priority-p1theme-reliabilitytype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions