Skip to content

fix(init): stop iOS setup when the app's publishable key belongs to another application - #639

Open
seanperez29 wants to merge 3 commits into
mainfrom
sean/ios-init-key-mismatch
Open

seanperez29 wants to merge 3 commits into
mainfrom
sean/ios-init-key-mismatch

Conversation

@seanperez29

@seanperez29 seanperez29 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What

clerk init on an existing iOS or macOS app now checks the publishable key the app passes to Clerk.configure against the linked Clerk application before it changes anything. If a configuration's key belongs to a different application, setup stops instead of setting up the linked one.

Why

A repository's Clerk link is shared by every project in the repository. When a repository is linked to application A but its iOS app is built with application B's publishable key, init reused A's link without returning application-required, then registered the Bundle ID and enabled Native API on A. The app still signs in against B, so the registration does nothing for it. assertApplicationChosen only runs for new projects and starters, and the clerk-swift skill only compares keys when the CLI returns application-required, so agents couldn't catch it either.

Steps to reproduce

  1. Link a repository to application A (clerk link --app <A>), with an existing iOS app inside it.
  2. In the app, call Clerk.configure(publishableKey: "pk_test_…") with a development key of application B.
  3. Run clerk init --json from the app's folder.

Before this change, init reused A's link, registered the app's Bundle ID on A, and enabled Native API on A. After it, init stops before any change with the status below.

Output after the change

These samples come from the unit-test fixtures in configured-key.test.ts (a linked application "Linked (app_test)" and another application "Other (app_other)"), with the scope path shortened. Keys are compared, never printed.

