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

Fix handling unicode characters in multipart filenames #2826

Merged
merged 5 commits into from
May 17, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 75 additions & 1 deletion Tests/VaporTests/ContentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ final class ContentTests: XCTestCase {
XCTAssertContains(res.body.string, "hi")
}
}

func testMultipartDecode() throws {
let data = """
--123\r
Expand Down Expand Up @@ -149,6 +149,52 @@ final class ContentTests: XCTestCase {
XCTAssertEqualJSON(res.body.string, expected)
}
}

func testMultipartDecodeUnicode() throws {
let data = """
--123\r
Content-Disposition: form-data; name="name"\r
\r
Vapor\r
--123\r
Content-Disposition: form-data; name="age"\r
\r
4\r
--123\r
Content-Disposition: form-data; name="image"; filename="她在吃水果.png"; filename*="UTF-8\'\'%E5%A5%B9%E5%9C%A8%E5%90%83%E6%B0%B4%E6%9E%9C.png"\r
\r
<contents of image>\r
--123--\r

"""
let expected = User(
name: "Vapor",
age: 4,
image: File(data: "<contents of image>", filename: "UTF-8\'\'%E5%A5%B9%E5%9C%A8%E5%90%83%E6%B0%B4%E6%9E%9C.png")
0xTim marked this conversation as resolved.
Show resolved Hide resolved
)

struct User: Content, Equatable {
var name: String
var age: Int
var image: File
}

let app = Application(.testing)
defer { app.shutdown() }

app.routes.get("multipart") { req -> User in
let decoded = try req.content.decode(User.self)
XCTAssertEqual(decoded, expected)
return decoded
}

try app.testable().test(.GET, "/multipart", headers: [
"Content-Type": "multipart/form-data; boundary=123"
], body: .init(string: data)) { res in
XCTAssertEqual(res.status, .ok)
XCTAssertEqualJSON(res.body.string, expected)
}
}

func testMultipartEncode() throws {
struct User: Content {
Expand Down Expand Up @@ -177,6 +223,34 @@ final class ContentTests: XCTestCase {
XCTAssertContains(res.body.string, "name=\"image\"")
}
}

func testMultiPartEncodeUnicode() throws {
struct User: Content {
static var defaultContentType: HTTPMediaType = .formData
var name: String
var age: Int
var image: File
}

let app = Application(.testing)
defer { app.shutdown() }

app.get("multipart") { req -> User in
return User(
name: "Vapor",
age: 4,
image: File(data: "<contents of image>", filename: "UTF-8\'\'%E5%A5%B9%E5%9C%A8%E5%90%83%E6%B0%B4%E6%9E%9C.png")
)
}
try app.testable().test(.GET, "/multipart") { res in
XCTAssertEqual(res.status, .ok)
let boundary = res.headers.contentType?.parameters["boundary"] ?? "none"
XCTAssertContains(res.body.string, "Content-Disposition: form-data; name=\"name\"")
XCTAssertContains(res.body.string, "--\(boundary)")
XCTAssertContains(res.body.string, "filename=UTF-8\'\'%E5%A5%B9%E5%9C%A8%E5%90%83%E6%B0%B4%E6%9E%9C.png")
XCTAssertContains(res.body.string, "name=\"image\"")
}
}

func testURLEncodedFormDecode() throws {
struct User: Content {
Expand Down