Skip to content

fix: refuse pubky auth requests for another pubky - #1492

Merged
ovitrif merged 3 commits into
masterfrom
fix/pubky-auth-spk
Nov 27, 2026
Merged

ovitrif merged 3 commits into
masterfrom
fix/pubky-auth-spk

Conversation

@jvsena42

@jvsena42 jvsena42 commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Fixes #1195
Twin: synonymdev/bitkit-android#2104

Grant sign-in links (pubkyauth://signin_grant, and Bitkit's bitkit://pubky-auth/signin and /setup handoffs) can carry spk=<z32 pubky>, the pubky the requesting app expects to approve (pubky/pubky-ring#399). Bitkit ignored it, signed with its own pubky, and the app then rejected the approval as a generic failure. Bitkit now refuses such a request and says which pubky it is for.

Description

  • Parsing: spk is read from the request's own query. Absent is fine. A repeated spk, an empty one, or anything other than a bare canonical 52-character z-base-32 key (so also pubky…/pk: prefixes, upper case, URL forms, padding variants) makes the request invalid, matching parse_expected_signer in the Pubky SDK (feat(sdk): let grant sign-in links require an expected identity pubky/pubky-homeserver#787). Signup requests ignore spk, as the SDK does. Every other request is held to it, so a sign-in link that the app's URL parser and the SDK read differently cannot skip the check.
  • Refusal before consent: when spk names a pubky other than this wallet's, the approval sheet does not open for consent. The user gets the existing auth error toast with "This request is for <first 4>...<last 4>, not this wallet's pubky."
  • Refusal before signing: the approval entry points compare spk with the public key derived from the secret key that would sign, and throw before any signing or relay call. This also covers the case where the wallet's pubky was not known when the sheet opened.
  • A request whose spk is this wallet's pubky behaves exactly as a request without spk.
  • No existing check is relaxed: the new checks only add rejections.
  • iOS specifics: PubkyAuthRequest.parseExpectedSigner and expectedSigner; AppViewModel.presentPubkyAuthApproval shows the toast (id PubkyAuthOtherPubkyToast) instead of the sheet; PubkyService.approveAuthRequest, approveAuth and approveAuthWithCompanionClaim throw PubkyAuthRequestError.signerMismatch before any signing call. New string pubky_auth__other_pubky in English and all 14 other locales.

Where the parsing belongs

Both apps parse grant links with parse_pubky_auth_url from pubky/paykit-rs (paykit-sdk/src/pubky_session.rs), not bitkit-core. paykit 0.1.1-rc9 pins pubky 0.16.2, which predates spk (added in pubky-homeserver v0.18.0), so today it ignores the parameter and PubkyAuthDetails has no field for it. The parsing in this PR is an app-side stopgap. Once paykit-rs moves to an SDK with expected_signer and exposes it on PubkyAuthDetails, the SDK parser rejects malformed links and PubkySigner refuses a mismatch itself; the app then only needs the field for the message, and this parser can go.

Out of Scope

  • pubky/paykit-rs: exposing expected_signer from parse_pubky_auth_url and moving to a Pubky SDK that knows spk; this PR does not change the dependency.
  • PubkyAuthApprovalSheet.swift: an "app chose this pubky" notice on the approval screen, as Pubky Ring shows. Bitkit holds one pubky, and the issue asks only for the refusal.

Design

N/A — no design available. The Figma Auth (paykit) flow has no error or refusal state; the refusal uses the existing auth error toast ("Authorization Failed").

Preview

Simulator run on iPhone 17 Pro (iOS 26.5), Debug regtest build at 45a49bb. Authorize was never tapped.

Case Result Screenshot
1. No spk Pass ios-spk-absent
2. spk = this wallet Pass ios-spk-matching
3. spk = another pubky Pass ios-spk-other-pubky
4. Repeated spk Pass ios-spk-repeated
5a. Malformed spk Pass ios-spk-malformed
5b. Prefixed spk Pass ios-spk-prefixed
5c. Empty spk Pass ios-spk-empty
6. Paykit setup, other pubky Pass ios-spk-other-pubky-paykit
7. Scanner paste, other pubky Pass ios-spk-other-pubky-scanner

QA Notes

Setup

Debug simulator build (regtest), wallet onboarded, a Pubky profile created in Bitkit. Read the wallet's pubky with Profile > Copy (ProfileCopy) and xcrun simctl pbpaste <UDID>; OWN is the 52 characters after the pubky prefix. All links are dummy requests: do not tap Authorize.

OWN=<this wallet's pubky, 52 characters, no prefix>
OTHER=8um71us3fyw6h8wbcxb5ar3rwusy1a6u49956ikzojg3gcwd1dty
QUERY='caps=/pub/locks.app/:rw&relay=https%3A%2F%2Fhttprelay.pubky.app%2Finbox%2F&secret=e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3s&cid=locks.test&cpk=5jsjx1o6fzu6aeeo697r3i5rx15zq41kikcye8wtwdqm4nb4tryo'
open_link() { xcrun simctl openurl <UDID> "bitkit://pubky-auth/signin?$1"; }
open_link "$QUERY&spk=$OWN"            # opens the approval sheet
open_link "$QUERY&spk=$OTHER"          # refused: "This request is for 8um7...1dty, not this wallet's pubky."
open_link "$QUERY&spk=$OWN&spk=$OWN"   # invalid request
open_link "$QUERY&spk=pubky$OWN"       # invalid request
open_link "$QUERY&spk=not-a-public-key" # invalid request

Journeys

  • J1: new signin-expected-pubky.xml — a sign-in link naming this wallet's pubky opens the sheet; another pubky is refused with a message naming it (PubkyAuthOtherPubkyToast); a repeated, prefixed or malformed spk is an invalid request (PubkyAuthInvalidRequestToast)

Manual Tests

N/A

Automated Checks

  • added PubkyAuthRequestTests.swift — spk absent, matching, repeated, malformed, prefixed, non-canonical, through pubkyauth://signin_grant and the bitkit://pubky-auth/signin handoff; control characters cannot hide it; a canonical value that is not a public key is invalid; signup requests ignore it; sign-in links the URL parser reads differently are still held to it; prefix handling when matching
  • added PubkyAuthApprovalSheetTests.swift — a request naming another pubky shows the refusal toast and opens no sheet, with or without a readable key; it throws before the ordinary or companion approval runs and prepares no watch-only account; approveAuth and approveAuthWithCompanionClaim refuse it before anything else; a matching request opens the sheet and is approved; a grant signup with an existing identity is approved whatever its spk says; the message names the pubky

Evidence

Simulator device run: cases 1 to 7 pass, see Preview. Expected log lines were not confirmed.

Local checks at 6186f224a:

  • xcodebuild test -scheme Bitkit -configuration Debug -destination 'platform=iOS Simulator,name=iPhone 17' -only-testing:BitkitTests — 4646 passed, 5 failed. The 5 failures are AddressTypeIntegrationTests (3) and UtxoSelectionTests (2), which need a local regtest electrs ("LocalRegtest funds the local bitcoind, but the node syncs from ssl://electrs.bitkit.stag0…"); they do not touch this change. PubkyAuthRequestTests, PubkyAuthApprovalSheetTests and PubkyAuthURLSchemeTests passed.
  • swiftformat --lint on the changed Swift files — 0 files require formatting.
  • node scripts/validate-translations.js — 0 errors.

Gaps

  • Settings > watch-only accounts list was not opened for case 6; regtest assumed from the Debug build.
  • The 5 local-regtest integration tests above did not run meaningfully on this machine.

Review

An independent review (fresh agent, no implementation context) found nothing HIGH or MEDIUM. It confirmed that every signing entry point is guarded and that the parsing matches the SDK. Its LOW findings, each checked against the code first:

  • Control characters could hide spk (iOS). URLComponents percent-encodes a literal tab or line break, so s<TAB>pk= was not read as spk, while the SDK's parser drops those characters. Fixed on iOS: any ASCII control character makes the request invalid. Android's java.net.URI already rejects them; a test now asserts it.
  • spk that is canonical z-base-32 but not a public key. It was refused as "another pubky". It is now an invalid request: the value goes through the Paykit key normalizer (Paykit.normalizePubkyPublicKey).
  • Side effects before the sign-time refusal (Android). When the wallet's pubky was unknown as the sheet loaded, the watch-only account was prepared before the refusal. Android only; fixed in the twin. On iOS the check already ran at the top of approveAuthRequest.
  • Order with a Ring-managed pubky. iOS showed "Use Pubky Ring" first. Both apps now refuse a request for another pubky first, comparing against the adopted Ring pubky when its key cannot be read, then show the Ring hint.
  • Test gaps. handlePubkyAuthApproval now ends in presentPubkyAuthApproval, which a test drives directly (toast shown, no sheet). Added a test that a signup_grant with an existing identity is approved with a foreign, malformed or repeated spk.
  • Legacy-link strictness. Kept: any request that is not a signup is held to spk, including legacy signin links the SDK would let through with a malformed value. Paykit accepts only grant links, so this changes nothing a user can reach.

Round 2 found no HIGH or MEDIUM; every signing entry point is checked.

  • signup_grant ignores spk. Confirmed against upstream, pubky/pubky-homeserver at d95161af3e: pubky-sdk/src/actors/auth/deep_links/signin_grant.rs:106 is the only caller of parse_expected_signer (query_params.rs:79), signup_grant.rs never reads spk, deep_link.rs:211 tests that signin and signup_grant ignore it, and docs/AUTH.md:81 scopes it to pubkyauth://signin_grant, as does the Open the pubky an auth request requires, instead of the key picker (expected identity) pubky/pubky-ring#399 spec ("Other request kinds don't carry it"). No behaviour change; the parser comment now cites these.

Merge order. This PR and #1502 edit neighbouring lines of the same files. Merge #1502 first; this PR then needs a rebase, keeping both sides of each conflict.

Models used

  • Planning/scoping: claude-fable-5-2 (reasoning: Not exposed)
  • Implementation: claude-fable-5-2 (reasoning: Not exposed)
  • Review: claude-fable-5-2 (reasoning: Not exposed)
  • Review rounds: 2

Metadata note: the sessions report the model id but no reasoning setting; the reviewer model is as reported by the coordinating session.

🤖 Generated with Claude Code

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
jvsena42 and others added 2 commits November 26, 2026 12:18
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@jvsena42
jvsena42 marked this pull request as ready for review November 26, 2026 17:29
@jvsena42
jvsena42 requested review from a team, ovitrif and piotr-iohk and removed request for a team November 26, 2026 17:29
@greptile-apps

greptile-apps Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5 · The PR appears safe to merge; no actionable issue was found.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issue was found.

Summary

Bitkit now reads spk from sign-in requests and refuses requests meant for another pubky.

  • Pubky sign-in links check the named pubky before approval.

Intentional scope recorded from jvsena42: signup ignores spk; legacy sign-in parsing stays strict; moving parsing into the SDK and adding an approval-screen notice are deferred.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Receive auth link] --> B[Parse request and spk]
  B -->|Invalid| C[Show invalid request message]
  B --> D{Names another pubky?}
  D -->|Yes, key known| E[Show refusal message]
  D -->|No or key unknown| F[Open consent sheet]
  F --> G[User approves]
  G --> H[Compare spk with signing key]
  H -->|Mismatch| E
  H -->|Matches or absent| I[Continue approval]
