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

close on invalid UTF-8 #35

Merged
merged 3 commits into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
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