X Tutup
Skip to content

iclouddrive: replace plaintext signin with SRP authentication#9209

Open
mikegillan wants to merge 7 commits intorclone:masterfrom
mikegillan:master
Open

iclouddrive: replace plaintext signin with SRP authentication#9209
mikegillan wants to merge 7 commits intorclone:masterfrom
mikegillan:master

Conversation

@mikegillan
Copy link

Summary

  • Replace the deprecated /appleauth/auth/signin plaintext endpoint with Apple's SRP-6a (Secure Remote Password) protocol
  • Implement the full SRP handshake: authStartauthFederatesignin/initsignin/complete
  • Add SRP crypto implementation (RFC 5054 2048-bit group, SHA-256, Apple's NoUserNameInX variant)
  • Support both s2k and s2k_fo password derivation protocols
  • Update auth headers for idmsa.apple.com (X-Apple-Auth-Attributes, X-Apple-Frame-Id, correct Origin/Referer)
  • 2FA and trust token flow unchanged

Apple deprecated the old signin endpoint and now blocks it, causing "Invalid Session Token" errors for all users once their trust token expires. This affects everyone — the issue has 119 comments and counting.

Fixes #8587

Test plan

  • go build ./backend/iclouddrive/... compiles
  • go vet ./backend/iclouddrive/... passes (no new warnings)
  • go test ./backend/iclouddrive/... — existing tests pass
  • 11 new unit tests for SRP crypto in api/srp_test.go — all pass
  • Manual test: rclone config → SRP handshake completes → 2FA prompt → trust token saved
  • Manual test: rclone lsd iCloudDrive: lists directories after auth

Checklist

  • I have read the contribution guidelines.
  • I have added tests for all changes in this PR if appropriate.
  • I have added documentation for the changes if appropriate.
  • All commit messages are in house style.
  • I'm done, this Pull Request is ready for review :-)

Apple deprecated the legacy /appleauth/auth/signin endpoint and now
blocks it with 503 responses. Replace it with Apple's SRP-6a variant
(federate, signin/init, signin/complete) which never transmits the
password. The 2FA and trust token flow remains unchanged.
Apple has deprecated the legacy /appleauth/auth/signin endpoint and
now blocks it, causing "Invalid Session Token" errors for all users
when their trust token expires. The browser login flow now requires
SRP (Secure Remote Password), a cryptographic handshake that never
transmits the password.

Replace Session.SignIn() with a multi-step SRP-6a flow:
1. authStart - initialize session at /authorize/signin
2. authFederate - submit account name to /federate
3. authSRPInit - exchange client public value for salt/B at /signin/init
4. authSRPComplete - send M1/M2 proofs to /signin/complete

The SRP implementation uses the RFC 5054 2048-bit group with SHA-256
and Apple's NoUserNameInX variant. Password derivation supports both
s2k and s2k_fo protocols via SHA-256 + PBKDF2.

The 2FA and trust token flow is unchanged. Auth headers for all
idmsa.apple.com requests now include X-Apple-Auth-Attributes,
X-Apple-Frame-Id, and use Origin/Referer of https://idmsa.apple.com.

Fixes rclone#8587
@ncw
Copy link
Member

ncw commented Feb 25, 2026

@mikegillan thanks for fixing this. I just ran the CI - looks like there are some lint things to fix.

I gave it a quick review and it looks great. It looks like it has had some testing from users - do you know how many people have tried the new code?

@mikegillan
Copy link
Author

@mikegillan thanks for fixing this. I just ran the CI - looks like there are some lint things to fix.

I gave it a quick review and it looks great. It looks like it has had some testing from users - do you know how many people have tried the new code?

I don't know but there have been at least two people in the the issue chat reporting that it worked for them. I think most users would be waiting for a nightly build to test it out since they might not have the ability to build locally. Is it possible to give a temp build to users who have indicated interest in the issue?

This is my first time contributing to a public project like this (I've only ever done corporate development) so please forgive me if I'm suggesting something stupid here.

@mikegillan
Copy link
Author

I've corrected the formatting on the imports, which should clear the linter errors.

@Unbounded6106
Copy link

I had to build the Windows version to generate the config file. On the Linux amd64 version, when I tried to configure iCloud, I was still getting the “Invalid Session Token” error. In the config file created on Linux, the “cookies” and “trust_token” fields were not being populated.

By creating the config file on Windows and importing it into Linux, everything works without any issues.

@mikegillan
Copy link
Author

Hi @ncw I see there's a build issue with Windows that doesn't look related to any of my changes. Is there anything else I can do to get this ready for merge? Over on the issue page there have been a few people asking how they can get their hands on it, so now I feel a responsibility to see this through. Please let me know if there's anything I can do to help. Thanks!

@zirkuswurstikus
Copy link

Hello,
cudos to @mikegillan.
I successfully build rclone on Apple Silicon.

mkdir -p /tmp/rclone
cd /tmp/rclone
git clone https://github.com/rclone/rclone.git .
git fetch origin pull/9209/head:pr-9209
git checkout pr-9209
GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go build -trimpath -ldflags='-s -w' -o build/rclone-pr9209-darwin-arm64 ./ 
rclone version   
rclone v1.74.0-DEV
- os/version: darwin 26.2 (64 bit)
- os/kernel: 25.2.0 (arm64)
- os/type: darwin
- os/arch: arm64 (ARMv8 compatible)
- go/version: go1.25.6
- go/linking: static
- go/tags: none

To authenticate use you Apple-ID password. App password do not work.

Thanks,
ZirkusWurstikus

@zirkuswurstikus
Copy link

Sidenote:
Successfully build for my good old Synology DS218

Linux syentology 3.10.108 #42962 SMP Tue Jul 29 14:26:48 CST 2025 armv7l GNU/Linux synology_armada38x_ds218j
rclone v1.74.0-DEV
- os/version: unknown
- os/kernel: 3.10.108 (armv7l)
- os/type: linux
- os/arch: arm (ARMv7 compatible)
- go/version: go1.25.7
- go/linking: static
- go/tags: none

@weskoop
Copy link

weskoop commented Mar 2, 2026

Compiled this PR tonight, it's working great. Effortless connection, Apple couldn't have made it easier.

Mounted a folder of notes, no issues either way.

~
❯ opt/rclone/rclone version
rclone v1.74.0-DEV
- os/version: cachyos (64 bit)
- os/kernel: 6.19.5-3-cachyos (x86_64)
- os/type: linux
- os/arch: amd64
- go/version: go1.26.0-X:nodwarf5
- go/linking: dynamic
- go/tags: none

Appreciate the hard work, both the pr and rclone project!

@zirkuswurstikus
Copy link

zirkuswurstikus commented Mar 2, 2026

Compiled this PR tonight, it's working great. Effortless connection, Apple couldn't have made it easier.

Did you run across the cosmetic issue that it authenticate twice? From what I see the code first authetificate and then run it again for testing access.

@harijay
Copy link

harijay commented Mar 2, 2026

Hi All Great to hear this is working. I tried with my own apple id and password and get a 400. Would love to know the process to set it up ? I tried
New remote
Apple Id : myappleid@icloud.com
y ( Type in my own password)
passwd: Entered in my apple id password
passwd: Entered in my apple id password
n ( no advanced config)

ANd I get this error:

Option password.
Password.
Choose an alternative below.
y) Yes, type in my own password
g) Generate random password
y/g> y
Enter the password:
password:
Confirm the password:
password:

