mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-25 04:47:51 +00:00
Reject * as a URL authentication type
`.type("*")` encoded as `;AUTH=*`, which parses as `.any`; `.type("*x")`
failed to parse, since `*` matched first.
RFC 5092: `iauth = ";AUTH=" ( "*" / enc-auth-type )`. `init?(_:)`
rejects `*`; the parser tries the longer `enc-auth-type` first.
This commit is contained in:
3 files changed
+34
-5
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user