Skip to content

fix(auth): handle HTTP 421 on validate without full re-login - #489

Open
amlucas0xff wants to merge 3 commits into
picklepete:masterfrom
amlucas0xff:fix/421-misdirected-request-on-validate
Open

fix(auth): handle HTTP 421 on validate without full re-login#489
amlucas0xff wants to merge 3 commits into
picklepete:masterfrom
amlucas0xff:fix/421-misdirected-request-on-validate

Conversation

@amlucas0xff

Copy link
Copy Markdown

Problem

All Linux users (and any non-Apple network) get a PyiCloudFailedLoginException despite having a valid session. The failure chain is:

  1. authenticate() calls _validate_token()POST /setup/ws/1/validate
  2. Apple returns HTTP 421 Misdirected Request — this is caused by HTTP/2 connection coalescing on Apple's CDN, not an auth failure
  3. pyicloud treats 421 as an invalid session token and triggers a full re-login via idmsa.apple.com
  4. idmsa.apple.com returns HTTP 503 (rate-limiting repeated non-Apple logins)
  5. Result: PyiCloudFailedLoginException even though session_token and trust_token are valid

This breaks any automated Linux usage of pyicloud: FUSE drive mounts, home automation, scripts, etc.

Fix

In authenticate(), when _validate_token() raises a 421 exception, fall through to _authenticate_with_token() instead of triggering a full password re-login.

_authenticate_with_token() posts the existing session_token + trust_token to /accountLogin directly. This:

  • Succeeds without a password or 2FA prompt
  • Does not hit idmsa.apple.com (avoids rate-limiting)
  • Reuses the valid trusted session

If _authenticate_with_token() also fails, we fall through to the original full re-login path as a last resort.

Verification

Tested on:

  • Arch Linux, kernel 6.18, Python 3.14
  • pyicloud 1.0.0
  • iCloudDriveFuse FUSE driver mounted successfully at ~/iCloud

Before fix: every mount attempt failed with Authentication required for Account. (421)Service Temporarily Unavailable (503).

After fix: authentication completes via token re-auth, drive mounts and lists files correctly.

Related issues

Relates to #330, #441 (421 authentication errors reported by Linux users)

Alexandre Marcel Lucas added 3 commits March 8, 2026 16:29
Apple's setup.icloud.com/validate returns HTTP 421 (Misdirected
Request) for connections from non-Apple networks (all Linux users).
This is caused by HTTP/2 connection coalescing on Apple's CDN —
not a real authentication failure.

Previously, pyicloud treated 421 as an invalid session token and
triggered a full re-login via idmsa.apple.com. That endpoint then
returns 503 (rate-limiting), causing PyiCloudFailedLoginException
even when the session_token and trust_token were perfectly valid.
The result: iCloud Drive FUSE mounts and any automated Linux use
of pyicloud were completely broken.

The fix: when authenticate() receives 421 from _validate_token(),
fall through to _authenticate_with_token() which posts the existing
session_token + trust_token directly to /accountLogin. This succeeds
without a password or 2FA prompt, and avoids hitting the auth rate
limit.

Verified working on Arch Linux (kernel 6.18, Python 3.14) with
pyicloud 1.0.0 and the iCloudDriveFuse FUSE driver.
Two additions:

1. "Known Issues & Fixes" section explaining the HTTP 421
   Misdirected Request problem affecting all Linux users, its
   root cause (Apple HTTP/2 coalescing), and how the fix works.

2. "Mounting iCloud Drive on Linux (FUSE)" section with a
   complete step-by-step guide covering package installation,
   pyicloud authentication, persistent cookie storage,
   iCloudDriveFuse setup, systemd auto-mount service, and
   token renewal maintenance.

This addresses a gap in the documentation that has left Linux
users unable to use pyicloud reliably without understanding the
421 behavior.
Linux users hitting the 421 error have no indication the project
has a known fix. Added a callout note at the top of the README
pointing directly to the Known Issues section and the FUSE mount
guide, so they find the solution immediately.
@dathbe

dathbe commented Mar 8, 2026

Copy link
Copy Markdown

I believe this project has been taken over by this repository: https://github.com/timlaing/pyicloud

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.

2 participants