Loading

Reviews (1) · Last reviewed commit: "docs: cite upstream spk scope in pubky a..." · Reviewed by Greptile

@talosmachina talosmachina left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No findings. Reads spk from sign-in requests, refuses a request naming another pubky before the sheet opens, and throws signerMismatch at every signing entry point before any signing or relay call. Reviewed 45a49bb, full tier.

What I checked, and 5 candidates I ruled out

Read in full: the diff of PubkyAuthRequest.swift, PubkyService.swift, AppViewModel.swift, PubkyAuthApprovalSheet.swift, the new tests in PubkyAuthRequestTests.swift and PubkyAuthApprovalSheetTests.swift
Call sites traced: approveAuth, approveAuthWithCompanionClaim, approveAuthRequest, approveSignupAuthorization, performAuthorization, handlePubkyAuthApproval, AdoptedPubkyReference.current
CI: SwiftFormat, translations validate and Greptile green; Run Tests, Run Integration Tests and build-local pending.
Build: Not built here: iOS does not build on Linux, so this review is reasoned from the code and from CI.

Ruled out

  • A signing path that skips the check: approveAuthRequest checks before the USDT endpoint, watch-only preparation and both approvals, and approveAuth / approveAuthWithCompanionClaim check again on entry, so approveSignupAuthorization's grant branch is covered too. The Ring signup path (approveRingAuth) is a signup, which upstream does not hold to spk.
  • Prefix mismatch: pubkyPublicKeyFromSecret may return pubky… (other call sites strip it), and isExpectedSigner drops the prefix only for a 57-character pubky… key; AdoptedPubkyReference.pubky is the bare z32 Ring publishes. The test drives the prefixed form.
  • Bitkit handoff URLs bypassing the parser: parse(url:) normalises bitkit://pubky-auth/... to pubkyauth://signin_grant before makeRequest, and the sheet passes request.rawUrl, so the sign-time re-parse sees the same URL.
  • Repeated or hidden spk: duplicate values throw, and any C0 or DEL character in the URL is rejected before URLComponents can percent-encode it.
  • Locale placeholder: pubky_auth__other_pubky carries {pubky} in all 15 Localizable.strings.