Edit advanced config?
y) Yes
n) No (default)
y/n> 

Error: HTTP error 400 (400 Bad Request) returned body: "{\"success\":false,\"error\":\"Invalid Session Token\"}"

@zirkuswurstikus
Copy link

zirkuswurstikus commented Mar 2, 2026

Hi All Great to hear this is working. I tried with my own apple id and password and get a 400. Would love to know the process to set it up ? I tried New remote Apple Id : myappleid@icloud.com y ( Type in my own password) passwd: Entered in my apple id password passwd: Entered in my apple id password n ( no advanced config)

Whats your output of rclone version ?

@seanbarney
Copy link

Hello. Thank you to @mikegillan for this PR. I am also on Apple Silicon and followed the build steps laid out by @zirkuswurstikus . I can confirm this has worked for me as well.

This fixes China mainland iCloud authentication by deriving the Origin
and Referer headers from authEndpoint instead of hardcoding idmsa.apple.com.

Fixes compatibility with PR rclone#8818 (China region support) and PR rclone#9209
(SRP authentication).

Signed-off-by: Xiangzhe <xiangzhedev@gmail.com>
@xz-dev
Copy link

xz-dev commented Mar 3, 2026

Nice job, I add more function for that to let it work with #8818

If it's looks fine for you , please merge it, thank you @mikegillan

@nmoura
Copy link

nmoura commented Mar 3, 2026

Just compiled this PR on Apple Silicon (M2 Air), mounted a directory and read a file without issues.

~/go/bin$ ./rclone version
rclone v1.74.0-DEV
- os/version: fedora 42 (64 bit)
- os/kernel: 6.18.10-402.asahi.fc42.aarch64+16k (aarch64)
- os/type: linux
- os/arch: arm64 (ARMv8 compatible)
- go/version: go1.25.7 X:nodwarf5
- go/linking: dynamic
- go/tags: none

Thanks, @mikegillan for this PR and all maintainers for this project.

@harijay
Copy link

harijay commented Mar 4, 2026

Hi all ,
The same flow consistently fails on Linux with whats below. Generated by CLAUDE , confirmed by me.

The error is same as before:

Error: HTTP error 400 (400 Bad Request) returned body: {"success":false,"error":"Invalid Session Token"}

Environment:

  • OS: Ubuntu 24.04 (Linux x86_64)
  • rclone: v1.73.1 and v1.74.0-beta

Observations:

  • SRP handshake appears to complete (password is accepted, no credential error)
  • Failure occurs at the session token validation step on Apple's servers
  • Identical Apple ID and app-specific password work via the browser-based cookie workaround
  • iCloud prerequisites confirmed: ADP off, "Access iCloud Data on the Web" on

