mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-30 05:28:46 +00:00
Fix parsing escapes in “quoted” strings (#686)
This commit is contained in:
1 parent
9c2c43f0a3
commit
0ccc3a2932
3 files changed
+72
-4
No files matched your search
@@ -1734,14 +1734,48 @@ extension GrammarParser {
|
||||
}
|
||||
|
||||
// quoted = DQUOTE *QUOTED-CHAR DQUOTE
|
||||
// QUOTED-CHAR = <any TEXT-CHAR except quoted-specials> /
|
||||
// "\" quoted-specials
|
||||
// quoted-specials = DQUOTE / "\"
|
||||
func parseQuoted(buffer: inout ParseBuffer, tracker: StackTracker) throws -> ByteBuffer {
|
||||
try PL.composite(buffer: &buffer, tracker: tracker) { buffer, tracker -> ByteBuffer in
|
||||
try PL.parseFixedString("\"", buffer: &buffer, tracker: tracker)
|
||||
var hasQuotedSpecial = false
|
||||
var lastIsEscape = false
|
||||
let data = try PL.parseZeroOrMoreCharacters(buffer: &buffer, tracker: tracker) { char in
|
||||
char.isQuotedChar
|
||||
if lastIsEscape {
|
||||
lastIsEscape = false
|
||||
return char.isQuotedSpecial
|
||||
} else if char.isQuotedChar {
|
||||
lastIsEscape = false
|
||||
return true
|
||||
} else if char == UInt8(ascii: "\\") {
|
||||
lastIsEscape = true
|
||||
hasQuotedSpecial = true
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
try PL.parseFixedString("\"", buffer: &buffer, tracker: tracker)
|
||||
return data
|
||||
guard hasQuotedSpecial else { return data }
|
||||
// Replace '\\' with '\' and '\"' with '"':
|
||||
var escaped = data
|
||||
var unescaped = ByteBuffer()
|
||||
while let next = escaped.readBytes(length: 1)?.first {
|
||||
if next == UInt8(ascii: "\\") {
|
||||
guard
|
||||
let special = escaped.readBytes(length: 1)?.first,
|
||||
special.isQuotedSpecial
|
||||
else {
|
||||
throw ParserError(hint: "Invalid escape in quoted string '\(String(decoding: data.readableBytesView, as: Unicode.UTF8.self))'")
|
||||
}
|
||||
unescaped.writeBytes([special])
|
||||
} else {
|
||||
unescaped.writeBytes([next])
|
||||
}
|
||||
}
|
||||
return unescaped
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -56,7 +56,8 @@ extension GrammarParser_Message_Tests {
|
||||
("X-GM-LABELS (\\Inbox \\Sent Important \"Muy Importante\")", " ", .gmailLabels([GmailLabel("\\Inbox"), GmailLabel("\\Sent"), GmailLabel("Important"), GmailLabel("Muy Importante")]), #line),
|
||||
("X-GM-LABELS (foo)", " ", .gmailLabels([GmailLabel("foo")]), #line),
|
||||
("X-GM-LABELS ()", " ", .gmailLabels([]), #line),
|
||||
("X-GM-LABELS (\\Drafts)", " ", .gmailLabels([GmailLabel("\\Drafts")]), #line),
|
||||
(#"X-GM-LABELS (\Drafts)"#, " ", .gmailLabels([GmailLabel(#"\Drafts"#)]), #line),
|
||||
(#"X-GM-LABELS ("\\Important")"#, " ", .gmailLabels([GmailLabel(#"\Important"#)]), #line),
|
||||
],
|
||||
parserErrorInputs: [],
|
||||
incompleteMessageInputs: []
|
||||
|
||||
@@ -1974,6 +1974,36 @@ extension ParserUnitTests {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - astring parseNString
|
||||
|
||||
extension ParserUnitTests {
|
||||
func testParseAString() {
|
||||
// 1*ASTRING-CHAR / quoted / literal
|
||||
self.iterateTests(
|
||||
testFunction: GrammarParser().parseAString,
|
||||
validInputs: [
|
||||
("NIL", " ", "NIL", #line),
|
||||
("A", " ", "A", #line),
|
||||
("Foo", " ", "Foo", #line),
|
||||
("a]b", " ", "a]b", #line),
|
||||
("{3}\r\nabc", "", "abc", #line),
|
||||
("{3+}\r\nabc", "", "abc", #line),
|
||||
(#""abc""#, "", "abc", #line),
|
||||
(#""a\\bc""#, "", #"a\bc"#, #line),
|
||||
(#""a\"bc""#, "", #"a"bc"#, #line),
|
||||
],
|
||||
parserErrorInputs: [
|
||||
(#""a\bc""#, "", #line),
|
||||
],
|
||||
incompleteMessageInputs: [
|
||||
("\"", "", #line),
|
||||
(#""a\""#, "", #line),
|
||||
("{1}\r\n", "", #line),
|
||||
]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - nstring parseNString
|
||||
|
||||
extension ParserUnitTests {
|
||||
@@ -1984,10 +2014,13 @@ extension ParserUnitTests {
|
||||
("NIL", "", nil, #line),
|
||||
("{3}\r\nabc", "", "abc", #line),
|
||||
("{3+}\r\nabc", "", "abc", #line),
|
||||
("\"abc\"", "", "abc", #line),
|
||||
(#""abc""#, "", "abc", #line),
|
||||
(#""a\\bc""#, "", #"a\bc"#, #line),
|
||||
(#""a\"bc""#, "", #"a"bc"#, #line),
|
||||
],
|
||||
parserErrorInputs: [
|
||||
("abc", " ", #line),
|
||||
(#""a\bc""#, "", #line),
|
||||
],
|
||||
incompleteMessageInputs: [
|
||||
("\"", "", #line),
|
||||
|
||||
Reference in new issue
Block a user