Skip to content

Commit

Permalink
set proper status code and message when server cannot decode protobuf…
Browse files Browse the repository at this point in the history
… message
  • Loading branch information
Hugo Arregui committed Feb 22, 2024
1 parent 976362d commit b2978ca
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 9 additions & 1 deletion ws-connector/src/controllers/handlers/ws-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,17 @@ export async function registerWsHandler(
userData.timeout = undefined
}

let packet: ClientPacket

try {
const packet = ClientPacket.decode(Buffer.from(message))
packet = ClientPacket.decode(Buffer.from(message))
} catch (err: any) {
logger.error(err)
ws.end(1007, Buffer.from('Cannot decode ClientPacket'))
return
}

try {
switch (userData.stage) {
case Stage.HANDSHAKE_START: {
if (!packet.message || packet.message.$case !== 'challengeRequest') {
Expand Down
8 changes: 6 additions & 2 deletions ws-connector/test/integration/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,16 @@ test('end to end test', ({ components }) => {
const ws = await createWs('/ws')
const fut = futureWithTimeout(3000, 'The socket was not closed')

ws.on('close', fut.resolve) // resolve on close
ws.on('close', (code, reason) => {
fut.resolve({ code, reason: reason.toString() })
})
ws.on('message', fut.reject) // fail on timeout and message

await socketConnected(ws)
await socketSend(ws, new Uint8Array([1, 2, 3, 4, 5, 6]))
await fut
const { code, reason } = await fut
expect(code).toEqual(1007)
expect(reason).toEqual('Cannot decode ClientPacket')
})

it('sends different address', async () => {
Expand Down

0 comments on commit b2978ca

Please sign in to comment.