JSON, relink offered (both configurations use B's key)
{
  "status": "input-required",
  "appIntegrationComplete": false,
  "link": {
    "flag": "--relink",
    "question": "This app is configured with Other (app_other)'s publishable key, but /Users/me/acme is linked to Linked (app_test). Switch the link to Other (app_other), or keep Linked (app_test) and change the app's key?",
    "issue": "/Users/me/acme is linked to Linked (app_test), but the publishable key MyApp passes to Clerk.configure in Debug and Release (from a string literal in MyApp/MyAppApp.swift) belongs to Other (app_other). Setting up Linked (app_test) would register this app on an application it doesn't sign in to, so no setup was applied. To set up Other (app_other) instead, run `clerk init --app app_other --json --relink` (this switches the link for every project in /Users/me/acme). To keep Linked (app_test), change the key for Debug and Release to Linked (app_test)'s development publishable key, then rerun clerk init.",
    "current": {
      "appId": "app_test",
      "appName": "Linked"
    },
    "requested": "app_other",
    "scope": "/Users/me/acme",
    "publishableKeys": [
      {
        "configuration": "Debug",
        "source": "a string literal in MyApp/MyAppApp.swift",
        "frontendApiHost": "other-app-42.clerk.accounts.dev",
        "application": {
          "appId": "app_other",
          "appName": "Other"
        }
      },
      {
        "configuration": "Release",
        "source": "a string literal in MyApp/MyAppApp.swift",
        "frontendApiHost": "other-app-42.clerk.accounts.dev",
        "application": {
          "appId": "app_other",
          "appName": "Other"
        }
      }
    ]
  },
  "next": [
    "If the user wants Other (app_other), run `clerk init --app app_other --json --relink`.",
    "If the user wants to keep Linked (app_test), change the key for Debug and Release to Linked (app_test)'s development publishable key where Clerk.configure gets it (`clerk apps list --json` lists the keys), then rerun clerk init."
  ]
}
JSON, no relink (Debug uses A's key, Release a key of an application the CLI can't list)
{
  "status": "input-required",
  "appIntegrationComplete": false,
  "link": {
    "question": "This app's configurations use publishable keys from different applications, and /Users/me/acme is linked to Linked (app_test). Which application should the app use?",
    "issue": "/Users/me/acme is linked to Linked (app_test), but the publishable key MyApp passes to Clerk.configure in Release (from the Info.plist entry ClerkPublishableKey, set by the CLERK_PUBLISHABLE_KEY build setting) belongs to an application whose Frontend API is clerk.other.example, which isn't among the applications the CLI could list. Debug uses Linked (app_test)'s key. clerk init sets up one application for every configuration it changes, so no setup was applied. To keep Linked (app_test), change the key for Release to Linked (app_test)'s production publishable key, then rerun clerk init.",
    "current": {
      "appId": "app_test",
      "appName": "Linked"
    },
    "scope": "/Users/me/acme",
    "publishableKeys": [
      {
        "configuration": "Release",
        "source": "the Info.plist entry ClerkPublishableKey, set by the CLERK_PUBLISHABLE_KEY build setting",
        "frontendApiHost": "clerk.other.example"
      }
    ]
  },
  "next": [
    "clerk init sets up one application for all the configurations it changes, so ask the user which application the app should use.",
    "If the user wants to keep Linked (app_test), change the key for Release to Linked (app_test)'s production publishable key where Clerk.configure gets it (`clerk apps list --json` lists the keys), then rerun clerk init."
  ]
}
Human mode (usage error, exit 2)
/Users/me/acme is linked to Linked (app_test), but the publishable key MyApp passes to Clerk.configure in Debug and Release (from a string literal in MyApp/MyAppApp.swift) belongs to Other (app_other). Setting up Linked (app_test) would register this app on an application it doesn't sign in to, so no setup was applied. To set up Other (app_other) instead, run `clerk init --app app_other` (this switches the link for every project in /Users/me/acme). To keep Linked (app_test), change the key for Debug and Release to Linked (app_test)'s development publishable key, then rerun clerk init.

How

  • New init/ios/configured-key.ts, called from runAppleInit right after the Xcode inspection, before setup's questions or any change to the project or Clerk. It reuses configuredPublishableKey and scanSwiftSources (today used by clerk doctor's production checks), unchanged. Those read a Swift string literal, or an Info.plist entry that's a literal or a $(BUILD_SETTING), and report anything else as unresolved with a reason.
  • It checks every configuration setup changes (Debug and Release, or --xcode-configuration) and compares each key's Frontend API host with the linked application's instances, from the Platform API application the CLI already reads. Keys are never printed. If the linked application can't be read, init stops with that error, since setup needs it anyway.
  • Mismatch: a usage error (exit 2) that names each mismatched configuration and the key's application when it's among the applications the CLI can list (listApplications, now part of NativeAPI), otherwise the key's Frontend API host. Keeping the link means passing the linked application's key for the same environment (development or production) to Clerk.configure.
    • Relinking (clerk init --app <id> with this run's setup options, plus --relink for agents) is offered only when it's sure to fix every configuration: every configuration's key was read, none uses the linked application, and they're all the same key or keys of the same known application. Otherwise the stop asks to change the keys, and names any configuration whose key couldn't be checked.
    • With --json, the stop is an input-required status shaped like the existing link stops: link: { flag, question, issue, current, requested, scope, publishableKeys: [{ configuration, source, frontendApiHost, application }] }, plus next. flag is --relink only when relinking is offered, and requested and application appear only when the key's application is known.
  • Production key of the linked application: not a mismatch. Setup continues, and human output warns when the configuration set up first (Debug, or --xcode-configuration) signs in against production.
  • Unreadable key (a constant, a bundled secrets file, the environment, a call inside #if, several calls): never blocks. The JSON result gets a new configuredKeys field with one entry per configuration (verified with instance and source, or unverified with reason), and the human preview notes unverified keys, once per reason.
  • Skipped for a new --starter app and an unchanged starter, since setup writes the linked application's key into those itself, and for dry runs.
  • The suggested relink command keeps this run's setup flags in human mode too, and keeps an explicit --framework ios or --framework macos (the application-required continuation for existing projects keeps it as well). Outside a Git repository, the message says what relinking really does to the link.
  • Web and other CLI flows are untouched.

Testing

  • configured-key.test.ts: an agent's JSON stop with the key's application named, and nothing registered, enabled, or written; the human stop when the application isn't visible; a failed application lookup; development and production keys of the linked application; an unreadable key reported as unverified, with one note for Debug and Release; Debug on the linked application with Release on another; keys of two applications, unlisted keys, and a listed plus an unlisted key (no relink); keys of the same other application (relink); a configuration whose key couldn't be checked (no relink); a failed read of the linked application; unchanged starters, including a rerun that adds the sign-in view.
  • index-ios.test.ts: init passes the link's scope, what relinking does outside a repository, and relink commands that keep the setup options and an explicit --framework.
  • bun run typecheck, bun run lint, bun run format:check, and bun run test pass on 98a4a91.
  • scripts/apple-setup/verify-*.ts only gained a listApplications stub for the wider NativeAPI type. They call prepareSetup, applySetup, and auditProduction directly, so the key check doesn't run in them, and they weren't run.

Notes

  • How often the key can be read, measured on 14 local iOS projects (read-only): all 7 projects set up by the CLI, the iOS setup prompt, or an agent (string literals). It couldn't read 4 hand-built apps: clerk-ios Quickstart, MacExampleApp, and CustomFlows load the key from a bundled plist, and clerk-convex-ios WorkoutTracker uses a constant. One app had no Clerk.configure(publishableKey:) call. So the stop only fires on a definite mismatch, and the rest get a note.
  • The new configuredKeys field and input-required shape are additive (not breaking under the --json contract), and the change is a fix, so the changeset is a patch.
  • Not handled, on purpose (rare, and each needed more code than it was worth): a key in another file of an untouched Xcode starter, a Clerk.configure call that sits only in a file compiled for another platform, platform-specific Info.plist entries such as ClerkPublishableKey-macos, and a linked instance with no publishable key.
  • Follow-up in clerk/skills: skills/clerk-swift/references/prebuilt.md and custom.md only cover application-required and should get a line for input-required with link.publishableKeys. The docs' iOS setup prompt already compares keys for existing projects, and its link.flag: "--relink" rule covers this status.

🤖 Generated with Claude Code

…nother application

A repository's Clerk link is shared by every project in it, so an existing
iOS or macOS app built with application B's publishable key could be set up
against the linked application A: init registered its Bundle ID and enabled
Native API on A. Before any prompt, remote read, or change, init now reads
the key the app passes to Clerk.configure in the configuration it sets up
and compares it with the linked application's instances.

A key that matches none of them stops setup with a usage error, naming the
key's application when the account can see it. With --json it's an
input-required status with link.flag "--relink", the key's application as
link.requested, and next steps to relink or change the app's key. A
production key of the linked application continues with a note, and a key
the CLI can't read never blocks; the result's new configuredKey field says
whether the key was verified.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: d8c176c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
clerk Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Native iOS and macOS initialization now checks readable configured publishable keys against the linked application before setup changes. It reports verified and unverified keys, and stops setup when a key belongs to another application. The mismatch response offers relinking only when the configurations meet the relink conditions. Retry commands preserve applicable framework, SDK, project, and JSON options.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related issues

  • clerk/cli#556: This change addresses the issue’s key-replacement concern for native iOS and macOS init, but does not change the web link() create-app path named in the issue.

Possibly related PRs

  • clerk/cli#582: Defines the clerk init --app relink behavior used by this change’s mismatch response.
  • clerk/cli#583: Defines JSON input-required reporting for the same native init path.
  • clerk/cli#547: Adds the native starter flow that this change recognizes when skipping checks for new or unchanged starters.

Suggested reviewers: mikepitre, wyattjoh, swolfand

Merge Risk: Minimal · up to d8c17

iOS and macOS init now stops before changing an app whose publishable key belongs to a different Clerk application. No concrete merge-blocking issue was found in this review.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the primary change: iOS setup stops when the app's publishable key belongs to another application. The changeset adds a configured-key check in runAppleInit that returns a mismatch sto…
Description check Passed The description explains the change in detail. It covers what changes, why it is needed, reproduction steps, sample outputs, the implementation approach, testing, and notes. It matches the changeset, …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the primary change: iOS setup stops when the app's publishable key belongs to another application. The changeset adds a configured-key check in runAppleInit that returns a mismatch stop. The title is specific and accurate, and it follows the fix(init) convention.

Full details: Description check

Explanation

The description explains the change in detail. It covers what changes, why it is needed, reproduction steps, sample outputs, the implementation approach, testing, and notes. It matches the changeset, which adds configured-key.ts, updates coordinator and index.ts, adds tests, and updates the README.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Linked repositories: Some configured repositories could not be included in this review. Cross-repository analysis may be incomplete. Please check linked-repository access and settings.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Team
  • Run ID: ffccf271-e4c7-484b-b908-33c84ce098f9
Commits

Reviewing files that changed from the base of the PR and between 98a4a91 and d8c176c.

Files selected for processing: 4

  • packages/cli-core/src/commands/init/README.md
  • packages/cli-core/src/commands/init/index.ts
  • packages/cli-core/src/commands/init/ios/coordinator.ts
  • packages/cli-core/src/commands/init/ios/remote.ts
Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

Included review availability: This review used your included allowance. 6 included reviews remain after this review. Your included PR review attempts over the past 7 days set your current allowance at 8 reviews per hour.

…emedy

Review-loop changes to the iOS publishable key check:

- Check every configuration setup changes (Debug and Release, or
  --xcode-configuration), not only the first; the JSON result reports
  configuredKeys per configuration and the stop lists every mismatched
  configuration in link.publishableKeys.
- Offer --relink only when it fixes every configuration: each key was
  read, none uses the link, and they're one key or keys of one known
  application. Otherwise ask to change the keys, naming configurations
  whose key couldn't be checked.
- Name the linked application's key for the same environment in the
  remedy, keep this run's setup flags and an explicit --framework in the
  suggested command, and describe what relinking does outside Git.
- Stop when the linked application can't be read instead of skipping the
  check; route listApplications through NativeAPI; reuse profileLabel and
  InstanceEnvironment; print the stop as compact JSON.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@seanperez29
seanperez29 marked this pull request as ready for review November 19, 2026 04:17
@blacksmith-sh

blacksmith-sh Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Matches Agent stops on another app's key

Before, init set up Acme Linked anyway (registered the Bundle ID, enabled Native API, edited 2 files). After, it stops with exit 2 and input-required naming Other with --relink, sends only 2 read requests and changes nothing; the suggested relink then sets up Other.

Merge-base

clerk init --json

This PR d8c176c3

clerk init --json

Suggested relink

Also checked · 4 match

Matches Human-mode stop and relink command
Matches Linked app's own key still sets up
Matches Unreadable key never blocks
Matches Mixed configurations: no relink

Proof on d8c176c3 (from 98a4a911) · mocked: Xcode (xcodebuild) (Xcode only runs on macOS; a fake xcodebuild returns the project's build settings...), Clerk Platform API (needs CLERK_PLATFORM_API_KEY) · Did Proof get it right? Yes · No

View full Proof report · React 👎 or comment @codesmith dismiss this proof to dismiss

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant