Commit Graph
1 Commits
Author SHA1 Message Date
yetone b657a2121b gateway: URL-safe or unpadded inline base64 goes upstream as standard base64 (#5697)
Cause: Google's Python SDK (google-genai) writes a part's bytes with
base64.urlsafe_b64encode, so an image sent with Part.from_bytes reaches
magpie's /v1beta/models/{model}:generateContent with '-' and '_' in
inlineData.data. Google's own API reads either alphabet; magpie passed
the string on as it came, and dludream's Gemini provider (gemini-2, a
Gemini API behind a proxy) answered 400 "Failed to base64-decode image
data". The same request with '+' and '/' answered. Every reader of an
image's bytes inside magpie (HEIC and webp conversion, resizing, the
describer, token counts) decodes the standard alphabet too, so such an
image was passed over by each of them as well.

Fix: stdBase64Body (base64_input.go) writes inline base64 a client sends
as standard, padded base64 at the top of serve, before anything reads
the body, and in count_tokens: every image block of every protocol
(Chat and Responses data: URLs, Anthropic base64 sources, tool results)
through walkImages, and every Gemini inlineData part whatever its type
(a PDF, a recording; turns, function responses, the system instruction,
camel or snake case), spliced in place so the rest of the body keeps its
bytes. So every route has it: Gemini passed through to a Gemini
provider, translated to Chat, Messages or Responses, a plugin's fetch,
and another client's image sent to a Gemini provider. Base64 that is
standard and padded already is not touched (the body comes back byte for
byte), nor is a string with both alphabets or one that isn't base64.
A byte scan (a 64+ character base64 run ending a string with '-'/'_' in
it or a length not a multiple of 4) keeps the walk off other requests.

Test: the fixture is the reporter's literal case, a 32x32 white PNG as
PIL writes it, base64.urlsafe_b64encode'd (it has a '-'; the test checks
standard base64 refuses it). TestURLSafeBase64ReachesUpstreamAsStandard
posts it through the gateway to a fake upstream that answers as
dludream's did (400 "Failed to base64-decode image data" for non-standard
base64): Gemini to a Gemini provider named gemini-2 at the reporter's
path gemini-2/models/gemini-3.8-flash, Gemini to Chat, Messages and
Responses, Chat and Messages to Gemini, and an unpadded URL-safe PDF.
TestStdBase64Body covers each protocol's block shape, fields kept, and
standard bodies left byte-identical; TestGeminiStdBase64SystemFirst a
system instruction before the turns.

Run on mote at this tree (on origin/main b85475e57):
- go test -count=1 -timeout 45m ./internal/gateway/: ok (902s)
- the new tests with -v: every subtest passes
- go vet ./internal/gateway/ for GOOS=linux, windows, darwin: clean
  (windows and darwin also over ./internal/...; linux over ./internal/...
  stops in wails' cgo files when cross-compiled, untouched here)
- gofmt -l: clean
Rebased onto 2c31815c5 (a one-line Kiro mover min in internal/provider);
at the pushed head the new tests, the HEIC/webp tests and go vet of
internal/gateway ran again: ok.

Broken by hand: with the stdBase64Body line in serve commented out, all
7 subtests of TestURLSafeBase64ReachesUpstreamAsStandard went red with
the upstream's 400; without the sort of the Gemini edits,
TestGeminiStdBase64SystemFirst went red (both parts left URL-safe).

Not tried: the reporter's real upstream (Google's API or their proxy);
the fake answers as their report shows. Not tried with the Python SDK
itself; the fixture is the bytes it writes. Inline base64 of non-image
files in Chat (file_data, input_audio) and Anthropic document blocks
is not rewritten: their official SDKs send standard base64, and no
report has them otherwise.

Reported-by: dludream
2026-11-08 20:53:19 +08:00