From 856323251ab1f45455ccf32d2ae77b17c8b4cca9 Mon Sep 17 00:00:00 2001 From: Majd Alfhaily Date: Sun, 8 Nov 2026 18:12:21 +0100 Subject: [PATCH] fix: handle authentication server dialogs --- internal/store/emulator/bridge.go | 52 ++++++++++++ .../store/emulator/generated_profile_test.go | 1 + pkg/appstore/appstore_login.go | 80 +++++++++++++++++++ pkg/appstore/appstore_login_test.go | 44 ++++++++++ 4 files changed, 177 insertions(+) diff --git a/internal/store/emulator/bridge.go b/internal/store/emulator/bridge.go index addf437..55098d7 100644 --- a/internal/store/emulator/bridge.go +++ b/internal/store/emulator/bridge.go @@ -26,6 +26,29 @@ func (r *Runtime) symbol(name string) { } switch name { + case "AMSError": + // AMSAccountAuthenticationPlugin uses this factory when Apple returns a + // server dialog and native presentation is disabled. Preserve the error + // as an NSError so the framework can finish the request and the caller can + // inspect the original response instead of aborting the guest. + info := map[string]uint64{} + if description := r.arg(1); description != 0 { + info["NSLocalizedDescription"] = description + } + + if reason := r.arg(2); reason != 0 { + info["NSLocalizedFailureReason"] = reason + } + + if underlying := r.arg(3); underlying != 0 { + info["NSUnderlyingError"] = underlying + } + + r.set(r.New("NSError", map[string]uint64{ + "domain": r.New("NSString", "AMSErrorDomain"), + "code": r.New("NSNumber", int64(r.arg(0))), + "userInfo": r.New("NSDictionary", info), + })) case "xpc_get_type": object := r.objects[r.arg(0)] if object != nil && object.Class == "HostXPCDictionary" { @@ -745,6 +768,29 @@ func (r *Runtime) message(receiver uint64, selector string, super bool) { return } + // Store tasks use keep-alives to request host process assertions. The guest + // runs synchronously inside the CLI, so retaining a scoped token is enough. + if class == "AMSKeepAlive" { + switch selector { + case "keepAliveWithName:style:", "rbs_keepAliveWithName:style:": + if meta { + value := r.New("AMSKeepAlive", nil) + r.objects[value].Properties["name"] = r.arg(2) + r.objects[value].Properties["style"] = r.arg(3) + r.set(value) + + return + } + case "invalidate", "rbs_invalidate": + r.set(0) + + return + case "name", "style": + r.set(object.Properties[selector]) + + return + } + } if entry := r.lookup(class, selector, meta); !super && entry != 0 { r.jump(entry, receiver, selector) @@ -1961,6 +2007,12 @@ func (r *Runtime) formatAt(format string, sp uint64) string { } else { obj := r.objects[value] if obj == nil { + if class := r.classNames[value]; class != "" { + result.WriteString(class) + + break + } + panic(fmt.Sprintf("unknown format object %#x", value)) } diff --git a/internal/store/emulator/generated_profile_test.go b/internal/store/emulator/generated_profile_test.go index a6f0098..8b774b5 100644 --- a/internal/store/emulator/generated_profile_test.go +++ b/internal/store/emulator/generated_profile_test.go @@ -89,6 +89,7 @@ var _ = Describe("Generated profile response branches", func() { Entry("version lookup GET", "lookup", http.MethodGet, "application/json", `{"results":{"fixture-value":{"offers":[]}}}`, http.StatusOK, false), Entry("streamed package range", "download", http.MethodGet, "application/octet-stream", "fixture-value\x00\xff", http.StatusPartialContent, true), Entry("authentication failure", "authenticate", http.MethodPost, "application/x-apple-plist", `failureType2034customerMessagefixture-value`, http.StatusOK, false), + Entry("authentication server dialog", "authenticate", http.MethodPost, "application/x-apple-plist", `customerMessagefixture-valuedialogmessageAccount lockedexplanationReset the account to continue.okButtonStringResetokButtonActionkindOpenURLurlhttps://iforgot.apple.com/cgi-bin/iforgot.cgi`, http.StatusOK, false), Entry("purchase failure", "purchase", http.MethodPost, "application/x-apple-plist", `failureType9610customerMessagefixture-value`, http.StatusOK, false), ) }) diff --git a/pkg/appstore/appstore_login.go b/pkg/appstore/appstore_login.go index e4c7ac0..9a1375d 100644 --- a/pkg/appstore/appstore_login.go +++ b/pkg/appstore/appstore_login.go @@ -159,10 +159,24 @@ type loginAccountResult struct { Address loginAddressResult `plist:"address,omitempty"` } +type loginDialogActionResult struct { + Kind string `plist:"kind,omitempty"` + URL string `plist:"url,omitempty"` +} + +type loginDialogResult struct { + Message string `plist:"message,omitempty"` + Explanation string `plist:"explanation,omitempty"` + OKButtonString string `plist:"okButtonString,omitempty"` + OKButtonAction loginDialogActionResult `plist:"okButtonAction,omitempty"` + CancelButtonString string `plist:"cancelButtonString,omitempty"` +} + type loginResult struct { FailureType string `plist:"failureType,omitempty"` CustomerMessage string `plist:"customerMessage,omitempty"` Account loginAccountResult `plist:"accountInfo,omitempty"` + Dialog loginDialogResult `plist:"dialog,omitempty"` DirectoryServicesID string `plist:"dsPersonId,omitempty"` PasswordToken string `plist:"passwordToken,omitempty"` } @@ -383,6 +397,8 @@ func (t *appstore) parseLoginResponse(res *http.Result[loginResult], attempt int if action := authenticationMachineAction(res.Headers); action != nil { err = action + } else if dialogErr := authenticationDialogError(res.Data); dialogErr != nil { + err = NewErrorWithMetadata(dialogErr, res) } else if res.StatusCode >= gohttp.StatusMultipleChoices && res.StatusCode < gohttp.StatusBadRequest { if redirect, err = res.GetHeader("location"); err != nil { err = fmt.Errorf("failed to retrieve redirect location: %w", err) @@ -422,6 +438,70 @@ func (t *appstore) parseLoginResponse(res *http.Result[loginResult], attempt int return retry, redirect, err } +func authenticationDialogError(result loginResult) error { + dialog := result.Dialog + if strings.TrimSpace(dialog.Message) == "" && strings.TrimSpace(dialog.Explanation) == "" && + strings.TrimSpace(dialog.OKButtonString) == "" && strings.TrimSpace(dialog.CancelButtonString) == "" && + strings.TrimSpace(dialog.OKButtonAction.Kind) == "" && strings.TrimSpace(dialog.OKButtonAction.URL) == "" { + return nil + } + + message := strings.TrimSpace(dialog.Message) + explanation := strings.TrimSpace(dialog.Explanation) + + if message == "" { + message = strings.TrimSpace(result.CustomerMessage) + } + + actionURL := trustedAuthenticationDialogURL(dialog.OKButtonAction) + if message == "" && explanation == "" && actionURL == "" { + return nil + } + + parts := make([]string, 0, 2) + if message != "" { + parts = append(parts, message) + } + + if explanation != "" && explanation != message { + parts = append(parts, explanation) + } + + detail := strings.Join(parts, ": ") + if detail == "" { + detail = "Apple requires action before sign-in can continue" + } + + if actionURL != "" { + label := strings.TrimSpace(dialog.OKButtonString) + if label == "" { + label = "Continue" + } + + detail += fmt.Sprintf(" (%s: %s)", label, actionURL) + } + + return errors.New(detail) +} + +func trustedAuthenticationDialogURL(action loginDialogActionResult) string { + if !strings.EqualFold(strings.TrimSpace(action.Kind), "OpenURL") { + return "" + } + + value, err := url.Parse(strings.TrimSpace(action.URL)) + if err != nil || value.Scheme != "https" || value.User != nil || value.Fragment != "" || value.Port() != "" && value.Port() != "443" { + return "" + } + + host := strings.ToLower(value.Hostname()) + if host != "apple.com" && !strings.HasSuffix(host, ".apple.com") { + return "" + } + + return value.String() +} + func authenticationMachineAction(headers map[string]string) *authenticationMachineActionError { var action, data string diff --git a/pkg/appstore/appstore_login_test.go b/pkg/appstore/appstore_login_test.go index d80ec0d..dc1fd9b 100644 --- a/pkg/appstore/appstore_login_test.go +++ b/pkg/appstore/appstore_login_test.go @@ -137,6 +137,20 @@ var _ = Describe("AppStore (Login)", func() { ) }) + DescribeTable("does not expose untrusted dialog actions", func(action loginDialogActionResult) { + result := loginResult{Dialog: loginDialogResult{Message: "Account action required", OKButtonString: "Continue", OKButtonAction: action}} + err := authenticationDialogError(result) + + Expect(err).To(MatchError("Account action required")) + }, + Entry("non-Apple URL", loginDialogActionResult{Kind: "OpenURL", URL: "https://example.com/reset"}), + Entry("insecure URL", loginDialogActionResult{Kind: "OpenURL", URL: "http://iforgot.apple.com/reset"}), + Entry("embedded credentials", loginDialogActionResult{Kind: "OpenURL", URL: "https://user@iforgot.apple.com/reset"}), + Entry("nonstandard port", loginDialogActionResult{Kind: "OpenURL", URL: "https://iforgot.apple.com:8443/reset"}), + Entry("fragment", loginDialogActionResult{Kind: "OpenURL", URL: "https://iforgot.apple.com/reset#token"}), + Entry("non-navigation action", loginDialogActionResult{Kind: "Buy", URL: "https://iforgot.apple.com/reset"}), + ) + When("fails to read Machine's MAC address", func() { BeforeEach(func() { mockMachine.EXPECT(). @@ -299,6 +313,36 @@ var _ = Describe("AppStore (Login)", func() { }) }) + When("store API returns an account recovery dialog", func() { + BeforeEach(func() { + mockClient.EXPECT(). + Send(gomock.Any()). + Return(http.Result[loginResult]{ + StatusCode: 200, + Data: loginResult{ + CustomerMessage: CustomerMessageAccountDisabled, + Dialog: loginDialogResult{ + Message: "This Apple Account has been locked for security reasons.", + Explanation: "Reset your password to continue.", + OKButtonString: "Reset", + OKButtonAction: loginDialogActionResult{ + Kind: "OpenURL", + URL: "https://iforgot.apple.com/cgi-bin/iforgot.cgi", + }, + }, + }, + }, nil) + }) + + It("surfaces the dialog and trusted recovery URL", func() { + _, err := as.Login(LoginInput{Password: testPassword}) + + Expect(err).To(MatchError(ContainSubstring("This Apple Account has been locked for security reasons."))) + Expect(err).To(MatchError(ContainSubstring("Reset your password to continue."))) + Expect(err).To(MatchError(ContainSubstring("Reset: https://iforgot.apple.com/cgi-bin/iforgot.cgi"))) + }) + }) + When("store API requires 2FA code", func() { BeforeEach(func() { mockClient.EXPECT().