[Bug] Claude-to-Codex forwards invalid-base64 and truncated PNG images upstream
Is it a request payload issue?
[x] Yes, this is a request payload issue. A Claude Messages image block is translated into an invalid Codex input_image data URL and rejected upstream.
[ ] No, it is another issue.
Describe the bug
internal/translator/codex/claude currently constructs data:<media-type>;base64,<data> for Claude message images and images nested inside tool_result.content without validating the base64 payload or a PNG’s terminal structure.
A screenshot or other PNG that was read before its final IEND chunk was flushed is therefore forwarded as input_image. Strict Codex-compatible/Grok upstreams reject the entire request with an HTTP 400 such as Invalid PNG image. Syntactically invalid base64 has the same outcome.
The desired fix must cover both image locations:
- a top-level Claude message content block with
type: "image"; - an image block inside a Claude
tool_result.contentarray.
This is distinct from #3362 (MIME sniffing in the opposite Claude Responses path) and #4737 (provider capability downgrade for otherwise valid multimodal tool results). Searches for corrupt PNG, truncated PNG, IEND, and Invalid PNG image found no equivalent issue.
CLI Type
Claude Code client routed through the Codex-compatible translator.
Model Name
Observed with a strict Grok/Codex-compatible target such as grok-4.6; the defect is in request translation and is not intended to be model-specific.
LLM Client
Claude Code or any Claude Messages-compatible client that sends base64 image content.
Request Information
Minimal message-image reproduction using a base64-decodable PNG that has no terminal IEND chunk:
{
"model": "grok-4.6",
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9aw=="
}
}
]
}
]
}
The same payload in tool output also reproduces it:
{
"model": "grok-4.6",
"messages": [
{
"role": "assistant",
"content": [
{"type": "tool_use", "id": "toolu_1", "name": "take_screenshot", "input": {}}
]
},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_1",
"content": [
{"type": "text", "text": "Screenshot result"},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9aw=="
}
}
]
}
]
}
]
}
A payload such as "data": "%%%not-base64%%%" covers the syntax-error variant.
Expected behavior
The translator should not forward a syntactically invalid or clearly truncated PNG as input_image.
- Replace the bad image with a short deterministic
input_textplaceholder such as[image data truncated or corrupt; skipped]. - Preserve surrounding text/tool-result ordering.
- Continue forwarding valid PNG images normally.
- Accept genuinely unpadded base64 if desired, but do not “repair” malformed or over-padded input into an accepted decode while forwarding the original malformed spelling.
Screenshots
Not applicable.
OS Type
Not OS-specific. The truncated-file race is most visible with screenshots that are read while still being written.
Additional context
This issue replaces standalone translator PR #5363 because AGENTS.md requires contributors without upstream WRITE, MAINTAIN, or ADMIN permission to file an issue and stop. The current account has READ permission, and the PR’s ensure-no-translator-changes check fails as designed.
Relevant files:
internal/translator/codex/claude/codex_claude_request.gointernal/translator/codex/claude/codex_claude_request_test.go
The review on #5363 found two important edge cases that should be part of the eventual implementation:
- Searching for the ASCII substring
IENDwithin the last few bytes is insufficient. Compressed IDAT data can coincidentally contain those bytes; require the canonical zero-length IEND chunk and CRC at the exact end. - Do not recover from standard-base64 failure by trimming every trailing
=and using raw decoding. That accepts over-padded input and can still forward the original invalid encoding to a strict upstream.
Intended implementation code
A small translator-local sanitizer can decode safely, validate PNG truncation, and return a canonical data URL:
var pngSignature = []byte("\x89PNG\r\n\x1a\n")
var pngIEND = []byte{0, 0, 0, 0, 'I', 'E', 'N', 'D', 0xae, 0x42, 0x60, 0x82}
func decodeImageBase64(data string) ([]byte, error) {
data = strings.TrimSpace(data)
raw, err := base64.StdEncoding.DecodeString(data)
if err == nil {
return raw, nil
}
// RawStdEncoding is only for genuinely unpadded input. If standard
// decoding rejected an input that already contains padding, preserve
// that failure instead of trimming arbitrary '=' bytes.
if strings.Contains(data, "=") {
return nil, err
}
return base64.RawStdEncoding.DecodeString(data)
}
func sanitizeImageData(mediaType, data string) (string, bool) {
raw, err := decodeImageBase64(data)
if err != nil || len(raw) < len(pngSignature) {
return "", true
}
declaredPNG := strings.EqualFold(strings.TrimSpace(mediaType), "image/png")
encodedPNG := bytes.HasPrefix(raw, pngSignature)
if declaredPNG && !encodedPNG {
return "", true
}
if (declaredPNG || encodedPNG) && !bytes.HasSuffix(raw, pngIEND) {
return "", true
}
if strings.TrimSpace(mediaType) == "" {
mediaType = "application/octet-stream"
}
canonical := base64.StdEncoding.EncodeToString(raw)
return fmt.Sprintf("data:%s;base64,%s", mediaType, canonical), false
}
The exact helper signature is flexible. The critical properties are exact-tail validation, no arbitrary padding trim, and canonical output after any accepted raw/unpadded decode. This detects the observed truncation/syntax failures; it should not claim full PNG integrity validation unless all chunks/CRCs are actually checked.
Suggested regression tests:
- message image with invalid base64 -> no
input_image, placeholder text present; - tool-result image with invalid base64 -> same behavior while preserving adjacent text;
- PNG missing IEND -> rejected;
IENDbytes near the tail but not the canonical final 12-byte chunk -> rejected;- valid canonical 1x1 PNG -> retained;
- genuinely unpadded valid PNG -> accepted and re-encoded canonically;
- over-padded valid PNG (for example extra
====) -> rejected; - declared
image/pngwith a non-PNG signature -> rejected; - a valid non-PNG image path remains unchanged.
Focused validation:
go test -count=1 ./internal/translator/codex/claude
go build -o test-output ./cmd/server
This issue occurs because the client sends malformed or invalid image data. CLIProxyAPI is only responsible for translating and forwarding valid payloads; it does not have the obligation or authority to validate or sanitize the integrity of user data.
If this behavior is caused by a client sending incorrectly formatted or truncated images, we recommend opening an issue in the corresponding client repository.