Repository navigation
fix(coderd): return 500, not 403, when AI Gateway authorization hits a database error - #31617
Conversation
|
All contributors have signed the CLA ✍️ ✅ |
…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]>
fa1d8a4 to
940ee18
Compare
| 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)) |
There was a problem hiding this comment.
There are probably more internal failure modes than there are external ones. so should we mark the opposite? unauthorizedError() rather than internalError()?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 👍
| // 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 { |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
The errname linter expects error types to end in Error. Generated by Coder Agents.
When coderd's
IsAuthorizedhits 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 intoErrUnknownKeyorErrUnknownUser.coderd now marks each database failure in
IsAuthorizedwith the dRPC error codeproto.ErrCodeInternal, where the failure happens. This covers the key, user, workspace, organization membership and effective group lookups. The key and user lookups returnErrUnknownKeyandErrUnknownUseronly forsql.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
IsAuthorizedcall still gets a 403. That's left for a follow-up.TestAuthorization_InternalErrorscovers each database failure and each rejection on the direct, workspace token and delegated paths.TestServeHTTP_FailureModescovers the gateway's status codes.Refs AIGOV-737
🤖 Generated with Claude Code