Skip to content

fix(coderd): return 500, not 403, when AI Gateway authorization hits a database error - #31617

Merged
SasSwart merged 3 commits into
mainfrom
sas/aigov-737-return-500-not-403-when-ai-gateway-org-resolution-hits-a
Nov 10, 2026
Merged

SasSwart merged 3 commits into
mainfrom
sas/aigov-737-return-500-not-403-when-ai-gateway-org-resolution-hits-a

Conversation

@SasSwart

@SasSwart SasSwart commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

When coderd's IsAuthorized hits a database error, the AI Gateway answers 403. A coderd outage therefore looks to clients like a permission problem. The gateway can't tell coderd's own failures from rejected requests, because coderd returns both as plain errors. Worse, the API key and user lookups turn any database error into ErrUnknownKey or ErrUnknownUser.

coderd now marks each database failure in IsAuthorized with the dRPC error code proto.ErrCodeInternal, where the failure happens. This covers the key, user, workspace, organization membership and effective group lookups. The key and user lookups return ErrUnknownKey and ErrUnknownUser only for sql.ErrNoRows. Rejections carry no code. The gateway answers a coded error with 500 and every other error with 403.

dRPC sends the code with the error, so the protocol version doesn't change. A gateway and coderd on different versions keep today's 403, because the older side neither sets nor reads the code.

A connection that drops during the IsAuthorized call still gets a 403. That's left for a follow-up.

TestAuthorization_InternalErrors covers each database failure and each rejection on the direct, workspace token and delegated paths. TestServeHTTP_FailureModes covers the gateway's status codes.

Refs AIGOV-737

🤖 Generated with Claude Code

@linear-code

linear-code Bot commented Nov 2, 2026

Copy link
Copy Markdown

AIGOV-737

@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@SasSwart SasSwart changed the title fix(coderd): return 500, not 403, when AI Gateway org resolution fails fix(coderd): return 500, not 403, when AI Gateway authorization hits a database error Nov 6, 2026
…a database error

IsAuthorized returned database failures as plain errors, and the API key
and user lookups turned any store error into ErrUnknownKey or
ErrUnknownUser. The AI Gateway answers every IsAuthorized error with
403, so a coderd outage looked like a permission problem to clients.

Mark each database failure in IsAuthorized with the dRPC error code
proto.ErrCodeInternal where it happens, and return ErrUnknownKey and
ErrUnknownUser only for sql.ErrNoRows. The gateway answers a coded
error with 500 and every other error with 403. dRPC sends the code with
the error, so the protocol version does not change, and mixed versions
keep the current 403.

Refs AIGOV-737

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@SasSwart
SasSwart force-pushed the sas/aigov-737-return-500-not-403-when-ai-gateway-org-resolution-hits-a branch from fa1d8a4 to 940ee18 Compare November 6, 2026 11:15

@SasSwart SasSwart left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self Review

Comment thread coderd/aibridged/proto/errors.go Resolved
Comment thread coderd/aibridged/aibridged_test.go Resolved
if err != nil {
s.logger.Warn(ctx, "failed to retrieve API key by id", slog.F("key_id", keyID), slog.Error(err))
return nil, ErrUnknownKey
return nil, internalError(xerrors.Errorf("get API key %s: %w", keyID, err))

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are probably more internal failure modes than there are external ones. so should we mark the opposite? unauthorizedError() rather than internalError()?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so should we mark the opposite? unauthorizedError() rather than internalError()?

what would be a use case for that? IRC, typically when there is unauthorizedError we return 404.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main issue is that if someone changes IsAuthorized and forgets to mark a new failure as internal, it is sent as a 403 again. Switching it makes sense: default to a 500, and return a 403 only for the known rejections. I would suggest making this change, but if it is too much, a more explicit comment on IsAuthorized should be enough 👍

@SasSwart
SasSwart marked this pull request as ready for review November 6, 2026 11:43
@SasSwart
SasSwart requested a review from ssncferreira November 6, 2026 11:43
// internalError marks err as a failure of coderd, such as a failed database
// query, rather than a rejection of the request. The gateway answers an error
// carrying [proto.ErrCodeInternal] with a 500 instead of a 403.
func internalError(err error) error {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: perhaps indicate this drpcInternalError

if err != nil {
s.logger.Warn(ctx, "failed to retrieve API key by id", slog.F("key_id", keyID), slog.Error(err))
return nil, ErrUnknownKey
return nil, internalError(xerrors.Errorf("get API key %s: %w", keyID, err))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so should we mark the opposite? unauthorizedError() rather than internalError()?

what would be a use case for that? IRC, typically when there is unauthorizedError we return 404.

@ssncferreira ssncferreira left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 I think defaulting to a 500 and marking only the known rejections would be nicer, but not a blocker

…nless they are rejections

IsAuthorized now treats every error as a failure of coderd, with
proto.ErrCodeInternal, unless it is a known rejection. Rejections have
their own type, so a new failure that nobody marks gets a 500 instead of
a 403. This replaces the per-call internalError marks.

The authorization tests now check that each rejection carries no code.

Generated by Coder Agents.
@SasSwart
SasSwart temporarily deployed to main November 10, 2026 03:24 — with GitHub Actions Inactive
The errname linter expects error types to end in Error.

Generated by Coder Agents.
@SasSwart
SasSwart temporarily deployed to main November 10, 2026 05:04 — with GitHub Actions Inactive
@SasSwart
SasSwart merged commit 1ec94c7 into main Nov 10, 2026
28 of 29 checks passed
@SasSwart
SasSwart deleted the sas/aigov-737-return-500-not-403-when-ai-gateway-org-resolution-hits-a branch November 10, 2026 05:39
@SasSwart
SasSwart temporarily deployed to main November 10, 2026 05:40 — with GitHub Actions Inactive
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 10, 2026

This branch was previously deployed

1 inactive deployment
main — f9cbe2a9 Deployed Nov 10, 2026 by SasSwart via cla #253046
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants