fix(proxy): strip stale encoding from upstream error responses (#1239)

This commit is contained in:
Kai (Tam Nhu) Tran
2026-05-13 09:31:43 -04:00
committed by GitHub
2 changed files with 32 additions and 0 deletions
@@ -50,6 +50,7 @@ export function createAnthropicErrorResponse(
): Response {
const responseHeaders = new Headers(headers);
responseHeaders.set('Content-Type', 'application/json');
responseHeaders.delete('Content-Encoding');
responseHeaders.delete('Content-Length');
return new Response(JSON.stringify(createAnthropicErrorPayload(type, message)), {
@@ -3,6 +3,7 @@ import * as fs from 'fs';
import * as http from 'http';
import * as os from 'os';
import * as path from 'path';
import * as zlib from 'zlib';
import { startOpenAICompatProxyServer } from '../../../src/proxy/server/proxy-server';
import type { OpenAICompatProfileConfig } from '../../../src/proxy/profile-router';
@@ -144,6 +145,36 @@ describe('openai proxy message edge cases', () => {
});
});
it('does not leak upstream content-encoding onto synthesized error responses', async () => {
const compressed = zlib.gzipSync(
JSON.stringify({ error: { message: 'invalid upstream request' } })
);
await startProxyWithHandler((_req, res) => {
res.writeHead(400, {
'Content-Type': 'application/json',
'Content-Encoding': 'gzip',
'Content-Length': String(compressed.length),
});
res.end(compressed);
});
const response = await requestProxy({
model: 'hf-model',
messages: [{ role: 'user', content: 'hello' }],
});
expect(response.status).toBe(400);
expect(response.headers.get('content-encoding')).toBeNull();
await expect(response.json()).resolves.toMatchObject({
type: 'error',
error: {
type: 'invalid_request_error',
message: 'invalid upstream request',
},
});
});
it('returns api_error when the upstream JSON response has no usable choices', async () => {
await startProxyWithHandler((_req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });