feat(bridge): add anthropic-compatible gateway surface#178
Conversation
| { | ||
| "type": "error", | ||
| "error": { | ||
| "type": error_type, | ||
| "message": message, | ||
| }, | ||
| }, |
Check warning
Code scanning / CodeQL
Information exposure through an exception Medium
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI about 2 months ago
In general, the fix is to avoid returning raw exception messages to the client. Instead, log the detailed exception server-side and send the client a generic, non-sensitive message (or, at most, a high-level description that doesn’t include stack traces, paths, or provider internals).
Specifically here, two except AnthropicBridgeError as exc: blocks currently call _anthropic_error_response(str(exc), ...). We should change these to:
- Log the exception (including stack trace if desired) using the existing
logger. - Return a generic error message (for example “Invalid Anthropic messages request” / “Invalid Anthropic count_tokens request” or “Anthropic bridge request failed”) that does not depend on
exc.
Concretely, in faigate/main.py:
-
Around lines 3000–3005 (Anthropic messages bridge route), replace the call that returns
_anthropic_error_response(str(exc), ...)with:- A
logger.warning(orlogger.error) including the exception object. - A call to
_anthropic_error_responsewith a fixed message such as"Invalid Anthropic messages request"and the sameerror_typeandstatus_codeas before.
- A
-
Around lines 3085–3089 (Anthropic
count_tokensroute), do the same: logexc, and return_anthropic_error_responsewith a fixed, non-tainted message such as"Invalid Anthropic count_tokens request".
No new imports are needed; logger already exists. This preserves HTTP semantics and error types while preventing exception-derived content from reaching clients.
| @@ -2998,8 +2998,9 @@ | ||
| canonical_request = _resolve_anthropic_requested_model(canonical_request) | ||
| execution = await _execute_chat_completion_body(canonical_request.to_openai_body(), headers) | ||
| except AnthropicBridgeError as exc: | ||
| logger.warning("Anthropic bridge messages request failed: %s", exc) | ||
| return _anthropic_error_response( | ||
| str(exc), | ||
| "Invalid Anthropic messages request", | ||
| error_type="invalid_request_error", | ||
| status_code=400, | ||
| ) | ||
| @@ -3083,8 +3083,9 @@ | ||
| try: | ||
| result, extra_headers = dispatch_anthropic_count_tokens(payload=body, headers=headers) | ||
| except AnthropicBridgeError as exc: | ||
| logger.warning("Anthropic count_tokens request failed: %s", exc) | ||
| return _anthropic_error_response( | ||
| str(exc), | ||
| "Invalid Anthropic count_tokens request", | ||
| error_type="invalid_request_error", | ||
| status_code=400, | ||
| ) |
Summary
Testing