From 9a7f9b29f05365f6f45d0146451b6bb9ac9c08ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zsolt=20Va=CC=81radi?= Date: Thu, 30 Mar 2017 13:41:00 +0200 Subject: [PATCH 1/2] Add failing test for vapor/vapor#938 --- Tests/FormDataTests/ParserTests.swift | 28 ++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/Tests/FormDataTests/ParserTests.swift b/Tests/FormDataTests/ParserTests.swift index baf87db..924dd5f 100644 --- a/Tests/FormDataTests/ParserTests.swift +++ b/Tests/FormDataTests/ParserTests.swift @@ -8,7 +8,8 @@ class ParserTests: XCTestCase { ("testFormData", testFormData), ("testWebkit", testWebkit), ("testForm", testForm), - ("testFormManyFields", testFormManyFields) + ("testFormManyFields", testFormManyFields), + ("testBoundaryLikeContent", testBoundaryLikeContent) ] func testFormData() throws { @@ -139,4 +140,29 @@ class ParserTests: XCTestCase { XCTAssertEqual(fields["field\(i)"]?.part.body.makeString(), "The Quick Brown Fox Jumps Over The Lazy Dog", "Field 'field\(i)' was parsed incorrectly!") } } + + func testBoundaryLikeContent() throws { + var message = "" + + message += "------WebKitFormBoundaryezkRLRyEVe1aMUVZ\r\n" + message += "Content-Disposition: form-data; name=\"file\"; filename=\"test.txt\"\r\n" + message += "Content-Type: text/plain\r\n" + message += "\r\n" + message += "---this is a test\r\n" + message += "------WebKitFormBoundaryezkRLRyEVe1aMUVZ--\r\n" + + let multipart = try Multipart.Parser(boundary: "----WebKitFormBoundaryezkRLRyEVe1aMUVZ") + let parser = FormData.Parser(multipart: multipart) + + var fields: [String: Field] = [:] + + parser.onField = { field in + fields[field.name] = field + } + + try parser.multipart.parse(message) + + XCTAssertEqual(fields["file"]?.filename, "test.txt") + XCTAssertEqual("---this is a test", fields["file"]?.part.body.string) + } } From 10acbcf6e1238d7e1eeaa1afc8f13a6632e0e888 Mon Sep 17 00:00:00 2001 From: tanner0101 Date: Mon, 3 Apr 2017 13:43:42 +0200 Subject: [PATCH 2/2] check for buffer mismatch every boundary parse cycle --- Sources/Multipart/BoundaryParser.swift | 3 +-- Tests/FormDataTests/ParserTests.swift | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Sources/Multipart/BoundaryParser.swift b/Sources/Multipart/BoundaryParser.swift index cc1c102..81c111e 100644 --- a/Sources/Multipart/BoundaryParser.swift +++ b/Sources/Multipart/BoundaryParser.swift @@ -43,8 +43,7 @@ final class BoundaryParser { let match = [.hyphen, .hyphen] + boundary if - (buffer.count <= 1 && byte == .hyphen) || - (buffer.count > 1 && buffer.count < match.count) + buffer.count < match.count && match[buffer.count] == byte { state = .parsing(buffer: buffer + [byte], trailingHyphenCount: trailingHyphenCount) break main diff --git a/Tests/FormDataTests/ParserTests.swift b/Tests/FormDataTests/ParserTests.swift index 924dd5f..d9c19c5 100644 --- a/Tests/FormDataTests/ParserTests.swift +++ b/Tests/FormDataTests/ParserTests.swift @@ -163,6 +163,6 @@ class ParserTests: XCTestCase { try parser.multipart.parse(message) XCTAssertEqual(fields["file"]?.filename, "test.txt") - XCTAssertEqual("---this is a test", fields["file"]?.part.body.string) + XCTAssertEqual("---this is a test", fields["file"]?.part.body.makeString()) } }