Merge confidence: 4/5, CI has not finished (Run Tests pending). The security path itself has tests for both the pre-sheet refusal and every signing entry point.

Reviewer model: Claude Opus 5.5 (claude-opus-5-5)
Reasoning effort: Not exposed (the run reports the model id only)

@piotr-iohk piotr-iohk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QA review

Scope: full PR diff against its merge base, at 45a49bb.

No new actionable code findings.

All changed surfaces were inspected against #1195, pinned pubky/paykit-rs (0b9c63d), upstream pubky/pubky-homeserver (d95161a), and twin synonymdev/bitkit-android#2104 at baeabb3. PubkyAuthRequest.parseExpectedSigner matches upstream canonical bare z-base-32 and Ed25519 key checks while rejecting ASCII control characters and duplicates and ignoring spk on signup links; AppViewModel.presentPubkyAuthApproval refuses foreign spk requests before consent (including ahead of the Ring hint for adopted identities); and every signing entry point (approveAuthRequest, approveAuth, approveAuthWithCompanionClaim) throws signerMismatch before USDT loading, watch-only preparation, identity republish, or relay calls. The new signin-expected-pubky.xml journey matches the Android twin and uses verified iOS accessibility identifiers. Existing PR reviews (talosmachina, Greptile) report no open concerns.

