diff --git a/Sources/NIOIMAPCore/Grammar/EncodedAuthenticationType.swift b/Sources/NIOIMAPCore/Grammar/EncodedAuthenticationType.swift index 357829d2..7c94c619 100644 --- a/Sources/NIOIMAPCore/Grammar/EncodedAuthenticationType.swift +++ b/Sources/NIOIMAPCore/Grammar/EncodedAuthenticationType.swift @@ -75,7 +75,7 @@ public struct EncodedAuthenticationType: Hashable, Sendable { /// returning `nil`. /// /// - parameter stringValue: The percent-encoded string, for example `PLAIN`. - /// - returns: A new value, or `nil` if `stringValue` is not a valid `enc-auth-type`. + /// - returns: A new value, or `nil` if `stringValue` is `*` or not a valid `enc-auth-type`. public init?(_ stringValue: String) { guard let normalized = Self.normalized(stringValue) else { return nil } self.stringValue = normalized @@ -91,10 +91,15 @@ public struct EncodedAuthenticationType: Hashable, Sendable { /// ``` /// enc-auth-type = 1*achar /// ``` - /// See ``URLCharacterClass/achar``. + /// See ``URLCharacterClass/achar``. Not allowed: `*`, since `;AUTH=*` means any mechanism + /// (``IMAPURLAuthenticationMechanism/any``). static func normalized(_ string: String) -> String? { - string.validatedAndNormalizedURLComponent(.achar) + guard string != Self.anyMechanism else { return nil } + return string.validatedAndNormalizedURLComponent(.achar) } + + /// What `;AUTH=` is followed by to mean any mechanism. + static let anyMechanism = "*" } extension String { diff --git a/Sources/NIOIMAPCore/Parser/Grammar/GrammarParser.swift b/Sources/NIOIMAPCore/Parser/Grammar/GrammarParser.swift index e1e11559..efb725e1 100644 --- a/Sources/NIOIMAPCore/Parser/Grammar/GrammarParser.swift +++ b/Sources/NIOIMAPCore/Parser/Grammar/GrammarParser.swift @@ -535,7 +535,11 @@ extension GrammarParser { ) throws -> EncodedAuthenticationType { try PL.composite(buffer: &buffer, tracker: tracker) { buffer, tracker -> EncodedAuthenticationType in let array = try PL.parseOneOrMore(buffer: &buffer, tracker: tracker, parser: self.parseAChar).reduce([], +) - return .init(unchecked: String(decoding: array, as: Unicode.UTF8.self)) + let string = String(decoding: array, as: Unicode.UTF8.self) + guard string != EncodedAuthenticationType.anyMechanism else { + throw ParserError(hint: "`*` is any mechanism, not an enc-auth-type") + } + return .init(unchecked: string) } } @@ -898,9 +902,10 @@ extension GrammarParser { return try PL.composite(buffer: &buffer, tracker: tracker) { (buffer, tracker) in try PL.parseFixedString(";AUTH=", buffer: &buffer, tracker: tracker) + // Longest match first: `;AUTH=*x` is the type `*x`, not any mechanism. return try PL.parseOneOf( - parseIMAPURLAuthenticationMechanism_any, parseIMAPURLAuthenticationMechanism_encoded, + parseIMAPURLAuthenticationMechanism_any, buffer: &buffer, tracker: tracker ) diff --git a/Tests/NIOIMAPCoreTests/Grammar/EncodedURLComponent+Tests.swift b/Tests/NIOIMAPCoreTests/Grammar/EncodedURLComponent+Tests.swift index 23a33360..4cd9ed11 100644 --- a/Tests/NIOIMAPCoreTests/Grammar/EncodedURLComponent+Tests.swift +++ b/Tests/NIOIMAPCoreTests/Grammar/EncodedURLComponent+Tests.swift @@ -28,6 +28,25 @@ struct EncodedURLComponentTests { #expect(EncodedAuthenticationType(string) == nil) } + /// `;AUTH=*` means any mechanism, so `*` can't be a mechanism name. + @Test("* is not an authentication type") + func asteriskAuthenticationType() { + #expect(EncodedAuthenticationType("*" as String) == nil) + #expect(EncodedAuthenticationType("**" as String) != nil) + } + + @Test( + "URL authentication mechanisms round-trip", + arguments: [IMAPURLAuthenticationMechanism.any, .type("**"), .type("*x")] + ) + func authenticationMechanismRoundTrip(_ mechanism: IMAPURLAuthenticationMechanism) { + TestUtilities.expectRoundTrip( + mechanism, + encoder: { $0.writeIMAPURLAuthenticationMechanism($1) }, + parser: GrammarParser().parseIMAPURLAuthenticationMechanism + ) + } + @Test("invalid bchar components are rejected", arguments: invalidBcharStrings) func invalidBchar(_ string: String) { #expect(EncodedMailbox(string) == nil)