From 0ccc3a2932922ddd48aacedfa6ece7cd98da690b Mon Sep 17 00:00:00 2001 From: Daniel Eggert Date: Thu, 10 Feb 2022 17:11:48 +0100 Subject: [PATCH] =?UTF-8?q?Fix=20parsing=20escapes=20in=20=E2=80=9Cquoted?= =?UTF-8?q?=E2=80=9D=20strings=20(#686)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Parser/Grammar/GrammarParser.swift | 38 ++++++++++++++++++- .../Grammar/GrammarParser+Message+Tests.swift | 3 +- .../Parser/IMAPParserTests.swift | 35 ++++++++++++++++- 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/Sources/NIOIMAPCore/Parser/Grammar/GrammarParser.swift b/Sources/NIOIMAPCore/Parser/Grammar/GrammarParser.swift index f07ae9cd..acfbd372 100644 --- a/Sources/NIOIMAPCore/Parser/Grammar/GrammarParser.swift +++ b/Sources/NIOIMAPCore/Parser/Grammar/GrammarParser.swift @@ -1734,14 +1734,48 @@ extension GrammarParser { } // quoted = DQUOTE *QUOTED-CHAR DQUOTE + // QUOTED-CHAR = / + // "\" 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 } } diff --git a/Tests/NIOIMAPCoreTests/Parser/Grammar/GrammarParser+Message+Tests.swift b/Tests/NIOIMAPCoreTests/Parser/Grammar/GrammarParser+Message+Tests.swift index 63786bc9..237f9240 100644 --- a/Tests/NIOIMAPCoreTests/Parser/Grammar/GrammarParser+Message+Tests.swift +++ b/Tests/NIOIMAPCoreTests/Parser/Grammar/GrammarParser+Message+Tests.swift @@ -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: [] diff --git a/Tests/NIOIMAPCoreTests/Parser/IMAPParserTests.swift b/Tests/NIOIMAPCoreTests/Parser/IMAPParserTests.swift index a5c19a73..58cd963c 100644 --- a/Tests/NIOIMAPCoreTests/Parser/IMAPParserTests.swift +++ b/Tests/NIOIMAPCoreTests/Parser/IMAPParserTests.swift @@ -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),