mirror of
https://github.com/tiennm99/ccs.git
synced 2026-09-02 18:18:43 +00:00
fix(proxy): strip stale encoding from upstream error responses
This commit is contained in:
@@ -50,6 +50,7 @@ export function createAnthropicErrorResponse(
|
|||||||
): Response {
|
): Response {
|
||||||
const responseHeaders = new Headers(headers);
|
const responseHeaders = new Headers(headers);
|
||||||
responseHeaders.set('Content-Type', 'application/json');
|
responseHeaders.set('Content-Type', 'application/json');
|
||||||
|
responseHeaders.delete('Content-Encoding');
|
||||||
responseHeaders.delete('Content-Length');
|
responseHeaders.delete('Content-Length');
|
||||||
|
|
||||||
return new Response(JSON.stringify(createAnthropicErrorPayload(type, message)), {
|
return new Response(JSON.stringify(createAnthropicErrorPayload(type, message)), {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import * as fs from 'fs';
|
|||||||
import * as http from 'http';
|
import * as http from 'http';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
|
import * as zlib from 'zlib';
|
||||||
import { startOpenAICompatProxyServer } from '../../../src/proxy/server/proxy-server';
|
import { startOpenAICompatProxyServer } from '../../../src/proxy/server/proxy-server';
|
||||||
import type { OpenAICompatProfileConfig } from '../../../src/proxy/profile-router';
|
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 () => {
|
it('returns api_error when the upstream JSON response has no usable choices', async () => {
|
||||||
await startProxyWithHandler((_req, res) => {
|
await startProxyWithHandler((_req, res) => {
|
||||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||||
|
|||||||
Reference in New Issue
Block a user