diff --git a/Bitkit/Models/PubkyAuthRequest.swift b/Bitkit/Models/PubkyAuthRequest.swift index c5c31bfe1..f805e5571 100644 --- a/Bitkit/Models/PubkyAuthRequest.swift +++ b/Bitkit/Models/PubkyAuthRequest.swift @@ -189,19 +189,90 @@ struct PubkyAuthRequest { isSignup && (!isGrantSignup || !hasIdentity) } - /// The network origin that receives the authorization. This is a delivery destination, not a service identity. - var relayOrigin: String? { - guard let components = URLComponents(string: relay), - let scheme = components.scheme?.lowercased(), - ["http", "https"].contains(scheme), - let host = components.host?.lowercased(), - !host.isEmpty - else { - return nil + /// The relay URL that receives the authorization, without user info or fragment so the host cannot be disguised. + /// This is a delivery destination, not a service identity. Nil only when the request has no relay. + var relayDisplayUrl: String? { + Self.displayRelay(relay) + } + + /// The relay as shown on the approval sheet: scheme, host, port, path and query, exactly as the Pubky SDK's URL parser + /// keeps them. Nil when `relay` is not already in that form apart from letter case, user info, the fragment and a + /// missing root path: a space, control or non-ASCII characters, a backslash before the query, characters the parser + /// percent-encodes in the path or in the query, a missing, percent-encoded or non-canonical host, a default, padded + /// or out-of-range port, or a `.` or `..` path segment. + static func displayRelay(_ relay: String) -> String? { + // rust-url 2.5.8 (`url/src/parser.rs`): the `PATH` set is percent-encoded in a path and a backslash becomes a + // slash; a query of an http(s) URL percent-encodes the `SPECIAL_QUERY` set. + let pathRewritten = "\\\"<>`{}" + let queryRewritten = "\"<>'" + guard relay.unicodeScalars.allSatisfy({ $0.value > 0x20 && $0.value < 0x7F }), + let schemeEnd = relay.range(of: "://"), schemeEnd.lowerBound > relay.startIndex + else { return nil } + let scheme = relay[.. Bool { + let matches = { (pattern: String, value: String) in + value.range(of: "^(?:\(pattern))$", options: .regularExpression) != nil && !value.contains("\n") + } + guard matches(#"(\[[0-9a-f:.]+\]|[a-z0-9._~-]+)(:[0-9]{1,5})?"#, hostPort) else { return false } + let portStart = hostPort.hasSuffix("]") ? nil : hostPort.lastIndex(of: ":") + let host = String(hostPort[..<(portStart ?? hostPort.endIndex)]) + if let portStart { + let portText = String(hostPort[hostPort.index(after: portStart)...]) + guard let port = Int(portText), String(port) == portText, port <= 65535, port != defaultPort else { return false } + } + if host.hasPrefix("[") { + return isCanonicalIpv6(String(host.dropFirst().dropLast())) + } + // A last label that reads as a number makes the SDK's URL parser treat the whole host as an IPv4 address. + let trimmedHost = host.hasSuffix(".") ? String(host.dropLast()) : host + let lastLabel = String(trimmedHost.split(separator: ".", omittingEmptySubsequences: false).last ?? "") + let octet = "(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])" + return !matches("[0-9]+|0x[0-9a-f]*", lastLabel) || matches("(\(octet)\\.){3}\(octet)", host) + } + + private static func isCanonicalIpv6(_ address: String) -> Bool { + let halves = address.components(separatedBy: "::") + let head = halves[0].split(separator: ":").map(String.init) + let tail = halves.count > 1 ? halves[1].split(separator: ":").map(String.init) : [] + let missing = 8 - head.count - tail.count + let isComplete = halves.count == 1 ? missing == 0 : halves.count == 2 && missing >= 1 + guard isComplete, !address.contains(".") else { return false } + let parsed = (head + Array(repeating: "0", count: missing) + tail).map { Int($0, radix: 16) } + let groups = parsed.compactMap { $0 } + guard groups.count == 8, groups.allSatisfy({ $0 <= 0xFFFF }) else { return false } + var runStart = 0 + var runLength = 0 + var index = 0 + while index < groups.count { + let end = groups[index...].firstIndex { $0 != 0 } ?? groups.count + if end - index > runLength { + runStart = index + runLength = end - index + } + index = max(end, index + 1) } - - let port = components.port.map { ":\($0)" } ?? "" - return "\(scheme)://\(host)\(port)" + let hex = { (part: ArraySlice) in part.map { String($0, radix: 16) }.joined(separator: ":") } + let canonical = runLength < 2 + ? hex(groups[...]) + : "\(hex(groups[.. Bool { @@ -344,6 +415,9 @@ struct PubkyAuthRequest { .compactMap { extractServiceName($0.path) } .filter { seenServiceNames.insert($0).inserted } let bitkitClaim = try parseBitkitClaim(url: url, capabilities: capabilities, requiresBitkitClaim: requiresBitkitClaim) + guard relay.isEmpty || displayRelay(relay) != nil else { + throw PubkyAuthRequestError.invalidUrl + } let expectedSigner = try parseExpectedSigner(url: url) return PubkyAuthRequest( rawUrl: url, diff --git a/Bitkit/Resources/Localization/ar.lproj/Localizable.strings b/Bitkit/Resources/Localization/ar.lproj/Localizable.strings index 52e1b4ed8..37a519134 100644 --- a/Bitkit/Resources/Localization/ar.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/ar.lproj/Localizable.strings @@ -764,7 +764,7 @@ "pubky_auth__watch_only_intro_title" = "اربح BITCOIN\nمن\nمحتواك"; "pubky_auth__watch_only_account_xpub_error" = "تعذر على Bitkit إنشاء xpub صالح للحساب."; "pubky_auth__trust_warning" = "تأكد من أنك تثق بالخدمة والمتصفح والجهاز قبل التفويض باستخدام pubky الخاص بك."; -"pubky_auth__authorization_relay" = "مُرحّل التفويض"; +"pubky_auth__authorization_relay" = "المُرحّل"; "pubky_auth__authorizing" = "جارٍ التفويض..."; "pubky_auth__success_title" = "تم التفويض بنجاح"; "pubky_auth__success_prefix" = "لقد فوّضت باستخدام pubky "; diff --git a/Bitkit/Resources/Localization/ca.lproj/Localizable.strings b/Bitkit/Resources/Localization/ca.lproj/Localizable.strings index 311a30660..10c3aaff8 100644 --- a/Bitkit/Resources/Localization/ca.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/ca.lproj/Localizable.strings @@ -848,7 +848,7 @@ "pubky_auth__watch_only_intro_title" = "GUANYA BITCOIN\nAMB EL TEU\nCONTINGUT"; "pubky_auth__watch_only_account_xpub_error" = "Bitkit no ha pogut crear un xpub de compte vàlid."; "pubky_auth__trust_warning" = "Assegura't que confies en el servei, el navegador i el dispositiu abans d'autoritzar amb el teu pubky."; -"pubky_auth__authorization_relay" = "RELÉ D'AUTORITZACIÓ"; +"pubky_auth__authorization_relay" = "RELÉ"; "pubky_auth__authorizing" = "Autoritzant..."; "pubky_auth__success_title" = "Autorització correcta"; "pubky_auth__success_prefix" = "T'has autoritzat amb el pubky "; diff --git a/Bitkit/Resources/Localization/cs.lproj/Localizable.strings b/Bitkit/Resources/Localization/cs.lproj/Localizable.strings index 0f4390e98..4b801d8e7 100644 --- a/Bitkit/Resources/Localization/cs.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/cs.lproj/Localizable.strings @@ -907,7 +907,7 @@ "pubky_auth__watch_only_intro_title" = "VYDĚLÁVEJTE BITCOIN\nZE SVÉHO\nOBSAHU"; "pubky_auth__watch_only_account_xpub_error" = "Bitkit nemohl vytvořit platný xpub účtu."; "pubky_auth__trust_warning" = "Než se autorizujete pomocí svého pubky, ujistěte se, že službě, prohlížeči a zařízení důvěřujete."; -"pubky_auth__authorization_relay" = "AUTORIZAČNÍ RELAY"; +"pubky_auth__authorization_relay" = "RELAY"; "pubky_auth__authorizing" = "Autorizování..."; "pubky_auth__success_title" = "Autorizace proběhla úspěšně"; "pubky_auth__success_prefix" = "Autorizovali jste se pomocí pubky "; diff --git a/Bitkit/Resources/Localization/de.lproj/Localizable.strings b/Bitkit/Resources/Localization/de.lproj/Localizable.strings index 1c00c95d3..eebd6333c 100644 --- a/Bitkit/Resources/Localization/de.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/de.lproj/Localizable.strings @@ -905,7 +905,7 @@ "pubky_auth__watch_only_intro_title" = "VERDIENE BITCOIN\nMIT DEINEN\nINHALTEN"; "pubky_auth__watch_only_account_xpub_error" = "Bitkit konnte keinen gültigen Konto-xpub erstellen."; "pubky_auth__trust_warning" = "Stelle sicher, dass du dem Dienst, dem Browser und dem Gerät vertraust, bevor du dich mit deinem pubky autorisierst."; -"pubky_auth__authorization_relay" = "AUTORISIERUNGS-RELAY"; +"pubky_auth__authorization_relay" = "RELAY"; "pubky_auth__authorizing" = "Autorisiere..."; "pubky_auth__success_title" = "Autorisierung erfolgreich"; "pubky_auth__success_prefix" = "Du hast dich mit dem pubky "; diff --git a/Bitkit/Resources/Localization/el.lproj/Localizable.strings b/Bitkit/Resources/Localization/el.lproj/Localizable.strings index 3b2e0e0d0..9e99067ef 100644 --- a/Bitkit/Resources/Localization/el.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/el.lproj/Localizable.strings @@ -795,7 +795,7 @@ "pubky_auth__watch_only_intro_title" = "ΚΕΡΔΙΣΤΕ BITCOIN\nΑΠΟ ΤΟ\nΠΕΡΙΕΧΟΜΕΝΟ ΣΑΣ"; "pubky_auth__watch_only_account_xpub_error" = "Το Bitkit δεν μπόρεσε να δημιουργήσει έγκυρο xpub λογαριασμού."; "pubky_auth__trust_warning" = "Βεβαιωθείτε ότι εμπιστεύεστε την υπηρεσία, τον browser και τη συσκευή πριν εξουσιοδοτήσετε με το pubky σας."; -"pubky_auth__authorization_relay" = "ΑΝΑΜΕΤΑΔΟΤΗΣ ΕΞΟΥΣΙΟΔΟΤΗΣΗΣ"; +"pubky_auth__authorization_relay" = "ΑΝΑΜΕΤΑΔΟΤΗΣ"; "pubky_auth__authorizing" = "Εξουσιοδότηση..."; "pubky_auth__success_title" = "Επιτυχής Εξουσιοδότηση"; "pubky_auth__success_prefix" = "Εξουσιοδοτήσατε με το pubky "; diff --git a/Bitkit/Resources/Localization/en.lproj/Localizable.strings b/Bitkit/Resources/Localization/en.lproj/Localizable.strings index 455904ca4..0ab5ab662 100644 --- a/Bitkit/Resources/Localization/en.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/en.lproj/Localizable.strings @@ -895,7 +895,7 @@ "pubky_auth__watch_only_intro_title" = "EARN MONEY\nFROM YOUR\nCONTENT"; "pubky_auth__watch_only_account_xpub_error" = "Bitkit could not create a valid account xpub."; "pubky_auth__trust_warning" = "Make sure you trust the service, browser, and device before authorizing with your pubky."; -"pubky_auth__authorization_relay" = "AUTHORIZATION RELAY"; +"pubky_auth__authorization_relay" = "RELAY"; "pubky_auth__authorizing" = "Authorizing..."; "pubky_auth__success_title" = "Authorization Successful"; "pubky_auth__success_prefix" = "You authorized with pubky "; diff --git a/Bitkit/Resources/Localization/es-419.lproj/Localizable.strings b/Bitkit/Resources/Localization/es-419.lproj/Localizable.strings index ea5caaaf9..dd7b64e75 100644 --- a/Bitkit/Resources/Localization/es-419.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/es-419.lproj/Localizable.strings @@ -908,7 +908,7 @@ "pubky_auth__watch_only_intro_title" = "GANE BITCOIN\nCON SU\nCONTENIDO"; "pubky_auth__watch_only_account_xpub_error" = "Bitkit no pudo crear un xpub de cuenta válido."; "pubky_auth__trust_warning" = "Asegúrese de confiar en el servicio, el navegador y el dispositivo antes de autorizar con su pubky."; -"pubky_auth__authorization_relay" = "RELAY DE AUTORIZACIÓN"; +"pubky_auth__authorization_relay" = "RELAY"; "pubky_auth__authorizing" = "Autorizando..."; "pubky_auth__success_title" = "Autorización exitosa"; "pubky_auth__success_prefix" = "Autorizó con el pubky "; diff --git a/Bitkit/Resources/Localization/es.lproj/Localizable.strings b/Bitkit/Resources/Localization/es.lproj/Localizable.strings index 23daa0952..c175e8226 100644 --- a/Bitkit/Resources/Localization/es.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/es.lproj/Localizable.strings @@ -856,7 +856,7 @@ "pubky_auth__watch_only_intro_title" = "GANE BITCOIN\nCON SU\nCONTENIDO"; "pubky_auth__watch_only_account_xpub_error" = "Bitkit no pudo crear un xpub de cuenta válido."; "pubky_auth__trust_warning" = "Asegúrese de que confía en el servicio, el navegador y el dispositivo antes de autorizar con su pubky."; -"pubky_auth__authorization_relay" = "RELÉ DE AUTORIZACIÓN"; +"pubky_auth__authorization_relay" = "RELÉ"; "pubky_auth__authorizing" = "Autorizando..."; "pubky_auth__success_title" = "Autorización correcta"; "pubky_auth__success_prefix" = "Ha autorizado con el pubky "; diff --git a/Bitkit/Resources/Localization/fr.lproj/Localizable.strings b/Bitkit/Resources/Localization/fr.lproj/Localizable.strings index 2d666b449..dfbb9e6b6 100644 --- a/Bitkit/Resources/Localization/fr.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/fr.lproj/Localizable.strings @@ -908,7 +908,7 @@ "pubky_auth__watch_only_intro_title" = "GAGNEZ DES BITCOINS\nGRÂCE À VOTRE\nCONTENU"; "pubky_auth__watch_only_account_xpub_error" = "Bitkit n'a pas pu créer de xpub de compte valide."; "pubky_auth__trust_warning" = "Assurez-vous de faire confiance au service, au navigateur et à l'appareil avant de vous authentifier avec votre pubky."; -"pubky_auth__authorization_relay" = "RELAIS D'AUTORISATION"; +"pubky_auth__authorization_relay" = "RELAIS"; "pubky_auth__authorizing" = "Autorisation…"; "pubky_auth__success_title" = "Autorisation réussie"; "pubky_auth__success_prefix" = "Vous vous êtes authentifié avec le pubky "; diff --git a/Bitkit/Resources/Localization/it.lproj/Localizable.strings b/Bitkit/Resources/Localization/it.lproj/Localizable.strings index 79d8bc9c7..a15e3f9b8 100644 --- a/Bitkit/Resources/Localization/it.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/it.lproj/Localizable.strings @@ -889,7 +889,7 @@ "pubky_auth__watch_only_intro_title" = "GUADAGNA BITCOIN\nCON I TUOI\nCONTENUTI"; "pubky_auth__watch_only_account_xpub_error" = "Bitkit non è riuscito a creare un xpub valido per l'account."; "pubky_auth__trust_warning" = "Assicurati di fidarti del servizio, del browser e del dispositivo prima di autorizzare con il tuo pubky."; -"pubky_auth__authorization_relay" = "RELAY DI AUTORIZZAZIONE"; +"pubky_auth__authorization_relay" = "RELAY"; "pubky_auth__authorizing" = "Autorizzazione..."; "pubky_auth__success_title" = "Autorizzazione Riuscita"; "pubky_auth__success_prefix" = "Hai autorizzato con il pubky "; diff --git a/Bitkit/Resources/Localization/nl.lproj/Localizable.strings b/Bitkit/Resources/Localization/nl.lproj/Localizable.strings index 2e2471a21..c0ce102b0 100644 --- a/Bitkit/Resources/Localization/nl.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/nl.lproj/Localizable.strings @@ -905,7 +905,7 @@ "pubky_auth__watch_only_intro_title" = "VERDIEN BITCOIN\nMET UW\nCONTENT"; "pubky_auth__watch_only_account_xpub_error" = "Bitkit kon geen geldige account-xpub aanmaken."; "pubky_auth__trust_warning" = "Zorg dat u de service, browser en het apparaat vertrouwt voordat u met uw pubky autoriseert."; -"pubky_auth__authorization_relay" = "AUTORISATIERELAY"; +"pubky_auth__authorization_relay" = "RELAY"; "pubky_auth__authorizing" = "Autoriseren..."; "pubky_auth__success_title" = "Autorisatie geslaagd"; "pubky_auth__success_prefix" = "U hebt geautoriseerd met pubky "; diff --git a/Bitkit/Resources/Localization/pl.lproj/Localizable.strings b/Bitkit/Resources/Localization/pl.lproj/Localizable.strings index ce220f7e5..270a4112e 100644 --- a/Bitkit/Resources/Localization/pl.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/pl.lproj/Localizable.strings @@ -907,7 +907,7 @@ "pubky_auth__watch_only_intro_title" = "ZARABIAJ BITCOINY\nNA SWOICH\nTREŚCIACH"; "pubky_auth__watch_only_account_xpub_error" = "Bitkit nie mógł utworzyć prawidłowego xpub konta."; "pubky_auth__trust_warning" = "Upewnij się, że ufasz usłudze, przeglądarce i urządzeniu, zanim autoryzujesz za pomocą swojego pubky."; -"pubky_auth__authorization_relay" = "PRZEKAŹNIK AUTORYZACJI"; +"pubky_auth__authorization_relay" = "PRZEKAŹNIK"; "pubky_auth__authorizing" = "Autoryzowanie..."; "pubky_auth__success_title" = "Autoryzacja zakończona"; "pubky_auth__success_prefix" = "Autoryzowano za pomocą pubky "; diff --git a/Bitkit/Resources/Localization/pt-BR.lproj/Localizable.strings b/Bitkit/Resources/Localization/pt-BR.lproj/Localizable.strings index 03e5352af..77ab822da 100644 --- a/Bitkit/Resources/Localization/pt-BR.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/pt-BR.lproj/Localizable.strings @@ -907,7 +907,7 @@ "pubky_auth__watch_only_intro_title" = "GANHE BITCOIN\nCOM O SEU\nCONTEÚDO"; "pubky_auth__watch_only_account_xpub_error" = "A Bitkit não conseguiu criar um xpub de conta válido."; "pubky_auth__trust_warning" = "Certifique-se de que confia no serviço, no navegador e no dispositivo antes de autorizar com sua pubky."; -"pubky_auth__authorization_relay" = "RELAY DE AUTORIZAÇÃO"; +"pubky_auth__authorization_relay" = "RELAY"; "pubky_auth__authorizing" = "Autorizando..."; "pubky_auth__success_title" = "Autorização Concluída"; "pubky_auth__success_prefix" = "Você autorizou com a pubky "; diff --git a/Bitkit/Resources/Localization/pt.lproj/Localizable.strings b/Bitkit/Resources/Localization/pt.lproj/Localizable.strings index dafdbb0ad..9d1f5fb06 100644 --- a/Bitkit/Resources/Localization/pt.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/pt.lproj/Localizable.strings @@ -813,7 +813,7 @@ "pubky_auth__watch_only_intro_title" = "GANHE BITCOIN\nCOM O SEU\nCONTEÚDO"; "pubky_auth__watch_only_account_xpub_error" = "A Bitkit não conseguiu criar uma xpub de conta válida."; "pubky_auth__trust_warning" = "Confirme que confia no serviço, no navegador e no dispositivo antes de autorizar com o seu pubky."; -"pubky_auth__authorization_relay" = "RELAY DE AUTORIZAÇÃO"; +"pubky_auth__authorization_relay" = "RELAY"; "pubky_auth__authorizing" = "A autorizar..."; "pubky_auth__success_title" = "Autorização concluída"; "pubky_auth__success_prefix" = "Autorizou com o pubky "; diff --git a/Bitkit/Resources/Localization/ru.lproj/Localizable.strings b/Bitkit/Resources/Localization/ru.lproj/Localizable.strings index a5c4b51ac..2fe8638c5 100644 --- a/Bitkit/Resources/Localization/ru.lproj/Localizable.strings +++ b/Bitkit/Resources/Localization/ru.lproj/Localizable.strings @@ -903,7 +903,7 @@ "pubky_auth__watch_only_intro_title" = "ЗАРАБАТЫВАЙТЕ БИТКОИН\nНА СВОЁМ\nКОНТЕНТЕ"; "pubky_auth__watch_only_account_xpub_error" = "Bitkit не смог создать действительный xpub аккаунта."; "pubky_auth__trust_warning" = "Убедитесь, что вы доверяете сервису, браузеру и устройству, прежде чем авторизоваться с вашим pubky."; -"pubky_auth__authorization_relay" = "РЕТРАНСЛЯТОР АВТОРИЗАЦИИ"; +"pubky_auth__authorization_relay" = "РЕТРАНСЛЯТОР"; "pubky_auth__authorizing" = "Авторизация..."; "pubky_auth__success_title" = "Авторизация Выполнена"; "pubky_auth__success_prefix" = "Вы авторизовались с pubky "; diff --git a/Bitkit/Views/Sheets/PubkyAuthApproval/PubkyAuthApprovalSheet.swift b/Bitkit/Views/Sheets/PubkyAuthApproval/PubkyAuthApprovalSheet.swift index 66974ac40..228a5df4f 100644 --- a/Bitkit/Views/Sheets/PubkyAuthApproval/PubkyAuthApprovalSheet.swift +++ b/Bitkit/Views/Sheets/PubkyAuthApproval/PubkyAuthApprovalSheet.swift @@ -438,22 +438,29 @@ struct PubkyAuthApprovalSheet: View { .padding(.bottom, 32) } - if let relayOrigin = config.request.relayOrigin { - relayOriginSection(relayOrigin) - .padding(.bottom, 32) - } + let relayUrl = config.request.relayDisplayUrl + let showsPaykitAccess = config.request.bitkitClaim?.includesPaykitAccess == true if !config.request.permissions.isEmpty { permissionsSection + if relayUrl != nil || showsPaykitAccess { + sectionDivider + } + } + + if let relayUrl { + relaySection(relayUrl) + if showsPaykitAccess { + sectionDivider + } } - if config.request.bitkitClaim?.includesPaykitAccess == true { + if showsPaykitAccess { VStack(alignment: .leading, spacing: 8) { CaptionMText(t("pubky_auth__details"), textColor: .white64).frame(minHeight: 18) BodySText(t("pubky_auth__paykit_access_description"), textColor: .textPrimary, kerning: 0, lineHeight: 20) .fixedSize(horizontal: false, vertical: true) } - .padding(.top, 32) .accessibilityElement(children: .contain) .accessibilityIdentifier("PubkyAuthPaykitAccess") } @@ -539,15 +546,33 @@ struct PubkyAuthApprovalSheet: View { } } - private func relayOriginSection(_ relayOrigin: String) -> some View { + private func relaySection(_ relayUrl: String) -> some View { VStack(alignment: .leading, spacing: 8) { - CaptionMText(t("pubky_auth__authorization_relay"), textColor: .white64) - BodySSBText(relayOrigin) - .accessibilityIdentifier("PubkyAuthRelayOrigin") - CustomDivider(color: .white10) + CaptionMText(t("pubky_auth__authorization_relay"), textColor: .white64).frame(minHeight: 18) + HStack(alignment: .top, spacing: 4) { + Image("globe") + .resizable() + .scaledToFit() + .foregroundColor(.white) + .frame(width: 16, height: 16) + .padding(.top, 2) + .accessibilityHidden(true) + + BodySSBText(pubkyAuthLiteralText(relayUrl)) + .fixedSize(horizontal: false, vertical: true) + .textSelection(.enabled) + .accessibilityIdentifier("PubkyAuthRelayOrigin") + + Spacer(minLength: 0) + } } } + private var sectionDivider: some View { + CustomDivider(color: .white10) + .padding(.vertical, 12) + } + private var successDescriptionText: some View { let authorization = requesterText.isEmpty ? t("pubky_auth__success_prefix") + "" + truncatedPublicKey + "" diff --git a/BitkitTests/PubkyAuthApprovalSheetTests.swift b/BitkitTests/PubkyAuthApprovalSheetTests.swift index 077a59b72..a12dc2d0e 100644 --- a/BitkitTests/PubkyAuthApprovalSheetTests.swift +++ b/BitkitTests/PubkyAuthApprovalSheetTests.swift @@ -179,6 +179,35 @@ final class PubkyAuthApprovalSheetTests: XCTestCase { } } + @MainActor + func testExpectedSignerAndRelayRulesHoldTogether() throws { + let relay = "https%3A%2F%2Fhttprelay.pubky.app%2Finbox%2Fo%27connor%3Ftoken%3D%7Babc%7D" + let shown = "https://httprelay.pubky.app/inbox/o'connor?token={abc}" + let base = "pubkyauth://signin_grant?caps=/pub/example/:rw&relay=\(relay)" + + "&secret=e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3s&cid=paykit.test&cpk=\(approvalTestClientPublicKey)" + + let matching = try PubkyAuthRequest.parse(url: "\(base)&spk=\(approvalTestSignerPublicKey)") + XCTAssertEqual(matching.expectedSigner, approvalTestSignerPublicKey) + XCTAssertEqual(matching.relayDisplayUrl, shown) + let sheets = SheetViewModel() + let app = AppViewModel(sheetViewModel: sheets, navigationViewModel: NavigationViewModel()) + app.presentPubkyAuthApproval(matching, signerPublicKey: approvalTestSignerPublicKey, hasSecretKey: true) + XCTAssertEqual(sheets.pubkyAuthApprovalSheetItem?.request.relayDisplayUrl, shown) + + let other = try PubkyAuthRequest.parse(url: "\(base)&spk=\(approvalTestClientPublicKey)") + XCTAssertEqual(other.relayDisplayUrl, shown) + let refusedSheets = SheetViewModel() + let refusedApp = AppViewModel(sheetViewModel: refusedSheets, navigationViewModel: NavigationViewModel()) + ToastWindowManager.shared.hideToast() + var shownToasts: [Bitkit.Toast] = [] + let subscription = ToastWindowManager.shared.$currentToast.compactMap { $0 }.sink { shownToasts.append($0) } + defer { subscription.cancel() } + refusedApp.presentPubkyAuthApproval(other, signerPublicKey: approvalTestSignerPublicKey, hasSecretKey: true) + XCTAssertNil(refusedSheets.pubkyAuthApprovalSheetItem) + XCTAssertEqual(shownToasts.map(\.accessibilityIdentifier), ["PubkyAuthOtherPubkyToast"]) + ToastWindowManager.shared.hideToast() + } + func testRefusalNamesThePubkyTheRequestIsFor() { let description = pubkyAuthApprovalFailureDescription( PubkyAuthRequestError.signerMismatch(expectedSigner: approvalTestClientPublicKey) diff --git a/BitkitTests/PubkyAuthRequestTests.swift b/BitkitTests/PubkyAuthRequestTests.swift index bfbe3ee32..00662bd73 100644 --- a/BitkitTests/PubkyAuthRequestTests.swift +++ b/BitkitTests/PubkyAuthRequestTests.swift @@ -83,14 +83,143 @@ final class PubkyAuthRequestTests: XCTestCase { } } - func testRelayOriginShowsOnlyTheAuthorizationDestination() throws { + func testRelayDisplayUrlKeepsThePathAndDropsUserInfoAndFragment() throws { let url = "bitkit://pubky-auth/setup?caps=\(PubkyAuthClaim.requiredCapabilities)" + "&relay=https%3A%2F%2FRelay.Example%3A8443%2Finbox%2F&secret=\(secret)" + "&cid=paykit.test&cpk=\(publicKey)&x-bitkit-claim=watch-only-account-v1" let request = try PubkyAuthRequest.parse(url: url) - XCTAssertEqual(request.relayOrigin, "https://relay.example:8443") + XCTAssertEqual(request.relayDisplayUrl, "https://relay.example:8443/inbox/") + + let shown = [ + "https://httprelay.pubky.app/link": "https://httprelay.pubky.app/link", + "https://httprelay.pubky.app": "https://httprelay.pubky.app/", + "HTTPS://httprelay.pubky.app/link": "https://httprelay.pubky.app/link", + "http://localhost:8080/link": "http://localhost:8080/link", + "https://relay.example:0/link": "https://relay.example:0/link", + "https://relay.example:80/link": "https://relay.example:80/link", + "https://evil.example?@relay.example": "https://evil.example/?@relay.example", + "https://relay.example/?x=@evil.example": "https://relay.example/?x=@evil.example", + "https://evil.example#@relay.example": "https://evil.example/", + "https://relay.example/a/.../b/..c/": "https://relay.example/a/.../b/..c/", + "https://relay.example/inbox?next=../x": "https://relay.example/inbox?next=../x", + "https://[::1]/link": "https://[::1]/link", + "http://[::1]:443/": "http://[::1]:443/", + "https://relay.example:65535/link": "https://relay.example:65535/link", + "https://[2001:DB8::1]/link": "https://[2001:db8::1]/link", + "https://[::ffff:102:304]/link": "https://[::ffff:102:304]/link", + "https://relay.example/a/..;x": "https://relay.example/a/..;x", + "https://relay.example/a/..%2f": "https://relay.example/a/..%2f", + "https://[2001:db8:0:1::]/link": "https://[2001:db8:0:1::]/link", + "http://user@10.0.2.2:15412/link?x=1#y": "http://10.0.2.2:15412/link?x=1", + "https://trusted.example@relay.example/in%20box/": "https://relay.example/in%20box/", + "https://relay.example/a|b?c=[e]": "https://relay.example/a|b?c=[e]", + "https://relay.example/o'neil/inbox/": "https://relay.example/o'neil/inbox/", + "https://relay.example/a^b/[c]": "https://relay.example/a^b/[c]", + "https://relay.example/inbox/?x={y}": "https://relay.example/inbox/?x={y}", + "https://relay.example/inbox/?x=a^b|c": "https://relay.example/inbox/?x=a^b|c", + "https://relay.example/inbox/?x=a`b": "https://relay.example/inbox/?x=a`b", + "https://relay.example/inbox/?x=a\\b": "https://relay.example/inbox/?x=a\\b", + "https://relay.example?x={y}": "https://relay.example/?x={y}", + "https://us'er:pa{ss}@relay.example/inbox/": "https://relay.example/inbox/", + "https://relay.example/inbox/#a\"b'\\": "https://relay.example/inbox/", + "https://relay.example?x=1": "https://relay.example/?x=1", + "https://relay.example./link": "https://relay.example./link", + "https://-re_lay~.1abc/link": "https://-re_lay~.1abc/link", + "https://xn--e1aybc.example/link": "https://xn--e1aybc.example/link", + "https://[2001:DB8::1]:8443/link": "https://[2001:db8::1]:8443/link", + "https://127.0.0.1/link": "https://127.0.0.1/link", + "https://evil.example/@httprelay.pubky.app/link": "https://evil.example/@httprelay.pubky.app/link", + ] + for (relay, expected) in shown { + XCTAssertEqual(PubkyAuthRequest.displayRelay(relay), expected, relay) + } + } + + func testRelayWhoseDestinationCannotBeReadWithCertaintyIsNotShownAsAnotherHost() { + let unreadable = [ + "https://evil.example\\@httprelay.pubky.app/link", + "https://httprelay.pubky.app\\.evil.example/link", + "https://реле.example/link", + "https://%65vil.example/link", + "https://0x7f.1/link", + "https://2130706433/link", + "https://1.2.3/link", + "https://127.0.0.1./link", + "https://relay.example /link", + "https://relay\t.example/link", + "https://relay.example\n/link", + "https://relay.example:/link", + "https://relay.example:443/link", + "http://relay.example:80/link", + "https://relay.example:65536/link", + "https://relay.example:08443/link", + "https://relay.example/inbox/../../other/", + "https://relay.example/inbox/./x", + "https://relay.example/inbox/%2e%2e/other/", + "https://relay.example/inbox/%2E./other/", + "https://relay.example/inbox/..", + "https://relay.example/..?x=1", + "https://relay.example/a{b}", + "https://relay.example/a?b='c'", + "https://relay.example/a\"b", + "https://relay.example/a}b", + "https://relay.example/a\\b", + "https://relay.example/a/\\..\\b", + "https://user\\@relay.example/inbox/", + "https://relay.example/inbox/?x=\"y\"", + "https://relay.example/inbox/?x=", + "https://relay.example/inbox/?x='y", + "https://relay.example/inbox/?x=a b", + "https://relay.example/ab", + "https://relay.example/a`b", + "https://relay.example/inbox/%2e/x", + "https://relay.example/inbox/.%2E/other/", + "https://[::1]:443/", + "https://[2001:db8:0:0:0:0:0:1]/link", + "https://[2001:0db8::1]/link", + "https://[1:0:0:2:0:0:0:3]/link", + "https://[::ffff:1.2.3.4]/link", + "https://[1::2::3]/link", + "https://[::12345]/link", + "https://[1:2:3:4:5:6:7:8:9]/link", + "https:///inbox/", + "https:/relay.example/inbox/", + "https:relay.example/inbox/", + "ftp://relay.example/inbox/", + "relay.example/inbox/", + "not a url", + "", + ] + for relay in unreadable { + XCTAssertNil(PubkyAuthRequest.displayRelay(relay), relay) + } + } + + func testRequestWithARelayThatCannotBeShownIsInvalid() throws { + // The Ring signup path hands the relay over as written, so it is the one the SDK would parse differently. + let ringSignup = "pubkyring://signup?hs=\(publicKey)&secret=\(secret)&caps=%2Fpub%2Fexample.app%2F%3Arw&relay=" + let request = try PubkyAuthRequest.parse(url: ringSignup + "https%3A%2F%2Frelay.example%2Finbox%2F") + XCTAssertEqual(request.relayDisplayUrl, "https://relay.example/inbox/") + + for relay in [ + "https%3A%2F%2Fevil.example%5C%40relay.example%2Finbox%2F", + "https%3A%2F%2Frel%C3%A4y.example%2Finbox%2F", + "https%3A%2F%2F0x7f.1%2Finbox%2F", + "https%3A%2F%2Frelay.example%2Finbox%2F..%2F..%2Fother%2F", + "https%3A%2F%2Frelay.example%2Finbox%2F%252e%252e%2Fother%2F", + ] { + XCTAssertThrowsError(try PubkyAuthRequest.parse(url: ringSignup + relay), relay) { + XCTAssertEqual($0 as? PubkyAuthRequestError, .invalidUrl, relay) + } + } + + let direct = try PubkyAuthRequest.parse(url: directSignupUrl(action: "direct_signup")) + XCTAssertEqual(direct.relay, "") + XCTAssertNil(direct.relayDisplayUrl) } func testProtocolUrlRejectsBitkitSpecificSetupHandoffWithoutClaimMarker() { diff --git a/changelog.d/next/1502.changed.md b/changelog.d/next/1502.changed.md new file mode 100644 index 000000000..f64d4e8bb --- /dev/null +++ b/changelog.d/next/1502.changed.md @@ -0,0 +1 @@ +Changed the Pubky authorization screen to show the relay's full address next to a globe icon. diff --git a/journeys/pubky-auth/open-signin-link.xml b/journeys/pubky-auth/open-signin-link.xml index af87c764a..7c1d4f7a4 100644 --- a/journeys/pubky-auth/open-signin-link.xml +++ b/journeys/pubky-auth/open-signin-link.xml @@ -6,7 +6,7 @@ If the simulator asks to open the link in Bitkit, tap Open Verify the authorize button (id "PubkyAuthAuthorize") is visible Wait 5 seconds, then verify the authorize button (id "PubkyAuthAuthorize") is still visible - Verify the relay origin (id "PubkyAuthRelayOrigin") shows "https://httprelay.pubky.app" + Verify the relay (id "PubkyAuthRelayOrigin") shows "https://httprelay.pubky.app/inbox/" next to a globe icon under the RELAY label and below the requested permissions Verify the Paykit access row (id "PubkyAuthPaykitAccess") is not visible Tap Cancel (id "PubkyAuthCancel") Verify the authorize button (id "PubkyAuthAuthorize") is no longer visible