Hypothesis:
Apple may be fingerprinting the SRP session request and rejecting it based on platform/user-agent characteristics specific to the Linux client, or there is a subtle difference in the
SRP implementation that only manifests on Linux.

Workaround: Browser-based cookie extraction via icloud_cookie_extractor.sh (see #8587).

@zirkuswurstikus
Copy link

zirkuswurstikus commented Mar 4, 2026

@harijay according to you post you are using a 1.74-BETA. Do you follow the provided instruction to build rclone?

Run this and paste output


mkdir -p /tmp/rclone
cd /tmp/rclone
git clone https://github.com/rclone/rclone.git .
git fetch origin pull/9209/head:pr-9209
git checkout pr-9209
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -trimpath -ldflags='-s -w' -o build/rclone-pr9209-linux-amd64 ./

rclone version

@harijay
Copy link

harijay commented Mar 4, 2026

Thanks @zirkuswurstikus . I didnt notice that AI pulled from beta branch instead of the PR. It is all working now . Was super thrilled when the 2FA popped up on my phone. I had been trying to get this functionality for months now.

You all rock!!

Hari

Here is the CLAUDE message

Update: Works on Linux when built directly from this PR branch

After being prompted by @zirkuswurstikus to build from the PR branch rather than using the beta release, I can confirm SRP auth works on Linux.

Build used:
rclone v1.74.0-DEV

  • os/version: ubuntu 24.04 (64 bit)
  • os/kernel: 6.17.0-14-generic (x86_64)
  • go/version: go1.25.0

Built with:
git fetch origin pull/9209/head:pr-9209
git checkout pr-9209
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -trimpath -ldflags='-s -w' -o build/rclone-pr9209-linux-amd64 ./

Key finding: You must use your real Apple ID password, not an app-specific password. The SRP handshake requires the real password for the cryptographic exchange. Previous failures
(including my earlier report of authSRPComplete: sign in failed: 401) were caused by using an app-specific password via rclone config create non-interactively.

The interactive rclone config flow correctly prompted for the 2FA code sent to my phone, completed the SRP handshake, and stored session cookies. rclone lsd successfully listed my
iCloud Drive.

@harijay according to you post you are using a 1.74-BETA. Do you follow the provided instruction to build rclone?

Run this and paste output


mkdir -p /tmp/rclone
cd /tmp/rclone
git clone https://github.com/rclone/rclone.git .
git fetch origin pull/9209/head:pr-9209
git checkout pr-9209
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -trimpath -ldflags='-s -w' -o build/rclone-pr9209-linux-amd64 ./

rclone version

@mikegillan
Copy link
Author

mikegillan commented Mar 4, 2026 via email

Refactor SRP auth headers to use dynamic origin
@mikegillan
Copy link
Author

Thanks to @xz-dev for the fix for mainland China, this should work for everyone now. We just need someone to approve it!

Lyapsus added a commit to Lyapsus/rclone that referenced this pull request Mar 5, 2026
Add read-only iCloud Photos support via `service = photos` config.
Lists photo libraries (Personal + Shared), albums (11 smart + user),
and photos/videos. Downloads via CloudKit URLs.

Based on @coughlanio's PoC (rclone#8734). SRP auth from @mikegillan's rclone#9209.

Photos changes from rclone#8734:
- Fix Storable() (was false, broke transfers)
- Fix List() dir entry paths (missing parent prefix)
- Fix Open(): fshttp.NewClient, shouldRetry, FixRangeOption
- Fix SetModTime: fs.ErrorCantSetModTime
- Add ctx propagation, sync.Mutex, photo name cache
- Add Root() encoding, stable timestamps, DirCacheFlush, ErrorIsFile
- Add compile-time interface checks
- Add batched album count queries

SRP changes from rclone#9209:
- Replace deprecated /signin with SRP-6a protocol
- Add SRP crypto (RFC 5054 2048-bit, SHA-256)
- Update auth headers for idmsa.apple.com

Fixes rclone#7982
@JoshPaterson
Copy link

This worked for me as well. Thank you to all who worked on it!

OS: Ubuntu 22.04 (64 bit)
Kernel: 5.15.0-171-generic (x86_64)
Architecture: amd64
Go version: go1.25.3
Build command: CGO_ENABLED=0 go build -trimpath -ldflags='-s -w' -o rclone ./

@Lyapsus
Copy link

Lyapsus commented Mar 7, 2026

@JoshPaterson would you mind to also look at #8734? Should hopefully add photos support!

@JoshPaterson
Copy link

I am receiving two different types of errors for files that are in folders that were shared to me by someone else. Files that I shared from my account to others don't have errors.

  • Subfolders within folders that were shared with me had "HTTP 400 Bad Request" errors
  • Files within folders shared with me had "RootURL not set" errors.

Please let me know if you'd like more information about these errors or the files that caused them.

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.

Invalid Session Token while connecting to iCloud
X Tutup