Validation and limitations: Executed node scripts/validate-translations.js locally at 45a49bb (0 errors across all 15 .lproj/Localizable.strings files). CI SwiftFormat and validate passed at 45a49bb. iOS Xcode build and BitkitTests could not be executed in this Linux review environment, and CI Run Tests, Run Integration Tests, and build-local were still in progress when checked.

Recommended before device testing

Device testing: not performed in this review. The author reports passing simulator screenshots for cases 1–7 at 45a49bb (noting that Settings > Watch-Only Accounts was not checked for case 6); J1 (signin-expected-pubky.xml) still requires QA execution.

Suggested additional test cases

  • iOS — Paykit /setup handoff with foreign spk leaves watch-only accounts unchanged: On an onboarded regtest build with a local Pubky identity, note the current entries in Settings > Watch-Only Accounts, then open bitkit://pubky-auth/setup?caps=<Paykit required capabilities>&relay=https%3A%2F%2Fhttprelay.pubky.app%2Finbox%2F&secret=e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3t7e3s&cid=paykit.test&cpk=5jsjx1o6fzu6aeeo697r3i5rx15zq41kikcye8wtwdqm4nb4tryo&x-bitkit-claim=watch-only-account-v1&spk=8um71us3fyw6h8wbcxb5ar3rwusy1a6u49956ikzojg3gcwd1dty. Expected: PubkyAuthOtherPubkyToast shows This request is for 8um7...1dty, not this wallet's pubky., neither PubkyAuthWatchOnlyConsent nor PubkyAuthAuthorize opens, and Settings > Watch-Only Accounts remains unchanged.

