fix(translation): reject malformed request fields - #287
Conversation
Signed-off-by: nachiketb <nachiketb@nvidia.com>
WalkthroughRequest decoding now validates Anthropic ChangesRequest validation
Estimated code review effort: 2 (Simple) | ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
crates/switchyard-translation/src/codecs/anthropic/buffered.rs (1)
347-374: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick winReject invalid entries in a
systemarray.The decoder accepts an array that contains non-object blocks, non-
textblocks, or atextblock with a non-stringtextmember. Lines 356-365 silently discard or coerce those entries. For example,{"system":[{"type":"image"},{"type":"text","text":7}]}decodes successfully with changed instruction content.Validate every array member as a text block. Return a
TranslationErrorwith the indexed path when validation fails. Add regression cases for invalid array members.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@crates/switchyard-translation/src/codecs/anthropic/buffered.rs` around lines 347 - 374, Update decode_anthropic_system so every array member must be an object with type "text" and a string text field; return TranslationError with the member’s indexed path for any invalid entry instead of skipping or defaulting it. Preserve valid string and text-block decoding, and add regression cases covering non-object, non-text, and non-string text members.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@crates/switchyard-translation/src/codecs/anthropic/buffered.rs`:
- Around line 347-374: Update decode_anthropic_system so every array member must
be an object with type "text" and a string text field; return TranslationError
with the member’s indexed path for any invalid entry instead of skipping or
defaulting it. Preserve valid string and text-block decoding, and add regression
cases covering non-object, non-text, and non-string text members.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 9db98ce9-193c-4d6c-91d5-6067aaf1325b
📒 Files selected for processing (3)
crates/switchyard-translation/src/codecs/anthropic/buffered.rscrates/switchyard-translation/src/codecs/responses/buffered.rscrates/switchyard-translation/tests/request_translation.rs
c9c0251 to
0385596
Compare
Signed-off-by: nachiketb <nachiketb@nvidia.com>
0385596 to
ea8d20e
Compare
What
Reject malformed Anthropic
systemandmax_tokensfields and malformed OpenAI Responsesinputfields during request decoding.Why
These explicitly invalid values were previously coerced into text or treated as absent. That changed request semantics, allowed malformed requests to reach an upstream model, and could create unintended usage.
Fixes SWITCH-1188.
Fixes SWITCH-1189.
Fixes SWITCH-1190.
How
systemas no system instruction; otherwise require a string or text-block array.max_tokensto be a non-negative integer.inputto be a string or array.What to review
Live validation
Built the release server from this branch and ran every issue reproducer with live credentials:
system={}orsystem=trueexpected string or array of text blocks at $.systemmax_tokens=-1ormax_tokens="8"invalid value at $.max_tokens: expected a non-negative integerinput=trueorinput=nullexpected string or array at $.inputAfter all six rejected Switchyard requests,
/v1/statsreportedtotal_requests: 0, confirming that none reached routing or an upstream model. A valid routed control then returned HTTP 200 and incrementedtotal_requeststo 1.Validation
cargo test -p switchyard-translationcargo clippy -p switchyard-translation --all-targets -- -D warningsSummary by CodeRabbit