-
Notifications
You must be signed in to change notification settings - Fork 68
Modify Mail to allow folding of long header fields #123
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
Open
sbeitzel
wants to merge
7
commits into
Kitura:master
Choose a base branch
from
sbeitzel:feature/121_wrap_unwrap
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
78e7c41
Fix #121 by wrapping headers that we might have assembled as too long.
sbeitzel ac30e26
Update file headers with Apache License, per the contributor guidelines
sbeitzel 49faad1
Renamed the files with fold/unfold in mind. Modified unit tests to wo…
sbeitzel 6163d7e
Renamed unit test to be more descriptive. Put folding logic in the Ma…
sbeitzel 6e784f9
Isolate magic numbers in the folding code. Minor touch-ups to unit te…
sbeitzel b549ca5
Update copyright with project
sbeitzel d0e8ba6
Merge branch 'Kitura:master' into feature/121_wrap_unwrap
sbeitzel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /** | ||
| * Copyright Kitura 2021-2022 | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| **/ | ||
|
|
||
| @testable import SwiftSMTP | ||
| import LoggerAPI | ||
| import XCTest | ||
|
|
||
| internal class TestLogger: Logger { | ||
| var messages = [String]() | ||
|
|
||
| func log(_ type: LoggerMessageType, msg: String, functionName: String, lineNum: Int, fileName: String) { | ||
| let message = "[\(type.description.uppercased())] \(msg)" | ||
| messages.append(message) | ||
| } | ||
|
|
||
| func isLogging(_ level: LoggerMessageType) -> Bool { true } | ||
| } | ||
|
|
||
| class TestHeaderFolding: XCTestCase { | ||
| let spaciousSubject = "Sample subject with extra whitespace inside" | ||
| var mailMessage: Mail { | ||
| Mail(from: Mail.User(name: "Test User", email: "[email protected]"), | ||
| to: manyRecipients, | ||
| cc: [absurdlyLongEmail], | ||
| subject: spaciousSubject, | ||
| text: "Just some message", | ||
| additionalHeaders: ["X-OBNOXIOUS": egregiousLine, | ||
| "X-SHORT": spaciousSubject]) | ||
| } | ||
|
|
||
| let manyRecipients: [Mail.User] = Array.init(repeating: Mail.User(email: "[email protected]"), | ||
| count: 30) | ||
| let absurdlyLongEmail = Mail.User(name: "Unfortunate Joe", | ||
| email: "unfortunate_joe_1234567890_123456789_123456783_123456784_123456785_123456786@dumbster.local") | ||
|
|
||
| // this is a base64 encoding of Package.swift | ||
| let egregiousLine = "Ly8gc3dpZnQtdG9vbHMtdmVyc2lvbjo1LjAKCmltcG9ydCBQYWNrYWdlRGVzY3JpcHRpb24KCmxldCBwYWNrYWdlID0gUGFja2FnZSgKICAgIG5hbWU6ICJTd2lmdFNNVFAiLAogICAgcHJvZHVjdHM6IFsKICAgICAgICAubGlicmFyeSgKICAgICAgICAgICAgbmFtZTogIlN3aWZ0U01UUCIsCiAgICAgICAgICAgIHRhcmdldHM6IFsiU3dpZnRTTVRQIl0pLAogICAgICAgIF0sCiAgICBkZXBlbmRlbmNpZXM6IFsKICAgICAgICAucGFja2FnZSh1cmw6ICJodHRwczovL2dpdGh1Yi5jb20vS2l0dXJhL0JsdWVTb2NrZXQuZ2l0IiwgZnJvbTogIjIuMC4yIiksCiAgICAgICAgLnBhY2thZ2UodXJsOiAiaHR0cHM6Ly9naXRodWIuY29tL0tpdHVyYS9CbHVlU1NMU2VydmljZS5naXQiLCBmcm9tOiAiMi4wLjEiKSwKICAgICAgICAucGFja2FnZSh1cmw6ICJodHRwczovL2dpdGh1Yi5jb20vS2l0dXJhL0JsdWVDcnlwdG9yLmdpdCIsIGZyb206ICIyLjAuMSIpLAogICAgICAgIC5wYWNrYWdlKHVybDogImh0dHBzOi8vZ2l0aHViLmNvbS9LaXR1cmEvTG9nZ2VyQVBJLmdpdCIsIGZyb206ICIxLjkuMjAwIiksCiAgICAgICAgXSwKICAgIHRhcmdldHM6IFsKICAgICAgICAudGFyZ2V0KAogICAgICAgICAgICBuYW1lOiAiU3dpZnRTTVRQIiwKICAgICAgICAgICAgZGVwZW5kZW5jaWVzOiBbIlNvY2tldCIsICJTU0xTZXJ2aWNlIiwgIkNyeXB0b3IiLCAiTG9nZ2VyQVBJIl0pLAogICAgICAgIC50ZXN0VGFyZ2V0KAogICAgICAgICAgICBuYW1lOiAiU3dpZnRTTVRQVGVzdHMiLAogICAgICAgICAgICBkZXBlbmRlbmNpZXM6IFsiU3dpZnRTTVRQIl0pLAogICAgICAgIF0KKQo=" | ||
|
|
||
| var previousLogger: Logger? | ||
| var currentLogger: TestLogger? | ||
|
|
||
| override func setUpWithError() throws { | ||
| previousLogger = Log.logger | ||
| currentLogger = TestLogger() | ||
| Log.logger = currentLogger | ||
| } | ||
|
|
||
| override func tearDownWithError() throws { | ||
| Log.logger = previousLogger | ||
| currentLogger = nil | ||
| } | ||
|
|
||
| // Here we test two things: 1) a short line will not be folded, | ||
| // and 2) a line containing multiple consecutive whitespace characters | ||
| // will not be changed. | ||
| func testShortHeaderUnchanged() throws { | ||
| let allHeaders = mailMessage.headersString.split(separator: "\r\n") | ||
| let unmodified = "X-SHORT: \(spaciousSubject)" | ||
| for header in allHeaders where header.hasPrefix("X-SHORT") { | ||
| XCTAssertEqual(String(header), unmodified) | ||
| } | ||
| } | ||
|
|
||
| // Whatever else is true of the folded header, the folding process should | ||
| // not ever produce a line which consists solely of whitespace. | ||
| func testNoWSOnlyLines() throws { | ||
| let allHeaders = mailMessage.headersString.split(separator: "\r\n") | ||
| for header in allHeaders { | ||
| XCTAssert(!header.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty) | ||
| } | ||
| } | ||
|
|
||
| // This test looks at folding a long name plus email address, which won't | ||
| // contain a comma. The fold should happen at the space between the name | ||
| // and the email. | ||
| func testAbsurdEmail() throws { | ||
| let allHeaders = mailMessage.headersString.split(separator: "\r\n") | ||
| var foundCC = false | ||
| var testEncountered = false | ||
| for header in allHeaders { | ||
| if foundCC { | ||
| // we are now at the first line *after* the CC: line | ||
| XCTAssertEqual(header, " <unfortunate_joe_1234567890_123456789_123456783_123456784_123456785_123456786@dumbster.local>") | ||
| testEncountered = true | ||
| break | ||
| } else if header.hasPrefix("CC:") { | ||
| foundCC = true | ||
| XCTAssertEqual(header, "CC: \("Unfortunate Joe".mimeEncoded!) ") | ||
| } | ||
| } | ||
| XCTAssertTrue(testEncountered, "The CC line did not get folded!") | ||
| } | ||
|
|
||
| // This test looks at what happens when a header value does not | ||
| // contain any spaces at all, and is thus unfoldable, while still | ||
| // being longer than the recommended length. | ||
| func testUnfoldableHeader() throws { | ||
| let allHeaders = mailMessage.headersString.split(separator: "\r\n") | ||
| for header in allHeaders where header.hasPrefix("X-OBNOXIOUS") { | ||
| XCTAssertEqual(header, "X-OBNOXIOUS: \(egregiousLine)") | ||
| } | ||
| // Incidentally, the line is longer than the RFC mandated maximum length. | ||
| // Currently, the library does not throw an error in this case, but | ||
| // the folding code should at least log a message about it, so that | ||
| // consumers of the library can have some help figuring out why their | ||
| // mail message was rejected by a remote SMTP server. | ||
| guard let currentLogger = currentLogger else { | ||
| throw XCTSkip("There is no logger installed!") | ||
| } | ||
| XCTAssert(!currentLogger.messages | ||
| .filter({ $0.hasPrefix("[ERROR] Header line length") }) | ||
| .isEmpty) | ||
| } | ||
|
|
||
| // This test looks at a long list of email addresses. There are plenty | ||
| // of commas and whitespaces, so folding is possible. It should happen | ||
| // at the whitespace, and not in the middle of an address. | ||
| func testFoldOnWhitespace() throws { | ||
| let allHeaders = mailMessage | ||
| .headersString.split(separator: "\r\n") | ||
| for header in allHeaders where header.hasPrefix("TO: ") { | ||
| XCTAssert(header.hasSuffix("[email protected], ")) | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.