Skip to content

Commit

Permalink
Fix: close on invalid UTF-8 (#35)
Browse files Browse the repository at this point in the history
* The Webserver will now close on receiving invalid UTF-8

* added test to check for this behavior in future
  • Loading branch information
Andrew-Lees11 authored Apr 16, 2018
1 parent d3a6625 commit 5a8b470
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/KituraWebSocket/WebSocketConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public class WebSocketConnection {
}
}
else {
// Error converting to String
closeConnection(reason: .invalidDataContents, description: "Failed to convert received payload to UTF-8 String", hard: true)
}
from.length -= 1
}
Expand Down
22 changes: 22 additions & 0 deletions Tests/KituraWebSocketTests/ProtocolErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ProtocolErrorTests: KituraTest {
("testInvalidRSVCode", testInvalidRSVCode),
("testJustContinuationFrame", testJustContinuationFrame),
("testJustFinalContinuationFrame", testJustFinalContinuationFrame),
("testInvalidUTF", testInvalidUTF),
("testTextAndBinaryFrames", testTextAndBinaryFrames),
("testUnmaskedFrame", testUnmaskedFrame)
]
Expand Down Expand Up @@ -185,6 +186,27 @@ class ProtocolErrorTests: KituraTest {
}
}

func testInvalidUTF() {
register(closeReason: .noReasonCodeSent)

performServerTest() { expectation in
let testString = "Testing, 1,2,3"
let dataPayload = testString.data(using: String.Encoding.utf16)!
let payload = NSMutableData()
payload.append(dataPayload)

let expectedPayload = NSMutableData()
var part = self.payload(closeReasonCode: .invalidDataContents)
expectedPayload.append(part.bytes, length: part.length)
part = self.payload(text: "Failed to convert received payload to UTF-8 String")
expectedPayload.append(part.bytes, length: part.length)

self.performTest(framesToSend: [(true, self.opcodeText, payload)],
expectedFrames: [(true, self.opcodeClose, expectedPayload)],
expectation: expectation)
}
}

func testTextAndBinaryFrames() {
register(closeReason: .protocolError)

Expand Down

0 comments on commit 5a8b470

Please sign in to comment.