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

Header directive parameter quoting #2441

Merged
merged 2 commits into from Jul 15, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 20 additions & 2 deletions Sources/Vapor/HTTP/Headers/HTTPHeaders+Directive.swift
Expand Up @@ -177,7 +177,11 @@ extension HTTPHeaders {
for directive in directives {
let string: String
if let parameter = directive.parameter {
string = "\(directive.value)=\(parameter)"
if self.shouldQuote(parameter) {
string = "\(directive.value)=\"\(parameter.escapingDoubleQuotes())\""
} else {
string = "\(directive.value)=\(parameter)"
}
} else {
string = .init(directive.value)
}
Expand All @@ -188,20 +192,31 @@ extension HTTPHeaders {

return main.joined(separator: ", ")
}

private func shouldQuote(_ parameter: Substring) -> Bool {
parameter.contains(where: {
$0 == .space || $0 == .doubleQuote
})
}
}
}

private extension Substring {
/// Converts all `\"` to `"`.
func unescapingDoubleQuotes() -> Substring {
return self.lazy.split(separator: "\\").reduce(into: "") { (result, part) in
self.lazy.split(separator: "\\").reduce(into: "") { (result, part) in
if result.isEmpty || part.first == "\"" {
result += part
} else {
result += "\\" + part
}
}
}

/// Converts all `"` to `\"`.
func escapingDoubleQuotes() -> String {
self.split(separator: "\"").joined(separator: "\\\"")
}
}


Expand All @@ -227,6 +242,9 @@ private extension Character {
static var period: Self {
.init(".")
}
static var space: Self {
.init(" ")
}

var isDirectiveKey: Bool {
self.isLetter || self.isNumber || self == .dash || self == .underscore || self == .period
Expand Down
13 changes: 12 additions & 1 deletion Tests/VaporTests/HTTPHeaderTests.swift
@@ -1,7 +1,7 @@
@testable import Vapor
import XCTest

final class HTTPHeaderValueTests: XCTestCase {
final class HTTPHeaderTests: XCTestCase {
func testValue() throws {
var parser = HTTPHeaders.DirectiveParser(string: "foobar")
XCTAssertEqual(parser.nextDirectives(), [.init(value: "foobar")])
Expand Down Expand Up @@ -196,4 +196,15 @@ final class HTTPHeaderValueTests: XCTestCase {
let upper = HTTPMediaType(type: "foo", subType: "BAR")
XCTAssertEqual(lower, upper)
}

// https://github.com/vapor/vapor/issues/2439
func testContentDispositionQuotedFilename() throws {
var headers = HTTPHeaders()
headers.contentDisposition = .init(.formData, filename: "foo")
XCTAssertEqual(headers.first(name: .contentDisposition), "form-data; filename=foo")
headers.contentDisposition = .init(.formData, filename: "foo bar")
XCTAssertEqual(headers.first(name: .contentDisposition), #"form-data; filename="foo bar""#)
headers.contentDisposition = .init(.formData, filename: "foo\"bar")
XCTAssertEqual(headers.first(name: .contentDisposition), #"form-data; filename="foo\"bar""#)
}
}