Fix Invidious login failing for passwords with special characters

Use URLComponents/URLQueryItem for standard form-URL encoding instead
of manual percent-encoding with CharacterSet.alphanumerics, which
included non-ASCII Unicode letters and had an unsafe raw-value fallback.
This commit is contained in:
Arkadiusz Fal
2026-02-12 06:04:16 +01:00
parent d546c0a976
commit d8d16280ff

View File

@@ -349,16 +349,16 @@ actor InvidiousAPI: InstanceAPI {
/// - instance: The Invidious instance to log in to /// - instance: The Invidious instance to log in to
/// - Returns: The session ID (SID) cookie value /// - Returns: The session ID (SID) cookie value
func login(email: String, password: String, instance: Instance) async throws -> String { func login(email: String, password: String, instance: Instance) async throws -> String {
// Build form-urlencoded body // Build form-urlencoded body using URLComponents for standard encoding
let bodyComponents = [ var components = URLComponents()
"email": email, components.queryItems = [
"password": password, URLQueryItem(name: "email", value: email),
"action": "signin" URLQueryItem(name: "password", value: password),
URLQueryItem(name: "action", value: "signin")
] ]
let formAllowed = CharacterSet.alphanumerics.union(CharacterSet(charactersIn: "-._~")) // URLQueryItem leaves '+' unencoded, but in form-urlencoded '+' means space
let bodyString = bodyComponents let bodyString = components.percentEncodedQuery?
.map { "\($0.key)=\($0.value.addingPercentEncoding(withAllowedCharacters: formAllowed) ?? $0.value)" } .replacingOccurrences(of: "+", with: "%2B") ?? ""
.joined(separator: "&")
guard let bodyData = bodyString.data(using: .utf8) else { guard let bodyData = bodyString.data(using: .utf8) else {
throw APIError.invalidRequest throw APIError.invalidRequest