Reviewer model: gemini-4-neon
Reasoning effort: high

@ovi-reviewer ovi-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: ✅ Approve

Findings: None.

QA for 45a49bb: Debug regtest build, E2E mode, iPhone 17 Pro simulator on iOS 27.0.

Sign in with the expected pubky (J1) ✅ Matching spk opened approval with the wallet's pubky; foreign, repeated, prefixed and malformed values were refused.

Drove signin-expected-pubky.xml: copied the profile pubky, confirmed the matching approval card, and cancelled. The foreign-key toast named 8um7...1dty, with no approval sheet after five seconds. Repeated, prefixed and malformed spk showed PubkyAuthInvalidRequestToast without an Authorize button. Nothing was authorized.

signin.2x.mp4
Refused setup leaves watch-only accounts unchanged ✅ The foreign-key setup request opened no consent sheet, and Server Accounts stayed empty after reopening it.

Checked the additional case requested in the QA review. The /setup handoff carried watch-only-account-v1 and the foreign spk. The refusal named the other pubky, neither consent nor Authorize appeared, and Settings → Advanced → Server Accounts remained at zero after reopening the list.

setup.2x.mp4
Auth parser and signing checks ✅ The focused XCTest suites passed; assertions were checked in XCTest output.

Ran xcodebuild test for PubkyAuthRequestTests, PubkyAuthApprovalSheetTests and PubkyAuthURLSchemeTests. They cover malformed and repeated keys, prefix handling, signup exemptions, pre-sheet refusal, and rejection before ordinary/companion approval or watch-only preparation. The recording shows the hosted test app; the pass result comes from XCTest output.

auth-unit.2x.mp4

Live relay delivery was not driven: the journey uses dummy requests and stops before authorization. The author's Preview and Evidence cover the unchanged no-spk and scanner-paste cases; those were not repeated here.

Reviewed the full edce3cd..45a49bb diff, all 24 changed files, and the ordinary, companion, signup and Ring approval paths. Sign-time guards and signup exemptions were exercised by XCTest rather than live relay approval. This agrees with the existing code review. SwiftFormat and translation validation passed.

Twin: synonymdev/bitkit-android#2104 at baeabb3 matches the validation and refusal behavior; iOS uses its dedicated bitkit://pubky-auth handoff.


Reviewed by gpt-6.1-sol-high

@ovitrif ovitrif left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utAck

@ovitrif

ovitrif commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

Merged: approved on 45a49bb, J1 and the refused setup case passed on a simulator run of that head, checks green, no open threads.

@ovitrif
ovitrif merged commit 4b13fce into master Nov 27, 2026
45 checks passed
@ovitrif
ovitrif deleted the fix/pubky-auth-spk branch November 27, 2026 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refuse a Pubky auth request that names another pubky (spk)

4 participants