Skip to content

Commit

Permalink
fix: handle non ascii chars in headers
Browse files Browse the repository at this point in the history
  • Loading branch information
aarlaud committed Dec 12, 2024
1 parent 34002d0 commit 14cb4d9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
16 changes: 10 additions & 6 deletions lib/server/routesHandlers/postResponseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const handlePostResponse = (req: Request, res: Response) => {
.on('data', function (data) {
try {
logger.trace(
{ ...logContext, dataLength: data.length },
{ ...logContext, dataLength: Buffer.byteLength(data, 'utf8') },
'Received data event',
);
let bytesRead = 0;
Expand All @@ -52,12 +52,16 @@ export const handlePostResponse = (req: Request, res: Response) => {
);
}

let statusAndHeadersLength = Buffer.byteLength(
statusAndHeaders,
'utf8',
);
if (
statusAndHeadersSize > 0 &&
statusAndHeaders.length < statusAndHeadersSize
statusAndHeadersLength < statusAndHeadersSize
) {
const endPosition = Math.min(
bytesRead + statusAndHeadersSize - statusAndHeaders.length,
bytesRead + statusAndHeadersSize - statusAndHeadersLength,
data.length,
);
logger.trace(
Expand All @@ -66,8 +70,8 @@ export const handlePostResponse = (req: Request, res: Response) => {
);
statusAndHeaders += data.toString('utf8', bytesRead, endPosition);
bytesRead = endPosition;

if (statusAndHeaders.length === statusAndHeadersSize) {
statusAndHeadersLength = Buffer.byteLength(statusAndHeaders, 'utf8');
if (statusAndHeadersLength === statusAndHeadersSize) {
logger.trace(
{ ...logContext, statusAndHeaders },
'Converting to json',
Expand All @@ -92,7 +96,7 @@ export const handlePostResponse = (req: Request, res: Response) => {
logger.trace(
{
...logContext,
currentSize: statusAndHeaders.length,
currentSize: statusAndHeadersLength,
expectedSize: statusAndHeadersSize,
},
'Was unable to fit all information into a single data object',
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/client/filters.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@
"origin": "http://localhost:9000"
},

{
"path": "/echo-with-unicode",
"method": "POST",
"origin": "http://localhost:9000"
},

{
"path": "/echo-body/filtered",
"method": "POST",
Expand Down
16 changes: 16 additions & 0 deletions test/functional/server-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,22 @@ describe('proxy requests originating from behind the broker server', () => {
expect(Buffer.from(response.data)).toEqual(body);
expect(response.headers['x-broker-ws-response']).not.toBeNull();
});

it('successfully broker POST with unicode body and header values', async () => {
const response = await axiosClient.post(
`http://localhost:${bs.port}/broker/${brokerToken}/echo-with-unicode`,
{ some: { example: 'json' } },
);
expect(decodeURIComponent(response.headers.test)).toEqual(
'Special-Char-碰撞.proj',
);
expect(response.status).toEqual(200);
expect(response.data).toStrictEqual({
some: { example: 'json' },
test: 'Special-Char-碰撞.proj',
});
});

it('successfully broker GET', async () => {
const response = await axiosClient.get(
`http://localhost:${bs.port}/broker/${brokerToken}/echo-param/xyz`,
Expand Down
15 changes: 15 additions & 0 deletions test/setup/test-web-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,21 @@ const applyEchoRoutes = (app: Express) => {
},
);

echoRouter.post(
'/echo-with-unicode/:param?',
(req: express.Request, resp: express.Response) => {
const unicodeValue = 'Special-Char-碰撞.proj';
const body = JSON.parse(req.body);
body.test = unicodeValue;
const contentType = req.get('Content-Type');
if (contentType) {
resp.type(contentType);
}
resp.setHeader('test', encodeURIComponent(unicodeValue));
resp.send(JSON.stringify(body));
},
);

// mimics functionality of https://httpbin.org/headers
echoRouter.get(
'/echo-headers/httpbin',
Expand Down

0 comments on commit 14cb4d9

Please sign in to comment.