Skip to content

fix(core): resend sign-in requests Apple's edge drops - #19

Closed
Kosthi wants to merge 2 commits into
masterfrom
fix/auth-transient-retry
Closed

Kosthi wants to merge 2 commits into
masterfrom
fix/auth-transient-retry

Conversation

@Kosthi

@Kosthi Kosthi commented Aug 31, 2026

Copy link
Copy Markdown
Owner

Closes the failure reported in #17.

What is happening

Sign-in fails outright when Apple answers with something that never reached MZFinance. #17 reports a 301 carrying an HTML body and no Location header; the same report also saw a contentless 204. majd/ipatool#520 saw both, and — the useful part — watched which endpoint misbehaved swap between attempts from the same host minutes apart:

attempt 1:  buy.itunes.apple.com/…/wa/authenticate  -> 301, HTML, no Location
            auth.itunes.apple.com/auth/v1/native/fast/  -> 204, empty
attempt 2:  buy.itunes.apple.com/…/wa/authenticate  -> 204, empty
            auth.itunes.apple.com/auth/v1/native/fast/  -> 301, HTML, no Location

None of those replies carry Apple's backend headers (apple-originating-system, x-responding-instance), so they are the edge dropping the request, not a verdict on the account. An unsigned probe from a working host still gets the application tier's 403 + x-responding-instance: MZFinance:…, and #17's own bag dump still lists MZFinance: [authenticate] under sign-sap-request. The endpoint is alive; the request is not arriving.

So this does not move sign-in to auth.itunes.apple.com/auth/v1/native/fast, which #17 proposes. Upstream tried that route (majd/ipatool#507, #514) and removed it again in majd/ipatool@abd86cb when SAP signing landed; its endpoint allowlist matches ours today.

Why the existing retry never fired

login()'s loop only retries on a StoreError parsed out of a plist body, so a reply that is empty or is HTML fails before it is reached. A 301 with no Location also falls past the redirect branch straight into the plist parser — which is why the user-visible error is invalid type: string "<html>", expected a map.

The change

Resend the identical signed bytes up to 3 times, 250ms apart, while the reply carries no store response.

  • Signing happens once per attempt, outside the resend loop. Apple signs the exact bytes it receives and those do not change between sends.
  • The attempt value in the body is Apple's own counter and stays put — a resend is not a new attempt.
  • Retryable: 204, 404, 5xx, and a 3xx with no Location. But a body that parses as a property list settles the request whatever the status — Apple reports real failures under a 5xx, and dispatching on status alone would swallow them.
  • Deliberately not retried: 403 (the application tier rejecting an unsigned request; resending buries the SapSignatureRequired diagnosis) and 429 (hammering a rate limit is the one response guaranteed to make it worse).
  • Exhausting all three sends reports every status seen and names the network path as the suspect, instead of leaving the reader hunting for a bug in the request.
  • A non-plist body now reports its status and a snippet rather than reaching the parser.

Upstream took the same approach in majd/ipatool@30d19a6, but keys only off the status and leaves the Location-less 3xx unhandled (pkg/http/client.go passes every 3xx straight through) — the exact shape #17 hit.

Testing

is_dropped_request and looks_like_plist are pure and unit-tested: bare 301, pod 302, 204, HTML 404, 5xx, 5xx carrying a plist, 403, 429, plus the HTML-vs-plist discrimination. cargo clippy --workspace --all-targets is clean; cargo test --workspace passes 109 + 7.

The resend loop itself is not covered — the workspace has no HTTP mock dev-dependency, and adding one felt out of scope here.

Not addressed

StoreError::RateLimited still has no construction site, so a 429 reports as a non-plist body rather than as a rate limit. Small, but separate.

Sign-in fails outright when Apple answers with something that never
reached MZFinance. Issue #17 reports a 301 carrying an HTML body and no
Location header; the same report also saw a contentless 204, and
majd/ipatool#520 saw both, with which endpoint misbehaved swapping
between attempts from the same host minutes apart. None of those replies
carry Apple's backend headers, so they are the edge dropping the request
rather than a verdict on the account -- the reporter's own bag probe
still lists MZFinance/authenticate under sign-sap-request, and sign-in
works from other hosts at the same moment.

The existing retry loop cannot help: it only fires on a StoreError
parsed out of a plist body, so a reply that is empty or is HTML fails
before it is reached. A 301 without a Location also falls past the
redirect branch into the plist parser, which is why the failure surfaces
as `invalid type: string "<html>", expected a map`.

Resend the identical signed bytes up to three times, 250ms apart, while
the reply carries no store response. Signing happens once per attempt,
outside the resend loop: Apple signs the exact bytes it receives and
those do not change between sends. The `attempt` value in the body is
Apple's own counter and stays put -- a resend is not a new attempt.

The retryable set is 204, 404, 5xx and a 3xx with no Location, but a
body that parses as a property list settles the request whatever the
status: Apple reports real failures under a 5xx, and dispatching on the
status alone would swallow them. Two statuses are deliberately excluded.
403 is the application tier rejecting an unsigned request, and resending
it would bury the SapSignatureRequired diagnosis. 429 is a rate limit,
and hammering it is the one response guaranteed to make it worse.

When all three sends are dropped, report every status seen and say the
network path is the suspect, so the next reader does not go looking for
a bug in the request. A non-plist body now reports its status and a
snippet instead of reaching the parser at all.

Upstream took the same approach in majd/ipatool@30d19a6, but keys only
off the status and leaves the Location-less 3xx unhandled, which is the
exact shape #17 hit.

Refs #17
The User-Agent is load-bearing and nothing said so. `bag.xml` hands out a
different `authenticateAccount` depending on who asks, so the endpoint
allowlist and the SAP setup URL both follow from this constant -- with
the iTunes UA the setup URL loses its `/legacy` suffix, which may well be
a different handshake variant.

Issue #17 read a bag dump taken with a different UA as evidence that
Apple had migrated sign-in for everyone, which is the mistake this table
is here to prevent.

Refs #17

@Kosthi Kosthi left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Superseded by #25, which migrates Store authentication to auth/v1/native with Store ADI device provisioning and non-plist response handling.

@Kosthi Kosthi closed this Nov 30, 2026
@Kosthi
Kosthi deleted the fix/auth-transient-retry branch November 30, 2026 08:13
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