Skip to content

Commit 73b7531

Browse files
authored
Merge pull request #457 from Vkt0r/content-length
Fix an issue causing a crash when the Content-Lenght was negative
2 parents 90c40e4 + 851330e commit 73b7531

File tree

5 files changed

+36
-16
lines changed

5 files changed

+36
-16
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ All notable changes to this project will be documented in this file. Changes not
4141
- Replace CircleCI for continuous integration in favor of Github Actions. ([#446](https://github.com/httpswift/swifter/pull/446)) by [@Vkt0r](https://github.com/Vkt0r)
4242
- Fix `SUPPORTED_PLATFORMS` for tvOS. This helps Carthage to build only the specified platform when the option `--platform` is used. ([#464](https://github.com/httpswift/swifter/pull/464)) by [@jasminlapalme](https://github.com/jasminlapalme)
4343

44+
## Fixed
45+
- Fix an issue causing a crash when the `Content-Lenght` was negative. ([#457](https://github.com/httpswift/swifter/pull/457)) by [@Vkt0r](https://github.com/Vkt0r)
46+
4447
# [1.4.7]
4548

4649
## Added

Gemfile.lock

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,51 @@ GEM
1111
colored2 (3.1.2)
1212
cork (0.3.0)
1313
colored2 (~> 3.1)
14-
danger (6.1.0)
14+
danger (8.0.5)
1515
claide (~> 1.0)
1616
claide-plugins (>= 0.9.2)
1717
colored2 (~> 3.1)
1818
cork (~> 0.1)
19-
faraday (~> 0.9)
19+
faraday (>= 0.9.0, < 2.0)
2020
faraday-http-cache (~> 2.0)
21-
git (~> 1.5)
22-
kramdown (~> 2.0)
21+
git (~> 1.7)
22+
kramdown (~> 2.3)
2323
kramdown-parser-gfm (~> 1.0)
2424
no_proxy_fix
2525
octokit (~> 4.7)
2626
terminal-table (~> 1)
27-
danger-swiftlint (0.23.0)
27+
danger-swiftlint (0.24.4)
2828
danger
2929
rake (> 10)
3030
thor (~> 0.19)
31-
faraday (0.17.0)
31+
faraday (1.0.1)
3232
multipart-post (>= 1.2, < 3)
33-
faraday-http-cache (2.0.0)
34-
faraday (~> 0.8)
35-
git (1.5.0)
33+
faraday-http-cache (2.2.0)
34+
faraday (>= 0.8)
35+
git (1.7.0)
36+
rchardet (~> 1.8)
3637
kramdown (2.3.0)
3738
rexml
3839
kramdown-parser-gfm (1.1.0)
3940
kramdown (~> 2.0)
4041
multipart-post (2.1.1)
4142
nap (1.1.0)
4243
no_proxy_fix (0.1.2)
43-
octokit (4.14.0)
44+
octokit (4.18.0)
45+
faraday (>= 0.9)
4446
sawyer (~> 0.8.0, >= 0.5.3)
4547
open4 (1.3.4)
46-
public_suffix (4.0.1)
47-
rake (13.0.0)
48+
public_suffix (4.0.6)
49+
rake (13.0.1)
50+
rchardet (1.8.0)
4851
rexml (3.2.4)
4952
sawyer (0.8.2)
5053
addressable (>= 2.3.5)
5154
faraday (> 0.8, < 2.0)
5255
terminal-table (1.8.0)
5356
unicode-display_width (~> 1.1, >= 1.1.1)
5457
thor (0.20.3)
55-
unicode-display_width (1.6.0)
58+
unicode-display_width (1.7.0)
5659

5760
PLATFORMS
5861
ruby
@@ -62,4 +65,4 @@ DEPENDENCIES
6265
danger-swiftlint
6366

6467
BUNDLED WITH
65-
1.17.2
68+
1.17.3

XCode/Sources/HttpParser.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
import Foundation
99

10-
enum HttpParserError: Error {
10+
enum HttpParserError: Error, Equatable {
1111
case invalidStatusLine(String)
12+
case negativeContentLength
1213
}
1314

1415
public class HttpParser {
@@ -29,6 +30,11 @@ public class HttpParser {
2930
request.queryParams = urlComponents?.queryItems?.map { ($0.name, $0.value ?? "") } ?? []
3031
request.headers = try readHeaders(socket)
3132
if let contentLength = request.headers["content-length"], let contentLengthValue = Int(contentLength) {
33+
// Prevent a buffer overflow and runtime error trying to create an `UnsafeMutableBufferPointer` with
34+
// a negative length
35+
guard contentLengthValue >= 0 else {
36+
throw HttpParserError.negativeContentLength
37+
}
3238
request.body = try readBody(socket, size: contentLengthValue)
3339
}
3440
return request

XCode/Sources/WebSockets.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public class WebSocketSession: Hashable, Equatable {
233233
frm.rsv3 = fst & 0x10
234234
guard frm.rsv1 == 0 && frm.rsv2 == 0 && frm.rsv3 == 0
235235
else {
236-
throw WsError.protocolError("Reserved frame bit has not been negocitated.")
236+
throw WsError.protocolError("Reserved frame bit has not been negociated.")
237237
}
238238
let opc = fst & 0x0F
239239
guard let opcode = OpCode(rawValue: opc) else {

XCode/Tests/SwifterTestsHttpParser.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ class SwifterTestsHttpParser: XCTestCase {
101101
XCTAssert(false, "Parser should not throw any errors if there is a valid 'Content-Length' header.")
102102
}
103103

104+
do {
105+
_ = try parser.readHttpRequest(TestSocket("GET / HTTP/1.0\r\nContent-Length: -1\r\n\r\n"))
106+
} catch let error {
107+
let error = error as? HttpParserError
108+
XCTAssertNotNil(error)
109+
XCTAssertEqual(error!, HttpParserError.negativeContentLength)
110+
}
111+
104112
do {
105113
_ = try parser.readHttpRequest(TestSocket("GET / HTTP/1.0\nContent-Length: 5\n\n12345"))
106114
} catch {

0 commit comments

Comments
 (0)