Skip to content

iclouddrive: support China Mainland GCBD accounts - #9995

Merged
ncw merged 2 commits into
rclone:masterfrom
zsyeh:iclouddrive-gcbd-domain
Nov 11, 2026
Merged

ncw merged 2 commits into
rclone:masterfrom
zsyeh:iclouddrive-gcbd-domain

Conversation

@zsyeh

@zsyeh zsyeh commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Handle Apple accountLogin responses containing {"domainToUse":"iCloud.com.cn"} for China Mainland iCloud accounts.

  • Keep Apple's account domain in the auth session and reconnect/2FA state; save region = china for GCBD remotes. Global remotes do not gain an empty region setting.
  • Select the matching identity, setup, Origin, Referer, OAuth redirect URI, and PCS endpoints per session. Drive and Photos service URLs continue to come from Apple accountLogin.
  • When accountLogin selects a different domain, repeat SRP on that domain before accountLogin; reusing the first domain's SRP result leaves the web auth token missing and breaks PCS.
  • Preserve the existing global endpoints when no domain redirect is returned. The advanced region option accepts global or china, with automatic detection as the normal path.

Fixes #8257. Related to #8587.

Verification

  • go test -count=1 ./backend/iclouddrive/... — passed.
  • go vet -unreachable=false ./backend/iclouddrive/... — passed.
  • golangci-lint run ./... — 0 issues.
  • make rclone — passed on Linux amd64.
  • Live GCBD remote: fresh 2FA and saved trust-token automatic domain discovery both completed; lsd, ls, and downloading a small file succeeded. No credentials or file names are included here.
  • Full make quicktest on this machine is blocked by the local NFS mount environment (cmd/nfsmount mount tests fail with fsconfig() failed: NFS: mount program didn't pass remote address). GOFLAGS="-skip=TestMount|TestSubpathMount" make quicktest passed.

@ncw

ncw commented Oct 14, 2026

Copy link
Copy Markdown
Member

Hi @zsyeh, thank you for this. It's a careful piece of work, and detecting the domain from Apple's accountLogin redirect is much nicer for users than making China Mainland users find and set an option. Thanks for testing it against a real GCBD account too. I've closed the two older PRs for #8257 in favour of this one.

Some things to change before merging:

  1. The session-save callback and the config steps call m.Set(configDomain, session.DomainToUse) even when the domain is empty. That writes a domain_to_use = line into the config of every existing non-China remote. Could you only set it when it is non-empty or has changed?
  2. Option naming: once released, a config option name is there forever. In Add iCloud GCBD (China Mainland) Option in iCloud Drive backend #8257 I suggested a region option with global and china values. I think that's clearer to users than Apple's internal domain_to_use name, and the backend can map it to iCloud.com.cn. Could you rename it? Please also expand the Help text to say it's for China Mainland (GCBD) accounts and is normally detected automatically.
  3. Please add a short note to docs/content/iclouddrive.md saying China Mainland accounts are supported and detected automatically.
  4. Please add Fixes #8257 to the commit message.

Minor: in TestAccountLoginDomainRedirect, require.Equal runs inside the HTTP handler goroutine. assert.Equal is safe there; require isn't.

Thanks again!

@CHN-CYC

CHN-CYC commented Oct 23, 2026

Copy link
Copy Markdown

Hi @zsyeh ,

Thank you for your work on bringing China Mainland iCloud Drive support to rclone!

I've been following this PR closely, as this feature is particularly important for Linux users with China Mainland Apple IDs, including myself.

I noticed that the maintainer provided some review feedback on October 14, but there haven't been any further updates since then.

May I ask if you're planning to address the remaining review comments? Is there anything currently blocking progress?

It would be great to see this feature merged and included in an upcoming stable release. And I really appreciate the effort you've already put into this.

Thanks again for your work!

@zsyeh
zsyeh force-pushed the iclouddrive-gcbd-domain branch from 8181a0a to 490c398 Compare October 23, 2026 18:37
@zsyeh

zsyeh commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Hi @zsyeh ,

Thank you for your work on bringing China Mainland iCloud Drive support to rclone!

I've been following this PR closely, as this feature is particularly important for Linux users with China Mainland Apple IDs, including myself.

I noticed that the maintainer provided some review feedback on October 14, but there haven't been any further updates since then.

May I ask if you're planning to address the remaining review comments? Is there anything currently blocking progress?

It would be great to see this feature merged and included in an upcoming stable release. And I really appreciate the effort you've already put into this.

Thanks again for your work!

Thanks for following up and for your support!

I plan to address the remaining review comments and push an update soon.

Thanks for your patience!

@zsyeh

zsyeh commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

@ncw @CHN-CYC Thanks for the review and follow-up. I have updated this PR in 490c3983e:

  • The config steps and session-save callback write region only after Apple selects the China Mainland domain. Global remotes do not gain an empty region setting.
  • The advanced option is now region with global and china values. Its help text explains GCBD and automatic detection.
  • Added the China Mainland note to docs/content/iclouddrive.md.
  • Added Fixes #8257 to the commit message.
  • Changed the assertion inside the HTTP handler to assert.Equal.

The backend tests, go vet, full Go lint, and the full quicktest run with the two local NFS mount tests skipped passed. I rebuilt and checked the updated binary against the GCBD remote after migrating my local config to region = china. No further blocker on my side; ready for another review.

@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.

This is looking much better now, thank you.

See inline for the finishing touches.

Also, one-line comments on each of the new helpers setSessionRegion, saveSessionRegion, finishConfigAuthentication, endpointsForDomain would match the rest of the file.

Thank you :-)

}

if err := icloud.Session.ValidateSMSCode(ctx, code, phoneID, mode); err != nil {
if errors.Is(err, api.ErrDomainChanged) {

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.

This branch can never run - see above

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The SMS path has the same call chain: ValidateSMSCode -> TrustSession -> AuthWithToken -> ErrDomainChanged when accountLogin returns 302. TestValidateCodeCanChangeDomain/SMS in d1464bdd5 verifies it. I have retained this branch and retryConfigDomain pending your review of this evidence.

}

if err := icloud.Session.Validate2FACode(ctx, code); err != nil {
if errors.Is(err, api.ErrDomainChanged) {

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.

This branch can never trigger as the Validate functions only call the securitycode and /2sv/trust endpoints, and only AuthWithToken returns ErrDomainChanged. China accounts still work, because the domain is detected on the first NewFs via the retry loop in authenticateSession, which matches your live testing. But this code is dead, and I'd rather remove it than keep a path that looks like it does something. Could you delete those two branches and retryConfigDomain?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I traced this through the current code: Validate2FACode calls TrustSession, which calls AuthWithToken after /2sv/trust. AuthWithToken can return ErrDomainChanged on the accountLogin 302, so this branch is reachable during config/reconnect 2FA. TestValidateCodeCanChangeDomain/trusted_device in d1464bdd5 exercises the complete call chain with a 302 response. Removing this branch would leave that domain redirect unhandled. I have kept it open for your review.

Comment thread backend/iclouddrive/icloud.go Resolved

@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.

Hi @zsyeh, thanks for the update, and for pushing back on my point about retryConfigDomain with a test rather than just deleting it. You're right: TrustSession finishes with AuthWithToken, so ErrDomainChanged can come back from both Validate2FACode and ValidateSMSCode, and TestValidateCodeCanChangeDomain shows that nicely. Sorry for the confusion.

This looks great now - will merge - thank you :-)

@ncw
ncw merged commit 571b965 into rclone:master Nov 11, 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.

Add iCloud GCBD (China Mainland) Option in iCloud Drive backend

3 participants