Skip to content

Commit

Permalink
fix(server): Limit internal server error close message size
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Oct 19, 2021
1 parent 21e44db commit 8479f76
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
22 changes: 22 additions & 0 deletions src/__tests__/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,28 @@ for (const { tServer, startTServer } of tServers) {
// },
);

it('should limit the internal server error message size', async () => {
const { url } = await startTServer({
onConnect: () => {
throw new Error(
'i am exactly 124 characters long i am exactly 124 characters long i am exactly 124 characters long i am exactly 124 characte',
);
},
});

const client = await createTClient(url);
client.ws.send(
stringifyMessage<MessageType.ConnectionInit>({
type: MessageType.ConnectionInit,
}),
);

await client.waitForClose((event) => {
expect(event.code).toBe(CloseCode.InternalServerError);
expect(event.reason).toBe('Internal server error');
});
});

describe('Keep-Alive', () => {
it('should dispatch pings after the timeout has passed', async (done) => {
const { url } = await startTServer(undefined, 50);
Expand Down
5 changes: 4 additions & 1 deletion src/use/fastify-websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ export function makeHandler<
} catch (err) {
socket.close(
CloseCode.InternalServerError,
isProd ? 'Internal server error' : err.message,
// close reason should fit in one frame https://datatracker.ietf.org/doc/html/rfc6455#section-5.2
isProd || err.message.length > 123
? 'Internal server error'
: err.message,
);
}
}),
Expand Down
5 changes: 4 additions & 1 deletion src/use/uWebSockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,10 @@ export function makeBehavior<
} catch (err) {
socket.end(
CloseCode.InternalServerError,
isProd ? 'Internal server error' : err.message,
// close reason should fit in one frame https://datatracker.ietf.org/doc/html/rfc6455#section-5.2
isProd || err.message.length > 123
? 'Internal server error'
: err.message,
);
}
},
Expand Down
5 changes: 4 additions & 1 deletion src/use/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ export function useServer<
} catch (err) {
socket.close(
CloseCode.InternalServerError,
isProd ? 'Internal server error' : err.message,
// close reason should fit in one frame https://datatracker.ietf.org/doc/html/rfc6455#section-5.2
isProd || err.message.length > 123
? 'Internal server error'
: err.message,
);
}
}),
Expand Down

0 comments on commit 8479f76

Please sign in to comment.