Skip to content

iclouddrive: fix 2FA failing with 409 even when the code is valid - #9660

Merged
ncw merged 1 commit into
rclone:masterfrom
punya10:fix-iclouddrive-2fa-409
Jul 29, 2026
Merged

ncw merged 1 commit into
rclone:masterfrom
punya10:fix-iclouddrive-2fa-409

Conversation

@punya10

@punya10 punya10 commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

What is the problem you are trying to fix?

Configuring an iclouddrive remote fails after entering the 2FA code (both trusted-device push and SMS paths) with:

validate2FACode failed: HTTP error 409 (409 ) returned body: "{... \"securityCode\": {\"code\": \"...\", \"valid\": true} ...}"

Note the body says the code is valid — yet rclone reports failure. Reported in #9324 and #9359 (and several forum threads).

What is the root cause?

Apple changed the semantics of the idmsa validation endpoints (observed ~mid-2026): POST /verify/trusteddevice/securitycode and POST /verify/phone/securitycode now return HTTP 409 even on successful validation. A --dump headers trace shows the 409 response carries everything a success used to carry:

  • X-Apple-Session-Token: … (only issued on successful validation)
  • fresh Scnt and X-Apple-Auth-Attributes
  • X-Apple-ID-Account-Country
  • body: "securityCode": {…, "valid": true}

rclone's Session.Request treats any non-2xx as an error, so both validate paths abort before TrustSession — discarding the session token Apple just issued.

What is your fix?

Add Session.acceptedDespiteConflict(resp): if the response is a 409 and carries X-Apple-Session-Token, absorb the session headers (extractHeaders) and treat validation as successful, then continue to TrustSession as before. Applied to both Validate2FACode and ValidateSMSCode. A plain 409 without the token (e.g. a genuinely wrong/expired code) still fails as before.

How was it tested?

Built from master (c99b2d11e) and verified against a live account (2FA, 13 trusted devices, ADP off, standard recovery): before the patch, config failed with the exact error above on every attempt (device-push and SMS codes, all reported valid:true); with the patch, rclone config create completes the 2FA step, the trust token is persisted, and lsd / lsjson -R / copy against the remote work.

Checklist

  • I have read the contribution guidelines
  • I have added tests for all changes in this PR if appropriate (not appropriate — requires live Apple auth endpoints)
  • I have added documentation for the changes if appropriate (no user-facing behaviour change)
  • All commit messages are in house style
  • I'm done, this Pull Request is ready for review :-)

🤖 Generated with Claude Code

Since around mid-2026 Apple's idmsa endpoints
POST /verify/trusteddevice/securitycode and
POST /verify/phone/securitycode return HTTP 409 (instead of 2xx) even
when the submitted code is accepted: the response body carries
"securityCode": {..., "valid": true} and - decisively - the response
headers include a fresh X-Apple-Session-Token, scnt and
X-Apple-Auth-Attributes, which are only issued on successful
validation. rclone treated any 409 as failure and aborted before
TrustSession, so configuring an iclouddrive remote always failed after
the 2FA step with:

    validate2FACode failed: HTTP error 409 (409 ) returned body:
    "{... \"securityCode\": {\"code\": \"...\", \"valid\": true} ...}"

Treat a 409 response that carries X-Apple-Session-Token as success:
absorb the session headers and continue to TrustSession. Applies to
both the trusted-device and SMS validation paths.

Verified against a live account (13 trusted devices, ADP off): config
now completes and lsjson/lsd/copy work.

Fixes rclone#9324
Fixes rclone#9359

Co-Authored-By: Claude Opus 5 <[email protected]>
@andrew-schulman

andrew-schulman commented Jul 29, 2026 •

Copy link
Copy Markdown

I've also just started to encounter this problem, since the last time I updated my icloud credentials, about 3 weeks ago. Confirmed that dc2a413 fixes it. Thanks @punya10 .

@ncw ncw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for this.

I'm going to merge it now since it has had some user testing.

@ncw
ncw merged commit 731f2a6 into rclone:master Jul 29, 2026
9 checks passed
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.

3 participants