Repository navigation
sentry api UTF-8-decodes binary responses #1303
Description
Summary
The CLI command sentry api <endpoint> UTF-8-decodes every response body, including binary ones. The Sentry HTTP API returns correct bytes (curl works); the CLI corrupts them before write. Any binary endpoint — event attachments, minidumps, source map archives, debug files, replay segments, profiles — comes back unusable when redirected to a file. Classic symptom: download is ~1.5–1.8× larger than the source and riddled with EF BF BD (UTF-8 encoding of U+FFFD).
Impact
Workflows that download attachments via:
sentry api ".../attachments/<id>/?download=1" > screenshot.pngget a corrupted file and have to fall back to raw curl plus manual Authorization plumbing. This is a general foot-gun on the shared rawApiRequest path, not screenshot-specific.
Reproduction
sentry api \
"/projects/<org>/<project>/events/<event-id>/attachments/<id>/?download=1" \
> cli.png
curl -sSL -H "Authorization: Bearer $(sentry auth token)" \
"https://<org>.sentry.io/api/0/projects/<org>/<project>/events/<event-id>/attachments/<id>/?download=1" \
> curl.png
file cli.png curl.png
xxd -l 8 cli.png
xxd -l 8 curl.pngsentry api (CLI) |
curl (HTTP API) |
|
|---|---|---|
file |
data (unrecognized) | PNG image data, … |
| size | ~1.7× real size | exact API size |
| first 8 bytes | ef bf bd 50 4e 47 0d 0a |
89 50 4e 47 0d 0a 1a 0a |
Leading 0x89 of the PNG signature (and every other non-UTF-8 byte ≥ 0x80) is replaced with EF BF BD. Reproduced on current main (0.38.0); present at least since 0.23.0 (not a recent regression).
Root cause
High confidence. On the CLI only. rawApiRequest in src/lib/api/infrastructure.ts always does:
const text = await response.text(); // UTF-8 decode → corruption
let responseBody: unknown;
try {
responseBody = JSON.parse(text);
} catch {
responseBody = text;
}For non-UTF-8 payloads this is a lossy bytes → string (errors=replace) → bytes round-trip; originals are unrecoverable.
The api command then routes that string through the text formatter to stdout (formatApiResponse / CommandOutput). There is no content-type branch for binary.
Same repo already streams binary correctly for sentry build download via downloadBuildArtifact() in src/lib/api/preprod-artifacts.ts.
Expected behavior
- Non-textual responses stream byte-for-byte when stdout is redirected (
> file.png), curl-equivalent - No text formatter / no trailing newline on binary bodies
- Text/JSON responses unchanged
- TTY + binary: refuse or warn and point at redirect
Proposed fix
rawApiRequest: decode as text only for textualContent-Type(text/*,application/json,+json, XML, etc.). Otherwise return raw bytes (Uint8Array/Buffer). Prefer a text allowlist so unknown types default to binary-safe.sentry api: when body is binary and stdout is redirected, write raw bytes (bypass text formatter, no trailing newline). On a TTY, refuse or warn.- Tests: PNG/bytes fixture proving
89 50 4e 47round-trips throughsentry api+ redirect.
Workaround
curl -sSL -H "Authorization: Bearer $(sentry auth token)" \
"https://<org>.sentry.io/api/0/projects/<org>/<project>/events/<event-id>/attachments/<id>/?download=1" \
-o screenshot.pngEnvironment
- Affected: current main (
0.38.0); present at least since0.23.0(not a recent regression) - Not a Sentry HTTP API bug — curl against the same endpoint returns valid bytes
Out of scope (follow-ups)
sentry api --output <path>- First-class
sentry event attachment list|download - Surfacing attachments on
sentry issue view/ feedback issues
Requested by Saïd Redouane via Junior.
Activity
fix(api): stream binary responses without UTF-8 decoding
fix(api): stream binary responses without UTF-8 decoding (getsentry#1305