Skip to content

fix: report host_not_found when a host JWT names an unknown host - #36

Open
Karavil wants to merge 2 commits into
better-auth:mainfrom
Karavil:fix/host-not-found-error-code
Open

fix: report host_not_found when a host JWT names an unknown host#36
Karavil wants to merge 2 commits into
better-auth:mainfrom
Karavil:fix/host-not-found-error-code

Conversation

@Karavil

@Karavil Karavil commented Aug 4, 2026

Copy link
Copy Markdown

The failure

A host authenticates with a host+jwt whose iss resolves to no host row -- the host was never registered, was revoked, or its row is not reachable by the identifier being sent. The middleware answers agent_not_found.

That sends the operator to look at the agent record, which is usually fine, while the host row is the thing that is missing. HOST_NOT_FOUND already exists in AGENT_AUTH_ERROR_CODES and the routes use it for exactly this condition, so the two paths disagree on what to call the same state.

host+jwt, iss names no host  ->  agent_not_found   (looks like an agent problem)
                             ->  host_not_found    (after)

The change

One line in the host branch of createAgentAuthBeforeHook. The agent branch is untouched, so a genuine agent miss still reports agent_not_found.

This is a behavioural change to a response body, so it is worth a note in release notes -- a client matching on the agent_not_found string for this case would need to also accept host_not_found.

Tests

Two cases in host-not-found-error.test.ts: an unknown host on a host+jwt reports host_not_found, and an unknown agent on an agent+jwt still reports agent_not_found. Reverting the one-line change fails the first and leaves the second passing.

Full suite: 258 passed, 15 files. tsc --noEmit clean.

Independent of #35, which fixes why a spec-compliant kid-less host lands in this branch at all. Either can merge first.

The middleware's host branch raised AGENT_NOT_FOUND when `iss` resolved
to no host row, which points the operator at the agent record while the
host row is the one missing. HOST_NOT_FOUND already exists and the routes
use it for this same condition.

The agent branch is untouched, so a genuine agent miss still reports
AGENT_NOT_FOUND.
@Karavil
Karavil marked this pull request as ready for review August 4, 2026 02:25
Drops the `as any` cast on the test instance and routes both assertions
through the existing expectError helper. Behaviour is unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant