Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autobahn suite #3251

Merged
merged 9 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions lib/web/websocket/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,9 @@ function closeWebSocketConnection (ws, code, reason, reasonByteLength) {
/** @type {import('stream').Duplex} */
const socket = ws[kResponse].socket

socket.write(frame.createFrame(opcodes.CLOSE), (err) => {
if (!err) {
ws[kSentClose] = sentCloseFrameState.SENT
}
})
socket.write(frame.createFrame(opcodes.CLOSE))

ws[kSentClose] = sentCloseFrameState.PROCESSING
ws[kSentClose] = sentCloseFrameState.SENT

// Upon either sending or receiving a Close control frame, it is said
// that _The WebSocket Closing Handshake is Started_ and that the
Expand Down
15 changes: 14 additions & 1 deletion lib/web/websocket/receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ const assert = require('node:assert')
const { parserStates, opcodes, states, emptyBuffer, sentCloseFrameState } = require('./constants')
const { kReadyState, kSentClose, kResponse, kReceivedClose } = require('./symbols')
const { channels } = require('../../core/diagnostics')
const { isValidStatusCode, failWebsocketConnection, websocketMessageReceived, utf8Decode, isControlFrame, isContinuationFrame } = require('./util')
const {
isValidStatusCode,
isValidOpcode,
failWebsocketConnection,
websocketMessageReceived,
utf8Decode,
isControlFrame,
isContinuationFrame
} = require('./util')
const { WebsocketFrameSend } = require('./frame')
const { CloseEvent } = require('./events')

Expand Down Expand Up @@ -58,6 +66,11 @@ class ByteParser extends Writable {
const opcode = buffer[0] & 0x0F
const masked = (buffer[1] & 0x80) === 0x80

if (!isValidOpcode(opcode)) {
failWebsocketConnection(this.ws, 'Invalid opcode received')
return callback()
}

if (masked) {
failWebsocketConnection(this.ws, 'Frame cannot be masked')
return callback()
Expand Down
12 changes: 11 additions & 1 deletion lib/web/websocket/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@ function isContinuationFrame (opcode) {
return opcode === opcodes.CONTINUATION
}

function isTextBinaryFrame (opcode) {
return opcode === opcodes.TEXT || opcode === opcodes.BINARY
}

function isValidOpcode (opcode) {
return isTextBinaryFrame(opcode) || isContinuationFrame(opcode) || isControlFrame(opcode)
}

// https://nodejs.org/api/intl.html#detecting-internationalization-support
const hasIntl = typeof process.versions.icu === 'string'
const fatalDecoder = hasIntl ? new TextDecoder('utf-8', { fatal: true }) : undefined
Expand Down Expand Up @@ -255,5 +263,7 @@ module.exports = {
websocketMessageReceived,
utf8Decode,
isControlFrame,
isContinuationFrame
isContinuationFrame,
isTextBinaryFrame,
isValidOpcode
}
16 changes: 13 additions & 3 deletions lib/web/websocket/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const { ByteParser } = require('./receiver')
const { kEnumerableProperty, isBlobLike } = require('../../core/util')
const { getGlobalDispatcher } = require('../../global')
const { types } = require('node:util')
const { ErrorEvent } = require('./events')
const { ErrorEvent, CloseEvent } = require('./events')

let experimentalWarned = false

Expand Down Expand Up @@ -594,9 +594,19 @@ function onParserDrain () {
}

function onParserError (err) {
fireEvent('error', this, () => new ErrorEvent('error', { error: err, message: err.reason }))
let message
let code

if (err instanceof CloseEvent) {
message = err.reason
code = err.code
} else {
message = err.message
}

fireEvent('error', this, () => new ErrorEvent('error', { error: err, message }))

closeWebSocketConnection(this, err.code)
closeWebSocketConnection(this, code)
}

module.exports = {
Expand Down
42 changes: 42 additions & 0 deletions test/autobahn/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// @ts-check
const { WebSocket } = require('../..')

let currentTest = 1
let testCount

function nextTest () {
let ws

if (currentTest > testCount) {
ws = new WebSocket('ws://localhost:9001/updateReports?agent=ws')
return
}

console.log(`Running test case ${currentTest}/${testCount}`)

ws = new WebSocket(
`ws://localhost:9001/runCase?case=${currentTest}&agent=ws`
)
ws.addEventListener('message', (data) => {
ws.send(data.data)
})
ws.addEventListener('close', () => {
currentTest++
process.nextTick(nextTest)
})
ws.addEventListener('error', (e) => {
console.error(e.error)
currentTest++
process.nextTick(nextTest)
})
}

const ws = new WebSocket('ws://localhost:9001/getCaseCount')
ws.addEventListener('message', (data) => {
testCount = parseInt(data.data)
})
ws.addEventListener('close', () => {
if (testCount > 0) {
nextTest()
}
})
7 changes: 7 additions & 0 deletions test/autobahn/config/fuzzingserver.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"url": "ws://127.0.0.1:9001",
"outdir": "./reports/clients",
"cases": ["*"],
"exclude-cases": [],
"exclude-agent-cases": {}
}
6 changes: 6 additions & 0 deletions test/autobahn/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
docker run -it --rm \
-v "${PWD}/config:/config" \
-v "${PWD}/reports:/reports" \
-p 9001:9001 \
--name fuzzingserver \
crossbario/autobahn-testsuite
Loading