Compare commits

...
Author SHA1 Message Date
Lucas WintherandClaude Opus 5.5 5a6ceb59ff docs: record the Proton Drive handover with Gitea as the only copy
ci / protondrive (push) Canceled after 25m17s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 23:30:47 +01:00
Lucas WintherandClaude Opus 5.5 98552c81dc docs: record the public GitHub branch and the upstream post
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 23:23:55 +01:00
Lucas WintherandClaude Opus 5.5 a1e80580e2 docs: hand over the Proton Drive work after the plan 002 review
ci / protondrive (push) Failing after 27m24s
Correct the polling interval wording, record the comparison with
clayliddell/proton-drive-go-sdk, log the review findings left open, and
add a draft post for the upstream discussion.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 23:13:48 +01:00
Lucas WintherandClaude Opus 5.5 623c3fee2d protondrive: stop a misbehaving server from making one events poll run forever
Read at most 100 pages of events per poll; the rest are read by the next
poll.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 23:13:48 +01:00
Lucas WintherandClaude Opus 5.5 b4fa0fc8cc docs: bring the Proton Drive handoffs and architecture up to date
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 23:16:52 +01:00
Lucas WintherandClaude Opus 5.5 fa43dad943 docs: hand off Proton Drive M5 at the end of the day
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 23:15:13 +01:00
Lucas WintherandClaude Opus 5.5 d912e6ba24 protondrive: record what Task 7 of the events work left open
Record the refresh race investigation and the remaining race detector
reports in the security tests, give the reasons the TestMain and two
crypto items are left, and tick Task 7 in the plan.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 23:09:55 +01:00
Lucas WintherandClaude Opus 5.5 8382fba094 protondrive: make three tests hold up on a loaded machine
Seen failing with three race-enabled suites run at once:

- TestDownloadRefetchOtherHash had 300 ms to corrupt a block in; 2 s
- TestPollerShutdownAndReuse could apply the replayed events after its
  last use, emptying the cache, so polling stopped; it now keeps using
  the Fs while it waits
- polls of the fake remote ending after captureLog put the log config
  back raced with it; the fake remote no longer polls after captureLog

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 21:01:26 +01:00
Lucas WintherandClaude Opus 5.5 021591a0ca protondrive: document that environment-set remotes can't share a refresh
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:50:46 +01:00
Lucas WintherandClaude Opus 5.5 1517d8e3d8 protondrive: say how to reconnect when the saved key password is wrong
A saved session whose key password unlocks no key failed with "is the
password right?" alone; it now also says to run rclone config
reconnect, as other unusable saved sessions do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:50:07 +01:00
Lucas WintherandClaude Opus 5.5 01882f7910 protondrive: warn in the docs not to share rclone config output
rclone config create, show and dump print the saved session, whose
salted key password outlives a sign out. Also say that a cache unused
for 10 minutes is forgotten, and make the fake server's init.d stop
kill protonfake if it hasn't stopped within 10s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:48:40 +01:00
Lucas WintherandClaude Opus 5.5 c1aa97fe34 protondrive: refuse oversized encrypted names before decrypting them
An uncompressed encrypted name, passphrase, hash key or extended
attributes far larger than the limit for its kind was decrypted into
memory in full before being refused; one of 10 MiB took about 230 MiB
to list. It is now refused by its size first. Also wrap the error for
folders nested too deep once rather than at every level, where a
hostile server could make it about 88 KB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:47:39 +01:00
Lucas WintherandClaude Opus 5.5 a066fa3e39 protondrive: stop security tests changing the log level under a poller
captureLog writes rclone's global log config, which an Fs polling for
changes reads; three hostile server cases called it with an Fs still
polling, a data race seen under load. They now shut the Fs down or
make it after capturing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:44:27 +01:00
Lucas WintherandClaude Opus 5.5 abd6701b2b protondrive: finish a poll for changes in flight before Shutdown returns
Shutdown cancelled a poll in flight without waiting for it, so a poll
whose response had already arrived could still log after Shutdown
returned; the race detector caught one racing with a test's change to
the log level. Shutdown and Disconnect now wait for the poll, though
not for any notification it makes.

Also make TestPollerShutdownAndReuse wait for its polls rather than
count them after a fixed sleep, which failed under load.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:42:00 +01:00
Lucas WintherandClaude Opus 5.5 ebdb2307c9 protondrive: make three tests end cleanly when they fail
TestListingRacingChange now releases its held request however it ends,
rather than hanging until the package timeout, protonfake's TestRun
stops and waits for its server on a failed assertion, and
TestClientBootstrapCreateVolumeRace starts both clients at once rather
than always waiting 2 s for one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:38:31 +01:00
Lucas WintherandClaude Opus 5.5 669d7c08d1 protondrive: don't send a block's storage token on to a redirected host
Go's HTTP client drops only standard credential headers on a redirect
to another host, so pm-storage-token and x-pm-uid went with it; they
are now dropped too. Also let a later pass of a sync retry an upload
whose source changed while it was read, rather than failing it for
the whole run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:35:43 +01:00
Lucas WintherandClaude Opus 5.5 272fbe5ccb protondrive: don't fail a key lookup because another caller gave up
A caller waiting for another's fetch of the same public keys got that
caller's context error if it was cancelled; it now fetches them
itself. Also:

- build each own address's public key ring once at sign-in rather
  than on every lookup, 2-3 times per decrypted node
- a listing caching a node under a new parent forgets the old
  parent's listing, as caching a single node already did

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:33:45 +01:00
Lucas WintherandClaude Opus 5.5 d934016ba2 protondrive: fail a listing whose pages repeat instead of hanging
A server answering More with an anchor already followed made a listing
page until its context ended; it now fails at once. Also read an
extended attribute Size of null as missing, so the block sizes give
the size, rather than as 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:31:38 +01:00
Lucas WintherandClaude Opus 5.5 a60b5fc04a protondrive: stop polling for changes once the cache goes unused
With nothing subscribed to changes, a node cache neither read nor
filled for 10 minutes is now cleared, which stops polling until the
Fs is used again; before, an Fs never shut down polled for ever.

Also keep to one events request per interval:

- a Retry-After on an events request no longer holds up the listing
  that filled the cache; it delays the next poll instead
- after Shutdown, polling resumes from the cursor it had, an interval
  after reuse, instead of fetching a new cursor at once
- once the start cursor fetch has failed, the next is fetched after
  the backoff rather than inline

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:28:33 +01:00
Lucas WintherandClaude Opus 5.5 7b7cec65b8 protondrive: keep the cache when an event names a parent for the root
The root can't move, so an event reporting a parent for it now drops
only the root's listing rather than everything cached. Also:

- staleNode.parentID is the cached parent, empty for uncached nodes;
  the reported parent is always newParentID
- log at debug level when an events page with More set doesn't advance
- drop invalidateTree's walk of listings, which found nothing the
  walk of nodes doesn't, and a dead case in changedPaths
- test that refresh and a gone volume forget pending nodes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:17:50 +01:00
Lucas WintherandClaude Opus 5.5 9019302d2e protondrive: make the test server's events page size reset to default on 0
SetEventsPageSize of 0 or less served empty pages with More set for
ever; it now restores the default. Also check that deleting a draft
revision emits no event.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:15:21 +01:00
Lucas WintherandClaude Opus 5.5 1f1db51f6c test_all: skip FsChangeNotify against the real Proton Drive
fstests waits 27 s for changes and Proton allows one events request per
30 s. The live events check otherwise passed; record its findings and
tick plan 002 Task 6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:06:56 +01:00
Lucas WintherandClaude Opus 5.5 209c4c1b2e protondrive: log each page of events read at debug level
One line per page with the event count, the cursor and whether more
follow, so -vv shows what each poll read. Only IDs are logged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:06:56 +01:00
Lucas WintherandClaude Opus 5.5 4f09343620 protondrive: make the test server's events match Proton's
Checked live: rename, move and trash emit UpdateMetadata, a new
revision Update, and the Link fields of an event are the link's state
when the events are read, so a link deleted for good has no parent and
isn't trashed. The test server now does the same; injected events keep
their own fields. The backend already treats UpdateMetadata like
Update; a test pins a rename as UpdateMetadata and a folder made and
removed between polls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:05:48 +01:00
Lucas WintherandClaude Opus 5.5 cb94ae034c protondrive: say in the enable_caching help that the cache follows other clients
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 18:53:57 +01:00
Lucas WintherandClaude Opus 5.5 8fecab6450 protondrive: keep the root folder when the directory cache is flushed
DirCacheFlush reset the directory cache to the volume root until the
root was found again, so a change notified in between would be named
relative to the volume root. It now keeps the root folder's ID, as
flushes made for change events do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 18:53:57 +01:00
Lucas WintherandClaude Opus 5.5 4fa81cbe49 protondrive: test change tracking of a remote rooted at a file
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 18:53:40 +01:00
Lucas WintherandClaude Opus 5.5 16acb17940 protondrive: notify the old path of a folder moved while not cached
A folder the node cache no longer held but the directory cache did, as
after the cache is cleared, was notified only by its new parent when
another client moved it, so a mount kept showing it at its old path.
Its old path is now notified as well, and flushed as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 18:53:25 +01:00
Lucas WintherandClaude Opus 5.5 0ea3605988 protondrive: name new items exactly in change notifications
An item's event can be applied before rclone caches the item, as for
its own uploads and new folders. Its folder is still notified at once,
and the item itself is now notified at its path if it is cached within
the next two polls, with no extra requests. This makes mounts and the
FsChangeNotify integration test see exact paths for such items.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 18:53:04 +01:00
Lucas WintherandClaude Opus 5.5 2fa149c373 protondrive: document change notification
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 18:41:15 +01:00
Lucas WintherandClaude Opus 5.5 2ccddb5585 protondrive: notify mounts of changes made by other clients
Add ChangeNotify, fed by the volume events: an item is reported at its
old path, and its new folder if it moved, when it was cached; otherwise
its folder is reported. An expired cursor reports the root. Every event
batch also flushes the folders it names from the dircache, so a folder
renamed or moved by another client is found under its new path and
nothing is left at its old one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 18:37:11 +01:00
Lucas WintherandClaude Opus 5.5 27e1a9c541 fstests: fix data race in FsChangeNotify
The notify function runs on the backend's goroutine while the test
reads the changes it records, so guard them with a mutex. This showed
up as a race detector failure for backends whose ChangeNotify is
tested with -race.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 18:09:38 +01:00
Lucas WintherandClaude Opus 5.5 c7af34305f protondrive: never notify event subscribers from inside a user operation
The first poll after a fill of the cache runs on the goroutine which
filled it, which may hold the dircache's lock or VFS's. It no longer
waits for the poller's lock there, and hands any notification (a gone
volume) to a goroutine of its own, so a subscriber taking those locks
can't deadlock.

Also clamp events_min_interval for Proton's endpoint written with a
trailing dot, document that onFill must be set before the cache is
shared, test a failing start cursor on the first fill, and make the
backoff test serial and less tight, as it counts shared goroutines and
pool buffers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 16:57:59 +01:00
Lucas WintherandClaude Opus 5.5 355fb0699d protondrive: poll volume events while the cache is in use
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 16:29:55 +01:00
Lucas WintherandClaude Opus 5.5 877bb5d10c protondrive: read volume events and invalidate the cache from them
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 16:05:46 +01:00
Lucas WintherandClaude Opus 5.5 d607fc6581 protondrive: emulate volume events in the fake server
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 15:53:25 +01:00
Lucas WintherandClaude Opus 5.5 6b02131ba3 docs: plan Proton Drive events and change notification (M5)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 15:42:33 +01:00
Lucas WintherandClaude Opus 5.5 fbf2d565e8 docs: bring the Proton Drive handoff up to date after the final review
ci / protondrive (push) Successful in 23m29s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 15:12:51 +01:00
Lucas WintherandClaude Opus 5.5 944b360054 protondrive: follow the renamed Proton code constant in the fake's tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 14:55:58 +01:00
Lucas WintherandClaude Opus 5.5 ad3552e2e5 docs: note the Proton Drive block size cache in the architecture
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 14:48:33 +01:00
Lucas WintherandClaude Opus 5.5 bfa9b7122b protondrive: tighten tests and docs from the final review, log the rest
Timing bounds in parallel tests leave room for a slow race runner, the
block 502 test checks its fault fired, refusedCommit's 408 exclusion is
pinned, the user docs explain the "server doesn't show it yet" error and
the conformance checklist matches the current draft and trust rules.
Every other minor from the review is listed in docs/TODO.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 14:48:17 +01:00
Lucas WintherandClaude Opus 5.5 d3aee2af19 protondrive: tidy small review findings
Log lines no longer cut a name inside a UTF-8 sequence, the "superseded
upload" error says the file's state rather than claiming a revision is
active, a test-only block decrypter moves to the tests, a one-line alias
of encryptPassphrase goes, the upload godoc is rewrapped and the fake's
server struct is gofmt clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 14:46:52 +01:00
Lucas WintherandClaude Opus 5.5 30cfcac416 protondrive: retry a token refresh that failed with a server error or timeout
When the access token expired and the refresh request got a 5xx, 429 or
408, the operation failed instead of being retried, and a 408 even ended
the session as if the refresh token had been rejected. Such failures are
now retried like any other call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 14:45:39 +01:00
Lucas WintherandClaude Opus 5.5 634a2423eb protondrive: make the tests honour RCLONE_CONFIG whichever tests run
Only fstest.Initialise, run by the integration test, applied
RCLONE_CONFIG, so a filtered test run read the default rclone config,
which may hold a real Proton remote. TestMain now applies it first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 14:44:18 +01:00
Lucas WintherandClaude Opus 5.5 b5da26169a ci: give the Proton Drive race suite room on a slow runner and pin its tools
The race suite ran under Go's default 10 minute limit, which a slow
runner would hit; it now has 40 minutes in a 60 minute job. Go is the
1.27 line the code was verified with, not go.mod's minimum, so
govulncheck checks a patched standard library, and govulncheck is
pinned to v1.9.0 so a new release can't break the build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 14:41:48 +01:00
Lucas WintherandClaude Opus 5.5 b4a23a31ed protondrive: log Proton's announced drive requirements at debug level
Proton announces requirements it will enforce, such as a minimum client
version, in x-pm-drive-requirements response headers. They are now
logged at debug level whenever they change, so they show in -vv bug
reports.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 14:41:19 +01:00
Lucas WintherandClaude Opus 5.5 5dbf5b04c6 protondrive: stop telling users to upgrade rclone on unrelated Proton errors
Proton's generic "requirements not met" code 2000, which it returns for
a node changed since it was read (as when a lost rename response is
resent), aborted the whole run with "upgrade rclone". It is now an
ordinary error that isn't retried. Code 2028 (a feature disabled,
rate-limited or blocked) is retried, and the codes Proton actually uses
for a rejected or missing app version, 5003 and 5001, now give the
upgrade message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 14:40:13 +01:00
Lucas WintherandClaude Opus 5.5 788a711dc9 protondrive: make multi-thread downloads of files without block sizes linear
A file whose extended attributes don't record its block sizes, as some
other clients write, was read from the first block for every ranged
read, so a multi-thread copy, which opens each chunk as a range,
downloaded data quadratically: about 8.5 GiB for a 1 GiB file. The sizes
of blocks already read are now remembered per Fs (bounded to 16
revisions and 65536 blocks), so later chunks start at the right block.
A block list that disagrees with what was learnt is an integrity error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 14:37:40 +01:00
Lucas WintherandClaude Opus 5.5 674b538ca5 protondrive: keep the draft client ID stable for remotes configured from the environment
A remote set up with environment variables or a connection string can't
save its draft client ID, so it got a new random one on every run, and a
draft left by an earlier run then blocked uploading that name as if
another client were uploading it. When the ID can't be saved it is now
derived from the remote name and the account's volume, so it is the same
on every run. A remote in the config file still gets a random ID, saved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 14:32:14 +01:00
Lucas WintherandClaude Opus 5.5 94eedc942f protondrive: ignore modification times and hashes the owner didn't sign
Extended attributes whose signature failed or was missing were used, so
a server could set a file's modification time, size and SHA-1 at will:
for example make `rclone sync --update` skip every changed file. Without
ignore_signature_errors they are now not used and an ERROR is logged:
the modification time is the server's, there is no SHA-1, and the size
comes from the recorded block sizes, which reads check. The file stays
readable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 14:29:40 +01:00
Lucas WintherandClaude Opus 5.5 67e4cfeb7a protondrive: never reuse the session key of an unverified name on rename or move
Renaming or moving a node whose name signature failed reused the old
name's session key, which a server that made that name knows, so it could
read the new name. The new name now gets a new session key unless the
old name's signature verified.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 14:26:02 +01:00
Lucas WintherandClaude Opus 5.5 b3f5685e62 protondrive: refuse writing into a folder whose name hash key is unsigned
A folder's hash key whose signature failed or was missing was only
logged, and the names of everything written into the folder were then
hashed with it, so a server which made the key could test guesses at
those names. Creating, uploading, renaming, moving or copying into such
a folder is now refused unless ignore_signature_errors is set, when an
ERROR is logged once per folder. Reading is unaffected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 14:24:56 +01:00
Lucas WintherandClaude Opus 5.5 db7afafc08 protondrive: refuse node keys whose Argon2 parameters would exhaust memory
A node or share key from the server was unlocked with whatever Argon2
parameters it carried, so a crafted key could make every listing
including it allocate up to 2 TiB and crash rclone. Node and share keys
locked with Argon2 beyond 4 passes, parallelism 8 or 128 MiB are now
refused before the key derivation runs, as an integrity error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 14:23:50 +01:00
Lucas WintherandClaude Opus 5.5 e95317b52a protondrive: refuse files whose content key signature is missing
A server could replace a file's content key with one it made, without a
signature: updates were then encrypted with a key the server knows, and
ranged reads returned whatever it encrypted under that key. A missing or
invalid content key signature now makes the file unverified, like an
unverified node key: reads and updates are refused unless
ignore_signature_errors is set, when an ERROR is logged once per file and
operation. The fake gains corruptions for this and for the hash key,
name and extended attribute checks that follow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 14:21:14 +01:00
Lucas WintherandClaude Opus 5.5 ea8d09ce43 docs: say which Proton Drive test files can't go upstream without the fake
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 14:10:53 +01:00
Lucas WintherandClaude Opus 5.5 29ad269444 docs: record the Proton Drive live verification results
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 13:53:13 +01:00
Lucas WintherandClaude Opus 5.5 b440df2e1f docs: note the conflict-cleanup delete after an unknown upload outcome
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 12:33:37 +01:00
Lucas WintherandClaude Opus 5.5 61733fce6e protondrive: add re-keyed fixtures of files written by Proton's web app and CLI
Captured from a throwaway test account on 2026-11-12 and re-keyed onto
test-only keys; no key, passphrase, ID or email of the account is in them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 12:32:19 +01:00
Lucas WintherandClaude Opus 5.5 f8e398fce1 protondrive: skip the live integration test during a golden capture
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 12:31:02 +01:00
Lucas WintherandClaude Opus 5.5 be00536078 protondrive: never delete an upload's draft unless Proton refused the commit
After a failed commit the client read the file back and deleted the
draft if the read showed it uncommitted. Proton's server can take a
moment to show a commit, so a stale read could send a delete against a
file or revision which had in fact been committed.

The draft is now deleted only when the server refused the commit with a
4xx API error on a single attempt which was not resent; any other
failure leaves it and reports that the upload may have succeeded. A
cancelled upload whose commit succeeded stops waiting for the server to
show it at once. The fake server's read lag can also serve a file
without an active revision.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 12:30:07 +01:00
Lucas WintherandClaude Opus 5.5 1b2fcbd212 protondrive: make the golden capture fail closed on foreign keys
The capture names its folder with -capture-remote and refuses -remote,
so a mistyped -run can't start the integration tests against the
account. Every key written has a placeholder user ID; a node key holding
another user's email, or a signature by or intended for a key not being
replaced, fails the capture without naming it. Salt notations are
written only where the original had one.

TestGoldenFixtures now also walks every key, key packet and signature of
a fixture, inside encrypted messages too, and checks folder modification
times; tampered fixtures are shown to fail.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 12:19:05 +01:00
Lucas WintherandClaude Opus 5.5 af6fdd7899 protondrive: fix uploads failing while Proton catches up after a commit
Proton's server can take a moment to show a change: a file read back
just after its commit may be missing or still a draft with no active
revision. Uploads then failed with "upload committed, but the file now
has another revision (revision  is active ...)" or "object not found".

The read back after a commit, the read of a server-side copy and, with
enable_caching off, the read of a folder to write into now retry with
backoff for about 7 s while the change doesn't show. A committed upload
the server still doesn't show after that fails with a new "committed,
but the server doesn't show it yet" error, deletes nothing and is not
retried at once. The fake server gains SetReadLag to model the lag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 11:57:02 +01:00
Lucas WintherandClaude Opus 5.5 a2dc6f71f1 protondrive: test against re-keyed files written by Proton's own clients
TestGoldenCapture (-capture DIR -capture-label NAME -remote NAME:PATH)
captures a folder another client wrote, re-keyed onto keys made for the
tests: blocks and signature-free SEIPD packets kept byte for byte, key
packets re-wrapped, signatures re-made with the same parameters (names,
hash keys and extended attributes encrypted again with their own session
keys, as their signatures are embedded), IDs and emails replaced.
TestGoldenFixtures reads every fixture in testdata/golden through the
backend from the fake server with every signature checked.

The fake server can now seed stored folders and keeps the times of
seeded nodes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 11:35:36 +01:00
Lucas WintherandClaude Opus 5.5 502a32b5f3 protondrive: name the SDK commit the client was last checked against
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 10:30:31 +01:00
Lucas WintherandClaude Opus 5.5 22b9c4fdc3 protondrive: fix ranged reads of files which don't record block sizes
A file written by an old client without recorded block sizes was read
in ranges by assuming 4 MiB blocks, so a range past the first block could
return the wrong bytes, or too few, without an error. Such a range is now
read from the first block, discarding the bytes before it, with every
block still checked against its hash.

Also identify the SDK the conformance checklist was checked against by
commit, and align the enable_caching help with the docs page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 10:19:53 +01:00
Lucas WintherandClaude Opus 5.5 7be50f9e22 protondrive: update docs for the rewritten backend
Rewrite the hand-written parts of the Proton Drive docs page for the
native client: browser and password sign-in with a transcript taken from
the current code, the third-party notice, what is verified on upload and
download, the ranged-read caveat, the trust rules and the
ignore_signature_errors option, the two protocol limits, the --dump
warning, filename rules, drafts, trash and limits. The generated options
section is left for make backenddocs.

Describe everything ignore_signature_errors allows in its help text,
update the feature list and score in protondrive.yaml, and add the SDK
conformance checklist and fork notes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 10:06:19 +01:00
Lucas WintherandClaude Opus 5.5 c1464a39ab docs: mark Task 19 done in the Proton Drive handoff
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 09:54:41 +01:00
Lucas WintherandClaude Opus 5.5 fd7a0dc98d chore: allow golangci-lint run in the Claude Code settings
The owner added the rule; docs/PERMISSIONS.md already listed it in the
Allow tier. Also record that the force-with-lease deny rule misses the
`--force-with-lease=` form.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 09:53:08 +01:00
Lucas WintherandClaude Opus 5.5 cf7577dc1d protondrive: drop Proton-API-Bridge and go-proton-api
The old libraries were only used by the differential crypto tests. Those
go, and TestFrozenFixtures moves into crypto_internal_test.go without its
-update branch, so files the old library wrote stay checked through the
frozen fixtures in testdata/oldlib. go mod tidy removes the two libraries
and the modules only they pulled in; relvacode/iso8601 leaves go.mod.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 09:51:43 +01:00
Lucas WintherandClaude Opus 5.5 b667a1ab0f docs: record the owner's answers, the history rewrite and Task 18 in the Proton Drive handoff
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 09:29:46 +01:00
Lucas WintherandClaude Opus 5.5 5afbe9e4fa protondrive: run the wider suites against the fake server in fork CI
Add the TestProtonDriveFake init.d script, which builds and starts
protonfake on port 28642 and passes on the session it prints, and a
test_all entry for it, so fs/operations, fs/sync, vfs and cmd/bisync
run against the fake server. Five tests that need a second Fs of the
same account to see what the first wrote are ignored, as each Fs
caches separately, and cmd/gitannex is skipped as for every init.d
remote; both are in docs/TODO.md.

Add a Gitea Actions workflow that builds, vets, lints, race-tests,
fuzzes and vulnerability-checks the backend, and re-verify the agent
permission table on the host toolchain.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 09:23:26 +01:00
Lucas WintherandClaude Fable 5.2 65840bdc57 docs: record open questions and credential locations in the Proton Drive handoff
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-11 21:24:07 +01:00
Lucas WintherandClaude Fable 5.2 4282540e02 docs: correct the Proton Drive handoff after a cold read
build / windows (push) Skipped
build / other_os (push) Skipped
build / mac_amd64 (push) Skipped
build / mac_arm64 (push) Skipped
build / linux (push) Skipped
build / go1.26 (push) Skipped
build / linux_386 (push) Skipped
build / lint (push) Skipped
build / android-all (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/386 (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/amd64 (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/arm/v6 (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/arm/v7 (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/arm64 (push) Skipped
Build & Push Docker Images / Merge & Push Final Docker Image (push) Waiting for needed jobs
A reader with only the repository walked the handoff and the remaining
plan tasks against the code. Mark the plan and spec lines that later
rulings superseded, add corrections for Tasks 18 to 21, fix stale TODO
and PERMISSIONS entries, ignore the skill workspace, point AGENTS.md at
the handoff and take the test account usernames out of it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-11 21:21:43 +01:00
Lucas WintherandClaude Fable 5.2 31f6aafd91 docs: hand off the paused Proton Drive backend rewrite
Development of plan 001 is paused after Task 17 of 21. Record the state,
the rulings made during execution, the test setup and how to resume in
docs/plans/001-protondrive-tier1-HANDOFF.md, commit the execution ledger,
task briefs, implementer reports and reviews, and mark the plan and spec
with their current status.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-11 21:09:43 +01:00
Lucas WintherandClaude Opus 5.5 9c90998f76 protondrive: correct the memory a full read may hold and note what remains unbounded
The comment and spec put the block metadata a full read holds at a few
hundred bytes a block; with realistic tokens and URLs the cap allows
about 250-350 MiB, and those fields and API response bodies aren't
bounded at all. TODO also asks the live check to confirm the
root-of-trust rule on a drive made by Proton's own apps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 20:46:18 +01:00
Lucas WintherandClaude Opus 5.5 0cd3af4295 protondrive: return only the bytes asked for when a range covers the whole file
A range covering the whole file was read as a plain full read, so a
file whose recorded size understated its content returned more bytes
than the range asked for. It is now checked as a full read but stops
at the end of the range.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 20:45:54 +01:00
Lucas WintherandClaude Opus 5.5 8666b66038 protondrive: stop at a folder the server reports as its own ancestor
Fetching a file or folder fetches its folders up to one already known,
and a server reporting folders as each other's parents made that
recurse until rclone crashed. A cycle, or folders nested more than 2048
deep, is now an error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 20:44:19 +01:00
Lucas WintherandClaude Opus 5.5 52a44e41ce protondrive: log an error for each unverified file or folder used with ignore_signature_errors
With ignore_signature_errors set, reading a file, writing into a folder,
or moving, copying or renaming either when its key isn't verified went
ahead without a word. Each now logs an error, once per file or folder
and operation, saying what was done and why the key isn't trusted - and
that what is moved or copied out is trusted from then on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 20:42:14 +01:00
Lucas WintherandClaude Opus 5.5 15bc60ead9 protondrive: verify your own address in any letter case with your own keys
The check that the drive is shared and signed by your own address
ignored letter case, but keys were looked up by exact address, so a
server naming your address in capitals could serve keys of its own for
it. Addresses are now matched ignoring ASCII case only, everywhere, and
your own address in any such case is always verified with your own
keys, never ones the server supplies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 20:40:43 +01:00
Lucas WintherandClaude Opus 5.5 c28077174e protondrive: check warm listings within the listing budget test
The warm listing budget, with caching on and off, reuses the folders
TestBudgetList seeds instead of seeding its own, which saves about 15s
under -race. The assertions are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 20:30:05 +01:00
Lucas WintherandClaude Opus 5.5 16480e3d80 protondrive: refuse to write names which aren't valid UTF-8
With an encoding which lets invalid UTF-8 through, rclone could create
a file or folder whose name Proton's clients, and rclone itself, then
refuse to read, so it vanished from listings. Such names are now
refused when writing.

Found by FuzzValidateName.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 20:28:21 +01:00
Lucas WintherandClaude Opus 5.5 18b4f7d1d1 protondrive: don't list a file as empty when its recorded size is missing or invalid
A file whose extended attributes had a SHA-1 but a missing, negative or
malformed Size was listed with size 0, so copying it failed with a size
mismatch. Its size is now the sum of its recorded block sizes, if they
are usable, and otherwise unknown, as for a file without extended
attributes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 20:27:33 +01:00
Lucas WintherandClaude Opus 5.5 f89be42eaf protondrive: don't cache a folder listing read while the folder changed
A listing whose pages were read before a file or folder was made in,
copied into or trashed from the folder cached what it read when it
finished, overwriting the change. As new files are added to a cached
listing rather than dropping it, the stale listing then lasted: a file
just uploaded stayed missing, and a trashed one stayed listed, until
something else dropped the listing.

The node cache now counts changes to the children of each folder being
listed, and a listing is cached only if none happened since its first
page was read. The count is kept only while a listing is in progress.

The fake server gains a Hold fault, which answers a request with the
response made on arrival only once released, to test this without
sleeps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 20:25:35 +01:00
Lucas WintherandClaude Opus 5.5 e7e43095d8 protondrive: restore the config storage even when rclone's config is in memory
The password test's cleanup restored the config path before the
storage, and setting the storage does nothing while the path is empty,
so with an in-memory config the storage was left behind.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 20:10:25 +01:00
Lucas WintherandClaude Opus 5.5 2a1a8f046e protondrive: restore rclone's config storage after the password test
TestPasswordStoredObscured installed the config file storage for its
temporary config and left it in place for the tests after it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 20:03:39 +01:00
Lucas WintherandClaude Opus 5.5 202b64cf1e protondrive: cap the number of blocks a file may list
A full read holds the list of a file's blocks until it has checked the
signed manifest, and how long that list could grow was bounded only by
a size the server sends unsigned. A file listing more than 2^19 blocks
(2 TiB in 4 MiB blocks) is now an error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 20:03:18 +01:00
Lucas WintherandClaude Opus 5.5 58fde59f34 protondrive: check the signed block list when a range asks for the whole file
A request for the whole file as a range, as "bytes=0-" from serve http
or webdav clients, was read as a ranged read, which can't check the
file's signed block list. It is now read as a full read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 20:01:02 +01:00
Lucas WintherandClaude Opus 5.5 cb6e6759b8 protondrive: trust a drive only if it is shared and signed by your own address
The drive's share and root folder were accepted when signed by any
address whose keys the server served, so a hostile server could vouch
for a whole drive of its own making. If the share wasn't created, or
the root folder signed, by one of the account's own addresses, reading
and writing now fail with an error saying so, unless
ignore_signature_errors is set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 20:00:16 +01:00
Lucas WintherandClaude Opus 5.5 787bb3b3fa protondrive: refuse to move or copy a file or folder whose key isn't verified
Moving or copying a file or folder out of a folder with a forged key
had rclone sign the server's key as the user's, after which it read
without error. Moving, copying or renaming a node whose key isn't
verified now fails unless ignore_signature_errors is set, and anything
beneath such a folder, including what rclone writes there with the
option set, stays unverified.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 19:57:18 +01:00
Lucas WintherandClaude Opus 5.5 12ce57ed31 protondrive: add request budget tests and fuzz targets
Request budgets, measured against the fake server and asserted at the
measured counts, for listing (cold and warm, with and without
enable_caching), a no-op sync by modification time and by checksum,
NewObject three folders deep, uploads and making folders. A TestMain
hook fails the package if any test, through any fake server, requested
a removed share-scoped route; the fake keeps a process-wide tally of
them for it.

Fuzz targets, seeded from testdata/oldlib and from what the fake
writes, for name validation, link decoding, extended attributes, API
error bodies with Retry-After and session fork payloads. Each checks
invariants of what is accepted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 19:48:18 +01:00
Lucas WintherandClaude Opus 5.5 90b2a48e71 protondrive: skip nodes the server sends without an ID or with a negative size
A node which decrypted was handed on whatever its link ID and encrypted
size, so a faulty or hostile server could make rclone cache a node
under an empty ID or list a file with a negative size. Such nodes are
now left out of listings and logged, as nodes with invalid names are.

Found by FuzzDecodeLink.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 19:33:39 +01:00
Lucas WintherandClaude Opus 5.5 32710eefd1 protondrive: refuse metadata which decompresses to an excessive size
Names, node passphrases, hash keys and extended attributes are
encrypted to public keys, so the server, or anyone, can write them as a
small compressed message which expands to gigabytes when decrypted.
They were decompressed into memory without a limit.

Names, passphrases and hash keys are now limited to 64 KiB and extended
attributes to 16 MiB, enough for the block sizes of an 8 TiB file.

Extended attributes with a negative Size are now read as having no
size, as the reference SDK reads other invalid sizes, so a file is never
listed with a negative size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 19:22:28 +01:00
Lucas WintherandClaude Opus 5.5 3fac25de75 protondrive: don't list a folder again after each file or folder made in it
Making a file or folder, or a server-side copy, forgot the cached
listing of its folder, so the next upload into that folder - which
checks the name is free - listed the whole folder again. Copying M new
files into a folder of N cost on the order of M*N/100 extra requests.

The new node is now added to the cached listing. A failed call still
forgets it, as the call may have taken effect.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 19:19:14 +01:00
Lucas WintherandClaude Opus 5.5 751a3ea357 protondrive: test config obscuring the password and refused updates
The password test now runs rclone config with the password in plain
text and checks it is stored obscured; a test checks a file whose key
isn't its owner's can't be updated; and the log test checks every
session token the fake issued, which it now labels by kind, not only
the saved ones.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 18:59:21 +01:00
Lucas WintherandClaude Opus 5.5 248f467054 protondrive: don't save refreshed tokens after disconnect
A token refresh still in flight when the remote was disconnected could
write its new tokens back to the config after Disconnect removed them.
Disconnect now waits for it and removes them afterwards.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 18:59:21 +01:00
Lucas WintherandClaude Opus 5.5 9598e22d90 protondrive: check a file's signed block list before handing out any of it
A full read verified the manifest signature only at the end, so blocks
a server swapped, dropped or took from an older revision reached the
reader - and rclone cat or a mount - before the error. A full read now
lists every block and verifies the manifest before releasing the first
byte, and a block listed again with a different hash is an integrity
error. Ranged reads are unchanged.

The fake's "rollback" corruption is renamed "block-rollback": serving a
whole earlier revision under the current revision ID can't be detected,
which the spec and TODO now say.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 18:51:15 +01:00
Lucas WintherandClaude Opus 5.5 88654eb04d protondrive: distrust files and folders beneath a folder with a forged key
A server could put a folder with a key of its own making into a real
folder and serve anonymous files beneath it whose every signature
checked out against that key, so they were read without an error.
A key checked against an unverified folder key is now unverified too,
all the way down: reading such a file, or creating, uploading, moving
or copying anything into such a folder, fails unless
ignore_signature_errors is set. Listing it still works.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 18:48:11 +01:00
Lucas WintherandClaude Opus 5.5 f4105adfc1 protondrive: add hostile-server and secret-hygiene tests
A hostile fake server tampers with block hashes, block order and count,
serves an older revision's or another key's blocks, signs a node
passphrase with its own key and gives nodes names such as "../x", "a/b",
NUL and 10 MiB: each read fails or the node is left out, with and
without ignore_signature_errors, and a copy or sync writes nothing
wrong or outside its destination. The log at debug level, and with
--dump headers, holds no password, key password, session or storage
token, node passphrase or session key; browser sign-in leaves no
password in the config and password sign-in keeps it obscured.

The fake gains rollback and foreign-block corruptions and exposes the
secrets it issued and a file's node secrets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 18:34:50 +01:00
Lucas WintherandClaude Opus 5.5 db97ef7c8d protondrive: stop a call rejected during disconnect retrying without credentials
A call in flight when the remote was disconnected, and then rejected
as unauthorised, was sent again with empty credentials. It now fails
with the "not signed in" error instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 18:22:53 +01:00
Lucas WintherandClaude Opus 5.5 59f0f31b34 protondrive: name the remote to reconnect when the session expires or is signed out
An expired session, or one signed out with "rclone config disconnect"
while a long running rclone was using it, told the user to reconnect a
remote literally called "remote:". The error now names the remote.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 18:22:02 +01:00
Lucas WintherandClaude Opus 5.5 6065801968 protondrive: refuse to read a file whose key isn't signed by its owner
A file whose node passphrase signature didn't verify was read with
only an error logged when it was listed, although a server could have
substituted a key of its own making and with it any content. Reading
such a file now fails unless ignore_signature_errors is set, as for a
content key or manifest signature which fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 18:21:07 +01:00
Lucas WintherandClaude Opus 5.5 8600815384 protondrive: add regression tests for known failures
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 18:05:33 +01:00
Lucas WintherandClaude Opus 5.5 14128d59c0 protondrive: say how to read a file whose signature fails to verify
The error for a content key or manifest signature which fails now
ends with a hint to set ignore_signature_errors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 17:59:16 +01:00
Lucas WintherandClaude Opus 5.5 6e2410de69 protondrive: name the remote in the CAPTCHA error and offer browser sign-in in config
A CAPTCHA on password sign-in told the user to reconnect a remote
literally called "remote:". The error from NewFs now names the remote
and is fatal, and rclone config goes back to the choice of sign-in
method with a hint to use the web browser.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 17:58:35 +01:00
Lucas WintherandClaude Opus 5.5 c00162ed20 protondrive: keep folders with unseen content, saved sessions on config edits and a usable Fs after shutdown
- rmdir asks the server whether a folder has any child, so it never
  trashes a folder holding Proton Docs, unreadable files or children
  added by another client since it was listed
- rclone config asks before replacing a saved session (default no), and
  signs the old session out after signing in again
- Shutdown only drops caches; after Disconnect the remote reports that
  it isn't signed in
- update, move and copy refuse a file another client has trashed
- ranged reads are computed on the decrypted size whatever
  original_file_size says

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 17:47:54 +01:00
Lucas WintherandClaude Opus 5.5 0ec7dce0e5 protondrive: rewrite the backend on a native client following Proton's SDK
The backend no longer uses Proton-API-Bridge or go-proton-api at run
time. Saved sessions from earlier versions keep working without signing
in again, and every option keeps its name and meaning. rclone config
now offers sign-in with a web browser. The full backend test suite runs
against an in-process fake Proton server.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 17:28:24 +01:00
Lucas WintherandClaude Opus 5.5 414de88c99 protondrive: add hermetic contract test harness
Add protonfake, a command which serves the fake Proton server on a
given address and prints the endpoint and a saved session of a user
with a Drive volume as key=value lines, for an fstest test server
script. protontest gains NewStandalone and Close for it.

Add setupFakeRemote, which configures TestProtonDriveFake: with a
fresh session on the fake through environment variables, and
TestIntegrationFake, which runs the backend contract suite against it.
It is skipped until the backend has an endpoint option.

client_salted_key_pass is written base64 encoded, the meaning the key
has in existing configs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 17:01:53 +01:00
Lucas WintherandClaude Opus 5.5 e23de0b586 protondrive: make the volume creation race test deterministic
TestClientBootstrapCreateVolumeRace failed now and then in whole
package race runs with "expected 2 POST /drive/volumes, got 1".

loadAccount holds the process-wide unlockMu while unlocking keys, so
the second client's first GET drive/v2/shares/my-files is sent one
key unlock, or under load many queued unlocks, after the first's. The
500ms Delay fault on both GETs shifted them equally and so did not
keep them together: when the first client's create had finished
before the second client's GET was handled, the second client found
the volume and never POSTed. The client handles that ordering
correctly; only the test's assertion depended on timing.

Add a Gather option to protontest faults, which holds each faulted
request until all Times have arrived, and use it for both GETs and
both POSTs. The test now starts the second client as late as the
server lets it, which failed 10 of 10 runs with the Delay fault.

Gathering the POSTs runs both create volume checks in the fake at
once, which exposed a data race there: go-crypto records signature
checks in a key on its first use, and the new user's address key had
never been used. Check each request with its own copy of the key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 16:37:14 +01:00
Lucas WintherandClaude Opus 5.5 e65c785c15 protondrive: state the v6 file fixture's provenance in the fixture
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 15:07:50 +01:00
Lucas WintherandClaude Opus 5.5 e76b6783b2 protondrive: add whole-file differential tests
Files uploaded by the new client are read whole by the old library's
readers, and files written by the old library (go-proton-api v1.0.4,
Proton-API-Bridge v1.0.5), in the legacy and the v6 format, are
downloaded by the new client through the fake server. Frozen fixtures
of both formats keep the second direction tested once the old library
is gone.

protontest gains read-only access to a file as stored, seeding of a
stored file, and users built from fixed address and root keys. It now
signs no earlier than the signing key's creation time, which a key
supplied by a test may be.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 14:52:55 +01:00
Lucas WintherandClaude Opus 5.5 dc7d977e17 protondrive: delete an upload's draft only on proof its commit failed
After a failed commit the draft was deleted whenever the file read
back did not show the upload's revision active. A file changed by
another client since (a newer revision, or trashed) proves nothing,
and deleting then removed committed content. Now the draft is deleted
only when the read shows a new file still a draft, or the revision the
upload started from still active; otherwise nothing is deleted and
the outcome is reported as unknown. A successful commit whose file
already has another revision when read back is an error rather than
returning another client's revision as the upload's.

The fake gains SeedRevision, giving a file a new revision as another
client would.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 14:25:08 +01:00
Lucas WintherandClaude Opus 5.5 1f1b2bb325 protondrive: never delete an upload whose commit may have succeeded
After a failed commit the upload read the file back with the caller's
context. When that context was cancelled, or the read failed, the
commit was taken as not done and its draft deleted, which deletes the
file or revision if the commit had in fact taken effect. The read now
runs past cancellation, the draft is deleted only when the read shows
the revision is not active, and an outcome that can't be read is
reported as possibly successful without deleting anything.

Also fix a data race in the fake's storage upload handler and add
tests for a failing source and for a failing block with the producer
blocked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 14:18:00 +01:00
Lucas WintherandClaude Opus 5.5 9a4ed61a88 protondrive: add verified block upload
Upload follows the SDK's upload module (js/v0.23.0) step for step:
draft file or draft revision, name-conflict handling (own draft
deleted and retried once, another client's draft refused unless
replace_existing_draft, an existing file given a new revision),
verification code, blocks encrypted and checked against the server's
content key, upload URLs requested in batches, uploads bounded per file
(upload_concurrency, default 4, max 5) and per client (10 blocks),
size and SHA1 checks, signed manifest and extended attributes, commit
with a check that a failed commit is not in fact active, and draft
cleanup on any failure or cancellation. Blocks are streamed through
lib/pool buffers; a block is tried 3 times and a storage 404 requests a
new URL. Cancellation is bounded everywhere, including waits for the
client's upload tokens.

The fake server gains the upload routes with strict checks: names,
passphrases and extended attributes must be non-AEAD, the file key a
v4 key with an ordinary S2K, the content key packet a v6 PKESK and
blocks SEIPDv2; verifier tokens, block indexes, declared hashes and the
manifest signature are checked at commit. It now signs a day in the
past and checks signatures in Dump at its own clock, so clock-skew
tests can run in both directions.

Where the plan and the SDK differ on the wire, the SDK wins:
- IntendedUploadSize follows the SDK: for a new file it is sent only
  when the rounded size is over 100,000 bytes; for a new revision it is
  reduceSizePrecision (4095 under 4 KiB, 100,000 under 100 kB).
- BlockList entries carry no Hash or Size (deprecated, unused); the
  fake checks a Hash only if one is sent.
- An empty stream of unknown size is uploaded as zero blocks, which
  the SDK refuses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 14:07:27 +01:00
Lucas WintherandClaude Opus 5.5 e8b1359760 protondrive: reject absurd recorded block sizes when downloading
A recorded block size above 8 MiB is now an integrity error, so the
sizes can't overflow, and a full read always lists the blocks so it
reaches the manifest check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 13:26:35 +01:00
Lucas WintherandClaude Opus 5.5 4bcafe5cd4 protondrive: add verified ranged download
Download a range of a file's content block by block: block URLs come
from the revision's paged block list (20 per page), blocks are fetched
through the storage pacer holding a download token without the
session's credentials, checked against their SHA-256 before decrypting
and decrypted into pool buffers. A full read checks the manifest
signature, by the content author's address or the node key for an
anonymous upload, and the content key packet signature is checked on
open with the SDK's fallback to every address key for files created
before 2023.

The retry and capped Retry-After logic of apiCall is shared with block
downloads through the new pacedCall.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 13:20:18 +01:00
Lucas WintherandClaude Opus 5.5 ee81e742f2 protondrive: add folder, move, copy and trash operations
Add createFolder, rename, move, copyNode, trash and emptyTrash to the
new client, following the SDK's nodesManagement.ts: names and
passphrases are re-encrypted to the new parent with their session keys
and signed by the share's address; the node passphrase signature is
sent on move and copy only for an anonymous node; API code 2500 maps to
errExists and 2501 to errNotFound. Every mutation updates the node
cache, and nodeCache.put forgets both parents' listings when a node's
parent changes.

The fake gains the matching routes plus checkAvailableHashes. It
rejects SEIPDv2 or v6 PKESK names, passphrases, hash keys and extended
attributes, and folder keys which are not v4, advertise AEAD or are not
locked with an iterated and salted S2K. It enforces name hash
uniqueness, OriginalHash, no move into a descendant and the anonymous
signature rule. Dump now also checks every name hash, and
SeedAnonymousFile seeds a public-link upload.

Wire format: the create folder request sends SignatureEmail as the SDK
does, although CreateFolderDto lists SignatureAddress.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 12:59:34 +01:00
Lucas WintherandClaude Opus 5.5 a8856ff7cd protondrive: cap Retry-After and retry public key lookups
A 429's Retry-After is now honoured for at most 60 seconds and is
waited out by the call alone, so a hostile value can neither stall the
client nor hold every other call behind the shared pacer. Public key
lookups during listing are paced and retried like every other call
instead of failing the listing on one 429 or 5xx.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 12:42:20 +01:00
Lucas WintherandClaude Opus 5.5 2065b7b09a protondrive: add node listing with cache and pacing
Add the client the new backend will use above the wire: it loads the
account, finds or creates the main volume, share and root, and lists
folders by paging child IDs and fetching their metadata 100 at a time
with at most 15 requests in flight, decrypting with the verification
keys the SDK's nodes module chooses.

A brand-new account has no volume: my-files answers code 2501 and the
client creates the volume as the SDK's shares module does, accepting
code 2500 from a concurrent creator.

Nodes whose name or key can't be decrypted, or whose name is not valid,
are left out with one error each; Proton Docs and Sheets are left out
with one notice; failed signature checks are logged once and the node
is still listed.

API calls are paced and retried: 429 waits for Retry-After before
retrying, 408 and 5xx are retried, other client errors are not.

errExists moves to Task 9, which first uses it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 12:32:38 +01:00
Lucas WintherandClaude Opus 5.5 27d06dca19 protondrive: add drive state to the fake server
Every fake user now has a real, correctly encrypted drive: a volume, a
main share and a root folder, built as the SDK's
generateVolumeBootstrap does. Seeding helpers create folders, files in
the current format (v6 PKESK content key, SEIPDv2 GCM blocks, encrypted
block signatures, signed manifest, extended attributes), legacy files
(v3 PKESK, SEIPDv1) and Proton Docs nodes; Corrupt damages them as a
hostile server would and Dump decrypts and verifies the whole tree.

The read routes (my-files, volume, share, link metadata, children,
revision) answer with the field names of the SDK's driveTypes.ts, and
block bytes are served from the same server behind pm-storage-token.

AddUserWithoutDrive models a brand-new account, whose my-files answers
HTTP 422 code 2501, and POST /drive/volumes creates its volume after
checking the key material decrypts with the address key.

protontest/crypto.go is written from the SDK independently of the
backend's crypto.go. Interface additions are recorded in the plan.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 12:14:13 +01:00
Lucas WintherandClaude Opus 5.5 3b63f3c7d5 protondrive: add differential crypto tests against the old library
Cross-test crypto.go against go-proton-api v1.0.4 and Proton-API-Bridge
v1.0.5 in both directions, compare account key unlocking with
proton.Unlock, and freeze old-library output under testdata/oldlib so
it stays tested once the old libraries are gone. github.com/relvacode/iso8601
becomes a direct requirement because the test parses modification
times as the bridge does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 11:52:16 +01:00
Lucas WintherandClaude Opus 5.5 fdcb6ab322 protondrive: verify hash keys signed by older rclone versions
Folder hash keys written by go-proton-api (and any message gopenpgp
encrypts and signs in one go) carry intended recipient fingerprints in
their signature. Decrypting with the session key alone made gopenpgp
reject those as "intended recipients in non-encrypted message", so
every folder created by released rclone reported a hash key signature
error. The intended recipients are now checked against the decryption
keys by hand.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 11:52:16 +01:00
Lucas WintherandClaude Opus 5.5 a1e1333c7d protondrive: check node signatures against the verification keys only
gopenpgp looks the signer of an inline signature up in the decryption
keys as well as the verification keys, so a name or extended attributes
signed by the parent or node key reported as verified whatever keys the
caller asked for. Decrypt the session key first and the data with the
session key alone, so only the verification keys count, as in the SDK.

Also clear the decrypted passphrase when the node key fails to unlock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 11:33:33 +01:00
Lucas WintherandClaude Opus 5.5 0bb2dbdf34 protondrive: add node cryptography
Add the OpenPGP operations of the Drive data model as pure functions:
node keys and passphrases, names, hash keys and lookup hashes, content
keys, blocks, verifier tokens, manifests and extended attributes.

The formats follow the reference SDK (driveCrypto.ts, openPGPCrypto.ts),
which differs from the plan's Task 5 interfaces in these ways, now
updated in the plan:

- node keys are v4 EdDSA/ECDH with user ID "Drive key" and ordinary S2K
  locking; file keys (aead=true) advertise SEIPDv2 with AES-256-GCM and
  folder keys don't, as the SDK's enableAead
- the content key is v6 only when the node key advertises SEIPDv2, as
  the SDK's preference-following session key generation
- the content key signature is checked on read against the node key and
  extra (address) keys
- block signatures are encrypted with the content session key, as the
  SDK's encryptSignature
- hash keys are signed by the node key; reading also accepts address keys
- reencryptPassphrase signs anew, and encryptName takes the current name
  session key for rename and move
- name length is counted in UTF-16 code units, as the SDK

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 11:27:57 +01:00
Lucas WintherandClaude Fable 5.2 666df153e2 docs: run plan tests with RCLONE_CONFIG=/notfound
Fork-only. Not for upstream.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-11 11:14:27 +01:00
Lucas WintherandClaude Opus 5.5 483c3a1518 protondrive: test that address keys with a bad signature are refused
Add a fake server switch that signs an address key's passphrase with an
unrelated key or removes the signature, and test that loading fails on
the primary address and leaves out a secondary one. Also clear an
unlocked key that can't be added to its key ring.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 11:13:50 +01:00
Lucas WintherandClaude Opus 5.5 6ec49842c4 protondrive: load and unlock account keys
Load the user and addresses and unlock their keys, one unlock at a time
across all sessions so concurrent sign-ins don't multiply peak memory.
Address key passphrases are taken from their Token only when the user
key's signature on it verifies, as the SDK requires. Other users' public
keys are fetched once per account and cached, an email without a Proton
address having no keys.

Password sign-in now derives the key password from the salt of the
primary user key rather than the first salt, which is wrong for accounts
whose user keys have different salts. The SDK uses the first salt; it
remains the fallback when no salt matches.

The fake server gives each user real Curve25519 user and address keys,
and serves GET /core/v4/users, /core/v4/addresses and /core/v4/keys/all.

gopenpgp v3 moves to a direct dependency.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 11:08:40 +01:00
Lucas WintherandClaude Opus 5.5 5a8bb0247f protondrive: test that password sign-in rejects a bad server proof
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 10:59:52 +01:00
Lucas WintherandClaude Opus 5.5 2ba0848dcc protondrive: add password and browser sign-in
Add SRP password sign-in (with TOTP, two-password accounts and a clear
error when Proton asks for a CAPTCHA), browser sign-in through a session
fork, and sign-out to the new session, with the matching routes in the
fake server. The fake does real server-side SRP with go-srp and issues
a per-user key salt.

Where the plan and the SDK differ, the SDK wins:
- the fork sign-in URL escapes its payload like encodeURIComponent
  (authWeb.ts generateSignInUrl), so the plan's literal URL is the
  unescaped form;
- POST /core/v4/auth sends PersistentCookies 1 and an empty Payload
  (auth.ts authViaPassword);
- the key password uses the first KeySalts entry (auth.ts
  getUserKeyPassword).

protontest.ApproveFork takes the sign-in URL instead of the user code,
since the payload key exists only in the URL; the plan is updated.

go-srp moves to a direct dependency.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 10:56:04 +01:00
Lucas WintherandClaude Opus 5.5 9d894ab1b7 protondrive: keep two sessions on one config working when both refresh
When two sessions that share stored credentials refresh at the same
time, both post the same single-use refresh token and the server
rejects one. The session that loses now waits briefly for the winner's
tokens to appear in the store and uses them, instead of failing with
"session expired".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 10:37:19 +01:00
Lucas WintherandClaude Opus 5.5 ea41704ade protondrive: add session with safe token refresh
Add the session used for authenticated API calls: it sends x-pm-uid,
the bearer token, x-pm-appversion and the Proton Accept header, decodes
failures into api.Error, and on HTTP 401 refreshes the tokens and
retries once. Refresh is single-flight, first adopts tokens another
session saved to the shared store, and on a rejected refresh token
returns a fatal "session expired" error without clearing the stored
credentials or retrying the refresh. The session also tracks the
server's clock from the Date header.

The app version maps development builds to the "alpha" channel, as
Proton's x-pm-appversion grammar has no "dev" channel.

The fake server gains users, sessions, single-use refresh tokens and
401 for invalid access tokens on authenticated routes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 10:33:43 +01:00
Lucas WintherandClaude Opus 5.5 2e3dbac3a4 protondrive: add fake Proton server skeleton for tests
Add the api.Error type with Proton response codes and the protontest
package: an httptest-based fake that rejects legacy share-scoped routes
with 410, enforces the published x-pm-appversion grammar, refuses
x-pm-drive-sdk-version, counts requests per route, skews the Date header
and injects scripted faults.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 10:27:41 +01:00
Lucas WintherandClaude Fable 5.2 06700aaf22 docs: add implementation plan for the Proton Drive rewrite (M0-M4)
Fork-only. Not for upstream.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-11 10:23:09 +01:00
Lucas WintherandClaude Fable 5.2 83ae5d1876 docs: approve Proton Drive spec with the fork owner's decisions
Fork-only. Not for upstream.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-11 10:17:10 +01:00
Lucas WintherandClaude Fable 5.2 6f92948dac docs: add draft spec for an SDK-conformant Proton Drive backend
Fork-only. Not for upstream.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-11 10:12:23 +01:00
Lucas WintherandClaude Fable 5.2 a28ee25855 build: add agent docs, permissions and spec/plan layout for the fork
Fork-only. Appends a Fork Workflow section to AGENTS.md and adds
GEMINI.md, per-tool permission files and docs/ pages for agent driven
development. Not for upstream.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-11 10:01:49 +01:00
189 changed files with 45182 additions and 1069 deletions

No files matched your search

+25
View File
@@ -0,0 +1,25 @@
{
"permissions": {
"allow": [
"Bash(git status:*)", "Bash(git diff:*)", "Bash(git log:*)", "Bash(git show:*)",
"Bash(git rev-parse:*)", "Bash(git add:*)", "Bash(git commit:*)",
"Bash(go mod download:*)", "Bash(go build:*)", "Bash(go test:*)", "Bash(go vet:*)",
"Bash(gofmt -l:*)",
"Bash(golangci-lint run:*)"
],
"ask": [
"Bash(git push:*)", "Bash(git merge:*)", "Bash(git rebase:*)", "Bash(git switch:*)",
"Bash(git restore:*)", "Bash(git commit --amend:*)", "Bash(rm:*)",
"Bash(go get:*)", "Bash(go mod tidy:*)", "Bash(go install:*)",
"Bash(make:*)", "Bash(docker:*)"
],
"deny": [
"Bash(git push --force:*)", "Bash(git push -f:*)", "Bash(git push --force-with-lease:*)",
"Bash(git reset --hard:*)", "Bash(git clean:*)", "Bash(git add -f:*)", "Bash(git add --force:*)",
"Bash(git commit --no-verify:*)", "Bash(git commit -n:*)",
"Bash(rm -r:*)", "Bash(rm -R:*)", "Bash(rm -rf:*)", "Bash(rm -fr:*)", "Bash(rm -Rf:*)",
"Bash(rm -fR:*)", "Bash(rm --recursive:*)", "Bash(sudo:*)",
"Read(./.env)", "Read(./.env.*)", "Read(./secrets/**)"
]
}
}
+2
View File
@@ -0,0 +1,2 @@
sandbox_mode = "workspace-write"
approval_policy = "on-request"
+20
View File
@@ -0,0 +1,20 @@
# Rendered from docs/PERMISSIONS.md - change both in the same commit.
# Deny
prefix_rule(pattern = ["git", "push", ["--force", "-f", "--force-with-lease"]], decision = "forbidden",
justification = "Force-push rewrites shared history.")
prefix_rule(pattern = ["git", "reset", "--hard"], decision = "forbidden", justification = "Discards work.")
prefix_rule(pattern = ["git", "clean"], decision = "forbidden", justification = "Deletes untracked files.")
prefix_rule(pattern = ["git", "add", ["-f", "--force"]], decision = "forbidden",
justification = "Stages ignored files, e.g. secrets.")
prefix_rule(pattern = ["git", "commit", ["--no-verify", "-n"]], decision = "forbidden", justification = "Skips hooks.")
prefix_rule(pattern = ["rm", ["-r", "-R", "-rf", "-fr", "-Rf", "-fR", "--recursive"]], decision = "forbidden",
justification = "Recursive delete.")
prefix_rule(pattern = ["sudo"], decision = "forbidden", justification = "No privilege escalation.")
# Ask
prefix_rule(pattern = ["git", ["push", "merge", "rebase", "switch", "restore"]], decision = "prompt")
prefix_rule(pattern = ["git", "commit", "--amend"], decision = "prompt")
prefix_rule(pattern = ["rm"], decision = "prompt")
prefix_rule(pattern = ["go", ["get", "install"]], decision = "prompt")
prefix_rule(pattern = ["go", "mod", "tidy"], decision = "prompt")
prefix_rule(pattern = ["make"], decision = "prompt")
prefix_rule(pattern = ["docker"], decision = "prompt")
+20
View File
@@ -0,0 +1,20 @@
{
"tools": {
"allowed": [
"run_shell_command(git status)", "run_shell_command(git diff)", "run_shell_command(git log)",
"run_shell_command(git show)", "run_shell_command(git branch --list)",
"run_shell_command(git add)", "run_shell_command(git commit)",
"run_shell_command(go mod download)", "run_shell_command(go build)",
"run_shell_command(go test)", "run_shell_command(go vet)", "run_shell_command(gofmt -l)",
"run_shell_command(golangci-lint run)"
],
"exclude": [
"run_shell_command(git push --force)", "run_shell_command(git push -f)",
"run_shell_command(git push --force-with-lease)", "run_shell_command(git reset --hard)",
"run_shell_command(git clean)", "run_shell_command(git add -f)", "run_shell_command(git add --force)",
"run_shell_command(git commit --no-verify)", "run_shell_command(git commit -n)",
"run_shell_command(rm -r)", "run_shell_command(rm -R)", "run_shell_command(rm -rf)",
"run_shell_command(rm -fr)", "run_shell_command(rm -Rf)", "run_shell_command(sudo)"
]
}
}
+56
View File
@@ -0,0 +1,56 @@
# Fork CI for the Proton Drive backend. Every step is hermetic: the
# tests run against the fake server in backend/protondrive/protontest.
name: ci
on:
push:
pull_request:
env:
# Never pick up a config holding a real Proton remote.
RCLONE_CONFIG: /notfound
jobs:
protondrive:
runs-on: ubuntu-latest
# The race suite takes about 6 minutes on a fast machine and 7 on
# two cores; the slow runner may take several times that.
timeout-minutes: 60
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v5
with:
# The line the code was verified with, not go.mod's minimum,
# so govulncheck sees a patched standard library.
go-version: '1.27.x'
# Caches the module and build caches, keyed on go.sum.
cache: true
- name: Build
run: go build ./...
- name: Vet
run: go vet ./backend/protondrive/...
- name: Lint
uses: golangci/golangci-lint-action@v8
with:
version: v2.15.0
args: ./backend/protondrive/...
- name: Test
# Go's default 10 minute limit per test binary is too short
# for the race suite on a slow runner.
run: go test -race -timeout 40m ./backend/protondrive/...
- name: Fuzz
run: |
for target in $(go test -list '^Fuzz' ./backend/protondrive/ | grep '^Fuzz'); do
go test -run '^$' -fuzz "^${target}\$" -fuzztime 20s ./backend/protondrive/
done
- name: Vulnerability check
run: go run golang.org/x/vuln/cmd/govulncheck@v1.9.0 ./backend/protondrive/...
+6
View File
@@ -22,3 +22,9 @@ resource_windows_*.syso
/librclone.h /librclone.h
.devcontainer .devcontainer
/cmd/bisync/testdata/bisync_vscode_debuggers_launch.json /cmd/bisync/testdata/bisync_vscode_debuggers_launch.json
.env
.env.*
CLAUDE.local.md
.claude/settings.local.json
.worktrees/
.superpowers/
+13
View File
@@ -134,3 +134,16 @@ Uses golangci-lint v2 with config in `.golangci.yml`. Enabled linters: errcheck,
- Command docs are in the command source code (e.g., `cmd/ls/ls.go`). - Command docs are in the command source code (e.g., `cmd/ls/ls.go`).
- Don't commit autogenerated doc changes from `make backenddocs` or `make commanddocs`. - Don't commit autogenerated doc changes from `make backenddocs` or `make commanddocs`.
- Website docs are in `docs/content/` as markdown, built with Hugo (`make serve` to preview). - Website docs are in `docs/content/` as markdown, built with Hugo (`make serve` to preview).
## Fork Workflow
This fork carries agent tooling that upstream does not. Never include `docs/specs/`, `docs/plans/`, `docs/{ARCHITECTURE,DECISIONS,TODO,PERMISSIONS}.md`, `GEMINI.md`, `.claude/`, `.codex/`, `.gemini/` or this section in a PR to rclone/rclone.
- Required skill: `superpowers` - check at session start, install if missing. There is no product UI here, so `impeccable` is not needed.
- Specs go in `docs/specs/NNN-slug.md` (permanent). Plans go in `docs/plans/NNN-slug.md` and are removed with `git rm` before merge. This overrides the superpowers default paths.
- Paused or in-flight work is listed under "Now" in `docs/TODO.md` with the handoff to read first.
- Non-trivial work: brainstorm → spec → plan → TDD → review. For trivial fixes say "trivial, skipping spec" and proceed.
- Log things noticed but deliberately not fixed in `docs/TODO.md`, record decisions in `docs/DECISIONS.md`, and keep `docs/ARCHITECTURE.md` in step with structural changes - in the same commit as the code.
- `docs/PERMISSIONS.md` is the source for the `.claude/`, `.codex/` and `.gemini/` permission files - change them together.
- Never push, force-push or commit directly to `master`. Ask before adding or upgrading a dependency (`go get`, `go mod tidy`).
- Done means run in this session and seen passing: `go build`, `go test` on the touched packages, and `golangci-lint run` on them.
+8
View File
@@ -0,0 +1,8 @@
# GEMINI.md
**`AGENTS.md` is the single source of truth for this repository.** Read
`./AGENTS.md` before doing anything else and follow it exactly.
This file exists only so Gemini CLI finds the rules. It contains no project
rules of its own - add those to `AGENTS.md`. If anything here ever conflicts
with `AGENTS.md`, `AGENTS.md` wins.
+388
View File
@@ -0,0 +1,388 @@
package protondrive
import (
"context"
"errors"
"fmt"
"net/url"
"sync"
"github.com/ProtonMail/gopenpgp/v3/crypto"
"github.com/rclone/rclone/backend/protondrive/api"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/lib/rest"
)
// unlockMu is held while unlocking account keys. Unlocking is memory
// hungry, so unlocks for different sessions run one at a time.
var unlockMu sync.Mutex
// address is one of the user's addresses with its unlocked keys.
type address struct {
ID string
Email string
keyRing *crypto.KeyRing // unlocked address keys in the server's order, primary first
publicKR *crypto.KeyRing // the public parts of keyRing
primaryKeyID string // ID of the first key of keyRing
}
// account is the signed in user's unlocked keys and addresses.
type account struct {
userKR *crypto.KeyRing // unlocked user keys
addresses []*address // the primary address is first
mu sync.Mutex
foreign map[string]*publicKeyLookup // public keys of other users by emailKey
}
// publicKeyLookup is a fetch of another user's public keys, done when
// done is closed.
type publicKeyLookup struct {
done chan struct{}
keyRing *crypto.KeyRing
err error
}
// loadAccount fetches the user and addresses of the session and unlocks
// their keys with keyPassword.
//
// User keys which don't unlock are skipped, but an error is returned
// if none unlocks or if the primary address has no key which unlocks.
// Other addresses without a key which unlocks are left out.
func loadAccount(ctx context.Context, s *session, keyPassword []byte) (*account, error) {
var user api.UserResponse
if _, err := s.call(ctx, &rest.Opts{Method: "GET", Path: "/core/v4/users"}, nil, &user); err != nil {
return nil, fmt.Errorf("load account: get user: %w", err)
}
var addrs api.AddressesResponse
// The SDK fetches only the first page of 50 addresses.
opts := &rest.Opts{Method: "GET", Path: "/core/v4/addresses", Parameters: url.Values{"Page": {"0"}, "PageSize": {"50"}}}
if _, err := s.call(ctx, opts, nil, &addrs); err != nil {
return nil, fmt.Errorf("load account: get addresses: %w", err)
}
if len(addrs.Addresses) == 0 {
return nil, errors.New("load account: the account has no addresses")
}
unlockMu.Lock()
defer unlockMu.Unlock()
if s.unlockHook != nil {
s.unlockHook()
}
a := &account{foreign: make(map[string]*publicKeyLookup)}
var err error
a.userKR, err = unlockUserKeys(user.User.Keys, keyPassword)
if err != nil {
return nil, fmt.Errorf("load account: unlock user keys: %w", err)
}
var verifyTime int64
if t := s.serverTime(); !t.IsZero() {
verifyTime = t.Unix()
}
for i, addr := range addrs.Addresses {
kr, primaryKeyID, err := unlockAddressKeys(addr.Keys, keyPassword, a.userKR, verifyTime)
var publicKR *crypto.KeyRing
if err == nil {
publicKR, err = publicKeyRing(kr)
}
if err != nil {
if i == 0 {
a.close()
return nil, fmt.Errorf("load account: unlock keys of primary address: %w", err)
}
fs.Debugf(nil, "protondrive: skipping address %s: %v", addr.ID, err)
continue
}
a.addresses = append(a.addresses, &address{ID: addr.ID, Email: addr.Email, keyRing: kr, publicKR: publicKR, primaryKeyID: primaryKeyID})
}
return a, nil
}
// errKeyPassword is returned when no user key unlocks with the key
// password.
var errKeyPassword = errors.New("no user key unlocks, is the password right?")
// unlockUserKeys returns a key ring of the active user keys which
// unlock with keyPassword. It wraps errKeyPassword if none does.
func unlockUserKeys(keys []api.UserKey, keyPassword []byte) (*crypto.KeyRing, error) {
kr, err := crypto.NewKeyRing(nil)
if err != nil {
return nil, err
}
var lastErr error
for _, k := range keys {
if k.Active == 0 {
continue
}
unlocked, err := unlockKey(k.PrivateKey, keyPassword)
if err == nil {
if err = kr.AddKey(unlocked); err != nil {
unlocked.ClearPrivateParams()
}
}
if err != nil {
fs.Debugf(nil, "protondrive: can't unlock user key %s: %v", k.ID, err)
lastErr = err
}
}
if kr.CountEntities() == 0 {
if lastErr == nil {
return nil, errors.New("no active user key")
}
return nil, fmt.Errorf("%w: %w", errKeyPassword, lastErr)
}
return kr, nil
}
// unlockAddressKeys returns a key ring of the active address keys
// which unlock, in the order given, and the ID of the first of them.
//
// A key with a Token is locked with the passphrase in it, encrypted to
// and signed by the user keys in userKR; the signature is checked as
// at verifyTime (Unix seconds, or now if zero). A key without a Token
// is locked with keyPassword.
func unlockAddressKeys(keys []api.AddressKey, keyPassword []byte, userKR *crypto.KeyRing, verifyTime int64) (kr *crypto.KeyRing, firstID string, err error) {
kr, err = crypto.NewKeyRing(nil)
if err != nil {
return nil, "", err
}
var lastErr error
for _, k := range keys {
if k.Active == 0 {
continue
}
unlocked, err := unlockAddressKey(k, keyPassword, userKR, verifyTime)
if err == nil {
if err = kr.AddKey(unlocked); err != nil {
unlocked.ClearPrivateParams()
}
}
if err != nil {
fs.Debugf(nil, "protondrive: can't unlock address key %s: %v", k.ID, err)
lastErr = err
continue
}
if firstID == "" {
firstID = k.ID
}
}
if kr.CountEntities() == 0 {
if lastErr == nil {
return nil, "", errors.New("no active address key")
}
return nil, "", fmt.Errorf("no address key unlocks: %w", lastErr)
}
return kr, firstID, nil
}
// unlockAddressKey unlocks one address key as unlockAddressKeys does.
func unlockAddressKey(k api.AddressKey, keyPassword []byte, userKR *crypto.KeyRing, verifyTime int64) (*crypto.Key, error) {
if k.Token == "" {
return unlockKey(k.PrivateKey, keyPassword)
}
dh, err := crypto.PGP().Decryption().DecryptionKeys(userKR).New()
if err != nil {
return nil, err
}
res, err := dh.Decrypt([]byte(k.Token), crypto.Armor)
if err != nil {
return nil, fmt.Errorf("decrypt token: %w", err)
}
passphrase := res.Bytes()
defer clear(passphrase)
vb := crypto.PGP().Verify().VerificationKeys(userKR)
if verifyTime != 0 {
vb = vb.VerifyTime(verifyTime)
}
vh, err := vb.New()
if err != nil {
return nil, err
}
// As the SDK does, refuse a token without a valid signature.
verified, err := vh.VerifyDetached(passphrase, []byte(k.Signature), crypto.Armor)
if err == nil {
err = verified.SignatureError()
}
if err != nil {
return nil, fmt.Errorf("verify token: %w", err)
}
return unlockKey(k.PrivateKey, passphrase)
}
// unlockKey returns the armored private key unlocked with passphrase.
func unlockKey(armored string, passphrase []byte) (*crypto.Key, error) {
locked, err := crypto.NewKeyFromArmored(armored)
if err != nil {
return nil, err
}
return locked.Unlock(passphrase)
}
// signingKeyRing returns a key ring holding only the first key of the
// address, which the SDK calls the primary address key.
func (a *address) signingKeyRing() (*crypto.KeyRing, error) {
return crypto.NewKeyRing(a.keyRing.GetKeys()[0])
}
// primary returns the primary address.
func (a *account) primary() *address {
return a.addresses[0]
}
// byID returns the address with the ID id, or nil.
func (a *account) byID(id string) *address {
for _, addr := range a.addresses {
if addr.ID == id {
return addr
}
}
return nil
}
// emailKey returns email with ASCII letters lowercased, which is how
// emails are compared: Proton treats addresses case-insensitively, but
// Unicode case folding would let a non-ASCII letter, such as the Kelvin
// sign, stand for an ASCII one.
func emailKey(email string) string {
b := []byte(email)
for i, c := range b {
if 'A' <= c && c <= 'Z' {
b[i] = c + 'a' - 'A'
}
}
return string(b)
}
// byEmail returns the address email, compared as emailKey does, or
// nil.
func (a *account) byEmail(email string) *address {
key := emailKey(email)
for _, addr := range a.addresses {
if emailKey(addr.Email) == key {
return addr
}
}
return nil
}
// owns reports whether email is one of the account's addresses whose
// keys it holds, compared as emailKey does.
func (a *account) owns(email string) bool {
return a.byEmail(email) != nil
}
// apiCaller makes an API call with the semantics of session.call,
// without returning the response; client.apiCall is one which paces
// and retries.
type apiCaller func(ctx context.Context, opts *rest.Opts, request, response any) error
// publicKeys returns the public keys of the address email, which the
// caller must not modify.
//
// The keys of the user's own addresses, in any ASCII case, come from
// the account, never the server. Other users' keys are fetched once
// per emailKey and kept for the life of the account; an email without
// a Proton address has an empty key ring.
func (a *account) publicKeys(ctx context.Context, call apiCaller, email string) (*crypto.KeyRing, error) {
if addr := a.byEmail(email); addr != nil {
return addr.publicKR, nil
}
key := emailKey(email)
for {
a.mu.Lock()
l := a.foreign[key]
fetcher := l == nil
if fetcher {
l = &publicKeyLookup{done: make(chan struct{})}
a.foreign[key] = l
a.mu.Unlock()
l.keyRing, l.err = fetchPublicKeys(ctx, call, email)
if l.err != nil {
// Let the next caller try again.
a.forgetLookup(key, l)
}
close(l.done)
} else {
a.mu.Unlock()
}
select {
case <-l.done:
case <-ctx.Done():
return nil, ctx.Err()
}
if !fetcher && ctx.Err() == nil && (errors.Is(l.err, context.Canceled) || errors.Is(l.err, context.DeadlineExceeded)) {
// The fetching caller gave up, which says nothing of
// this one.
a.forgetLookup(key, l)
continue
}
return l.keyRing, l.err
}
}
// forgetLookup forgets the lookup l of the keys of key, if it is still
// the one in use.
func (a *account) forgetLookup(key string, l *publicKeyLookup) {
a.mu.Lock()
defer a.mu.Unlock()
if a.foreign[key] == l {
delete(a.foreign, key)
}
}
// publicKeyRing returns the public parts of the keys in kr.
func publicKeyRing(kr *crypto.KeyRing) (*crypto.KeyRing, error) {
pub, err := crypto.NewKeyRing(nil)
if err != nil {
return nil, err
}
for _, key := range kr.GetKeys() {
pk, err := key.ToPublic()
if err != nil {
return nil, err
}
if err := pub.AddKey(pk); err != nil {
return nil, err
}
}
return pub, nil
}
// fetchPublicKeys fetches the public keys of the Proton address email
// with call.
func fetchPublicKeys(ctx context.Context, call apiCaller, email string) (*crypto.KeyRing, error) {
kr, err := crypto.NewKeyRing(nil)
if err != nil {
return nil, err
}
var out api.PublicKeysResponse
opts := &rest.Opts{Method: "GET", Path: "/core/v4/keys/all", Parameters: url.Values{"Email": {email}, "InternalOnly": {"1"}}}
err = call(ctx, opts, nil, &out)
var apiErr *api.Error
if errors.As(err, &apiErr) && (apiErr.Code == api.CodeAddressMissing || apiErr.Code == api.CodeDomainExternal) {
return kr, nil
}
if err != nil {
return nil, fmt.Errorf("get public keys of %s: %w", email, err)
}
for _, k := range out.Address.Keys {
key, err := crypto.NewKeyFromArmored(k.PublicKey)
if err == nil {
err = kr.AddKey(key)
}
if err != nil {
return nil, fmt.Errorf("public key of %s: %w", email, err)
}
}
return kr, nil
}
// close clears the private key material of the account.
func (a *account) close() {
if a.userKR != nil {
a.userKR.ClearPrivateParams()
}
for _, addr := range a.addresses {
addr.keyRing.ClearPrivateParams()
}
}
@@ -0,0 +1,330 @@
package protondrive
import (
"context"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/ProtonMail/gopenpgp/v3/crypto"
"github.com/rclone/rclone/backend/protondrive/protontest"
"github.com/rclone/rclone/lib/rest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// newAccountSession returns a session for a new session of u.
func newAccountSession(srv *protontest.Server, u *protontest.User) *session {
tokens := srv.NewSession(u)
store := &memoryStore{creds: credentials{
UID: tokens.UID,
AccessToken: tokens.AccessToken,
RefreshToken: tokens.RefreshToken,
KeyPassword: u.KeyPassword(),
}}
return newSession(srv.Client(), srv.URL(), testAppVersion, store)
}
// sessionCaller returns an apiCaller making unpaced calls with s.
func sessionCaller(s *session) apiCaller {
return func(ctx context.Context, opts *rest.Opts, request, response any) error {
_, err := s.call(ctx, opts, request, response)
return err
}
}
// decryptWith decrypts the armored message with kr.
func decryptWith(t *testing.T, kr *crypto.KeyRing, armored string) string {
t.Helper()
dh, err := crypto.PGP().Decryption().DecryptionKeys(kr).New()
require.NoError(t, err)
res, err := dh.Decrypt([]byte(armored), crypto.Armor)
require.NoError(t, err)
return string(res.Bytes())
}
func TestLoadAccount(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
u.AddAddress("alias@proton.test")
s := newAccountSession(srv, u)
a, err := loadAccount(context.Background(), s, []byte(u.KeyPassword()))
require.NoError(t, err)
defer a.close()
require.NotNil(t, a.userKR)
require.Len(t, a.addresses, 2)
primary := a.primary()
require.NotNil(t, primary)
assert.Equal(t, u.Email(), primary.Email)
assert.NotEmpty(t, primary.ID)
assert.Same(t, primary, a.byID(primary.ID))
assert.Same(t, primary, a.byEmail(u.Email()))
assert.Nil(t, a.byID("unknown"))
assert.Nil(t, a.byEmail("unknown@proton.test"))
for _, email := range []string{u.Email(), "alias@proton.test"} {
addr := a.byEmail(email)
require.NotNil(t, addr, email)
assert.Equal(t, "secret for "+email, decryptWith(t, addr.keyRing, u.EncryptToAddress(email, []byte("secret for "+email))))
}
// The address keys are distinct.
dh, err := crypto.PGP().Decryption().DecryptionKeys(primary.keyRing).New()
require.NoError(t, err)
_, err = dh.Decrypt([]byte(u.EncryptToAddress("alias@proton.test", []byte("x"))), crypto.Armor)
assert.Error(t, err, "primary address key decrypted a message for the alias")
}
func TestLoadAccountTwoPassword(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddTwoPasswordUser("user", "login", "mailbox")
want, err := computeKeyPassword([]byte("mailbox"), u.KeySalt)
require.NoError(t, err)
assert.Equal(t, string(want), u.KeyPassword())
a, err := loadAccount(context.Background(), newAccountSession(srv, u), want)
require.NoError(t, err)
a.close()
}
func TestLoadAccountWrongKeyPassword(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
s := newAccountSession(srv, u)
for _, keyPassword := range [][]byte{[]byte("wrong"), nil} {
a, err := loadAccount(context.Background(), s, keyPassword)
require.Error(t, err)
assert.Nil(t, a)
assert.Contains(t, err.Error(), "unlock user keys")
}
}
func TestLoadAccountRotatedUserKey(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
u.AddUserKey()
// The old user key no longer unlocks with the key password, which
// must not stop the account loading.
a, err := loadAccount(context.Background(), newAccountSession(srv, u), []byte(u.KeyPassword()))
require.NoError(t, err)
defer a.close()
assert.Equal(t, "msg", decryptWith(t, a.primary().keyRing, u.EncryptToAddress(u.Email(), []byte("msg"))))
}
func TestLoadAccountBadAddressKeySignature(t *testing.T) {
t.Parallel()
for _, test := range []struct {
name string
empty bool
}{
{"impostor", false},
{"empty", true},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
// On the primary address the account doesn't load.
u := srv.AddUser("user", "pass")
srv.CorruptAddressKeySignature(u, u.Email(), test.empty)
a, err := loadAccount(context.Background(), newAccountSession(srv, u), []byte(u.KeyPassword()))
require.Error(t, err)
assert.Nil(t, a)
assert.Contains(t, err.Error(), "verify token")
// A secondary address is left out.
u = srv.AddUser("user2", "pass")
u.AddAddress("alias@proton.test")
srv.CorruptAddressKeySignature(u, "alias@proton.test", test.empty)
a, err = loadAccount(context.Background(), newAccountSession(srv, u), []byte(u.KeyPassword()))
require.NoError(t, err)
defer a.close()
assert.Nil(t, a.byEmail("alias@proton.test"))
assert.Len(t, a.addresses, 1)
assert.Equal(t, u.Email(), a.primary().Email)
})
}
}
func TestLoadAccountSerialised(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
s := newAccountSession(srv, u)
var active, peak, calls atomic.Int32
s.unlockHook = func() {
n := active.Add(1)
for {
p := peak.Load()
if n <= p || peak.CompareAndSwap(p, n) {
break
}
}
calls.Add(1)
time.Sleep(5 * time.Millisecond)
active.Add(-1)
}
var wg sync.WaitGroup
for range 16 {
wg.Add(1)
go func() {
defer wg.Done()
a, err := loadAccount(context.Background(), s, []byte(u.KeyPassword()))
if assert.NoError(t, err) {
a.close()
}
}()
}
wg.Wait()
assert.Equal(t, int32(16), calls.Load())
assert.Equal(t, int32(1), peak.Load(), "unlocks overlapped")
}
func TestAccountClose(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
a, err := loadAccount(context.Background(), newAccountSession(srv, u), []byte(u.KeyPassword()))
require.NoError(t, err)
a.close()
for _, kr := range []*crypto.KeyRing{a.userKR, a.primary().keyRing} {
for _, key := range kr.GetKeys() {
assert.False(t, key.IsPrivate(), "private key material left after close")
}
}
}
func TestPublicKeysCached(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
other := srv.AddUser("other", "pass")
s := newAccountSession(srv, u)
a, err := loadAccount(context.Background(), s, []byte(u.KeyPassword()))
require.NoError(t, err)
defer a.close()
otherAccount, err := loadAccount(context.Background(), newAccountSession(srv, other), []byte(other.KeyPassword()))
require.NoError(t, err)
defer otherAccount.close()
const route = "GET /core/v4/keys/all"
srv.ResetRequests()
for range 5 {
kr, err := a.publicKeys(context.Background(), sessionCaller(s), other.Email())
require.NoError(t, err)
require.Equal(t, 1, kr.CountEntities())
assert.False(t, kr.GetKeys()[0].IsPrivate())
// A message encrypted to the key decrypts with other's address key.
eh, err := crypto.PGP().Encryption().Recipients(kr).New()
require.NoError(t, err)
msg, err := eh.Encrypt([]byte("hello"))
require.NoError(t, err)
armored, err := msg.ArmorBytes()
require.NoError(t, err)
assert.Equal(t, "hello", decryptWith(t, otherAccount.primary().keyRing, string(armored)))
}
assert.Equal(t, 1, srv.Requests()[route])
srv.ResetRequests()
var first *crypto.KeyRing
for range 5 {
kr, err := a.publicKeys(context.Background(), sessionCaller(s), u.Email())
require.NoError(t, err)
require.Equal(t, 1, kr.CountEntities())
assert.False(t, kr.GetKeys()[0].IsPrivate())
if first == nil {
first = kr
}
assert.Same(t, first, kr, "the own address's public keys are built again")
}
assert.Zero(t, srv.Requests()[route])
// An email without a Proton account has no keys, and that is cached too.
for range 3 {
kr, err := a.publicKeys(context.Background(), sessionCaller(s), "nobody@example.com")
require.NoError(t, err)
assert.Zero(t, kr.CountEntities())
}
assert.Equal(t, 1, srv.Requests()[route])
// Case variants share a lookup, and an own address in any case
// never reaches the server.
srv.ResetRequests()
for _, email := range []string{"OTHER@proton.test", "Other@Proton.Test", strings.ToUpper(u.Email())} {
_, err := a.publicKeys(context.Background(), sessionCaller(s), email)
require.NoError(t, err)
}
assert.Zero(t, srv.Requests()[route])
}
func TestPublicKeysErrorNotCached(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
other := srv.AddUser("other", "pass")
s := newAccountSession(srv, u)
a, err := loadAccount(context.Background(), s, []byte(u.KeyPassword()))
require.NoError(t, err)
defer a.close()
srv.Inject(protontest.Fault{Pattern: "/core/v4/keys/all", Times: 1, Status: 500})
_, err = a.publicKeys(context.Background(), sessionCaller(s), other.Email())
require.Error(t, err)
kr, err := a.publicKeys(context.Background(), sessionCaller(s), other.Email())
require.NoError(t, err)
assert.Equal(t, 1, kr.CountEntities())
}
func TestSameEmail(t *testing.T) {
for _, tc := range []struct {
a, b string
same bool
}{
{"user@proton.me", "user@proton.me", true},
{"User@Proton.Me", "uSER@pROTON.mE", true},
{"user@proton.me", "user@proton.mee", false},
// Unicode case folding would match these to ASCII letters.
{"kim@proton.me", "\u212aim@proton.me", false},
{"sam@proton.me", "\u017fam@proton.me", false},
{"élan@proton.me", "Élan@proton.me", false},
} {
assert.Equal(t, tc.same, emailKey(tc.a) == emailKey(tc.b), "%q %q", tc.a, tc.b)
}
}
// TestPublicKeysWaiterRetries checks a caller waiting for another's
// fetch of the same keys fetches them itself if the other gave up,
// rather than failing with the other's context error.
func TestPublicKeysWaiterRetries(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
other := srv.AddUser("other", "pass")
s := newAccountSession(srv, u)
a, err := loadAccount(context.Background(), s, []byte(u.KeyPassword()))
require.NoError(t, err)
defer a.close()
// The lookup as a waiter sees it once the fetching caller's
// context ended.
l := &publicKeyLookup{done: make(chan struct{}), err: context.Canceled}
close(l.done)
a.mu.Lock()
a.foreign[emailKey(other.Email())] = l
a.mu.Unlock()
kr, err := a.publicKeys(context.Background(), sessionCaller(s), other.Email())
require.NoError(t, err)
assert.Equal(t, 1, kr.CountEntities())
}
+705
View File
@@ -0,0 +1,705 @@
// Package api has the type definitions for the Proton Drive API.
package api
import (
"encoding/json"
"fmt"
)
// Response codes returned in the Code field of every API response.
//
// The names follow ErrorCode in the Proton Drive SDK
// (client/js/src/internal/apiService/errorCodes.ts) unless noted.
const (
CodeOK = 1000
CodeOKMany = 1001
CodeOKAsync = 1002
CodeInvalidRequirements = 2000
CodeInvalidValue = 2001
CodeNotEnoughPermissions = 2011
CodeNotEnoughPermissionsToGrantPermissions = 2026
CodeAlreadyExists = 2500
CodeNotExists = 2501
CodeInsufficientQuota = 200001
CodeInsufficientSpace = 200002
CodeMaxFileSizeForFreeUser = 200003
CodeMaxPublicEditModeForFreeUser = 200004
CodeInsufficientVolumeQuota = 200100
CodeInsufficientDeviceQuota = 200101
CodeAlreadyMemberOfShareInVolumeWithAnotherAddress = 200201
CodeTooManyChildren = 200300
CodeNestingTooDeep = 200301
CodeInsufficientInvitationQuota = 200600
CodeInsufficientShareQuota = 200601
CodeInsufficientShareJoinedQuota = 200602
CodeInsufficientBookmarksQuota = 200800
// CodeFeatureBlocked is returned when a feature is disabled,
// rate-limited or blocked because of abuse (driveTypes.ts).
CodeFeatureBlocked = 2028
// CodeAppVersionMissing is returned when the request has no app
// version (go-proton-api AppVersionMissingCode).
CodeAppVersionMissing = 5001
// CodeAppVersionBad is returned when the app version is no longer
// accepted: "You need to update this app" (coreTypes.ts,
// go-proton-api AppVersionBadCode).
CodeAppVersionBad = 5003
// CodeInvalidSection is returned when a request header such as
// x-pm-appversion is malformed.
CodeInvalidSection = 2064
// CodePasswordWrong is returned when the login credentials are
// incorrect (go-proton-api PasswordWrong).
CodePasswordWrong = 8002
// CodeHumanVerification is returned when a CAPTCHA must be solved.
CodeHumanVerification = 9001
// CodeRefreshTokenInvalid is returned when the refresh token is
// rejected and the session must be established again.
CodeRefreshTokenInvalid = 10013
// CodeAddressMissing is returned by GET /core/v4/keys/all when no
// Proton address has the email (account accountApi.ts).
CodeAddressMissing = 33102
// CodeDomainExternal is returned by GET /core/v4/keys/all when the
// email's domain is not hosted by Proton (account accountApi.ts).
CodeDomainExternal = 33103
)
// Error is the body of an unsuccessful API response.
type Error struct {
Status int `json:"-"` // HTTP status of the response
Code int `json:"Code"` // Proton response code
Message string `json:"Error"` // human readable message
Details json.RawMessage `json:"Details,omitempty"` // request specific details
}
// Error returns the message with its code and HTTP status.
func (e *Error) Error() string {
return fmt.Sprintf("%s (Code=%d, Status=%d)", e.Message, e.Code, e.Status)
}
// AuthRefreshRequest is the body of POST /auth/v4/refresh.
type AuthRefreshRequest struct {
ResponseType string `json:"ResponseType"` // always "token"
GrantType string `json:"GrantType"` // always "refresh_token"
RefreshToken string `json:"RefreshToken"`
}
// AuthRefreshResponse is the response to POST /auth/v4/refresh.
type AuthRefreshResponse struct {
Code int `json:"Code"`
UID string `json:"UID"`
AccessToken string `json:"AccessToken"`
RefreshToken string `json:"RefreshToken"`
}
// AuthInfoRequest is the body of POST /core/v4/auth/info.
type AuthInfoRequest struct {
Intent string `json:"Intent"` // always "Proton"
Username string `json:"Username"`
}
// AuthInfoResponse is the response to POST /core/v4/auth/info: the
// server's SRP challenge.
type AuthInfoResponse struct {
Code int `json:"Code"`
Version int `json:"Version"` // SRP password hashing version
Modulus string `json:"Modulus"` // signed base64 SRP modulus
ServerEphemeral string `json:"ServerEphemeral"` // base64
Salt string `json:"Salt"` // base64 SRP salt
SRPSession string `json:"SRPSession"`
Username string `json:"Username"`
}
// AuthRequest is the body of POST /core/v4/auth.
type AuthRequest struct {
Username string `json:"Username"`
ClientEphemeral string `json:"ClientEphemeral"` // base64
ClientProof string `json:"ClientProof"` // base64
SRPSession string `json:"SRPSession"`
PersistentCookies int `json:"PersistentCookies"`
Payload map[string]string `json:"Payload"`
}
// Values of TwoFAInfo.Enabled, which may be combined.
const (
TwoFATOTP = 1
TwoFAFIDO2 = 2
)
// Values of AuthResponse.PasswordMode.
const (
PasswordModeOne = 1 // the login password also unlocks the keys
PasswordModeTwo = 2 // a separate mailbox password unlocks the keys
)
// TwoFAInfo describes the second factors an account requires.
type TwoFAInfo struct {
Enabled int `json:"Enabled"` // bitmask of TwoFATOTP and TwoFAFIDO2
}
// AuthResponse is the response to POST /core/v4/auth.
type AuthResponse struct {
Code int `json:"Code"`
UID string `json:"UID"`
UserID string `json:"UserID"`
AccessToken string `json:"AccessToken"`
RefreshToken string `json:"RefreshToken"`
TokenType string `json:"TokenType"`
ServerProof string `json:"ServerProof"` // base64
Scopes []string `json:"Scopes"`
PasswordMode int `json:"PasswordMode"`
TwoFA TwoFAInfo `json:"2FA"`
}
// Auth2FARequest is the body of POST /core/v4/auth/2fa.
type Auth2FARequest struct {
TwoFactorCode string `json:"TwoFactorCode"`
}
// KeySalt is the salt of one user key's password.
type KeySalt struct {
ID string `json:"ID"` // user key ID
KeySalt string `json:"KeySalt"` // base64
}
// KeySaltsResponse is the response to GET /core/v4/keys/salts.
type KeySaltsResponse struct {
Code int `json:"Code"`
KeySalts []KeySalt `json:"KeySalts"`
}
// ForkInitResponse is the response to GET /auth/v4/sessions/forks.
type ForkInitResponse struct {
Code int `json:"Code"`
Selector string `json:"Selector"`
UserCode string `json:"UserCode"`
}
// ForkStatusResponse is the response to GET
// /auth/v4/sessions/forks/{selector} once the user has signed in.
type ForkStatusResponse struct {
Code int `json:"Code"`
Payload string `json:"Payload"` // base64 AES-256-GCM encrypted key password
UID string `json:"UID"`
AccessToken string `json:"AccessToken"`
RefreshToken string `json:"RefreshToken"`
}
// UserKey is one of the user's keys.
type UserKey struct {
ID string `json:"ID"`
Version int `json:"Version"`
PrivateKey string `json:"PrivateKey"` // armored, locked with the key password
Primary int `json:"Primary"` // 1 for the primary key
Active int `json:"Active"` // 0 for a key which must not be unlocked
}
// User is the account of the signed in user.
type User struct {
ID string `json:"ID"`
Name string `json:"Name"`
Email string `json:"Email"`
Keys []UserKey `json:"Keys"`
UsedSpace int64 `json:"UsedSpace"` // bytes used by all products
MaxSpace int64 `json:"MaxSpace"` // bytes of storage all products share
}
// UserResponse is the response to GET /core/v4/users.
type UserResponse struct {
Code int `json:"Code"`
User User `json:"User"`
}
// AddressKey is one of an address's keys.
type AddressKey struct {
ID string `json:"ID"`
Version int `json:"Version"`
PrivateKey string `json:"PrivateKey"` // armored, locked with the passphrase in Token
// Token is the armored key passphrase encrypted to the user key,
// or empty for a legacy key locked with the key password.
Token string `json:"Token"`
Signature string `json:"Signature"` // armored detached signature of the passphrase by the user key
Primary int `json:"Primary"` // 1 for the primary key
Active int `json:"Active"` // 0 for a key which must not be unlocked
Flags int `json:"Flags"` // bitmap of key usages
}
// Address is one of the user's email addresses.
type Address struct {
ID string `json:"ID"`
Email string `json:"Email"`
Status int `json:"Status"` // 0 disabled, 1 enabled, 2 deleting
Type int `json:"Type"` // 1 original, 2 alias, 3 custom, 4 premium, 5 external
Priority int `json:"Priority"` // order of the addresses, lowest first
Keys []AddressKey `json:"Keys"`
}
// AddressesResponse is the response to GET /core/v4/addresses.
type AddressesResponse struct {
Code int `json:"Code"`
Addresses []Address `json:"Addresses"`
}
// PublicKey is a public key of an address.
type PublicKey struct {
PublicKey string `json:"PublicKey"` // armored
Primary int `json:"Primary"` // 1 for the primary key
Flags int `json:"Flags"` // bitmap of key usages
Source int `json:"Source"` // 0 for internal keys
}
// PublicKeysResponse is the response to GET /core/v4/keys/all.
type PublicKeysResponse struct {
Code int `json:"Code"`
Address struct {
Keys []PublicKey `json:"Keys"`
} `json:"Address"`
}
// Link types, as Link.Type encodes them.
const (
LinkTypeFolder = 1
LinkTypeFile = 2
LinkTypeAlbum = 3
)
// Link states, as Link.State encodes them.
const (
LinkStateDraft = 0
LinkStateActive = 1
LinkStateTrashed = 2
)
// Volume is the summary of a volume in PrimaryRootShareResponseDto.
type Volume struct {
VolumeID string `json:"VolumeID"`
UsedSpace int64 `json:"UsedSpace"`
}
// Share is a share with its key, as ShareDto.
type Share struct {
ShareID string `json:"ShareID"`
CreatorEmail string `json:"CreatorEmail"`
Key string `json:"Key"` // armored share key, locked with the passphrase
Passphrase string `json:"Passphrase"` // armored, encrypted to the address key
PassphraseSignature string `json:"PassphraseSignature"` // armored detached signature by the creator
AddressID string `json:"AddressID"` // address whose key decrypts the passphrase
}
// Link is the metadata every node has, as LinkDto. Strings the API
// sends as null are empty.
type Link struct {
LinkID string `json:"LinkID"`
Type int `json:"Type"` // LinkTypeFolder, LinkTypeFile or LinkTypeAlbum
ParentLinkID string `json:"ParentLinkID"` // empty for a root
State int `json:"State"` // LinkStateDraft, LinkStateActive or LinkStateTrashed
CreateTime int64 `json:"CreateTime"` // Unix seconds
ModifyTime int64 `json:"ModifyTime"` // Unix seconds
TrashTime int64 `json:"TrashTime"` // Unix seconds, 0 unless trashed
Name string `json:"Name"` // armored, encrypted to the parent key
NameHash string `json:"NameHash"` // lower-case hex lookup hash; empty for a root
NodeKey string `json:"NodeKey"` // armored, locked with the node passphrase
NodePassphrase string `json:"NodePassphrase"`
NodePassphraseSignature string `json:"NodePassphraseSignature"`
SignatureEmail string `json:"SignatureEmail"` // signer of the key and attributes; empty if anonymous
NameSignatureEmail string `json:"NameSignatureEmail"` // signer of the name; empty if anonymous
ThirdParty bool `json:"ThirdParty"`
Sdk bool `json:"Sdk"`
}
// Folder is the folder part of a node's metadata, as FolderDto.
type Folder struct {
NodeHashKey string `json:"NodeHashKey"` // armored, encrypted to the node key
XAttr string `json:"XAttr"` // armored extended attributes; may be empty
IsImported bool `json:"IsImported"`
}
// ActiveRevision is the summary of a file's active revision, as
// ActiveRevisionDto.
type ActiveRevision struct {
RevisionID string `json:"RevisionID"`
CreateTime int64 `json:"CreateTime"` // Unix seconds
EncryptedSize int64 `json:"EncryptedSize"`
ChecksumVerified bool `json:"ChecksumVerified"`
ManifestSignature string `json:"ManifestSignature"` // armored
XAttr string `json:"XAttr"` // armored extended attributes; may be empty
SignatureEmail string `json:"SignatureEmail"` // signer of the revision; empty if anonymous
IsImported bool `json:"IsImported"`
ThirdParty bool `json:"ThirdParty"`
Sdk bool `json:"Sdk"`
}
// File is the file part of a node's metadata, as FileDto.
type File struct {
ActiveRevision *ActiveRevision `json:"ActiveRevision"` // nil for a draft
TotalEncryptedSize int64 `json:"TotalEncryptedSize"`
ContentKeyPacket string `json:"ContentKeyPacket"` // base64
MediaType string `json:"MediaType"`
ContentKeyPacketSignature string `json:"ContentKeyPacketSignature"` // armored; may be empty
}
// LinkDetails is a node's metadata, as FileDetailsDto and
// FolderDetailsDto.
type LinkDetails struct {
Link Link `json:"Link"`
Folder *Folder `json:"Folder"` // set for a folder
File *File `json:"File"` // set for a file
}
// MyFilesResponse is the response to GET /drive/v2/shares/my-files, as
// PrimaryRootShareResponseDto.
type MyFilesResponse struct {
Code int `json:"Code"`
Volume Volume `json:"Volume"`
Share Share `json:"Share"`
Link LinkDetails `json:"Link"` // the root folder
}
// CreateVolumeRequest is the body of POST /drive/volumes, as
// CreateVolumeRequestDto without the deprecated names.
type CreateVolumeRequest struct {
AddressID string `json:"AddressID"`
AddressKeyID string `json:"AddressKeyID"` // the primary key of AddressID
ShareKey string `json:"ShareKey"`
SharePassphrase string `json:"SharePassphrase"`
SharePassphraseSignature string `json:"SharePassphraseSignature"`
FolderName string `json:"FolderName"`
FolderKey string `json:"FolderKey"`
FolderPassphrase string `json:"FolderPassphrase"`
FolderPassphraseSignature string `json:"FolderPassphraseSignature"`
FolderHashKey string `json:"FolderHashKey"`
}
// ShareReference identifies the main share of a volume and its root,
// as ShareReferenceResponseDto.
type ShareReference struct {
ShareID string `json:"ShareID"`
LinkID string `json:"LinkID"`
}
// VolumeDetails is a volume, as VolumeResponseDto.
type VolumeDetails struct {
ID string `json:"ID"`
VolumeID string `json:"VolumeID"`
UsedSpace int64 `json:"UsedSpace"`
State int `json:"State"`
Share ShareReference `json:"Share"`
}
// VolumeResponse is the response to POST /drive/volumes and GET
// /drive/volumes/{volumeID}, as GetVolumeResponseDto.
type VolumeResponse struct {
Code int `json:"Code"`
Volume VolumeDetails `json:"Volume"`
}
// LinksRequest is the body of POST /drive/v2/volumes/{volumeID}/links
// and /drive/v2/volumes/{volumeID}/trash_multiple, as LinkIDsRequestDto.
type LinksRequest struct {
LinkIDs []string `json:"LinkIDs"`
}
// LinksResponse is the response to POST
// /drive/v2/volumes/{volumeID}/links, as LoadLinkDetailsResponseDto.
// Unknown IDs are left out.
type LinksResponse struct {
Code int `json:"Code"`
Links []LinkDetails `json:"Links"`
}
// ChildrenResponse is the response to GET
// /drive/v2/volumes/{volumeID}/folders/{linkID}/children, as
// ListChildrenResponseDto.
type ChildrenResponse struct {
Code int `json:"Code"`
LinkIDs []string `json:"LinkIDs"`
AnchorID string `json:"AnchorID"` // pass as AnchorID for the next page
More bool `json:"More"`
}
// CreateFolderRequest is the body of POST
// /drive/v2/volumes/{volumeID}/folders, as CreateFolderDto with the
// SignatureEmail the SDK sends in place of SignatureAddress.
type CreateFolderRequest struct {
ParentLinkID string `json:"ParentLinkID"`
NodeKey string `json:"NodeKey"`
NodeHashKey string `json:"NodeHashKey"`
NodePassphrase string `json:"NodePassphrase"`
NodePassphraseSignature string `json:"NodePassphraseSignature"`
SignatureEmail string `json:"SignatureEmail"`
Name string `json:"Name"`
Hash string `json:"Hash"`
XAttr string `json:"XAttr,omitempty"`
}
// CreateFolderResponse is the response to POST
// /drive/v2/volumes/{volumeID}/folders, as CreateFolderResponseDto.
type CreateFolderResponse struct {
Code int `json:"Code"`
Folder struct {
ID string `json:"ID"`
} `json:"Folder"`
}
// RenameRequest is the body of PUT
// /drive/v2/volumes/{volumeID}/links/{linkID}/rename, as
// RenameLinkRequestDto.
type RenameRequest struct {
Name string `json:"Name"` // reusing the session key of the current name
Hash string `json:"Hash"`
NameSignatureEmail string `json:"NameSignatureEmail"`
OriginalHash string `json:"OriginalHash"` // current name hash, which must match
}
// MoveRequest is the body of PUT
// /drive/v2/volumes/{volumeID}/links/{linkID}/move, as
// MoveLinkV2RequestDto.
//
// NodePassphraseSignature and SignatureEmail are given if and only if
// the node's key is anonymous (has no SignatureEmail).
type MoveRequest struct {
ParentLinkID string `json:"ParentLinkID"`
NodePassphrase string `json:"NodePassphrase"` // reusing the session key of the current passphrase
NodePassphraseSignature string `json:"NodePassphraseSignature,omitempty"`
SignatureEmail string `json:"SignatureEmail,omitempty"`
Name string `json:"Name"` // reusing the session key of the current name
NameSignatureEmail string `json:"NameSignatureEmail"`
Hash string `json:"Hash"`
OriginalHash string `json:"OriginalHash"` // current name hash, which must match
}
// CopyRequest is the body of POST
// /drive/volumes/{volumeID}/links/{linkID}/copy, as CopyLinkRequestDto.
// The server copies everything else of the node.
//
// NodePassphraseSignature and SignatureEmail are given if and only if
// the node's key is anonymous (has no SignatureEmail).
type CopyRequest struct {
Name string `json:"Name"` // reusing the session key of the current name
NodePassphrase string `json:"NodePassphrase"` // reusing the session key of the current passphrase
Hash string `json:"Hash"`
TargetVolumeID string `json:"TargetVolumeID"`
TargetParentLinkID string `json:"TargetParentLinkID"`
NameSignatureEmail string `json:"NameSignatureEmail"`
NodePassphraseSignature string `json:"NodePassphraseSignature,omitempty"`
SignatureEmail string `json:"SignatureEmail,omitempty"`
}
// CopyResponse is the response to POST
// /drive/volumes/{volumeID}/links/{linkID}/copy, as CopyLinkResponseDto.
type CopyResponse struct {
Code int `json:"Code"`
LinkID string `json:"LinkID"` // the new node
}
// LinkResult is the outcome for one link of a request on many.
type LinkResult struct {
LinkID string `json:"LinkID"`
Response struct {
Code int `json:"Code"`
Error string `json:"Error"`
} `json:"Response"`
}
// MultiLinkResponse is the response to a request on many links, such
// as POST /drive/v2/volumes/{volumeID}/trash_multiple, as
// MultiResponsesPerLinkFactory.
type MultiLinkResponse struct {
Code int `json:"Code"`
Responses []LinkResult `json:"Responses"`
}
// RevisionResponse is the response to GET
// /drive/v2/volumes/{volumeID}/files/{linkID}/revisions/{revisionID},
// as GetRevisionResponseDto.
type RevisionResponse struct {
Code int `json:"Code"`
Revision Revision `json:"Revision"`
}
// Revision is a revision of a file with a page of its blocks, as
// DetailedRevisionResponseDto.
type Revision struct {
ID string `json:"ID"`
ManifestSignature string `json:"ManifestSignature"` // armored
Size int64 `json:"Size"` // encrypted size
State int `json:"State"`
XAttr string `json:"XAttr"` // armored extended attributes; may be empty
Blocks []Block `json:"Blocks"`
Thumbnails []Thumbnail `json:"Thumbnails"`
SignatureEmail string `json:"SignatureEmail"` // signer of the manifest; empty if anonymous
CreateTime int64 `json:"CreateTime"` // Unix seconds
}
// Block is a block of a revision with the URL to download it, as
// BlockResponseDto.
type Block struct {
Index int `json:"Index"` // from 1
Hash string `json:"Hash"` // base64 SHA-256 of the encrypted block
Token string `json:"Token"` // send as pm-storage-token
BareURL string `json:"BareURL"` // storage URL of the block
}
// Thumbnail is a thumbnail of a revision, as ThumbnailResponseDto.
type Thumbnail struct {
ThumbnailID string `json:"ThumbnailID"`
Type int `json:"Type"`
Hash string `json:"Hash"` // base64 SHA-256 of the encrypted thumbnail
Size int64 `json:"Size"`
}
// ConflictDetails are the Details of a CodeAlreadyExists error from
// creating a node, naming the node which has the name.
type ConflictDetails struct {
ConflictLinkID string `json:"ConflictLinkID"`
ConflictRevisionID string `json:"ConflictRevisionID"` // active revision; empty for a draft or folder
ConflictDraftRevisionID string `json:"ConflictDraftRevisionID"` // draft revision; empty if none
ConflictDraftClientUID string `json:"ConflictDraftClientUID"` // ClientUID of the draft revision's creator
}
// CreateFileRequest is the body of POST
// /drive/v2/volumes/{volumeID}/files, as CreateFileDto: a draft file.
type CreateFileRequest struct {
ParentLinkID string `json:"ParentLinkID"`
Name string `json:"Name"`
Hash string `json:"Hash"`
MIMEType string `json:"MIMEType"`
ClientUID *string `json:"ClientUID"` // identifies the creator's drafts; null if none
IntendedUploadSize *int64 `json:"IntendedUploadSize"` // rounded size for an early quota check; null if unknown
NodeKey string `json:"NodeKey"`
NodePassphrase string `json:"NodePassphrase"`
NodePassphraseSignature string `json:"NodePassphraseSignature"`
ContentKeyPacket string `json:"ContentKeyPacket"` // base64
ContentKeyPacketSignature string `json:"ContentKeyPacketSignature"` // armored, by the node key
SignatureAddress string `json:"SignatureAddress"` // signer of the name and passphrase
}
// CreateFileResponse is the response to POST
// /drive/v2/volumes/{volumeID}/files, as CreateDraftFileResponseDto.
type CreateFileResponse struct {
Code int `json:"Code"`
File struct {
ID string `json:"ID"`
RevisionID string `json:"RevisionID"` // the draft revision
} `json:"File"`
}
// CreateRevisionRequest is the body of POST
// /drive/v2/volumes/{volumeID}/files/{linkID}/revisions, as
// CreateRevisionRequestDto: a draft revision of an active file.
type CreateRevisionRequest struct {
CurrentRevisionID string `json:"CurrentRevisionID"` // the active revision, which must match
ClientUID *string `json:"ClientUID"`
IntendedUploadSize *int64 `json:"IntendedUploadSize"`
}
// CreateRevisionResponse is the response to POST
// /drive/v2/volumes/{volumeID}/files/{linkID}/revisions.
type CreateRevisionResponse struct {
Code int `json:"Code"`
Revision struct {
ID string `json:"ID"`
} `json:"Revision"`
}
// VerificationResponse is the response to GET
// /drive/v2/volumes/{volumeID}/links/{linkID}/revisions/{revisionID}/verification,
// as VerificationData.
type VerificationResponse struct {
Code int `json:"Code"`
VerificationCode string `json:"VerificationCode"` // base64
ContentKeyPacket string `json:"ContentKeyPacket"` // base64
}
// BlockUploadRequest is the body of POST /drive/blocks, as
// RequestUploadInput without its deprecated fields.
type BlockUploadRequest struct {
AddressID string `json:"AddressID"` // address signing the blocks
VolumeID string `json:"VolumeID"`
LinkID string `json:"LinkID"`
RevisionID string `json:"RevisionID"`
BlockList []BlockUploadInfo `json:"BlockList"`
ThumbnailList []struct{} `json:"ThumbnailList"`
}
// BlockUploadInfo is a block to upload, as RequestUploadBlockInput
// without its deprecated Size and Hash, which the SDK doesn't send.
type BlockUploadInfo struct {
Index int `json:"Index"` // from 1, consecutive
EncSignature string `json:"EncSignature"` // armored signature of the plaintext, encrypted
Verifier BlockVerifier `json:"Verifier"`
}
// BlockVerifier proves a block was encrypted with the right key.
type BlockVerifier struct {
Token string `json:"Token"` // base64 verification code XOR the start of the encrypted block
}
// BlockUploadResponse is the response to POST /drive/blocks, as
// RequestUploadResponse.
type BlockUploadResponse struct {
Code int `json:"Code"`
UploadLinks []UploadLink `json:"UploadLinks"`
}
// UploadLink is the storage URL to upload a block to, as BlockURL.
type UploadLink struct {
Index int `json:"Index"`
BareURL string `json:"BareURL"`
Token string `json:"Token"` // send as pm-storage-token
}
// CommitRevisionRequest is the body of PUT
// /drive/v2/volumes/{volumeID}/files/{linkID}/revisions/{revisionID},
// as CommitRevisionDto without its deprecated fields.
type CommitRevisionRequest struct {
ManifestSignature string `json:"ManifestSignature"` // armored, of the block hashes in order
SignatureAddress string `json:"SignatureAddress"` // signer of the manifest, blocks and attributes
XAttr string `json:"XAttr"` // armored extended attributes
ChecksumVerified bool `json:"ChecksumVerified"` // whether the SHA1 matched the source's
Photo *struct{} `json:"Photo"` // always null outside the photo volume
}
// Volume event types, as Event.EventType encodes them.
const (
EventDelete = 0 // deleted for good, or no longer accessible
EventCreate = 1
EventUpdate = 2 // new revision committed
EventUpdateMetadata = 3 // renamed, moved, trashed or restored
)
// LatestEventResponse is the response to GET
// /drive/volumes/{volumeID}/events/latest, as LatestEventIDResponseDto.
type LatestEventResponse struct {
Code int `json:"Code"`
EventID string `json:"EventID"`
}
// EventLink is the link an event is about, as EventLinkDataDto, in its
// state when the events are read rather than when the change was made.
type EventLink struct {
LinkID string `json:"LinkID"`
ParentLinkID string `json:"ParentLinkID"` // empty if null or missing
IsShared bool `json:"IsShared"`
IsTrashed bool `json:"IsTrashed"`
}
// Event is a change to a link of a volume, as EventV2ResponseDto. It
// names the link but not its name or type.
type Event struct {
EventID string `json:"EventID"`
EventType int `json:"EventType"` // EventDelete etc
Link EventLink `json:"Link"`
}
// EventsResponse is the response to GET
// /drive/v2/volumes/{volumeID}/events/{eventID}, as
// ListEventsV2ResponseDto without the sharing fields: the events after
// an event ID, oldest first.
type EventsResponse struct {
Code int `json:"Code"`
Events []Event `json:"Events"`
EventID string `json:"EventID"` // pass as the event ID of the next call
More bool `json:"More"` // whether more events follow EventID
Refresh bool `json:"Refresh"` // the event ID asked for is unknown or too old: EventID is the newest
}
+367
View File
@@ -0,0 +1,367 @@
package protondrive_test
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/rclone/rclone/backend/protondrive/protontest"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config"
"github.com/rclone/rclone/fs/object"
"github.com/rclone/rclone/fs/operations"
fssync "github.com/rclone/rclone/fs/sync"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Routes counted by the request budgets.
const (
linksRoute = "POST /drive/v2/volumes/{volumeID}/links"
revisionDraftRoute = "POST /drive/v2/volumes/{volumeID}/files/{linkID}/revisions"
verifierRoute = "GET /drive/v2/volumes/{volumeID}/links/{linkID}/revisions/{revisionID}/verification"
blockURLsRoute = "POST /drive/blocks"
blockPutRoute = "POST /storage/upload/{token}"
commitRoute = "PUT /drive/v2/volumes/{volumeID}/files/{linkID}/revisions/{revisionID}"
createFolderRoute = "POST /drive/v2/volumes/{volumeID}/folders"
latestEventRoute = "GET /drive/volumes/{volumeID}/events/latest"
eventsRoute = "GET /drive/v2/volumes/{volumeID}/events/{eventID}"
)
// TestMain runs the tests, then fails the run if any of them, through
// any fake server, requested a removed share-scoped route.
//
// The config file is RCLONE_CONFIG's, as on rclone's command line,
// whichever tests run: only fstest.Initialise would apply it, so the
// default config, which may hold a real Proton remote, would be read.
func TestMain(m *testing.M) {
if path, ok := os.LookupEnv("RCLONE_CONFIG"); ok {
if err := config.SetConfigPath(path); err != nil {
fmt.Fprintf(os.Stderr, "FAIL: RCLONE_CONFIG: %v\n", err)
os.Exit(1)
}
}
code := m.Run()
if legacy := protontest.LegacyRequests(); len(legacy) > 0 {
fmt.Fprintf(os.Stderr, "FAIL: TestBudgetNoLegacyRoutes: removed share-scoped routes were requested: %v\n", legacy)
code = 1
}
os.Exit(code)
}
// totalRequests returns the sum of the counts in requests.
func totalRequests(requests map[string]int) int {
n := 0
for _, count := range requests {
n += count
}
return n
}
// withoutEvents returns requests without the events routes, which
// the Fs requests on its own schedule; TestBudgetEvents checks them.
func withoutEvents(requests map[string]int) map[string]int {
delete(requests, latestEventRoute)
delete(requests, eventsRoute)
return requests
}
// ceilDiv returns n/d rounded up.
func ceilDiv(n, d int) int {
return (n + d - 1) / d
}
// childPages returns the number of child ID pages the fake returns for
// a folder of n children with page size pageSize: at least one.
func childPages(n, pageSize int) int {
return max(1, ceilDiv(n, pageSize))
}
// seedFiles seeds n small files into folder parentID.
func seedFiles(u *protontest.User, parentID string, n int) {
for i := range n {
u.SeedFile(parentID, fmt.Sprintf("file%03d.txt", i), []byte(fmt.Sprintf("file %d", i)), time.Now())
}
}
// budgetSizes are the folder sizes the listing budgets are checked at:
// around the metadata batch of 100 and the child page of 150.
var budgetSizes = []int{0, 1, 100, 101, 250}
// budgetPageSize is the child ID page size the listing budgets use.
const budgetPageSize = 150
// seedBudgetFolders seeds a folder "nN" of N files for each of sizes.
func seedBudgetFolders(srv *protontest.Server, u *protontest.User, sizes []int) {
srv.SetChildrenPageSize(budgetPageSize)
for _, n := range sizes {
seedFiles(u, u.SeedFolder(u.RootID(), fmt.Sprintf("n%d", n)), n)
}
}
// TestBudgetList checks listing a folder of N files costs one request
// per page of child IDs and one per batch of 100 nodes, within spec
// 9.4's P + ceil(N/100) + 1. The Fs is rooted at the folder, so finding
// it is part of NewFs.
//
// A second listing costs nothing with enable_caching at its default,
// and with it off costs the same as the first, no more. The latter is
// checked at the sizes which differ in kind - empty, one batch, several
// pages and batches - to save time.
//
// Measured, per route (children pages + links batches):
// N=0: 1+0, N=1: 1+1, N=100: 1+1, N=101: 1+2, N=250: 2+3 - exactly
// P + ceil(N/100), so the bound is that, without the spec's +1. Warm: 0
// at every N; with caching off the second listing repeats the first.
func TestBudgetList(t *testing.T) {
ctx := context.Background()
remote, srv, u := setupFakeRemote(t)
seedBudgetFolders(srv, u, budgetSizes)
list := func(t *testing.T, f fs.Fs, n int) map[string]int {
t.Helper()
srv.ResetRequests()
entries, err := f.List(ctx, "")
require.NoError(t, err)
require.Len(t, entries, n)
return withoutEvents(srv.Requests())
}
for _, n := range budgetSizes {
t.Run(fmt.Sprint(n), func(t *testing.T) {
root := fmt.Sprintf("n%d", n)
f := newFakeFs(t, remote, root)
cold := list(t, f, n)
pages, batches := childPages(n, budgetPageSize), ceilDiv(n, 100)
assert.LessOrEqual(t, cold[childrenRoute], pages, "%v", cold)
assert.LessOrEqual(t, cold[linksRoute], batches, "%v", cold)
assert.LessOrEqual(t, totalRequests(cold), pages+batches, "%v", cold)
assert.Empty(t, list(t, f, n), "warm listing")
if n == 100 || n == 101 {
return
}
t.Setenv(fakeEnvPrefix+"ENABLE_CACHING", "false")
uncached := newFakeFs(t, remote, root)
assert.Equal(t, cold, list(t, uncached, n), "first listing without caching")
assert.Equal(t, cold, list(t, uncached, n), "second listing without caching")
})
}
}
// TestBudgetNoopSync checks a cold sync of a tree already in sync, by
// size and modification time and by --checksum, lists each folder once
// and transfers nothing: SHA-1s come from the metadata.
//
// For D = 6 folders (the root and 5 below it) and N = 200 files (40 per
// folder), the plan's bound is 2*D + ceil(N/100) + 5 = 19. Measured, per
// route, either way: 6 children pages + 6 links batches = 12, one page
// and one batch per folder, as no folder has more than 100 children.
// The bound is that: 2*D.
func TestBudgetNoopSync(t *testing.T) {
const (
folders = 5
filesPerFolder = 40
budget = 2 * (folders + 1)
)
remote, srv, u := setupFakeRemote(t)
local := t.TempDir()
modTime := time.Now().Add(-time.Hour).Truncate(time.Second)
for d := range folders {
dir := fmt.Sprintf("dir%d", d)
id := u.SeedFolder(u.RootID(), dir)
require.NoError(t, os.Mkdir(filepath.Join(local, dir), 0o777))
for i := range filesPerFolder {
name := fmt.Sprintf("file%02d.txt", i)
content := []byte(fmt.Sprintf("content of %s/%s", dir, name))
u.SeedFile(id, name, content, modTime)
p := filepath.Join(local, dir, name)
require.NoError(t, os.WriteFile(p, content, 0o666))
require.NoError(t, os.Chtimes(p, modTime, modTime))
}
}
for _, checksum := range []bool{false, true} {
t.Run(fmt.Sprintf("checksum=%v", checksum), func(t *testing.T) {
ctx, ci := fs.AddConfig(context.Background())
ci.CheckSum = checksum
src, err := fs.NewFs(ctx, local)
require.NoError(t, err)
f := newFakeFs(t, remote, "")
srv.ResetRequests()
require.NoError(t, fssync.Sync(ctx, f, src, false))
got := withoutEvents(srv.Requests())
assert.LessOrEqual(t, totalRequests(got), budget, "%v", got)
assert.Zero(t, blockTransfers(got), "%v", got)
assert.Zero(t, got[draftRoute]+got[revisionDraftRoute], "drafts created: %v", got)
for route := range got {
assert.NotContains(t, route, "/revisions/", "file content read: %v", got)
}
})
}
}
// TestBudgetNewObject checks finding a file three folders deep, cold,
// costs one listing, a children page and a links batch, per folder on
// its path.
//
// The plan's bound is 2 per level + 1 = 7 for three levels. Measured
// for a/b/c/file.txt, per route: 4 children pages + 4 links batches =
// 8, as the root is listed too - it is a fourth folder on the path,
// and Proton has no route to look a child up by name. The bound is 2
// per folder listed: 2*4.
func TestBudgetNewObject(t *testing.T) {
ctx := context.Background()
remote, srv, u := setupFakeRemote(t)
parent := u.RootID()
for _, name := range []string{"a", "b", "c"} {
seedFiles(u, parent, 3)
parent = u.SeedFolder(parent, name)
}
u.SeedFile(parent, "file.txt", []byte("deep"), time.Now())
f := newFakeFs(t, remote, "")
srv.ResetRequests()
o, err := f.NewObject(ctx, "a/b/c/file.txt")
require.NoError(t, err)
assert.Equal(t, int64(4), o.Size())
got := withoutEvents(srv.Requests())
const listed = 4 // the root, a, b and c
assert.LessOrEqual(t, got[childrenRoute], listed, "%v", got)
assert.LessOrEqual(t, got[linksRoute], listed, "%v", got)
assert.LessOrEqual(t, totalRequests(got), 2*listed, "%v", got)
}
// TestBudgetUpload checks the requests of uploading new files to a
// folder: one listing of the folder to check the name is free, the
// first time only, then per file a draft, its verification code, at
// most one block URL request per block, one PUT per block, the commit
// and one read of the committed node.
//
// Measured, per route, into a folder of 3 files:
//
// small.txt, cold: children 1 + links 1 (the listing), draft 1,
// verifier 1, block URLs 1, block PUTs 1, commit 1, links 1 = 8
// big.bin, 3 blocks: draft 1, verifier 1, block URLs 1-3 (as blocks
// are ready), block PUTs 3, commit 1, links 1 = 8-10
// again.txt: as small.txt without the listing = 6
func TestBudgetUpload(t *testing.T) {
ctx := context.Background()
remote, srv, u := setupFakeRemote(t)
seedFiles(u, u.SeedFolder(u.RootID(), "dir"), 3)
f := newFakeFs(t, remote, "dir")
for _, test := range []struct {
name string
size int
blocks int
listed bool // the folder is listed to check the name is free
}{
{"small.txt", 5, 1, true},
{"big.bin", 2*blockSize + 5, 3, false},
{"again.txt", 5, 1, false},
} {
t.Run(test.name, func(t *testing.T) {
srv.ResetRequests()
content := patternedContent(test.size, 1)
_, err := operations.Copy(ctx, f, nil, test.name, object.NewMemoryObject(test.name, time.Now(), content))
require.NoError(t, err)
got := withoutEvents(srv.Requests())
listing := 0
if test.listed {
listing = 2 // a children page and a links batch
assert.Equal(t, 1, got[childrenRoute], "%v", got)
} else {
assert.Zero(t, got[childrenRoute], "folder listed again: %v", got)
}
assert.Equal(t, 1, got[draftRoute], "%v", got)
assert.Equal(t, 1, got[verifierRoute], "%v", got)
assert.GreaterOrEqual(t, got[blockURLsRoute], 1, "%v", got)
assert.LessOrEqual(t, got[blockURLsRoute], test.blocks, "%v", got)
assert.Equal(t, test.blocks, got[blockPutRoute], "%v", got)
assert.Equal(t, 1, got[commitRoute], "%v", got)
assert.Equal(t, listing/2+1, got[linksRoute], "%v", got)
assert.LessOrEqual(t, totalRequests(got), listing+4+2*test.blocks, "%v", got)
})
}
}
// TestBudgetMkdir checks making folders side by side lists their
// parent once, to check the first name is free, and then costs one
// request per folder.
//
// Measured, per route, into a folder of 3 files: x: children 1 +
// links 1 + create 1 = 3; y and z: create 1 each.
func TestBudgetMkdir(t *testing.T) {
ctx := context.Background()
remote, srv, u := setupFakeRemote(t)
seedFiles(u, u.SeedFolder(u.RootID(), "dir"), 3)
f := newFakeFs(t, remote, "dir")
srv.ResetRequests()
for _, name := range []string{"x", "y", "z"} {
require.NoError(t, f.Mkdir(ctx, name))
}
got := withoutEvents(srv.Requests())
assert.Equal(t, map[string]int{childrenRoute: 1, linksRoute: 1, createFolderRoute: 3}, got)
entries, err := f.List(ctx, "")
require.NoError(t, err)
assert.Len(t, entries, 6, "the cached listing holds the new folders")
}
// TestBudgetEvents checks the volume events cost nothing while the Fs
// caches nothing, and once it does, one request for the start cursor
// and then at most one poll per events_min_interval, 1s here (spec 9).
func TestBudgetEvents(t *testing.T) {
ctx := context.Background()
remote, srv, u := setupFakeRemote(t)
seedFiles(u, u.SeedFolder(u.RootID(), "dir"), 3)
f := newFakeFs(t, remote, "")
srv.ResetRequests()
time.Sleep(3 * time.Second)
got := srv.Requests()
assert.Zero(t, got[latestEventRoute]+got[eventsRoute], "idle: %v", got)
_, err := f.List(ctx, "dir")
require.NoError(t, err)
start := time.Now()
time.Sleep(3 * time.Second)
got = srv.Requests()
elapsed := time.Since(start)
assert.Equal(t, 1, got[latestEventRoute], "%v", got)
assert.GreaterOrEqual(t, got[eventsRoute], 1, "%v", got)
assert.LessOrEqual(t, got[eventsRoute], int(elapsed/time.Second), "%v", got)
}
// TestBudgetNoLegacyRoutes checks a run of everyday operations requests
// no removed share-scoped route. TestMain checks the same of every
// test in the package, through every fake server.
func TestBudgetNoLegacyRoutes(t *testing.T) {
ctx := context.Background()
remote, srv, u := setupFakeRemote(t)
seedFiles(u, u.SeedFolder(u.RootID(), "dir"), 3)
f := newFakeFs(t, remote, "")
o, err := operations.Copy(ctx, f, nil, "dir/new.txt", object.NewMemoryObject("dir/new.txt", time.Now(), []byte("new")))
require.NoError(t, err)
assert.Equal(t, "new", readObject(t, o))
_, err = operations.Move(ctx, f, nil, "moved/new.txt", o)
require.NoError(t, err)
require.NoError(t, operations.Purge(ctx, f, "dir"))
_, err = f.List(ctx, "moved")
require.NoError(t, err)
_, err = f.Features().About(ctx)
require.NoError(t, err)
for route := range srv.Requests() {
if strings.Contains(route, "/drive/shares/") {
assert.Equal(t, "GET /drive/shares/{shareID}", route)
}
}
assert.Empty(t, protontest.LegacyRequests())
}
+336
View File
@@ -0,0 +1,336 @@
package protondrive
import (
"slices"
"sync"
"sync/atomic"
"time"
)
// nodeCache holds decrypted nodes and folder listings in memory.
//
// A nil nodeCache, as newNodeCache returns when caching is disabled,
// holds nothing: its lookups miss and its updates do nothing. Cached
// nodes are shared and must not be modified.
type nodeCache struct {
mu sync.Mutex
nodes map[string]*node // by ID
listings map[string][]string // IDs of the complete listing of a folder, by folder ID
fetching map[string]*fetchState // folders being listed from the server, by folder ID
used atomic.Int64 // when the cache was last read or filled, in Unix nanoseconds
// onFill, if set, is called without mu held when the cache gains
// its first entry, from empty. It is read without mu, so it must
// be set before the cache is shared.
onFill func()
}
// fetchState tracks the listings of a folder being fetched, so that
// one which began before a change to the folder's children isn't
// cached over it.
type fetchState struct {
count int // listings in progress
changes uint64 // changes to the folder's children since the first began
}
// newNodeCache returns an empty cache, or nil if enabled is false.
func newNodeCache(enabled bool) *nodeCache {
if !enabled {
return nil
}
return &nodeCache{
nodes: make(map[string]*node),
listings: make(map[string][]string),
fetching: make(map[string]*fetchState),
}
}
// get returns the node id, or nil if it isn't cached.
func (c *nodeCache) get(id string) *node {
if c == nil {
return nil
}
c.touch()
return c.peek(id)
}
// peek is get, but doesn't count as a use of the cache: it is for the
// cache's own upkeep.
func (c *nodeCache) peek(id string) *node {
if c == nil {
return nil
}
c.mu.Lock()
defer c.mu.Unlock()
return c.nodes[id]
}
// touch records that the cache is in use now.
func (c *nodeCache) touch() {
c.used.Store(time.Now().UnixNano())
}
// unusedFor returns how long ago the cache was last read or filled; a
// nil cache, or one never used, counts as used now.
func (c *nodeCache) unusedFor() time.Duration {
if c == nil {
return 0
}
used := c.used.Load()
if used == 0 {
return 0
}
return time.Since(time.Unix(0, used))
}
// put caches n, replacing any node with its ID. If the cached node had
// another parent, the listings of both parents are forgotten.
func (c *nodeCache) put(n *node) {
if c == nil {
return
}
c.touch()
c.mu.Lock()
defer c.filled(c.emptyLocked())
defer c.mu.Unlock()
if old := c.nodes[n.ID]; old != nil && old.ParentID != n.ParentID {
c.childrenChangedLocked(old.ParentID)
c.childrenChangedLocked(n.ParentID)
}
c.nodes[n.ID] = n
}
// children returns the cached listing of folder parentID. It reports
// false if the listing, or any node in it, isn't cached.
func (c *nodeCache) children(parentID string) ([]*node, bool) {
if c == nil {
return nil, false
}
c.touch()
c.mu.Lock()
defer c.mu.Unlock()
ids, ok := c.listings[parentID]
if !ok {
return nil, false
}
out := make([]*node, 0, len(ids))
for _, id := range ids {
n := c.nodes[id]
if n == nil {
return nil, false
}
out = append(out, n)
}
return out, true
}
// setChildren caches nodes as the complete listing of folder parentID,
// and each of the nodes. The listings of other parents a node was
// cached under are forgotten.
func (c *nodeCache) setChildren(parentID string, nodes []*node) {
if c == nil {
return
}
c.touch()
c.mu.Lock()
defer c.filled(c.emptyLocked())
defer c.mu.Unlock()
c.setChildrenLocked(parentID, nodes)
}
// setChildrenLocked is setChildren with c.mu held.
func (c *nodeCache) setChildrenLocked(parentID string, nodes []*node) {
ids := make([]string, len(nodes))
for i, n := range nodes {
ids[i] = n.ID
if old := c.nodes[n.ID]; old != nil && old.ParentID != parentID {
c.childrenChangedLocked(old.ParentID)
}
c.nodes[n.ID] = n
}
c.listings[parentID] = ids
}
// startFetch records that a listing of folder parentID is being read
// from the server and returns the token to pass to endFetch, which
// must be called.
func (c *nodeCache) startFetch(parentID string) (token uint64) {
if c == nil {
return 0
}
c.mu.Lock()
defer c.mu.Unlock()
st := c.fetching[parentID]
if st == nil {
st = &fetchState{}
c.fetching[parentID] = st
}
st.count++
return st.changes
}
// endFetch ends a listing of folder parentID begun by startFetch with
// token. If complete is set, nodes are cached as the listing, unless
// the folder's children changed since it began, when what it read may
// be out of date.
func (c *nodeCache) endFetch(parentID string, token uint64, nodes []*node, complete bool) {
if c == nil {
return
}
c.touch()
c.mu.Lock()
defer c.filled(c.emptyLocked())
defer c.mu.Unlock()
st := c.fetching[parentID]
if complete && st.changes == token {
c.setChildrenLocked(parentID, nodes)
}
st.count--
if st.count == 0 {
delete(c.fetching, parentID)
}
}
// childrenChangedLocked forgets the listing of folder parentID, and
// marks its listings being fetched as out of date. c.mu must be held.
func (c *nodeCache) childrenChangedLocked(parentID string) {
delete(c.listings, parentID)
if st := c.fetching[parentID]; st != nil {
st.changes++
}
}
// add caches n, a node new to its parent, and adds it to the cached
// listing of its parent, if there is one.
func (c *nodeCache) add(n *node) {
if c == nil {
return
}
c.touch()
c.mu.Lock()
defer c.filled(c.emptyLocked())
defer c.mu.Unlock()
if old := c.nodes[n.ID]; old != nil && old.ParentID != n.ParentID {
c.childrenChangedLocked(old.ParentID)
c.childrenChangedLocked(n.ParentID)
}
c.nodes[n.ID] = n
if st := c.fetching[n.ParentID]; st != nil {
st.changes++
}
if ids, ok := c.listings[n.ParentID]; ok && !slices.Contains(ids, n.ID) {
c.listings[n.ParentID] = append(ids, n.ID)
}
}
// invalidate forgets the node id, its listing if it is a folder, and
// the listing of its parent.
func (c *nodeCache) invalidate(id string) {
if c == nil {
return
}
c.mu.Lock()
defer c.mu.Unlock()
if n := c.nodes[id]; n != nil {
c.childrenChangedLocked(n.ParentID)
} else {
// Its parent is unknown, and any listing being fetched may
// hold it.
for _, st := range c.fetching {
st.changes++
}
}
delete(c.nodes, id)
c.childrenChangedLocked(id)
}
// invalidateTree forgets the node id, the listing of its parent, and
// the node, listing and descendants of everything below it: the
// descendants of a folder deleted or moved are cached under paths they
// no longer have.
func (c *nodeCache) invalidateTree(id string) {
if c == nil {
return
}
c.mu.Lock()
defer c.mu.Unlock()
if n := c.nodes[id]; n != nil {
c.childrenChangedLocked(n.ParentID)
} else {
// Its parent is unknown, and any listing being fetched may
// hold it.
for _, st := range c.fetching {
st.changes++
}
}
// Descendants may be cached without their parent's listing, so
// they are found by parent ID. Every node a listing names is
// cached, so the listings add none.
children := make(map[string][]string)
for childID, n := range c.nodes {
children[n.ParentID] = append(children[n.ParentID], childID)
}
// Visited IDs end the walk of a cycle a hostile server reported.
visited := map[string]bool{id: true}
pending := []string{id}
for len(pending) > 0 {
next := pending[len(pending)-1]
pending = pending[:len(pending)-1]
delete(c.nodes, next)
c.childrenChangedLocked(next)
for _, childID := range children[next] {
if !visited[childID] {
visited[childID] = true
pending = append(pending, childID)
}
}
}
}
// invalidateChildren forgets the listing of folder parentID, but not
// the nodes in it.
func (c *nodeCache) invalidateChildren(parentID string) {
if c == nil {
return
}
c.mu.Lock()
defer c.mu.Unlock()
c.childrenChangedLocked(parentID)
}
// clear forgets everything.
func (c *nodeCache) clear() {
if c == nil {
return
}
c.mu.Lock()
defer c.mu.Unlock()
clear(c.nodes)
clear(c.listings)
for _, st := range c.fetching {
st.changes++
}
}
// empty reports whether nothing is cached. A nil cache is empty.
func (c *nodeCache) empty() bool {
if c == nil {
return true
}
c.mu.Lock()
defer c.mu.Unlock()
return c.emptyLocked()
}
// emptyLocked is empty with c.mu held.
func (c *nodeCache) emptyLocked() bool {
return len(c.nodes) == 0 && len(c.listings) == 0
}
// filled calls onFill if the cache was empty before an update, as
// wasEmpty says, and holds something now. c.mu must not be held.
func (c *nodeCache) filled(wasEmpty bool) {
if wasEmpty && c.onFill != nil && !c.empty() {
c.onFill()
}
}
+291
View File
@@ -0,0 +1,291 @@
package protondrive
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNodeCacheDisabled(t *testing.T) {
c := newNodeCache(false)
require.Nil(t, c)
n := &node{ID: "a", ParentID: "root"}
c.put(n)
c.setChildren("root", []*node{n})
assert.Nil(t, c.get("a"))
_, ok := c.children("root")
assert.False(t, ok)
c.invalidate("a")
c.invalidateChildren("root")
c.invalidateTree("root")
c.clear()
}
func TestNodeCache(t *testing.T) {
c := newNodeCache(true)
dir := &node{ID: "dir", ParentID: "root", IsFolder: true}
file := &node{ID: "file", ParentID: "dir"}
assert.Nil(t, c.get("dir"))
_, ok := c.children("root")
assert.False(t, ok)
c.setChildren("root", []*node{dir})
c.setChildren("dir", []*node{file})
assert.Same(t, dir, c.get("dir"))
assert.Same(t, file, c.get("file"))
got, ok := c.children("dir")
require.True(t, ok)
assert.Equal(t, []*node{file}, got)
// An empty listing is a listing.
c.setChildren("empty", nil)
got, ok = c.children("empty")
assert.True(t, ok)
assert.Empty(t, got)
// put replaces the node in the listing.
renamed := &node{ID: "file", ParentID: "dir", Name: "new"}
c.put(renamed)
got, ok = c.children("dir")
require.True(t, ok)
assert.Equal(t, []*node{renamed}, got)
// put of a node with another parent forgets both listings.
c.setChildren("other", nil)
moved := &node{ID: "file", ParentID: "other"}
c.put(moved)
assert.Same(t, moved, c.get("file"))
_, ok = c.children("dir")
assert.False(t, ok)
_, ok = c.children("other")
assert.False(t, ok)
_, ok = c.children("root")
assert.True(t, ok)
c.setChildren("dir", []*node{renamed})
// invalidate forgets the node and its parent's listing.
c.invalidate("file")
assert.Nil(t, c.get("file"))
_, ok = c.children("dir")
assert.False(t, ok)
_, ok = c.children("root")
assert.True(t, ok)
// and a folder's own listing.
c.setChildren("dir", []*node{file})
c.invalidate("dir")
_, ok = c.children("dir")
assert.False(t, ok)
_, ok = c.children("root")
assert.False(t, ok)
assert.Same(t, file, c.get("file"))
c.setChildren("dir", []*node{file})
c.invalidateChildren("dir")
_, ok = c.children("dir")
assert.False(t, ok)
assert.Same(t, file, c.get("file"))
c.setChildren("dir", []*node{file})
c.clear()
assert.Nil(t, c.get("file"))
_, ok = c.children("dir")
assert.False(t, ok)
}
func TestNodeCacheAdd(t *testing.T) {
c := newNodeCache(true)
a := &node{ID: "a", ParentID: "dir"}
b := &node{ID: "b", ParentID: "dir"}
// Without a listing of its parent, only the node is cached.
c.add(a)
assert.Same(t, a, c.get("a"))
_, ok := c.children("dir")
assert.False(t, ok)
c.setChildren("dir", []*node{a})
c.add(b)
got, ok := c.children("dir")
require.True(t, ok)
assert.Equal(t, []*node{a, b}, got)
// A node already listed is replaced, not listed twice.
b2 := &node{ID: "b", ParentID: "dir", Name: "new"}
c.add(b2)
got, ok = c.children("dir")
require.True(t, ok)
assert.Equal(t, []*node{a, b2}, got)
newNodeCache(false).add(a)
}
func TestNodeCacheFetch(t *testing.T) {
a := &node{ID: "a", ParentID: "dir"}
listed := func(c *nodeCache) bool {
_, ok := c.children("dir")
return ok
}
c := newNodeCache(true)
token := c.startFetch("dir")
c.endFetch("dir", token, []*node{a}, true)
assert.True(t, listed(c), "unchanged")
assert.Empty(t, c.fetching)
c = newNodeCache(true)
token = c.startFetch("dir")
c.endFetch("dir", token, []*node{a}, false)
assert.False(t, listed(c), "incomplete")
assert.Empty(t, c.fetching)
for name, change := range map[string]func(c *nodeCache){
"add": func(c *nodeCache) { c.add(&node{ID: "b", ParentID: "dir"}) },
"invalidate child": func(c *nodeCache) { c.invalidate("a") },
"invalidate unknown": func(c *nodeCache) { c.invalidate("unknown") },
"invalidate folder": func(c *nodeCache) { c.invalidate("dir") },
"invalidateChildren": func(c *nodeCache) { c.invalidateChildren("dir") },
"put moved in": func(c *nodeCache) { c.put(&node{ID: "other", ParentID: "dir"}) },
"put moved out": func(c *nodeCache) { c.put(&node{ID: "a", ParentID: "elsewhere"}) },
"clear": func(c *nodeCache) { c.clear() },
} {
t.Run(name, func(t *testing.T) {
c := newNodeCache(true)
c.put(a)
c.put(&node{ID: "other", ParentID: "elsewhere"})
first := c.startFetch("dir")
second := c.startFetch("dir")
change(c)
c.endFetch("dir", first, []*node{a}, true)
assert.False(t, listed(c), "first listing cached after a change")
// A fetch begun after the change is cached.
third := c.startFetch("dir")
c.endFetch("dir", second, []*node{a}, true)
assert.False(t, listed(c), "second listing cached after a change")
c.endFetch("dir", third, []*node{a}, true)
assert.True(t, listed(c))
assert.Empty(t, c.fetching, "fetch state kept")
})
}
// Changes elsewhere don't matter.
c = newNodeCache(true)
c.put(&node{ID: "x", ParentID: "other"})
token = c.startFetch("dir")
c.add(&node{ID: "y", ParentID: "other"})
c.invalidate("x")
c.invalidateChildren("other")
c.endFetch("dir", token, []*node{a}, true)
assert.True(t, listed(c))
var disabled *nodeCache
disabled.endFetch("dir", disabled.startFetch("dir"), nil, true)
}
func TestInvalidateTree(t *testing.T) {
c := newNodeCache(true)
c.setChildren("root", []*node{
{ID: "dir", ParentID: "root", IsFolder: true},
{ID: "keep", ParentID: "root"},
})
c.setChildren("dir", []*node{
{ID: "sub", ParentID: "dir", IsFolder: true},
{ID: "file", ParentID: "dir"},
})
c.setChildren("sub", []*node{{ID: "deep", ParentID: "sub"}})
// A descendant cached without its parent's listing.
c.put(&node{ID: "lone", ParentID: "sub"})
c.setChildren("elsewhere", []*node{{ID: "x", ParentID: "elsewhere"}})
dirFetch := c.startFetch("dir")
subFetch := c.startFetch("sub")
otherFetch := c.startFetch("elsewhere")
c.invalidateTree("dir")
for _, id := range []string{"dir", "sub", "file", "deep", "lone"} {
assert.Nil(t, c.get(id), id)
}
for _, id := range []string{"root", "dir", "sub"} {
_, ok := c.listings[id]
assert.False(t, ok, "listing %s kept", id)
}
assert.NotNil(t, c.get("keep"))
assert.NotNil(t, c.get("x"))
_, ok := c.children("elsewhere")
assert.True(t, ok)
// Listings of the tree in flight aren't cached; others are.
c.endFetch("dir", dirFetch, []*node{{ID: "file", ParentID: "dir"}}, true)
c.endFetch("sub", subFetch, []*node{{ID: "deep", ParentID: "sub"}}, true)
c.endFetch("elsewhere", otherFetch, []*node{{ID: "x", ParentID: "elsewhere"}}, true)
_, ok = c.children("dir")
assert.False(t, ok)
_, ok = c.children("sub")
assert.False(t, ok)
_, ok = c.children("elsewhere")
assert.True(t, ok)
assert.Empty(t, c.fetching)
// An unknown folder: its cached children go, and every listing in
// flight may hold it.
c = newNodeCache(true)
c.setChildren("ghost", []*node{{ID: "child", ParentID: "ghost"}})
c.put(&node{ID: "y", ParentID: "other"})
token := c.startFetch("other")
c.invalidateTree("ghost")
assert.Nil(t, c.get("child"))
assert.NotNil(t, c.get("y"))
c.endFetch("other", token, []*node{{ID: "y", ParentID: "other"}}, true)
_, ok = c.children("other")
assert.False(t, ok)
// A cycle, as a hostile server could report, ends.
c = newNodeCache(true)
c.put(&node{ID: "a", ParentID: "b", IsFolder: true})
c.put(&node{ID: "b", ParentID: "a", IsFolder: true})
c.invalidateTree("a")
assert.True(t, c.empty())
}
func TestNodeCacheEmpty(t *testing.T) {
var disabled *nodeCache
assert.True(t, disabled.empty())
c := newNodeCache(true)
assert.True(t, c.empty())
c.put(&node{ID: "a", ParentID: "root"})
assert.False(t, c.empty())
c.invalidate("a")
assert.True(t, c.empty())
// An empty listing is something cached.
c.setChildren("root", nil)
assert.False(t, c.empty())
c.clear()
assert.True(t, c.empty())
// Listings in flight aren't.
token := c.startFetch("root")
assert.True(t, c.empty())
c.endFetch("root", token, nil, false)
}
// TestNodeCacheSetChildrenMoved checks a listing holding a node cached
// under another parent forgets the other parent's listing, as put does.
func TestNodeCacheSetChildrenMoved(t *testing.T) {
t.Parallel()
c := newNodeCache(true)
c.setChildren("a", []*node{{ID: "x", ParentID: "a"}, {ID: "y", ParentID: "a"}})
token := c.startFetch("a")
c.setChildren("b", []*node{{ID: "x", ParentID: "b"}})
_, ok := c.children("a")
assert.False(t, ok, "the old parent's listing still holds the node")
got, ok := c.children("b")
require.True(t, ok)
assert.Equal(t, "b", got[0].ParentID)
c.endFetch("a", token, []*node{{ID: "x", ParentID: "a"}}, true)
_, ok = c.children("a")
assert.False(t, ok, "a listing of the old parent begun before is cached")
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+786
View File
@@ -0,0 +1,786 @@
package protondrive
import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"slices"
"strconv"
"sync"
"github.com/ProtonMail/gopenpgp/v3/crypto"
"github.com/rclone/rclone/backend/protondrive/api"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/lib/pool"
"github.com/rclone/rclone/lib/rest"
)
var (
// errIntegrity is returned when a file's content doesn't match
// what its uploader signed or claimed.
errIntegrity = errors.New("integrity check failed")
// errBlockHash is returned when a block doesn't match its hash
// however many times it is downloaded.
errBlockHash = errors.New("block hash mismatch")
// errDownloadClosed is returned by Read after Close.
errDownloadClosed = errors.New("download closed")
)
// signatureHint ends an error for a signature which fails, telling the
// user how to read the file anyway.
const signatureHint = "set ignore_signature_errors to read the file anyway"
const (
// defaultBlockSize is the plaintext size of every block but the
// last of a file the client uploads.
defaultBlockSize = 4 << 20
// blocksPerPage is the most blocks one revision request lists.
blocksPerPage = 20
// maxBlockSize is the largest block accepted, encrypted or
// decrypted.
maxBlockSize = 8 << 20
// blockAttempts is how many times a block whose hash doesn't match
// is downloaded.
blockAttempts = 3
// downloadAhead is the most blocks of one download fetched or
// waiting to be read, besides the one being read.
downloadAhead = 4
// minUnclaimedBlockSize bounds the blocks of a file which doesn't
// record its block sizes: there are at most its encrypted size over
// this, plus one.
minUnclaimedBlockSize = 1 << 20
// maxFileBlocks is the most blocks a file may list: 2 TiB in
// 4 MiB blocks, beyond any one file the largest Proton plan, of a
// few TB in all, could hold. A full read holds the metadata of
// every block until the manifest is checked, so this bounds how
// many whatever the server claims: about 250-350 MiB at the cap
// with realistic tokens and URLs. Their length isn't bounded.
maxFileBlocks = 1 << 19
// contentKeyFallbackUntil is the Unix time until which a content
// key packet may be signed by any of the account's address keys,
// as in the SDK.
contentKeyFallbackUntil = 1672531200 // 2023-01-01
)
// open returns a reader of limit bytes of the content of file n from
// offset, or to the end if limit < 0.
//
// A read from offset 0 to the end lists every block and checks the
// manifest signature before releasing any byte: if it fails, the first
// Read returns errIntegrity, unless IgnoreSignatureErrors is set, when
// it is logged. A ranged read checks each block against its listed
// hash but can't check the manifest. A node key or content key
// signature which fails is errIntegrity from open, on the same terms.
// The reader must be closed.
func (c *client) open(ctx context.Context, n *node, offset, limit int64) (io.ReadCloser, error) {
return c.openRead(ctx, n, offset, limit, offset == 0 && limit < 0)
}
// openWhole is open for limit bytes from offset 0 of a range which
// covers the whole file, checked as a full read is.
func (c *client) openWhole(ctx context.Context, n *node, limit int64) (io.ReadCloser, error) {
return c.openRead(ctx, n, 0, limit, true)
}
// openRead is open, listing every block and checking the manifest
// before releasing any byte if full is set, which needs offset 0.
func (c *client) openRead(ctx context.Context, n *node, offset, limit int64, full bool) (io.ReadCloser, error) {
if n.IsFolder || n.link == nil || n.link.File == nil || n.RevisionID == "" {
return nil, fmt.Errorf("open %q: not a file with content", n.Name)
}
if offset < 0 {
return nil, fmt.Errorf("open %q: negative offset %d", n.Name, offset)
}
sk, err := c.contentKey(n, "reading")
if err != nil {
return nil, fmt.Errorf("open %q: %w", n.Name, err)
}
var sizes []int64
if n.X != nil {
sizes = n.X.BlockSizes
}
full = full && offset == 0
if len(sizes) > c.maxFileBlocks {
return nil, fmt.Errorf("open %q: %w: %d blocks recorded, more than %d blocks", n.Name, errIntegrity, len(sizes), c.maxFileBlocks)
}
if sizes != nil {
var total int64
for i, size := range sizes {
// Bounding each size bounds the total too.
if size > maxBlockSize {
return nil, fmt.Errorf("open %q: %w: block %d recorded as %d bytes", n.Name, errIntegrity, i+1, size)
}
total += size
}
switch {
case full:
// A full read always lists the blocks, to reach the
// manifest.
case offset >= total:
limit = 0
case limit >= 0:
limit = min(limit, total-offset)
}
}
if limit == 0 {
return io.NopCloser(bytes.NewReader(nil)), nil
}
// Without recorded block sizes only the sizes learnt by earlier
// reads tell which block holds offset, as old clients didn't all
// write 4 MiB blocks, so the read starts at the last block they
// reach and discards the bytes before offset.
first, skip, last := 1, offset, -1
var startHash []byte
switch {
case sizes != nil:
first, skip = blockAt(sizes, offset)
if limit > 0 && !full {
last, _ = blockAt(sizes, offset+limit-1)
}
case !full:
first, skip, last, startHash = c.learnt.locate(n.RevisionID, offset, limit)
}
maxBlocks := len(sizes)
if sizes == nil {
maxBlocks = int(min(n.EncryptedSize/minUnclaimedBlockSize+1, 1<<30))
}
ctx, cancel := context.WithCancel(ctx)
r := &blockReader{
c: c,
n: n,
sk: sk,
ctx: ctx,
cancel: cancel,
slots: make(chan chan blockResult, downloadAhead),
sizes: sizes,
startHash: startHash,
skip: skip,
remaining: limit,
}
r.wg.Add(1)
go r.list(first, last, maxBlocks, full)
return r, nil
}
// blockAt returns the index, from 1, of the block holding plaintext
// offset pos of a file with block sizes sizes, and the offset of pos in
// it.
func blockAt(sizes []int64, pos int64) (index int, offset int64) {
for i, size := range sizes {
if pos < size {
return i + 1, pos
}
pos -= size
}
return len(sizes) + 1, pos
}
// contentKey returns the content session key of file n for op,
// "reading" or "updating", checking its signature as the SDK's
// decryptContentKeyPacket does, except that a missing signature fails
// too: anyone with the node's public key can make a content key packet.
//
// An unverified node key or content key is errIntegrity unless
// IgnoreSignatureErrors is set, when it is logged once per node and op:
// whoever made the key could read what is written with it and make up
// the content read.
func (c *client) contentKey(n *node, op string) (*crypto.SessionKey, error) {
hint := signatureHint
if op != "reading" {
hint = "set ignore_signature_errors to update the file anyway"
}
if n.keys.unverified != nil {
if !c.opt.IgnoreSignatureErrors {
return nil, fmt.Errorf("node key: %w: %w: %s", errIntegrity, n.keys.unverified, hint)
}
c.reportUnverified(op, n, "")
}
f := n.link.File
now := c.s.serverTime()
sk, verifyErr, err := decryptContentKey(f.ContentKeyPacket, f.ContentKeyPacketSignature, n.keys.keyRing, n.keyVerifyKR, now)
if err != nil {
return nil, err
}
if verifyErr != nil && !isNotSigned(verifyErr) && n.CreateTime.Unix() <= contentKeyFallbackUntil {
// Old clients signed with an address key, and a rename or
// move could change the node's signer without signing the
// content key packet again.
krs := make([]*crypto.KeyRing, 0, len(c.acct.addresses))
for _, addr := range c.acct.addresses {
krs = append(krs, addr.keyRing)
}
if allKR, err := joinKeyRings(krs...); err == nil && verifyDetached(sk.Key, f.ContentKeyPacketSignature, allKR, now) == nil {
fs.Debugf(nil, "protondrive: %q (ID %s): content key signed by another address of the account", n.Name, n.ID)
verifyErr = nil
}
}
if verifyErr != nil {
verifyErr = fmt.Errorf("content key: %w: %w", errIntegrity, verifyErr)
if !c.opt.IgnoreSignatureErrors {
return nil, fmt.Errorf("%w: %s", verifyErr, hint)
}
c.reportOnce("content key "+op, n.ID, func() {
fs.Errorf(nil, "protondrive: %s %q (ID %s): %v", op, n.Name, n.ID, verifyErr)
})
}
return sk, nil
}
// revisionBlocks returns the active revision of file n with at most
// pageSize of its blocks from block index from.
func (c *client) revisionBlocks(ctx context.Context, n *node, from, pageSize int) (*api.Revision, error) {
opts := &rest.Opts{
Method: "GET",
Path: c.volumePath("/drive/v2/volumes", "files", n.ID, "revisions", n.RevisionID),
Parameters: url.Values{
"FromBlockIndex": {strconv.Itoa(from)},
"PageSize": {strconv.Itoa(pageSize)},
},
}
var resp api.RevisionResponse
if err := c.apiCall(ctx, opts, nil, &resp); err != nil {
return nil, fmt.Errorf("list blocks: %w", err)
}
return &resp.Revision, nil
}
// blockURL returns block index of file n with a new download URL.
func (c *client) blockURL(ctx context.Context, n *node, index int) (api.Block, error) {
rev, err := c.revisionBlocks(ctx, n, index, 1)
if err != nil {
return api.Block{}, err
}
if len(rev.Blocks) == 0 || rev.Blocks[0].Index != index {
return api.Block{}, fmt.Errorf("list blocks: block %d is missing", index)
}
return rev.Blocks[0], nil
}
// downloadBlock returns the encrypted block at bareURL, which token
// unlocks, and its SHA-256. The block must be closed.
func (c *client) downloadBlock(ctx context.Context, bareURL, token string) (enc *pool.RW, sum []byte, err error) {
c.downloadTokens.Get()
defer c.downloadTokens.Put()
opts := &rest.Opts{
Method: "GET",
RootURL: bareURL,
// The storage host gets the block's token, never the
// session's credentials, which only session.call adds.
ExtraHeaders: map[string]string{"pm-storage-token": token},
}
err = c.pacedCall(ctx, c.storagePacer, func() (*http.Response, error) {
if enc != nil {
_ = enc.Close()
}
enc = pool.NewRW(pool.Global())
resp, err := c.s.srv.Call(ctx, opts)
if err != nil {
return resp, err
}
defer func() { _ = resp.Body.Close() }()
if resp.ContentLength > maxBlockSize {
return nil, fmt.Errorf("block is larger than %d bytes", maxBlockSize)
}
h := sha256.New()
size, err := enc.ReadFrom(io.TeeReader(io.LimitReader(resp.Body, maxBlockSize+1), h))
if err != nil {
return resp, err
}
if size > maxBlockSize {
return nil, fmt.Errorf("block is larger than %d bytes", maxBlockSize)
}
sum = h.Sum(nil)
return resp, nil
})
if err != nil {
if enc != nil {
_ = enc.Close()
}
return nil, nil, err
}
return enc, sum, nil
}
// blockResult is the outcome of a block of a download, or its end.
type blockResult struct {
index int
hash []byte // the block's SHA-256
plain *pool.RW // the decrypted block; nil on error or at the end
err error
end bool // no more blocks follow; err is the verdict on the file
}
// blockReader reads a range of a file, fetching blocks ahead of the
// reader in the background.
type blockReader struct {
c *client
n *node
sk *crypto.SessionKey
ctx context.Context
cancel context.CancelFunc
slots chan chan blockResult // results to read, in order
wg sync.WaitGroup // the lister and the fetches
sizes []int64 // claimed plaintext block sizes; nil if not recorded
// startHash is the hash the first block had when its size was
// learnt, if it was.
startHash []byte
mu sync.Mutex // held by Read and Close
cur *pool.RW // block being read
skip int64 // bytes to discard before the range, from the first block on
remaining int64 // bytes left to read; negative for all
err error // returned by every Read once set
closed bool
}
// list lists the blocks first to last (to the end if last < 0) and
// fetches them, then sends the end of the download. If verify is set,
// the list having started at the first block, it lists every block and
// checks the manifest before fetching any.
//
// A list of more than maxBlocks blocks is an error.
func (r *blockReader) list(first, last, maxBlocks int, verify bool) {
defer r.wg.Done()
end := make(chan blockResult, 1)
end <- blockResult{end: true, err: r.listBlocks(first, last, maxBlocks, verify)}
select {
case r.slots <- end:
case <-r.ctx.Done():
}
}
// listedBlock is a block of the list with its decoded hash.
type listedBlock struct {
block api.Block
hash []byte
}
// listBlocks does the work of list, returning the verdict on the file.
func (r *blockReader) listBlocks(first, last, maxBlocks int, verify bool) error {
var (
hashes [][]byte
listed []listedBlock // blocks waiting for the manifest check, if verify
manifest string
signer string
index = first
firstPage = true
)
for {
pageSize := blocksPerPage
if last > 0 {
pageSize = min(pageSize, last-index+1)
}
rev, err := r.c.revisionBlocks(r.ctx, r.n, index, pageSize)
if err != nil {
return err
}
if firstPage && verify {
manifest, signer = rev.ManifestSignature, rev.SignatureEmail
// As in the SDK, the manifest starts with the thumbnails.
for _, th := range rev.Thumbnails {
hash, err := decodeBlockHash(th.Hash)
if err != nil {
return fmt.Errorf("thumbnail: %w", err)
}
hashes = append(hashes, hash)
}
}
firstPage = false
if len(rev.Blocks) == 0 {
break
}
for _, b := range rev.Blocks {
switch {
case index > r.c.maxFileBlocks:
return fmt.Errorf("%w: more than %d blocks listed, more than any file can have", errIntegrity, r.c.maxFileBlocks)
case b.Index != index:
return fmt.Errorf("%w: block %d listed where block %d belongs", errIntegrity, b.Index, index)
case index > maxBlocks && r.sizes != nil:
return fmt.Errorf("%w: more blocks listed than the %d the file records", errIntegrity, maxBlocks)
case index > maxBlocks:
return fmt.Errorf("%w: more blocks listed than the file's encrypted size of %d allows", errIntegrity, r.n.EncryptedSize)
}
hash, err := decodeBlockHash(b.Hash)
if err != nil {
return fmt.Errorf("block %d: %w", index, err)
}
if index == first && r.startHash != nil && !bytes.Equal(hash, r.startHash) {
// The sizes placing the range came from another block
// list of the same revision.
return fmt.Errorf("%w: block %d listed with another hash than when it was read before", errIntegrity, index)
}
if verify {
// Nothing is released before the manifest vouches for
// every block's hash and place.
hashes = append(hashes, hash)
listed = append(listed, listedBlock{block: b, hash: hash})
} else if !r.fetch(b, hash) {
return r.ctx.Err()
}
if index == last {
return nil
}
index++
}
}
if r.sizes != nil && index <= len(r.sizes) {
return fmt.Errorf("%w: %d blocks listed of the %d the file records", errIntegrity, index-1, len(r.sizes))
}
if !verify {
return nil
}
if err := r.verifyManifest(hashes, manifest, signer); err != nil {
return err
}
for _, lb := range listed {
if !r.fetch(lb.block, lb.hash) {
return r.ctx.Err()
}
}
return nil
}
// decodeBlockHash returns the base64 SHA-256 hash.
func decodeBlockHash(b64 string) ([]byte, error) {
hash, err := base64.StdEncoding.DecodeString(b64)
if err == nil && len(hash) != sha256.Size {
err = fmt.Errorf("%d bytes long", len(hash))
}
if err != nil {
return nil, fmt.Errorf("bad hash: %w", err)
}
return hash, nil
}
// verifyManifest checks the manifest signature of the file's block
// hashes against the keys of signer, or the node key if it is empty.
func (r *blockReader) verifyManifest(hashes [][]byte, manifest, signer string) error {
verifyKR, err := r.c.verifyKeys(r.ctx, signer, r.n.keys.keyRing)
if err != nil {
return fmt.Errorf("verify manifest: %w", err)
}
err = verifyManifest(hashes, manifest, verifyKR, r.c.s.serverTime())
if err == nil {
return nil
}
err = fmt.Errorf("%w: %w", errIntegrity, err)
if !r.c.opt.IgnoreSignatureErrors {
return fmt.Errorf("%w: %s", err, signatureHint)
}
fs.Errorf(nil, "protondrive: %q (ID %s): %v", r.n.Name, r.n.ID, err)
return nil
}
// fetch queues block b, whose SHA-256 is hash, for the reader and
// fetches it in the background. It returns false if the download is
// cancelled first.
func (r *blockReader) fetch(b api.Block, hash []byte) bool {
slot := make(chan blockResult, 1)
select {
case r.slots <- slot:
case <-r.ctx.Done():
return false
}
r.wg.Add(1)
go func() {
defer r.wg.Done()
slot <- r.fetchBlock(b, hash)
}()
return true
}
// fetchBlock downloads and decrypts block b, whose SHA-256 is hash,
// requesting a new URL after a 404 (once) or a hash mismatch.
func (r *blockReader) fetchBlock(b api.Block, hash []byte) blockResult {
index := b.Index
refetched := false
for attempt := 1; ; {
plain, err := r.fetchOnce(b, hash)
var apiErr *api.Error
switch {
case err == nil:
return blockResult{index: index, hash: hash, plain: plain}
case r.ctx.Err() != nil:
return blockResult{index: index, err: r.ctx.Err()}
case errors.As(err, &apiErr) && apiErr.Status == http.StatusNotFound && !refetched:
// The URL may have expired.
refetched = true
case errors.Is(err, errBlockHash) && attempt < blockAttempts:
// The bytes may have been damaged on the way.
attempt++
default:
return blockResult{index: index, err: fmt.Errorf("block %d: %w", index, err)}
}
fs.Debugf(nil, "protondrive: %q (ID %s): block %d: %v: retrying with a new URL", r.n.Name, r.n.ID, index, err)
// hash stays the listed one, which the manifest covers.
if b, err = r.c.blockURL(r.ctx, r.n, index); err != nil {
return blockResult{index: index, err: fmt.Errorf("block %d: %w", index, err)}
}
if relisted, err := decodeBlockHash(b.Hash); err != nil || !bytes.Equal(relisted, hash) {
return blockResult{index: index, err: fmt.Errorf("block %d: %w: listed again with another hash", index, errIntegrity)}
}
}
}
// fetchOnce downloads block b and checks it against hash and its
// claimed size, returning it decrypted.
func (r *blockReader) fetchOnce(b api.Block, hash []byte) (*pool.RW, error) {
enc, sum, err := r.c.downloadBlock(r.ctx, b.BareURL, b.Token)
if err != nil {
return nil, err
}
defer func() { _ = enc.Close() }()
if !bytes.Equal(sum, hash) {
return nil, errBlockHash
}
dec, err := blockDecrypter(enc, r.sk)
if err != nil {
return nil, err
}
plain := pool.NewRW(pool.Global())
size, err := plain.ReadFrom(io.LimitReader(dec, maxBlockSize+1))
switch {
case err != nil:
err = fmt.Errorf("decrypt block: %w", err)
case size > maxBlockSize:
err = fmt.Errorf("block decrypts to more than %d bytes", maxBlockSize)
case r.sizes != nil && size != r.sizes[b.Index-1]:
err = fmt.Errorf("%w: block is %d bytes, the file records %d", errIntegrity, size, r.sizes[b.Index-1])
}
if err != nil {
_ = plain.Close()
return nil, err
}
return plain, nil
}
// Read reads the range of the file.
func (r *blockReader) Read(p []byte) (int, error) {
r.mu.Lock()
defer r.mu.Unlock()
if len(p) == 0 {
return 0, nil
}
for {
switch {
case r.closed:
return 0, errDownloadClosed
case r.err != nil:
return 0, r.err
case r.remaining == 0:
return 0, io.EOF
}
if r.cur == nil {
if err := r.next(); err != nil {
r.err = err
r.cancel()
}
continue
}
if r.remaining > 0 && int64(len(p)) > r.remaining {
p = p[:r.remaining]
}
n, err := r.cur.Read(p)
if n > 0 {
if r.remaining > 0 {
r.remaining -= int64(n)
}
return n, nil
}
if err != nil && err != io.EOF {
r.err = err
continue
}
_ = r.cur.Close()
r.cur = nil
}
}
// next makes the next block holding bytes of the range current,
// returning io.EOF at the end.
func (r *blockReader) next() error {
for {
var slot chan blockResult
select {
case slot = <-r.slots:
case <-r.ctx.Done():
return r.ctx.Err()
}
res := <-slot
switch {
case res.err != nil && r.ctx.Err() != nil:
return r.ctx.Err()
case res.err != nil:
return res.err
case res.end:
return io.EOF
}
if r.sizes == nil {
if err := r.c.learnt.learn(r.n.RevisionID, res.index, res.hash, res.plain.Size()); err != nil {
_ = res.plain.Close()
return err
}
}
if size := res.plain.Size(); r.skip > 0 && r.skip >= size {
r.skip -= size
_ = res.plain.Close()
continue
}
if r.skip > 0 {
if _, err := res.plain.Seek(r.skip, io.SeekStart); err != nil {
_ = res.plain.Close()
return err
}
r.skip = 0
}
r.cur = res.plain
return nil
}
}
// Close stops the download and frees its buffers. It may be called
// more than once.
func (r *blockReader) Close() error {
r.cancel()
r.mu.Lock()
defer r.mu.Unlock()
if r.closed {
return nil
}
r.closed = true
r.wg.Wait()
if r.cur != nil {
_ = r.cur.Close()
r.cur = nil
}
// Every queued slot has its result now.
for {
select {
case slot := <-r.slots:
if res := <-slot; res.plain != nil {
_ = res.plain.Close()
}
default:
return nil
}
}
}
// check interfaces
var _ io.ReadCloser = (*blockReader)(nil)
const (
// learntRevisions and learntBlocks bound the block sizes a client
// learns: of at most this many revisions, and this many blocks in
// all, about 2.5 MiB.
learntRevisions = 16
learntBlocks = 1 << 16
)
// learntBlock is the plaintext size of a block read, with its hash.
type learntBlock struct {
hash [sha256.Size]byte
size int64
}
// blockSizeCache holds the plaintext sizes of the blocks, from the
// first on, read of revisions which don't record their block sizes, so
// a later ranged read, as each chunk of a multi-thread copy is, can
// start at the block holding its offset. The zero value is empty.
type blockSizeCache struct {
mu sync.Mutex
revs map[string][]learntBlock // by revision ID
order []string // revision IDs, oldest first
blocks int // learnt in all
}
// locate returns where a read of limit bytes (to the end if negative)
// from offset of revision id starts, as for blockReader: the first
// block, the bytes of it to skip, the last block or -1 if not known,
// and the hash the first block had when its size was learnt, or nil if
// it wasn't.
func (b *blockSizeCache) locate(id string, offset, limit int64) (first int, skip int64, last int, startHash []byte) {
b.mu.Lock()
defer b.mu.Unlock()
blocks := b.revs[id]
first, skip, last = len(blocks)+1, offset, -1
for i, lb := range blocks {
if skip < lb.size {
first = i + 1
startHash = slices.Clone(lb.hash[:])
break
}
skip -= lb.size
}
if first > len(blocks) || limit <= 0 {
return first, skip, last, startHash
}
end := skip + limit - 1
for i := first - 1; i < len(blocks); i++ {
if end < blocks[i].size {
return first, skip, i + 1, startHash
}
end -= blocks[i].size
}
return first, skip, last, startHash
}
// learn records that block index of revision id, whose SHA-256 is hash,
// is size bytes. A block learnt before with another hash or size is
// errIntegrity: the server listed two blocks for one place.
func (b *blockSizeCache) learn(id string, index int, hash []byte, size int64) error {
b.mu.Lock()
defer b.mu.Unlock()
blocks, ok := b.revs[id]
if index <= len(blocks) {
if lb := blocks[index-1]; !bytes.Equal(lb.hash[:], hash) || lb.size != size {
return fmt.Errorf("%w: block %d listed with another hash than when it was read before", errIntegrity, index)
}
return nil
}
if index != len(blocks)+1 || len(hash) != sha256.Size {
return nil
}
if !ok {
if b.revs == nil {
b.revs = make(map[string][]learntBlock)
}
for len(b.order) >= learntRevisions {
b.forgetOldest()
}
b.order = append(b.order, id)
}
for b.blocks >= learntBlocks && len(b.order) > 1 && b.order[0] != id {
b.forgetOldest()
}
if b.blocks >= learntBlocks {
return nil
}
lb := learntBlock{size: size}
copy(lb.hash[:], hash)
b.revs[id] = append(blocks, lb)
b.blocks++
return nil
}
// forgetOldest drops the revision learnt first.
func (b *blockSizeCache) forgetOldest() {
b.blocks -= len(b.revs[b.order[0]])
delete(b.revs, b.order[0])
b.order = b.order[1:]
}
@@ -0,0 +1,883 @@
package protondrive
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"io"
"math"
"math/rand/v2"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/ProtonMail/go-crypto/openpgp/packet"
"github.com/rclone/rclone/backend/protondrive/protontest"
"github.com/rclone/rclone/lib/pacer"
"github.com/rclone/rclone/lib/pool"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
storagePattern = "GET /storage/blocks/{id}"
revisionPattern = "GET /drive/v2/volumes/{volumeID}/files/{linkID}/revisions/{revisionID}"
// bigSize is the size of a file of three blocks, the last partial.
bigSize = 9<<20 + 123
)
// testContent returns n bytes of reproducible random content.
func testContent(n int) []byte {
r := rand.New(rand.NewPCG(uint64(n), 1))
b := make([]byte, n)
for i := range b {
b[i] = byte(r.Uint32())
}
return b
}
// seedFile seeds file name with content in the root of u and returns
// its node as c reads it.
func seedFile(t *testing.T, c *client, u *protontest.User, name string, content []byte) *node {
t.Helper()
return getTestNode(t, c, u.SeedFile(u.RootID(), name, content, time.Now()))
}
// getTestNode returns node id as c reads it.
func getTestNode(t *testing.T, c *client, id string) *node {
t.Helper()
n, err := c.getNode(context.Background(), id)
require.NoError(t, err)
return n
}
// withoutBlockSizes returns a copy of n whose extended attributes
// don't record its block sizes, as some old clients wrote them.
func withoutBlockSizes(n *node) *node {
cp := *n
cp.X = nil
return &cp
}
// readRange opens n at offset for limit bytes and reads it to the end.
func readRange(t *testing.T, c *client, n *node, offset, limit int64) ([]byte, error) {
t.Helper()
rc, err := c.open(context.Background(), n, offset, limit)
if err != nil {
return nil, err
}
data, err := io.ReadAll(rc)
require.NoError(t, rc.Close())
return data, err
}
// roundTripFunc is an http.RoundTripper calling itself.
type roundTripFunc func(*http.Request) (*http.Response, error)
// RoundTrip calls f.
func (f roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return f(r)
}
// newTestClientTransport returns a client for a new session of u whose
// requests go through the transport wrap returns.
func newTestClientTransport(t testing.TB, srv *protontest.Server, u *protontest.User, opt clientOptions, wrap func(next http.RoundTripper) http.RoundTripper) *client {
t.Helper()
base := newAccountSession(srv, u)
s := newSession(&http.Client{Transport: wrap(srv.Client().Transport)}, srv.URL(), testAppVersion, base.store)
c, err := newClient(context.Background(), s, opt)
require.NoError(t, err)
t.Cleanup(c.close)
return c
}
// leakBaseline returns the goroutine count and pool buffers in use with
// no idle connections, for requireNoLeaks.
func leakBaseline(srv *protontest.Server) (goroutines, buffers int) {
srv.Client().CloseIdleConnections()
return runtime.NumGoroutine(), pool.Global().InUse()
}
// requireNoLeaks fails the test unless the goroutine count returns to
// goroutines within 2s and the pool buffers in use are back to buffers.
func requireNoLeaks(t *testing.T, srv *protontest.Server, goroutines, buffers int) {
t.Helper()
require.Eventually(t, func() bool {
srv.Client().CloseIdleConnections()
return runtime.NumGoroutine() <= goroutines
}, 2*time.Second, 20*time.Millisecond, "goroutines left running")
assert.Equal(t, buffers, pool.Global().InUse(), "pool buffers not returned")
}
func TestDownloadSizes(t *testing.T) {
t.Parallel()
c, _, u := newTestClient(t)
for _, size := range []int{0, 1, 4<<20 - 1, 4 << 20, 4<<20 + 1, 8 << 20, bigSize} {
t.Run(strconv.Itoa(size), func(t *testing.T) {
content := testContent(size)
n := seedFile(t, c, u, "f"+strconv.Itoa(size), content)
data, err := readRange(t, c, n, 0, -1)
require.NoError(t, err)
assert.True(t, bytes.Equal(content, data), "content differs")
})
}
}
func TestDownloadRanges(t *testing.T) {
t.Parallel()
c, srv, u := newTestClient(t)
content := testContent(bigSize)
claimed := seedFile(t, c, u, "big", content)
unrecorded := withoutBlockSizes(claimed)
for _, tc := range []struct {
offset, limit int64
blocks int // blocks fetched with the block sizes claimed
}{
{0, -1, 3},
{0, 1, 1},
{0, 0, 0},
{4<<20 - 1, 2, 2},
{4 << 20, -1, 2},
{bigSize - 1, -1, 1},
{bigSize, -1, 0},
{8 << 20, 100, 1},
{bigSize - 10, 100, 1},
} {
want := content[tc.offset:]
if tc.limit >= 0 {
want = want[:min(tc.limit, int64(len(want)))]
}
for _, v := range []struct {
name string
n *node
}{{"claimed", claimed}, {"unrecorded", unrecorded}} {
t.Run(v.name+"/"+strconv.FormatInt(tc.offset, 10)+"+"+strconv.FormatInt(tc.limit, 10), func(t *testing.T) {
srv.ResetRequests()
data, err := readRange(t, c, v.n, tc.offset, tc.limit)
require.NoError(t, err)
assert.True(t, bytes.Equal(want, data), "got %d bytes, want %d", len(data), len(want))
fetched := srv.Requests()[storagePattern]
if v.n == claimed {
assert.Equal(t, tc.blocks, fetched, "blocks fetched")
return
}
// The block sizes learnt by the first, full, read
// place the range; blocks past it may be fetched ahead
// before the reader is closed.
assert.GreaterOrEqual(t, fetched, tc.blocks, "blocks fetched")
assert.LessOrEqual(t, fetched, 3, "blocks fetched")
})
}
}
}
func TestDownloadRangesUnrecordedBlockSizes(t *testing.T) {
t.Parallel()
c, _, u := newTestClient(t)
const size = 6<<20 + 5
content := testContent(size)
// Blocks of 1 MiB, which a reader assuming 4 MiB blocks would
// place wrongly.
n := getTestNode(t, c, u.SeedFileInBlocks(u.RootID(), "small-blocks", content, 1<<20))
require.Nil(t, n.X.BlockSizes)
for _, tc := range []struct{ offset, limit int64 }{
{0, -1},
{0, 3 << 20},
{10, 100},
{1<<20 - 3, 10},
{4<<20 + 10, 100},
{4<<20 + 10, -1},
{2<<20 + 7, 3 << 20},
{size - 1, -1},
{size, -1},
} {
t.Run(strconv.FormatInt(tc.offset, 10)+"+"+strconv.FormatInt(tc.limit, 10), func(t *testing.T) {
want := content[tc.offset:]
if tc.limit >= 0 {
want = want[:min(tc.limit, int64(len(want)))]
}
data, err := readRange(t, c, n, tc.offset, tc.limit)
require.NoError(t, err)
assert.True(t, bytes.Equal(want, data), "got %d bytes, want %d", len(data), len(want))
})
}
}
func TestDownloadChunksUnrecordedBlockSizes(t *testing.T) {
t.Parallel()
c, srv, u := newTestClient(t)
const (
blocks = 32
chunk = 4 << 20
blockBytes = 1 << 20
)
content := testContent(blocks * blockBytes)
id := u.SeedFileInBlocks(u.RootID(), "chunks", content, blockBytes)
n := getTestNode(t, c, id)
require.Nil(t, n.X.BlockSizes)
// A multi-thread copy opens each chunk as a range of its own.
srv.ResetRequests()
for offset := int64(0); offset < int64(len(content)); offset += chunk {
data, err := readRange(t, c, n, offset, chunk)
require.NoError(t, err)
require.True(t, bytes.Equal(content[offset:offset+chunk], data), "chunk at %d", offset)
}
fetched := srv.Requests()[storagePattern]
t.Logf("%d blocks fetched for %d chunks of a file of %d blocks", fetched, len(content)/chunk, blocks)
// Reading each chunk from the first block would fetch about
// blocks^2/(2*blocks per chunk) = 128; each chunk may fetch up to
// downloadAhead blocks past its end.
assert.LessOrEqual(t, fetched, blocks+len(content)/chunk*downloadAhead, "chunks read from the first block")
// Once the sizes are known, a range fetches only its own blocks.
srv.ResetRequests()
data, err := readRange(t, c, n, 10*blockBytes+5, 2*blockBytes)
require.NoError(t, err)
assert.Equal(t, content[10*blockBytes+5:12*blockBytes+5], data)
assert.Equal(t, 3, srv.Requests()[storagePattern], "blocks fetched")
// A block list which changed since then is an integrity error.
u.Corrupt(id, "swap-blocks")
_, err = readRange(t, c, n, 5, 10)
require.ErrorIs(t, err, errIntegrity)
assert.Contains(t, err.Error(), "listed with another hash")
}
func TestDownloadLegacyFile(t *testing.T) {
t.Parallel()
c, _, u := newTestClient(t)
content := testContent(5<<20 + 7)
n := getTestNode(t, c, u.SeedLegacyFile(u.RootID(), "legacy", content))
data, err := readRange(t, c, n, 0, -1)
require.NoError(t, err)
assert.True(t, bytes.Equal(content, data), "content differs")
data, err = readRange(t, c, n, 4<<20+3, 10)
require.NoError(t, err)
assert.Equal(t, content[4<<20+3:4<<20+13], data)
}
func TestDownloadManifestSigners(t *testing.T) {
t.Parallel()
c, srv, u := newTestClient(t)
other := srv.AddUser("other", "pass")
content := testContent(1000)
for name, id := range map[string]string{
"other user": u.SeedFileSignedBy(other, u.RootID(), "other", content, time.Now()),
"anonymous": u.SeedAnonymousFile(u.RootID(), "anonymous", content, time.Now()),
} {
t.Run(name, func(t *testing.T) {
data, err := readRange(t, c, getTestNode(t, c, id), 0, -1)
require.NoError(t, err)
assert.Equal(t, content, data)
})
}
}
func TestDownloadContentKey(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
const second = "second@proton.test"
u.AddAddress(second)
c := newTestClientFor(t, srv, u, clientOptions{})
ignoring := newTestClientFor(t, srv, u, clientOptions{IgnoreSignatureErrors: true})
before2023 := time.Date(2022, 6, 1, 0, 0, 0, 0, time.UTC)
content := testContent(1000)
for _, tc := range []struct {
name string
signer string
created time.Time
ok bool
}{
{"unsigned", "", time.Now(), false},
{"unsigned before 2023", "", before2023, false},
{"signer address", u.Email(), time.Now(), true},
{"other address before 2023", second, before2023, true},
{"other address after 2023", second, time.Now(), false},
} {
t.Run(tc.name, func(t *testing.T) {
id := u.SeedFileContentKeySignedBy(tc.signer, tc.created, u.RootID(), tc.name, content)
data, err := readRange(t, c, getTestNode(t, c, id), 0, -1)
if !tc.ok {
require.ErrorIs(t, err, errIntegrity)
assert.Contains(t, err.Error(), "ignore_signature_errors")
data, err = readRange(t, ignoring, getTestNode(t, ignoring, id), 0, -1)
}
require.NoError(t, err)
assert.Equal(t, content, data)
})
}
}
func TestDownloadNodeKeySignature(t *testing.T) {
t.Parallel()
c, srv, u := newTestClient(t)
content := testContent(1000)
id := u.SeedFile(u.RootID(), "f", content, time.Now())
u.Corrupt(id, "passphrase-signature")
n := getTestNode(t, c, id)
data, err := readRange(t, c, n, 0, -1)
require.ErrorIs(t, err, errIntegrity)
assert.Contains(t, err.Error(), "ignore_signature_errors")
assert.Empty(t, data)
_, err = readRange(t, c, n, 10, 10)
require.ErrorIs(t, err, errIntegrity, "a ranged read")
ignoring := newTestClientFor(t, srv, u, clientOptions{IgnoreSignatureErrors: true})
data, err = readRange(t, ignoring, getTestNode(t, ignoring, id), 0, -1)
require.NoError(t, err)
assert.Equal(t, content, data)
}
func TestDownloadBlockHashMismatch(t *testing.T) {
t.Parallel()
c, srv, u := newTestClient(t)
n := seedFile(t, c, u, "f", testContent(1<<20))
u.Corrupt(n.ID, "block-hash")
srv.ResetRequests()
data, err := readRange(t, c, n, 0, -1)
require.ErrorIs(t, err, errBlockHash)
assert.Empty(t, data)
assert.Equal(t, 3, srv.Requests()[storagePattern], "block attempts")
// The listing page, the empty page ending the list and a new URL
// for each retry.
assert.Equal(t, 4, srv.Requests()[revisionPattern])
}
func TestDownloadSwappedBlocks(t *testing.T) {
t.Parallel()
c, _, u := newTestClient(t)
n := seedFile(t, c, u, "f", testContent(bigSize))
u.Corrupt(n.ID, "swap-blocks")
_, err := readRange(t, c, n, 0, -1)
require.ErrorIs(t, err, errIntegrity)
}
func TestDownloadTruncatedBlocks(t *testing.T) {
t.Parallel()
c, _, u := newTestClient(t)
n := seedFile(t, c, u, "f", testContent(bigSize))
u.Corrupt(n.ID, "truncate-blocks")
_, err := readRange(t, c, n, 0, -1)
require.ErrorIs(t, err, errIntegrity)
// Without block sizes, only the manifest shows the loss.
_, err = readRange(t, c, withoutBlockSizes(n), 0, -1)
require.ErrorIs(t, err, errIntegrity)
}
func TestDownloadBadManifest(t *testing.T) {
t.Parallel()
c, srv, u := newTestClient(t)
content := testContent(bigSize)
n := seedFile(t, c, u, "f", content)
u.Corrupt(n.ID, "manifest")
data, err := readRange(t, c, n, 0, -1)
require.ErrorIs(t, err, errIntegrity)
assert.Contains(t, err.Error(), "ignore_signature_errors")
assert.Empty(t, data, "no byte comes before the verdict")
// A ranged read can't check the manifest.
data, err = readRange(t, c, n, 0, 100)
require.NoError(t, err)
assert.Equal(t, content[:100], data)
ignoring := newTestClientFor(t, srv, u, clientOptions{IgnoreSignatureErrors: true})
data, err = readRange(t, ignoring, getTestNode(t, ignoring, n.ID), 0, -1)
require.NoError(t, err)
assert.True(t, bytes.Equal(content, data), "content differs")
}
func TestDownloadEmptyFileBadManifest(t *testing.T) {
t.Parallel()
c, _, u := newTestClient(t)
n := seedFile(t, c, u, "empty", nil)
u.Corrupt(n.ID, "manifest")
_, err := readRange(t, c, n, 0, -1)
require.ErrorIs(t, err, errIntegrity)
}
func TestDownloadStorage404Refetch(t *testing.T) {
t.Parallel()
c, srv, u := newTestClient(t)
content := testContent(1 << 20)
n := seedFile(t, c, u, "f", content)
srv.Inject(protontest.Fault{Pattern: "/storage/blocks/{id}", Status: http.StatusNotFound, Times: 1})
srv.ResetRequests()
data, err := readRange(t, c, n, 0, -1)
require.NoError(t, err)
assert.Equal(t, content, data)
assert.Equal(t, 2, srv.Requests()[storagePattern])
// The listing page, the new URL and the empty page ending the list.
assert.Equal(t, 3, srv.Requests()[revisionPattern])
// The URL is requested again only once.
srv.Inject(protontest.Fault{Pattern: "/storage/blocks/{id}", Status: http.StatusNotFound})
srv.ResetRequests()
_, err = readRange(t, c, n, 0, -1)
require.Error(t, err)
assert.Equal(t, 2, srv.Requests()[storagePattern])
}
func TestDownloadRefetchOtherHash(t *testing.T) {
t.Parallel()
c, srv, u := newTestClient(t)
n := seedFile(t, c, u, "f", testContent(1<<20))
// The block's URL expires, and the server lists it again with
// another block, which the checked manifest doesn't cover. The
// delay is the time to corrupt it in, which a loaded machine needs.
srv.Inject(protontest.Fault{Pattern: "/storage/blocks/{id}", Status: http.StatusNotFound, Times: 1, Delay: 2 * time.Second})
done := make(chan struct{})
var data []byte
var err error
go func() {
defer close(done)
data, err = readRange(t, c, n, 0, -1)
}()
require.Eventually(t, func() bool { return srv.Requests()[storagePattern] == 1 }, 5*time.Second, 5*time.Millisecond)
u.Corrupt(n.ID, "foreign-block")
<-done
require.ErrorIs(t, err, errIntegrity)
assert.Contains(t, err.Error(), "listed again with another hash")
assert.Empty(t, data)
}
func TestDownloadRetries5xx(t *testing.T) {
t.Parallel()
c, srv, u := newTestClient(t)
content := testContent(1 << 20)
n := seedFile(t, c, u, "f", content)
srv.Inject(protontest.Fault{Pattern: "/storage/blocks/{id}", Status: http.StatusServiceUnavailable, Times: 2})
srv.Inject(protontest.Fault{Pattern: "/drive/v2/volumes/{volumeID}/files/{linkID}/revisions/{revisionID}", Status: http.StatusBadGateway, Times: 1})
srv.ResetRequests()
data, err := readRange(t, c, n, 0, -1)
require.NoError(t, err)
assert.Equal(t, content, data)
assert.Equal(t, 3, srv.Requests()[storagePattern])
}
func TestDownloadStorageRetryAfter(t *testing.T) {
t.Parallel()
c, srv, u := newTestClient(t)
content := testContent(1000)
n := seedFile(t, c, u, "f", content)
srv.Inject(protontest.Fault{Pattern: "/storage/blocks/{id}", Status: http.StatusTooManyRequests, RetryAfter: 1, Times: 1})
start := time.Now()
data, err := readRange(t, c, n, 0, -1)
require.NoError(t, err)
assert.Equal(t, content, data)
assert.GreaterOrEqual(t, time.Since(start), time.Second)
}
func TestDownloadStorageHeaders(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
var (
mu sync.Mutex
headers []http.Header
)
c := newTestClientTransport(t, srv, u, clientOptions{}, func(next http.RoundTripper) http.RoundTripper {
return roundTripFunc(func(r *http.Request) (*http.Response, error) {
if strings.HasPrefix(r.URL.Path, "/storage/") {
mu.Lock()
headers = append(headers, r.Header.Clone())
mu.Unlock()
}
return next.RoundTrip(r)
})
})
n := seedFile(t, c, u, "f", testContent(4<<20+1))
_, err := readRange(t, c, n, 0, -1)
require.NoError(t, err)
require.Len(t, headers, 2)
for _, h := range headers {
assert.Empty(t, h.Get("Authorization"))
assert.Empty(t, h.Get("x-pm-uid"))
assert.NotEmpty(t, h.Get("pm-storage-token"))
assert.Equal(t, testAppVersion, h.Get("x-pm-appversion"))
}
// Block URLs on another host, as the real storage host is, get the
// same headers. The other host passes requests on to the fake.
var other []http.Header
storage := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
other = append(other, r.Header.Clone())
mu.Unlock()
req, err := http.NewRequestWithContext(r.Context(), "GET", srv.URL()+r.URL.Path, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
for _, k := range []string{"pm-storage-token", "x-pm-appversion"} {
req.Header.Set(k, r.Header.Get(k))
}
resp, err := srv.Client().Do(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
defer func() { _ = resp.Body.Close() }()
w.WriteHeader(resp.StatusCode)
_, _ = io.Copy(w, resp.Body)
}))
t.Cleanup(storage.Close)
require.NotEqual(t, srv.URL(), storage.URL)
moved := newTestClientTransport(t, srv, u, clientOptions{}, func(next http.RoundTripper) http.RoundTripper {
return roundTripFunc(func(r *http.Request) (*http.Response, error) {
resp, err := next.RoundTrip(r)
if err != nil || !strings.Contains(r.URL.Path, "/revisions/") {
return resp, err
}
body, err := io.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
return nil, err
}
body = bytes.ReplaceAll(body, []byte(srv.URL()+"/storage/"), []byte(storage.URL+"/storage/"))
resp.Body = io.NopCloser(bytes.NewReader(body))
resp.ContentLength = int64(len(body))
resp.Header.Del("Content-Length")
return resp, nil
})
})
_, err = readRange(t, moved, getTestNode(t, moved, n.ID), 0, -1)
require.NoError(t, err)
require.Len(t, other, 2)
for _, h := range other {
assert.Empty(t, h.Get("Authorization"))
assert.Empty(t, h.Get("x-pm-uid"))
assert.NotEmpty(t, h.Get("pm-storage-token"))
}
}
func TestDownloadBlockTooLarge(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
var fetches atomic.Int32
c := newTestClientTransport(t, srv, u, clientOptions{}, func(next http.RoundTripper) http.RoundTripper {
return roundTripFunc(func(r *http.Request) (*http.Response, error) {
if !strings.HasPrefix(r.URL.Path, "/storage/") {
return next.RoundTrip(r)
}
fetches.Add(1)
// An endless body of unknown length.
return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{},
Body: io.NopCloser(zeroReader{}),
ContentLength: -1,
Request: r,
}, nil
})
})
n := seedFile(t, c, u, "f", testContent(1000))
_, err := readRange(t, c, n, 0, -1)
require.ErrorContains(t, err, "larger than")
assert.NotErrorIs(t, err, errBlockHash)
assert.Equal(t, int32(1), fetches.Load(), "an oversized block is not fetched again")
}
// zeroReader reads zeros forever.
type zeroReader struct{}
// Read fills p with zeros.
func (zeroReader) Read(p []byte) (int, error) {
clear(p)
return len(p), nil
}
// endlessBlockList returns a transport wrapper whose revision pages are
// all full of copies of the file's first block, renumbered, counting
// the pages in pages.
func endlessBlockList(pages *atomic.Int32) func(next http.RoundTripper) http.RoundTripper {
var (
mu sync.Mutex
first map[string]any
)
return func(next http.RoundTripper) http.RoundTripper {
return roundTripFunc(func(r *http.Request) (*http.Response, error) {
resp, err := next.RoundTrip(r)
if err != nil || !strings.Contains(r.URL.Path, "/revisions/") {
return resp, err
}
pages.Add(1)
var body map[string]any
err = json.NewDecoder(resp.Body).Decode(&body)
_ = resp.Body.Close()
if err != nil {
return nil, err
}
rev := body["Revision"].(map[string]any)
mu.Lock()
if blocks := rev["Blocks"].([]any); first == nil && len(blocks) > 0 {
first = blocks[0].(map[string]any)
}
from, _ := strconv.Atoi(r.URL.Query().Get("FromBlockIndex"))
size, _ := strconv.Atoi(r.URL.Query().Get("PageSize"))
var blocks []any
for i := range size {
b := make(map[string]any, len(first))
for k, v := range first {
b[k] = v
}
b["Index"] = from + i
blocks = append(blocks, b)
}
mu.Unlock()
rev["Blocks"] = blocks
data, err := json.Marshal(body)
if err != nil {
return nil, err
}
resp.Body = io.NopCloser(bytes.NewReader(data))
resp.ContentLength = int64(len(data))
resp.Header.Del("Content-Length")
return resp, nil
})
}
}
func TestDownloadEndlessBlockList(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
var pages atomic.Int32
c := newTestClientTransport(t, srv, u, clientOptions{EnableCaching: true}, endlessBlockList(&pages))
n := seedFile(t, c, u, "f", testContent(4<<20+1))
for name, n := range map[string]*node{"claimed": n, "unrecorded": withoutBlockSizes(n)} {
t.Run(name, func(t *testing.T) {
pages.Store(0)
data, err := readRange(t, c, n, 0, -1)
require.ErrorIs(t, err, errIntegrity)
assert.LessOrEqual(t, len(data), 5*(4<<20))
assert.LessOrEqual(t, pages.Load(), int32(2))
})
}
}
func TestDownloadBlockCap(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
var pages atomic.Int32
c := newTestClientTransport(t, srv, u, clientOptions{EnableCaching: true}, endlessBlockList(&pages))
c.maxFileBlocks = 45
n := seedFile(t, c, u, "f", testContent(4<<20+1))
// The encrypted size, which nothing signs, claims room for many
// more blocks than the cap.
huge := withoutBlockSizes(n)
huge.EncryptedSize = 1 << 50
data, err := readRange(t, c, huge, 0, -1)
require.ErrorIs(t, err, errIntegrity)
assert.Contains(t, err.Error(), "more than 45 blocks")
assert.Empty(t, data)
assert.LessOrEqual(t, pages.Load(), int32(3), "listed past the cap")
// Recorded block sizes beyond the cap fail before any request.
claims := *n
x := *n.X
x.BlockSizes = make([]int64, 46)
claims.X = &x
pages.Store(0)
_, err = readRange(t, c, &claims, 0, -1)
require.ErrorIs(t, err, errIntegrity)
assert.Zero(t, pages.Load())
}
func TestDownloadCloseEarly(t *testing.T) {
c, srv, u := newTestClient(t)
n := seedFile(t, c, u, "f", testContent(8<<20))
goroutines, buffers := leakBaseline(srv)
rc, err := c.open(context.Background(), n, 0, -1)
require.NoError(t, err)
buf := make([]byte, 10)
_, err = io.ReadFull(rc, buf)
require.NoError(t, err)
require.NoError(t, rc.Close())
require.NoError(t, rc.Close())
_, err = rc.Read(buf)
require.Error(t, err)
requireNoLeaks(t, srv, goroutines, buffers)
}
func TestDownloadContextCancel(t *testing.T) {
c, srv, u := newTestClient(t)
n := seedFile(t, c, u, "f", testContent(8<<20))
// One of the blocks takes far longer than the test.
srv.Inject(protontest.Fault{Pattern: "/storage/blocks/{id}", Skip: 1, Times: 1, Delay: time.Minute})
goroutines, buffers := leakBaseline(srv)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
rc, err := c.open(ctx, n, 0, -1)
require.NoError(t, err)
time.AfterFunc(200*time.Millisecond, cancel)
start := time.Now()
data, err := io.ReadAll(rc)
require.ErrorIs(t, err, context.Canceled)
assert.Less(t, len(data), 8<<20)
assert.Less(t, time.Since(start), 10*time.Second)
require.NoError(t, rc.Close())
requireNoLeaks(t, srv, goroutines, buffers)
}
func TestDownloadHostileBlockSizes(t *testing.T) {
t.Parallel()
c, _, u := newTestClient(t)
small := testContent(1000)
big := testContent(4<<20 + 1)
for _, tc := range []struct {
name string
content []byte
sizes []int64
}{
{"overflowing", small, []int64{math.MaxInt64, 1}},
{"above the cap", small, []int64{maxBlockSize + 1}},
{"sum too small", big, []int64{1000}},
{"too few blocks", big, []int64{4 << 20}},
} {
t.Run(tc.name, func(t *testing.T) {
n := getTestNode(t, c, u.SeedFileClaimingBlockSizes(u.RootID(), tc.name, tc.content, tc.sizes))
require.Equal(t, tc.sizes, n.X.BlockSizes)
_, err := readRange(t, c, n, 0, -1)
require.ErrorIs(t, err, errIntegrity)
})
}
}
// frozenFileDir holds files the old library (go-proton-api v1.0.4 and
// Proton-API-Bridge v1.0.5) wrote, as the server stored them, one
// directory per format: "legacy" (v3 PKESK content key, SEIPDv1 blocks)
// and "v6" (v6 PKESK content key, SEIPDv2 blocks). The old library only
// writes v6 content as a revision of a file another client created, so
// in "v6" the node key and content key packet with its signature are
// SDK-style stand-ins made with plain gopenpgp.
const frozenFileDir = "testdata/oldlib/file"
// frozenFile is a file a client holding Keys stored, with what it
// encrypted. In a directory, file.json holds it but Stored, which is
// in link.json, revision.json and block-1.bin, block-2.bin...
type frozenFile struct {
Comment string
Email string // address which signed everything, the owner's primary address
Keys protontest.FixedKeys
Name string
ModTime string // as the writer recorded it in the extended attributes
Content []byte
Stored protontest.StoredFile `json:"-"`
}
// frozenFileParts returns the names of the files of f in a directory
// and the values they hold.
func frozenFileParts(f *frozenFile) map[string]any {
return map[string]any{"file.json": f, "link.json": &f.Stored.Link, "revision.json": &f.Stored.Revision}
}
// blockFileName returns the name of the file holding block i, from 0.
func blockFileName(i int) string {
return "block-" + strconv.Itoa(i+1) + ".bin"
}
// loadFrozenFile returns the file in dir.
func loadFrozenFile(t testing.TB, dir string) frozenFile {
t.Helper()
var f frozenFile
for name, v := range frozenFileParts(&f) {
data, err := os.ReadFile(filepath.Join(dir, name))
require.NoError(t, err)
require.NoError(t, json.Unmarshal(data, v), name)
}
for i := range f.Stored.Revision.Blocks {
data, err := os.ReadFile(filepath.Join(dir, blockFileName(i)))
require.NoError(t, err)
f.Stored.Revision.Blocks[i].Data = data
}
return f
}
// checkFrozenFile stores f for a user holding its keys and checks that
// the client reads its metadata, verifying every signature, and
// downloads its content.
func checkFrozenFile(t *testing.T, f frozenFile) {
t.Helper()
srv := protontest.New(t)
u := srv.AddUserWithKeys(f.Email, "pass", f.Keys)
c := newTestClientFor(t, srv, u, clientOptions{})
n := getTestNode(t, c, u.SeedStoredFile(u.RootID(), f.Stored))
assert.Equal(t, f.Name, n.Name)
require.NotNil(t, n.X, "extended attributes")
modTime, err := time.Parse("2006-01-02T15:04:05-0700", f.ModTime)
require.NoError(t, err)
assert.True(t, modTime.Equal(n.modTime()), "modification time %v, want %v", n.modTime(), modTime)
assert.Equal(t, int64(len(f.Content)), n.size(true))
assert.Equal(t, sha1Of(f.Content), n.sha1())
data, err := readRange(t, c, n, 0, -1)
require.NoError(t, err)
assert.True(t, bytes.Equal(f.Content, data), "content differs: %d bytes, want %d", len(data), len(f.Content))
assert.Empty(t, c.reported, "problems reported")
}
func TestFrozenFileFixture(t *testing.T) {
for format, want := range map[string]struct{ pkesk, seipd int }{"legacy": {3, 1}, "v6": {6, 2}} {
t.Run(format, func(t *testing.T) {
f := loadFrozenFile(t, filepath.Join(frozenFileDir, format))
// The fixture is in the format its directory names.
keyPacket, err := base64.StdEncoding.DecodeString(f.Stored.Link.ContentKeyPacket)
require.NoError(t, err)
assert.Equal(t, want.pkesk, readPackets(t, keyPacket, 1)[0].(*packet.EncryptedKey).Version, "content key packet")
require.NotEmpty(t, f.Stored.Revision.Blocks)
for i, b := range f.Stored.Revision.Blocks {
assert.Equal(t, want.seipd, readPackets(t, b.Data, 1)[0].(*packet.SymmetricallyEncrypted).Version, "block %d", i+1)
}
checkFrozenFile(t, f)
})
}
}
// TestStorageTokenNotRedirected checks a block's storage token isn't
// sent on to another host a storage request is redirected to.
func TestStorageTokenNotRedirected(t *testing.T) {
t.Parallel()
got := make(chan http.Header, 2)
other := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
got <- r.Header.Clone()
_, _ = w.Write([]byte("block"))
}))
defer other.Close()
storage := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
got <- r.Header.Clone()
http.Redirect(w, r, other.URL+"/block", http.StatusFound)
}))
defer storage.Close()
ctx := context.Background()
c := &client{
s: newSession(&http.Client{}, "http://127.0.0.1:1", testAppVersion, &memoryStore{}),
storagePacer: newDrivePacer(ctx),
downloadTokens: pacer.NewTokenDispenser(1),
}
enc, _, err := c.downloadBlock(ctx, storage.URL+"/block", "secret-token")
require.NoError(t, err)
_ = enc.Close()
assert.Equal(t, "secret-token", (<-got).Get("pm-storage-token"), "sent to the storage host")
assert.Empty(t, (<-got).Get("pm-storage-token"), "sent on to another host")
}
+574
View File
@@ -0,0 +1,574 @@
package protondrive
import (
"context"
"errors"
"fmt"
"math/rand/v2"
"slices"
"sync"
"time"
"github.com/rclone/rclone/backend/protondrive/api"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/lib/pacer"
"github.com/rclone/rclone/lib/rest"
)
const (
// minEventsInterval is the shortest time between polls of Proton's
// endpoint for events: the SDK's for the user's own volume.
minEventsInterval = 30 * time.Second
// eventsJitter is the most random time added to each wait between
// polls, as in the SDK.
eventsJitter = time.Second
// maxEventsBackoff is the longest wait between polls which fail.
maxEventsBackoff = 10 * time.Minute
// maxPendingNodes is the most nodes the poller waits to name.
maxPendingNodes = 1000
// pendingPolls is the number of polls after its change in which a
// node not cached then is named if it is cached by then.
pendingPolls = 2
// cacheIdleTime is how long the cache may go unused, with nothing
// subscribed, before the poller clears it and so stops polling.
cacheIdleTime = 10 * time.Minute
)
// errVolumeGone is returned by the events calls when the volume no
// longer exists or can't be reached: no later poll can succeed.
var errVolumeGone = errors.New("volume gone")
// eventChange is a change to a node the volume events report. Events
// name the node and its parent but not its name or type, and report its
// parent and trash state as they are when read, which may be after
// later changes.
type eventChange struct {
linkID string
parentID string // parent when read; empty if not reported
kind int // api.EventDelete etc
trashed bool // whether the node was in the trash when read
}
// staleNode is what the cache knew of a changed node before the change
// was applied.
type staleNode struct {
id string
parentID string // the cached parent; empty unless known
name string // empty unless known
isDir bool
known bool // whether the node was cached
newParentID string // the parent the change reports; empty if not reported
gone bool // whether the change deleted or trashed the node
late bool // whether the node changed in an earlier poll, before it was cached; see subscribe
}
// latestEventID returns the ID of the newest event of the volume, from
// which pollEvents reports the changes made after now. It wraps
// errVolumeGone if the volume is gone.
func (c *client) latestEventID(ctx context.Context) (string, error) {
opts := &rest.Opts{
Method: "GET",
Path: c.volumePath("/drive/volumes", "events", "latest"),
}
var resp api.LatestEventResponse
if err := c.eventsCall(ctx, opts, &resp); err != nil {
return "", eventsError("get latest event", err)
}
if resp.EventID == "" {
return "", errors.New("get latest event: no event ID in the response")
}
return resp.EventID, nil
}
// maxEventPages is the most pages of events one poll reads; the rest
// are read by the next poll.
const maxEventPages = 100
// pollEvents returns the changes after event cursor, oldest first, and
// the event ID to poll from next, reading up to maxEventPages pages.
//
// If the server no longer knows cursor it returns refresh set, no
// changes and the newest event ID: anything cached may be out of date.
// On error it returns no changes, and the poll should be retried from
// cursor. It wraps errVolumeGone if the volume is gone.
func (c *client) pollEvents(ctx context.Context, cursor string) (changes []eventChange, next string, refresh bool, err error) {
for pages := 1; ; pages++ {
opts := &rest.Opts{
Method: "GET",
Path: c.volumePath("/drive/v2/volumes", "events", cursor),
}
var page api.EventsResponse
if err := c.eventsCall(ctx, opts, &page); err != nil {
return nil, "", false, eventsError("list events", err)
}
if page.EventID == "" {
return nil, "", false, errors.New("list events: no event ID in the response")
}
if page.Refresh {
fs.Debugf(nil, "protondrive: events: event %s is no longer known: refreshing", cursor)
return nil, page.EventID, true, nil
}
fs.Debugf(nil, "protondrive: events: %d after %s (more %v)", len(page.Events), cursor, page.More)
changes = append(changes, eventChanges(page.Events)...)
// As in the SDK, a page without events ends the poll whatever
// More says, and so does one which doesn't advance.
if page.More && len(page.Events) > 0 && page.EventID == cursor {
fs.Debugf(nil, "protondrive: events: page after %s doesn't advance: its events will be read again", cursor)
}
if !page.More || len(page.Events) == 0 || page.EventID == cursor || pages >= maxEventPages {
return changes, page.EventID, false, nil
}
cursor = page.EventID
}
}
// eventsCall makes the events API call opts as apiCall does, but
// tries it only once and doesn't wait out a Retry-After: the poller
// backs off itself, and the returned error carries the Retry-After for
// pacer.IsRetryAfter.
func (c *client) eventsCall(ctx context.Context, opts *rest.Opts, response any) error {
var retryAfter time.Duration
err := c.apiPacer.CallNoRetry(func() (bool, error) {
resp, err := c.s.call(ctx, opts, nil, response)
retry, err := c.shouldRetry(ctx, resp, err)
if wait, ok := pacer.IsRetryAfter(err); ok {
// Kept from the pacer, which would hold every call of the
// client for it.
retryAfter = wait
err = errors.Unwrap(err)
}
return retry, err
})
if retryAfter > 0 {
return pacer.RetryAfterError(err, retryAfter)
}
return err
}
// eventsError returns err of the events call what, wrapping
// errVolumeGone for API code 2501.
func eventsError(what string, err error) error {
if apiErrorCode(err) == api.CodeNotExists {
return fmt.Errorf("%s: %w: %w", what, errVolumeGone, err)
}
return fmt.Errorf("%s: %w", what, err)
}
// eventChanges returns the changes events report, leaving out any
// which name no node.
func eventChanges(events []api.Event) []eventChange {
changes := make([]eventChange, 0, len(events))
for _, e := range events {
if e.Link.LinkID == "" {
fs.Debugf(nil, "protondrive: events: ignoring event %q naming no node", e.EventID)
continue
}
changes = append(changes, eventChange{
linkID: e.Link.LinkID,
parentID: e.Link.ParentLinkID,
kind: e.EventType,
trashed: e.Link.IsTrashed,
})
}
return changes
}
// applyEvents drops from the cache what changes make out of date, in
// order, and returns what it knew of each changed node before: stale[i]
// is for changes[i].
//
// A deleted node, and a folder moved or trashed, goes with everything
// cached below it. Another changed node goes with its own listing. The
// listings of the node's parents before and after the change go too.
func (c *client) applyEvents(changes []eventChange) (stale []staleNode) {
stale = make([]staleNode, 0, len(changes))
for _, ch := range changes {
s := staleNode{id: ch.linkID}
n := c.cache.peek(ch.linkID)
if n == nil && c.root != nil && ch.linkID == c.root.ID {
n = c.root
}
if n != nil {
s = staleNode{id: n.ID, parentID: n.ParentID, name: n.Name, isDir: n.IsFolder, known: true}
}
s.newParentID = ch.parentID
s.gone = ch.kind == api.EventDelete || ch.trashed
stale = append(stale, s)
// The root, the only node without a parent, can't be moved, so
// a parent reported for it leaves its descendants' paths alone.
moved := s.known && s.parentID != "" && ch.parentID != "" && ch.parentID != s.parentID
switch {
case ch.kind == api.EventDelete, s.isDir && (moved || ch.trashed):
c.cache.invalidateTree(ch.linkID)
default:
c.cache.invalidate(ch.linkID)
}
if ch.parentID != "" {
c.cache.invalidateChildren(ch.parentID)
}
}
return stale
}
// eventPoller polls the events of a client's volume and applies them
// to its cache, while the cache holds anything or anything subscribes.
//
// It polls from a timer, so no goroutine is left while it waits, and
// never more often than its interval. Polls which fail are retried
// after Fibonacci multiples of the interval, up to maxEventsBackoff.
type eventPoller struct {
c *client
floor time.Duration // shortest time between polls; 0 disables polling
jitter time.Duration // most random time added to each wait: eventsJitter, unless a test lowers it
idle time.Duration // how long the cache may go unused before it is cleared: cacheIdleTime, unless a test lowers it
runMu sync.Mutex // held by the timer's polls and by notifications, so they never overlap
pollMu sync.Mutex // held by every poll until it has updated the poller, for stop to wait for
mu sync.Mutex
interval time.Duration // time between polls asked for by setInterval
cursor string // event to poll from; empty until the start cursor is fetched
failures int // polls failed in a row
notBefore time.Time // no poll before this, as the server's Retry-After asked
timer *time.Timer // the next poll; nil if none is scheduled
running bool // whether a poll of gen is running
gen uint64 // polls of an earlier gen, from before stop, do nothing
ctx context.Context // context of the polls of gen
cancel context.CancelFunc // cancels ctx
closed bool // whether polling has stopped for good
subs map[int]func(stale []staleNode, refresh bool)
nextSub int
observer func(stale []staleNode, refresh bool) // called after subs; see observe
pending []pendingNode // nodes changed while not cached, oldest first; see subscribe
}
// pendingNode is a node which changed while it wasn't cached.
type pendingNode struct {
id string
polls int // polls left in which to look for it in the cache
}
// newEventPoller returns an idle poller of the events of c, polling no
// more often than every floor, or never if floor is 0.
func newEventPoller(c *client, floor time.Duration) *eventPoller {
return &eventPoller{
c: c,
floor: floor,
jitter: eventsJitter,
idle: cacheIdleTime,
subs: make(map[int]func([]staleNode, bool)),
}
}
// subscribe calls fn after each poll which changed anything, with what
// the cache knew of each changed node, or with refresh set if anything
// cached may have been out of date and the cache was cleared. Calls are
// made one at a time, on a goroutine of the poller's own, never on one
// which called wake or filled the cache, and fn must not call the
// poller.
//
// A node which changed while it wasn't cached, in a folder reported,
// as one being uploaded may be, is looked for in the cache in each of
// the next pendingPolls polls. If it is found it is passed again with
// late set, whether or not the poll changed anything.
//
// It keeps polling going until unsubscribe is called, once wake starts
// it. A call already started may still be made, or be running, after
// unsubscribe returns.
func (p *eventPoller) subscribe(fn func(stale []staleNode, refresh bool)) (unsubscribe func()) {
p.mu.Lock()
defer p.mu.Unlock()
id := p.nextSub
p.nextSub++
p.subs[id] = fn
return func() {
p.mu.Lock()
defer p.mu.Unlock()
delete(p.subs, id)
if len(p.subs) == 0 {
p.pending = nil
}
}
}
// addPendingLocked adds to the nodes to look for in the cache those in
// stale which weren't cached, if anything subscribes. It forgets the
// oldest beyond maxPendingNodes. p.mu must be held.
func (p *eventPoller) addPendingLocked(stale []staleNode) {
if len(p.subs) == 0 {
return
}
for _, s := range stale {
if s.known || s.gone || s.newParentID == "" {
continue
}
p.pending = slices.DeleteFunc(p.pending, func(pn pendingNode) bool { return pn.id == s.id })
p.pending = append(p.pending, pendingNode{id: s.id, polls: pendingPolls})
}
if n := len(p.pending) - maxPendingNodes; n > 0 {
p.pending = slices.Delete(p.pending, 0, n)
}
}
// resolvePendingLocked returns, with late set, the pending nodes now
// cached, and forgets them and those looked for in pendingPolls polls.
// p.mu must be held.
func (p *eventPoller) resolvePendingLocked() (late []staleNode) {
kept := p.pending[:0]
for _, pn := range p.pending {
if n := p.c.cache.peek(pn.id); n != nil {
late = append(late, staleNode{id: n.ID, parentID: n.ParentID, name: n.Name, isDir: n.IsFolder, known: true, late: true})
} else if pn.polls > 1 {
pn.polls--
kept = append(kept, pn)
}
}
clear(p.pending[len(kept):])
p.pending = kept
return late
}
// observe makes fn be called after each poll which changed anything,
// as subscribe does, after the subscribers. Unlike subscribe it doesn't
// keep polling going. It replaces any earlier fn; nil removes it.
func (p *eventPoller) observe(fn func(stale []staleNode, refresh bool)) {
p.mu.Lock()
defer p.mu.Unlock()
p.observer = fn
}
// setInterval sets the time between polls to d, if it is longer than
// the shortest the poller allows, from the next poll scheduled.
func (p *eventPoller) setInterval(d time.Duration) {
p.mu.Lock()
defer p.mu.Unlock()
p.interval = d
}
// wake starts polling if it isn't running and the cache holds anything
// or anything subscribes. The first wake fetches the start cursor
// before it returns, so every change made afterwards is seen, unless
// fetching it failed before, when it is fetched after a backoff. It
// doesn't wait for a poll or a notification.
func (p *eventPoller) wake() {
p.mu.Lock()
if p.closed || p.floor <= 0 || p.timer != nil || p.running || !p.wantedLocked() {
p.mu.Unlock()
return
}
if p.ctx == nil {
p.ctx, p.cancel = context.WithCancel(context.Background())
}
if p.cursor != "" || p.failures > 0 {
p.scheduleLocked()
p.mu.Unlock()
return
}
p.running = true
gen, ctx := p.gen, p.ctx
p.mu.Unlock()
p.poll(ctx, gen, true)
}
// stop stops polling, cancelling a poll in flight and waiting for it to
// end, but not for any notification it makes. A later wake starts
// polling again, from the cursor, after an interval.
func (p *eventPoller) stop() {
p.mu.Lock()
p.stopLocked()
p.mu.Unlock()
p.pollMu.Lock()
defer p.pollMu.Unlock()
}
// stopLocked is stop with p.mu held.
func (p *eventPoller) stopLocked() {
p.gen++
if p.timer != nil {
p.timer.Stop()
p.timer = nil
}
if p.cancel != nil {
p.cancel()
p.ctx, p.cancel = nil, nil
}
p.running = false
p.pending = nil
}
// close stops polling for good, as stop does.
func (p *eventPoller) close() {
p.mu.Lock()
p.closed = true
p.stopLocked()
p.mu.Unlock()
p.pollMu.Lock()
defer p.pollMu.Unlock()
}
// wantedLocked reports whether polling is wanted: the cache holds
// anything or anything subscribes. p.mu must be held.
func (p *eventPoller) wantedLocked() bool {
return len(p.subs) > 0 || !p.c.cache.empty()
}
// scheduleLocked schedules the next poll of the current gen and returns
// the wait. p.mu must be held.
func (p *eventPoller) scheduleLocked() time.Duration {
base := max(p.floor, p.interval)
// Fibonacci multiples of base for 1, 2, 3... failures: 1, 1, 2...
limit := max(base, maxEventsBackoff)
wait := base
for a, b, i := base, base, 1; i < p.failures && wait < limit; i++ {
a, b = b, a+b
wait = a
}
wait = min(wait, limit)
if p.jitter > 0 {
wait += rand.N(p.jitter + 1)
}
wait = max(wait, time.Until(p.notBefore))
gen := p.gen
p.timer = time.AfterFunc(wait, func() { p.run(gen) })
return wait
}
// run is the timer function of a poll scheduled in gen.
func (p *eventPoller) run(gen uint64) {
p.mu.Lock()
if gen != p.gen {
p.mu.Unlock()
return
}
p.timer = nil
if len(p.subs) == 0 && p.c.cache.unusedFor() >= p.idle {
fs.Debugf(nil, "protondrive: events: cache unused for %v: clearing it", p.idle)
p.c.cache.clear()
}
if !p.wantedLocked() {
// Idle: the cursor is kept for the next wake.
p.mu.Unlock()
return
}
p.running = true
ctx := p.ctx
p.mu.Unlock()
p.poll(ctx, gen, false)
}
// poll fetches the start cursor if there is none, else applies the
// events since the cursor, and then schedules the next poll if polling
// is still wanted. p.running must have been set in gen.
//
// From wake, inline is set: the caller may hold locks of its own (the
// dircache's, VFS's) which subscribers take, so it doesn't wait for
// runMu and leaves any notification to a goroutine of its own. Only
// the start cursor is fetched then, and no other poll of gen can run.
func (p *eventPoller) poll(ctx context.Context, gen uint64, inline bool) {
if !inline {
p.runMu.Lock()
defer p.runMu.Unlock()
}
p.pollMu.Lock()
pollDone := sync.OnceFunc(p.pollMu.Unlock)
defer pollDone()
p.mu.Lock()
cursor := p.cursor
p.mu.Unlock()
var (
changes []eventChange
next string
refresh bool
err error
)
if cursor == "" {
next, err = p.c.latestEventID(ctx)
} else {
changes, next, refresh, err = p.c.pollEvents(ctx, cursor)
}
p.mu.Lock()
if gen != p.gen {
// Stopped while polling.
p.mu.Unlock()
return
}
p.running = false
var stale []staleNode
gone := errors.Is(err, errVolumeGone)
switch {
case gone:
p.closed = true
refresh = true
p.c.cache.clear()
p.pending = nil
case err != nil:
p.failures++
if wait, ok := pacer.IsRetryAfter(err); ok {
p.notBefore = time.Now().Add(wait)
}
stale = p.resolvePendingLocked()
default:
p.failures = 0
p.cursor = next
if refresh {
p.c.cache.clear()
p.pending = nil
break
}
// Before applying changes, which may drop the nodes again.
stale = p.resolvePendingLocked()
if len(changes) > 0 {
applied := p.c.applyEvents(changes)
p.addPendingLocked(applied)
stale = append(stale, applied...)
}
}
var wait time.Duration
if !p.closed && p.wantedLocked() {
wait = p.scheduleLocked()
}
var subs []func([]staleNode, bool)
if refresh || len(stale) > 0 {
for _, fn := range p.subs {
subs = append(subs, fn)
}
if p.observer != nil {
subs = append(subs, p.observer)
}
}
p.mu.Unlock()
switch {
case gone:
fs.Errorf(nil, "protondrive: events: %v: no longer watching for changes", err)
case err != nil:
fs.Debugf(nil, "protondrive: events: %v: trying again in %v", err, wait)
}
pollDone()
notify := func() {
for _, fn := range subs {
fn(stale, refresh)
}
}
switch {
case len(subs) == 0:
case inline:
time.AfterFunc(0, func() {
p.runMu.Lock()
defer p.runMu.Unlock()
notify()
})
default:
notify()
}
}
+875
View File
@@ -0,0 +1,875 @@
package protondrive
import (
"context"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"slices"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/rclone/rclone/backend/protondrive/api"
"github.com/rclone/rclone/backend/protondrive/protontest"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/log"
"github.com/rclone/rclone/lib/random"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
latestEventRoute = "/drive/volumes/{volumeID}/events/latest"
eventsRoute = "/drive/v2/volumes/{volumeID}/events/{eventID}"
)
func TestLatestEventID(t *testing.T) {
t.Parallel()
ctx := context.Background()
c, srv, u := newTestClient(t)
start, err := c.latestEventID(ctx)
require.NoError(t, err)
require.NotEmpty(t, start)
assert.Equal(t, 1, srv.Requests()["GET "+latestEventRoute])
changes, next, refresh, err := c.pollEvents(ctx, start)
require.NoError(t, err)
assert.False(t, refresh)
assert.Empty(t, changes)
assert.Equal(t, start, next)
u.SeedFolder(u.RootID(), "dir")
latest, err := c.latestEventID(ctx)
require.NoError(t, err)
assert.NotEqual(t, start, latest)
}
func TestPollEventsPages(t *testing.T) {
t.Parallel()
ctx := context.Background()
c, srv, u := newTestClient(t)
srv.SetEventsPageSize(2)
start, err := c.latestEventID(ctx)
require.NoError(t, err)
dir := u.SeedFolder(u.RootID(), "dir")
other := u.SeedFolder(u.RootID(), "other")
file := u.SeedFile(dir, "file", []byte("x"), time.Now())
u.SetParent(file, other)
srv.InjectEvent(u, protontest.Event{Type: protontest.EventUpdateMetadata, LinkID: dir, ParentLinkID: u.RootID(), IsTrashed: true})
srv.InjectEvent(u, protontest.Event{Type: protontest.EventDelete, LinkID: dir})
srv.InjectEvent(u, protontest.Event{Type: protontest.EventUpdate, LinkID: other, ParentLinkID: u.RootID(), IsShared: true})
events := srv.Events(u)
require.Len(t, events, 7)
srv.ResetRequests()
changes, next, refresh, err := c.pollEvents(ctx, start)
require.NoError(t, err)
assert.False(t, refresh)
want := make([]eventChange, len(events))
for i, e := range events {
want[i] = eventChange{linkID: e.LinkID, parentID: e.ParentLinkID, kind: e.Type, trashed: e.IsTrashed}
}
assert.Equal(t, want, changes)
assert.Equal(t, 4, srv.Requests()["GET "+eventsRoute], "pages of 2 for 7 events")
latest, err := c.latestEventID(ctx)
require.NoError(t, err)
assert.Equal(t, latest, next)
changes, again, refresh, err := c.pollEvents(ctx, next)
require.NoError(t, err)
assert.False(t, refresh)
assert.Empty(t, changes)
assert.Equal(t, next, again)
}
func TestPollEventsLogsPages(t *testing.T) {
logs := captureLogLines(t)
ctx := context.Background()
oldLevel := log.Handler.SetLevel(slog.LevelDebug)
ci := fs.GetConfig(ctx)
oldLogLevel := ci.LogLevel
ci.LogLevel = fs.LogLevelDebug
t.Cleanup(func() {
ci.LogLevel = oldLogLevel
log.Handler.SetLevel(oldLevel)
})
c, srv, u := newTestClient(t)
srv.SetEventsPageSize(2)
start, err := c.latestEventID(ctx)
require.NoError(t, err)
for _, name := range []string{"a", "b", "c"} {
u.SeedFolder(u.RootID(), name)
}
_, next, _, err := c.pollEvents(ctx, start)
require.NoError(t, err)
assert.Len(t, logs.matching("protondrive: events: 2 after "+start+" (more true)"), 1)
assert.Len(t, logs.matching("protondrive: events: 1 after ", " (more false)"), 1)
assert.Empty(t, logs.matching("after "+next))
}
func TestPollEventsRefresh(t *testing.T) {
t.Parallel()
ctx := context.Background()
c, srv, u := newTestClient(t)
start, err := c.latestEventID(ctx)
require.NoError(t, err)
u.SeedFolder(u.RootID(), "dir")
srv.ExpireEvents(u)
changes, next, refresh, err := c.pollEvents(ctx, start)
require.NoError(t, err)
assert.True(t, refresh)
assert.Empty(t, changes)
latest, err := c.latestEventID(ctx)
require.NoError(t, err)
assert.Equal(t, latest, next)
// The new cursor is usable.
u.SeedFolder(u.RootID(), "after")
changes, _, refresh, err = c.pollEvents(ctx, next)
require.NoError(t, err)
assert.False(t, refresh)
assert.Len(t, changes, 1)
}
func TestPollEventsVolumeGone(t *testing.T) {
t.Parallel()
ctx := context.Background()
c, srv, _ := newTestClient(t)
start, err := c.latestEventID(ctx)
require.NoError(t, err)
srv.Inject(protontest.Fault{Pattern: eventsRoute, Status: http.StatusUnprocessableEntity, Code: api.CodeNotExists})
srv.Inject(protontest.Fault{Pattern: latestEventRoute, Status: http.StatusUnprocessableEntity, Code: api.CodeNotExists})
_, next, _, err := c.pollEvents(ctx, start)
assert.True(t, errors.Is(err, errVolumeGone), "%v", err)
assert.Empty(t, next)
_, err = c.latestEventID(ctx)
assert.True(t, errors.Is(err, errVolumeGone), "%v", err)
// Other errors are not the volume gone.
c2, srv2, _ := newTestClient(t)
srv2.Inject(protontest.Fault{Pattern: latestEventRoute, Status: http.StatusUnprocessableEntity, Code: api.CodeInvalidValue})
_, err = c2.latestEventID(ctx)
require.Error(t, err)
assert.False(t, errors.Is(err, errVolumeGone), "%v", err)
}
func TestPollEventsHostilePages(t *testing.T) {
t.Parallel()
ctx := context.Background()
for _, test := range []struct {
name string
body string
wantErr bool
changes int
next string
}{{
// As the SDK's fast-forward: an empty page ends the poll.
name: "more without events",
body: `{"Code":1000,"Events":[],"EventID":"later","More":true}`,
next: "later",
}, {
name: "more without a new ID",
body: `{"Code":1000,"Events":[{"EventID":"e","EventType":1,"Link":{"LinkID":"a"}}],"EventID":"start","More":true}`,
changes: 1,
next: "start",
}, {
name: "no ID",
body: `{"Code":1000,"Events":[],"EventID":"","More":false}`,
wantErr: true,
}, {
name: "refresh without an ID",
body: `{"Code":1000,"Events":[],"Refresh":true}`,
wantErr: true,
}, {
name: "event without a link ID",
body: `{"Code":1000,"Events":[{"EventID":"e","EventType":0,"Link":{"LinkID":""}}],"EventID":"e"}`,
next: "e",
}} {
t.Run(test.name, func(t *testing.T) {
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
var pages int
c := newTestClientTransport(t, srv, u, clientOptions{EnableCaching: true}, func(next http.RoundTripper) http.RoundTripper {
return roundTripFunc(func(r *http.Request) (*http.Response, error) {
if !strings.Contains(r.URL.Path, "/events/") || strings.HasSuffix(r.URL.Path, "/latest") {
return next.RoundTrip(r)
}
pages++
return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{"Content-Type": {"application/json"}},
Body: io.NopCloser(strings.NewReader(test.body)),
Request: r,
}, nil
})
})
changes, next, _, err := c.pollEvents(ctx, "start")
assert.Equal(t, 1, pages)
if test.wantErr {
assert.Error(t, err)
return
}
require.NoError(t, err)
assert.Len(t, changes, test.changes)
assert.Equal(t, test.next, next)
})
}
}
func TestPollEventsEndlessPages(t *testing.T) {
t.Parallel()
ctx := context.Background()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
var pages int
c := newTestClientTransport(t, srv, u, clientOptions{EnableCaching: true}, func(next http.RoundTripper) http.RoundTripper {
return roundTripFunc(func(r *http.Request) (*http.Response, error) {
if !strings.Contains(r.URL.Path, "/events/") || strings.HasSuffix(r.URL.Path, "/latest") {
return next.RoundTrip(r)
}
pages++
body := fmt.Sprintf(`{"Code":1000,"Events":[{"EventID":"e%d","EventType":2,"Link":{"LinkID":"a"}}],"EventID":"p%d","More":true}`, pages, pages)
return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{"Content-Type": {"application/json"}},
Body: io.NopCloser(strings.NewReader(body)),
Request: r,
}, nil
})
})
changes, next, refresh, err := c.pollEvents(ctx, "start")
require.NoError(t, err)
assert.False(t, refresh)
assert.Equal(t, maxEventPages, pages)
assert.Len(t, changes, maxEventPages)
assert.Equal(t, fmt.Sprintf("p%d", maxEventPages), next)
}
// eventsTestClient returns a client for no server whose cache holds
// root/{dir/{sub/{deep}, file}, top, other/}, with every listing.
func eventsTestClient() *client {
c := &client{cache: newNodeCache(true), root: &node{ID: "root", IsFolder: true}}
c.cache.setChildren("root", []*node{
{ID: "dir", ParentID: "root", Name: "dir", IsFolder: true},
{ID: "top", ParentID: "root", Name: "top"},
{ID: "other", ParentID: "root", Name: "other", IsFolder: true},
})
c.cache.setChildren("dir", []*node{
{ID: "sub", ParentID: "dir", Name: "sub", IsFolder: true},
{ID: "file", ParentID: "dir", Name: "file"},
})
c.cache.setChildren("sub", []*node{{ID: "deep", ParentID: "sub", Name: "deep"}})
c.cache.setChildren("other", nil)
return c
}
func TestApplyEventsInvalidates(t *testing.T) {
t.Parallel()
all := []string{"dir", "top", "other", "sub", "file", "deep"}
listings := []string{"root", "dir", "sub", "other"}
for _, test := range []struct {
name string
change eventChange
stale staleNode
goneNodes []string
goneListings []string
}{{
name: "delete folder",
change: eventChange{linkID: "dir", parentID: "root", kind: api.EventDelete},
stale: staleNode{id: "dir", parentID: "root", name: "dir", isDir: true, known: true, newParentID: "root", gone: true},
goneNodes: []string{"dir", "sub", "file", "deep"},
goneListings: []string{"root", "dir", "sub"},
}, {
name: "delete without parent",
change: eventChange{linkID: "file", kind: api.EventDelete},
stale: staleNode{id: "file", parentID: "dir", name: "file", known: true, gone: true},
goneNodes: []string{"file"},
goneListings: []string{"dir"},
}, {
name: "delete unknown",
change: eventChange{linkID: "gone", kind: api.EventDelete},
stale: staleNode{id: "gone", gone: true},
goneNodes: nil,
goneListings: nil,
}, {
name: "create",
change: eventChange{linkID: "new", parentID: "other", kind: api.EventCreate},
stale: staleNode{id: "new", newParentID: "other"},
goneListings: []string{"other"},
}, {
name: "rename file",
change: eventChange{linkID: "file", parentID: "dir", kind: api.EventUpdateMetadata},
stale: staleNode{id: "file", parentID: "dir", name: "file", known: true, newParentID: "dir"},
goneNodes: []string{"file"},
goneListings: []string{"dir"},
}, {
name: "rename folder",
change: eventChange{linkID: "sub", parentID: "dir", kind: api.EventUpdate},
stale: staleNode{id: "sub", parentID: "dir", name: "sub", isDir: true, known: true, newParentID: "dir"},
goneNodes: []string{"sub"},
goneListings: []string{"dir", "sub"},
}, {
name: "move file",
change: eventChange{linkID: "file", parentID: "other", kind: api.EventUpdate},
stale: staleNode{id: "file", parentID: "dir", name: "file", known: true, newParentID: "other"},
goneNodes: []string{"file"},
goneListings: []string{"dir", "other"},
}, {
name: "move folder",
change: eventChange{linkID: "dir", parentID: "other", kind: api.EventUpdate},
stale: staleNode{id: "dir", parentID: "root", name: "dir", isDir: true, known: true, newParentID: "other"},
goneNodes: []string{"dir", "sub", "file", "deep"},
goneListings: []string{"root", "dir", "sub", "other"},
}, {
name: "move unknown in",
change: eventChange{linkID: "stranger", parentID: "other", kind: api.EventUpdate},
stale: staleNode{id: "stranger", newParentID: "other"},
goneListings: []string{"other"},
}, {
name: "trash file",
change: eventChange{linkID: "top", parentID: "root", kind: api.EventUpdate, trashed: true},
stale: staleNode{id: "top", parentID: "root", name: "top", known: true, newParentID: "root", gone: true},
goneNodes: []string{"top"},
goneListings: []string{"root"},
}, {
name: "trash folder",
change: eventChange{linkID: "sub", parentID: "dir", kind: api.EventUpdateMetadata, trashed: true},
stale: staleNode{id: "sub", parentID: "dir", name: "sub", isDir: true, known: true, newParentID: "dir", gone: true},
goneNodes: []string{"sub", "deep"},
goneListings: []string{"dir", "sub"},
}, {
name: "untrash unknown",
change: eventChange{linkID: "back", parentID: "dir", kind: api.EventUpdate},
stale: staleNode{id: "back", newParentID: "dir"},
goneListings: []string{"dir"},
}, {
name: "update root",
change: eventChange{linkID: "root", kind: api.EventUpdate},
stale: staleNode{id: "root", isDir: true, known: true},
goneListings: []string{"root"},
}, {
// The root can't move, so its descendants keep their paths.
name: "update root naming a parent",
change: eventChange{linkID: "root", parentID: "volume", kind: api.EventUpdate},
stale: staleNode{id: "root", isDir: true, known: true, newParentID: "volume"},
goneListings: []string{"root"},
}, {
name: "unknown kind",
change: eventChange{linkID: "file", parentID: "dir", kind: 42},
stale: staleNode{id: "file", parentID: "dir", name: "file", known: true, newParentID: "dir"},
goneNodes: []string{"file"},
goneListings: []string{"dir"},
}} {
t.Run(test.name, func(t *testing.T) {
c := eventsTestClient()
stale := c.applyEvents([]eventChange{test.change})
assert.Equal(t, []staleNode{test.stale}, stale)
for _, id := range all {
gone := c.cache.get(id) == nil
assert.Equal(t, slices.Contains(test.goneNodes, id), gone, "node %s gone", id)
}
for _, id := range listings {
_, ok := c.cache.listings[id]
assert.Equal(t, slices.Contains(test.goneListings, id), !ok, "listing %s gone", id)
}
})
}
}
func TestApplyEventsSeveral(t *testing.T) {
t.Parallel()
c := eventsTestClient()
stale := c.applyEvents([]eventChange{
{linkID: "file", parentID: "dir", kind: api.EventUpdate},
{linkID: "file", parentID: "dir", kind: api.EventDelete},
{linkID: "top", parentID: "root", kind: api.EventCreate},
})
assert.Equal(t, []staleNode{
{id: "file", parentID: "dir", name: "file", known: true, newParentID: "dir"},
{id: "file", newParentID: "dir", gone: true},
{id: "top", parentID: "root", name: "top", known: true, newParentID: "root"},
}, stale)
assert.Nil(t, c.cache.get("file"))
assert.Nil(t, c.cache.get("top"))
assert.NotNil(t, c.cache.get("deep"))
// Without a cache the nodes are unknown.
c = &client{root: &node{ID: "root", IsFolder: true}}
stale = c.applyEvents([]eventChange{{linkID: "file", parentID: "dir", kind: api.EventDelete}})
assert.Equal(t, []staleNode{{id: "file", newParentID: "dir", gone: true}}, stale)
assert.Empty(t, c.applyEvents(nil))
}
func TestApplyEventsFromFake(t *testing.T) {
t.Parallel()
ctx := context.Background()
c, srv, u := newTestClient(t)
dir := u.SeedFolder(u.RootID(), "dir")
other := u.SeedFolder(u.RootID(), "other")
u.SeedFile(dir, "file", []byte("x"), time.Now())
listAll(t, c, u.RootID())
listAll(t, c, dir)
listAll(t, c, other)
start, err := c.latestEventID(ctx)
require.NoError(t, err)
// Another client moves dir into other.
mover := newTestClientFor(t, srv, u, clientOptions{})
n, err := mover.getNode(ctx, dir)
require.NoError(t, err)
require.NoError(t, mover.move(ctx, n, other, "dir"))
assert.Contains(t, listAll(t, c, u.RootID()), "dir", "cached")
changes, _, _, err := c.pollEvents(ctx, start)
require.NoError(t, err)
stale := c.applyEvents(changes)
require.Len(t, stale, 1)
assert.Equal(t, staleNode{id: dir, parentID: u.RootID(), name: "dir", isDir: true, known: true, newParentID: other}, stale[0])
assert.NotContains(t, listAll(t, c, u.RootID()), "dir")
assert.Contains(t, listAll(t, c, other), "dir")
assert.Contains(t, listAll(t, c, dir), "file")
}
// pollTimes records when a client requested pages of events.
type pollTimes struct {
mu sync.Mutex
times []time.Time
}
// get returns the times recorded so far.
func (p *pollTimes) get() []time.Time {
p.mu.Lock()
defer p.mu.Unlock()
return slices.Clone(p.times)
}
// gaps returns the time between each recorded poll and the next.
func (p *pollTimes) gaps() []time.Duration {
times := p.get()
var out []time.Duration
for i := 1; i < len(times); i++ {
out = append(out, times[i].Sub(times[i-1]))
}
return out
}
// Timing of the polling clients: short, so the tests are quick.
const (
testPollInterval = 200 * time.Millisecond
testPollJitter = 50 * time.Millisecond
)
// newPollingClient returns a client with caching enabled which polls
// events at least testPollInterval apart, with at most testPollJitter
// added, recording when it requests pages of events.
func newPollingClient(t *testing.T) (*client, *protontest.Server, *protontest.User, *pollTimes) {
t.Helper()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
polls := &pollTimes{}
c := newTestClientTransport(t, srv, u, clientOptions{EnableCaching: true, EventsInterval: testPollInterval}, func(next http.RoundTripper) http.RoundTripper {
return roundTripFunc(func(r *http.Request) (*http.Response, error) {
if strings.HasPrefix(r.URL.Path, "/drive/v2/volumes/") && strings.Contains(r.URL.Path, "/events/") {
polls.mu.Lock()
polls.times = append(polls.times, time.Now())
polls.mu.Unlock()
}
return next.RoundTrip(r)
})
})
c.events.jitter = testPollJitter
return c, srv, u, polls
}
// eventRequests returns the requests srv had for the start cursor and
// for pages of events.
func eventRequests(srv *protontest.Server) (latest, pages int) {
got := srv.Requests()
return got["GET "+latestEventRoute], got["GET "+eventsRoute]
}
// logLines holds the lines rclone logs while they are captured.
type logLines struct {
mu sync.Mutex
lines []string
}
// matching returns the lines captured so far which contain each of subs.
func (l *logLines) matching(subs ...string) []string {
l.mu.Lock()
defer l.mu.Unlock()
var out []string
for _, line := range l.lines {
if !slices.ContainsFunc(subs, func(s string) bool { return !strings.Contains(line, s) }) {
out = append(out, line)
}
}
return out
}
// captureLogLines captures rclone's log output at its usual level until
// the test ends. Tests using it must not be parallel.
func captureLogLines(t *testing.T) *logLines {
l := &logLines{}
log.Handler.SetOutput(func(level slog.Level, text string) {
l.mu.Lock()
defer l.mu.Unlock()
l.lines = append(l.lines, text)
})
t.Cleanup(log.Handler.ResetOutput)
return l
}
// addPending makes p wait to name the uncached node id, which a
// subscriber must be waiting for.
func addPending(p *eventPoller, id string) {
p.mu.Lock()
defer p.mu.Unlock()
p.addPendingLocked([]staleNode{{id: id, newParentID: "folder"}})
}
func TestPollerIdleMakesNoRequests(t *testing.T) {
t.Parallel()
c, srv, u, _ := newPollingClient(t)
u.SeedFolder(u.RootID(), "dir")
// Nothing is cached and nothing subscribed, so waking does nothing.
c.events.wake()
time.Sleep(3 * (testPollInterval + testPollJitter))
latest, pages := eventRequests(srv)
assert.Zero(t, latest+pages, "%v", srv.Requests())
}
func TestPollerPollsWhileCached(t *testing.T) {
t.Parallel()
c, srv, u, polls := newPollingClient(t)
listAll(t, c, u.RootID())
latest, pages := eventRequests(srv)
assert.Equal(t, 1, latest, "the start cursor is fetched when the cache fills")
assert.Zero(t, pages)
listAll(t, c, u.RootID())
time.Sleep(5*testPollInterval + testPollJitter)
c.events.stop()
latest, pages = eventRequests(srv)
assert.Equal(t, 1, latest)
assert.GreaterOrEqual(t, pages, 3, "about one poll per interval")
assert.LessOrEqual(t, pages, 5, "at most one poll per interval")
for _, gap := range polls.gaps() {
assert.GreaterOrEqual(t, gap, testPollInterval, "two polls within one interval")
}
}
func TestPollerSetInterval(t *testing.T) {
t.Parallel()
c, _, u, polls := newPollingClient(t)
c.events.setInterval(testPollInterval / 2)
listAll(t, c, u.RootID())
require.Eventually(t, func() bool { return len(polls.get()) >= 2 }, 5*time.Second, 5*time.Millisecond)
assert.GreaterOrEqual(t, polls.gaps()[0], testPollInterval, "shorter than the floor")
c.events.setInterval(3 * testPollInterval)
n := len(polls.get())
require.Eventually(t, func() bool { return len(polls.get()) >= n+2 }, 5*time.Second, 5*time.Millisecond)
c.events.stop()
// The poll scheduled before the change waits the old interval.
assert.GreaterOrEqual(t, polls.gaps()[n], 3*testPollInterval, "longer than the floor")
}
func TestPollerAppliesOtherClientsChange(t *testing.T) {
t.Parallel()
c, _, u, _ := newPollingClient(t)
dir := u.SeedFolder(u.RootID(), "dir")
listAll(t, c, dir)
u.SeedFile(dir, "new.txt", []byte("new"), time.Now())
assert.NotContains(t, listAll(t, c, dir), "new.txt", "the listing is cached")
assert.Eventually(t, func() bool {
_, ok := listAll(t, c, dir)["new.txt"]
return ok
}, testPollInterval+testPollJitter+500*time.Millisecond, 10*time.Millisecond, "the new file isn't listed after a poll")
}
func TestPollerRefresh(t *testing.T) {
t.Parallel()
c, srv, u, polls := newPollingClient(t)
var mu sync.Mutex
refreshes := 0
unsubscribe := c.events.subscribe(func(_ []staleNode, refresh bool) {
mu.Lock()
defer mu.Unlock()
if refresh {
refreshes++
}
})
defer unsubscribe()
refreshed := func() int {
mu.Lock()
defer mu.Unlock()
return refreshes
}
listAll(t, c, u.RootID())
// Just after a poll the next is an interval away, so the cursor is
// forgotten before it is used again.
require.Eventually(t, func() bool { return len(polls.get()) > 0 }, 2*time.Second, time.Millisecond)
u.SeedFolder(u.RootID(), "dir")
c.events.runMu.Lock()
addPending(c.events, "uncached")
srv.ExpireEvents(u)
c.events.runMu.Unlock()
require.Eventually(t, func() bool { return refreshed() > 0 }, 2*time.Second, time.Millisecond)
assert.True(t, c.cache.empty(), "the cache is cleared")
assert.Empty(t, c.events.pendingIDs(), "nodes are still pending")
n := len(polls.get())
time.Sleep(3 * (testPollInterval + testPollJitter))
assert.Greater(t, len(polls.get()), n, "polling continues")
assert.LessOrEqual(t, len(polls.get()), n+3, "polls in a loop")
assert.Equal(t, 1, refreshed())
// The new cursor is used.
assert.Contains(t, listAll(t, c, u.RootID()), "dir")
u.SeedFolder(u.RootID(), "after")
assert.Eventually(t, func() bool {
_, ok := listAll(t, c, u.RootID())["after"]
return ok
}, testPollInterval+testPollJitter+500*time.Millisecond, 10*time.Millisecond)
assert.Equal(t, 1, refreshed())
}
// TestPollerBackoff isn't parallel: it counts goroutines and pool
// buffers, which other tests share.
func TestPollerBackoff(t *testing.T) {
c, srv, u, polls := newPollingClient(t)
goroutines, buffers := leakBaseline(srv)
srv.Inject(protontest.Fault{Pattern: eventsRoute, Status: http.StatusServiceUnavailable, Times: 4})
listAll(t, c, u.RootID())
// Four polls fail, the fifth and sixth succeed.
require.Eventually(t, func() bool { return len(polls.get()) >= 6 }, 10*time.Second, 5*time.Millisecond)
c.events.stop()
gaps := polls.gaps()
for i, times := range []int{1, 1, 2, 3, 1} {
want := time.Duration(times) * testPollInterval
assert.GreaterOrEqual(t, gaps[i], want, "gap %d: %v", i, gaps)
assert.Less(t, gaps[i], want+testPollJitter+2*testPollInterval, "gap %d: %v", i, gaps)
}
requireNoLeaks(t, srv, goroutines, buffers)
}
func TestPollerVolumeGone(t *testing.T) {
logs := captureLogLines(t)
c, srv, u, _ := newPollingClient(t)
var refreshes atomic.Int32
unsubscribe := c.events.subscribe(func(_ []staleNode, refresh bool) {
if refresh {
refreshes.Add(1)
}
})
defer unsubscribe()
srv.Inject(protontest.Fault{Pattern: eventsRoute, Status: http.StatusUnprocessableEntity, Code: api.CodeNotExists})
addPending(c.events, "uncached")
listAll(t, c, u.RootID())
require.Eventually(t, c.cache.empty, 2*time.Second, time.Millisecond, "the cache is cleared")
assert.Empty(t, c.events.pendingIDs(), "nodes are still pending")
time.Sleep(2 * (testPollInterval + testPollJitter))
listAll(t, c, u.RootID())
c.events.wake()
time.Sleep(2 * (testPollInterval + testPollJitter))
latest, pages := eventRequests(srv)
assert.Equal(t, 1, latest)
assert.Equal(t, 1, pages, "polling stopped")
assert.Len(t, logs.matching("ERROR", "protondrive: events"), 1)
assert.Equal(t, int32(1), refreshes.Load())
}
func TestPollerFirstLatestFails(t *testing.T) {
t.Parallel()
c, srv, u, _ := newPollingClient(t)
srv.Inject(protontest.Fault{Pattern: latestEventRoute, Status: http.StatusServiceUnavailable, Times: 1})
dir := u.SeedFolder(u.RootID(), "dir")
assert.Contains(t, listAll(t, c, u.RootID()), "dir", "the listing fails with the start cursor")
// The cursor is fetched again and polling goes on.
require.Eventually(t, func() bool {
_, pages := eventRequests(srv)
return pages >= 1
}, 5*time.Second, 5*time.Millisecond, "polling doesn't start")
listAll(t, c, dir)
u.SeedFile(dir, "new.txt", []byte("new"), time.Now())
assert.Eventually(t, func() bool {
_, ok := listAll(t, c, dir)["new.txt"]
return ok
}, 5*time.Second, 10*time.Millisecond)
}
// TestPollerFirstLatestRateLimited checks a Retry-After on the start
// cursor fetched when the cache fills doesn't hold up the caller, and
// delays the next try instead.
func TestPollerFirstLatestRateLimited(t *testing.T) {
t.Parallel()
c, srv, u, _ := newPollingClient(t)
srv.Inject(protontest.Fault{Pattern: latestEventRoute, Status: http.StatusTooManyRequests, RetryAfter: 2, Times: 1})
start := time.Now()
listAll(t, c, u.RootID())
assert.Less(t, time.Since(start), time.Second, "the listing waited for the Retry-After")
require.Eventually(t, func() bool {
latest, _ := eventRequests(srv)
return latest >= 2
}, 10*time.Second, 5*time.Millisecond, "the start cursor isn't fetched again")
assert.GreaterOrEqual(t, time.Since(start), 2*time.Second, "tried again before the Retry-After")
}
// TestPollerStopKeepsCursor checks polling resumed after stop goes on
// from the cursor, rather than fetching a new one at once, so no two
// requests come within an interval.
func TestPollerStopKeepsCursor(t *testing.T) {
t.Parallel()
c, srv, u, polls := newPollingClient(t)
listAll(t, c, u.RootID())
require.Eventually(t, func() bool { return len(polls.get()) >= 1 }, 5*time.Second, time.Millisecond)
c.events.stop()
c.cache.clear()
listAll(t, c, u.RootID())
require.Eventually(t, func() bool { return len(polls.get()) >= 3 }, 5*time.Second, time.Millisecond)
c.events.stop()
latest, _ := eventRequests(srv)
assert.Equal(t, 1, latest, "the start cursor is fetched again")
for _, gap := range polls.gaps() {
assert.GreaterOrEqual(t, gap, testPollInterval, "two polls within one interval")
}
}
// TestPollerStopWaitsForPoll checks stop returns only once a poll in
// flight has finished, so nothing of it, its logging included, runs
// after Shutdown.
func TestPollerStopWaitsForPoll(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
held, release := make(chan struct{}, 1), make(chan struct{})
c := newTestClientTransport(t, srv, u, clientOptions{EnableCaching: true, EventsInterval: testPollInterval}, func(next http.RoundTripper) http.RoundTripper {
return roundTripFunc(func(r *http.Request) (*http.Response, error) {
resp, err := next.RoundTrip(r)
if strings.HasPrefix(r.URL.Path, "/drive/v2/volumes/") && strings.Contains(r.URL.Path, "/events/") {
// The response is in, as if the poll were cancelled
// just after it arrived.
select {
case held <- struct{}{}:
default:
}
<-release
}
return resp, err
})
})
c.events.jitter = testPollJitter
listAll(t, c, u.RootID())
select {
case <-held:
case <-time.After(5 * time.Second):
t.Fatal("no poll")
}
stopped := make(chan struct{})
go func() {
c.events.stop()
close(stopped)
}()
select {
case <-stopped:
t.Error("stop returned while a poll was in flight")
case <-time.After(200 * time.Millisecond):
}
close(release)
select {
case <-stopped:
case <-time.After(5 * time.Second):
t.Fatal("stop didn't return after the poll finished")
}
}
// TestPollerIdleCacheCleared checks a cache unused for the idle time is
// cleared, which stops polling, unless something subscribes.
func TestPollerIdleCacheCleared(t *testing.T) {
t.Parallel()
const idle = 3 * testPollInterval
c, srv, u, polls := newPollingClient(t)
c.events.idle = idle
u.SeedFolder(u.RootID(), "dir")
listAll(t, c, u.RootID())
// Used, the cache is kept. Other clients' changes aren't use.
for range 6 {
time.Sleep(testPollInterval)
u.SeedFolder(u.RootID(), random.String(8))
c.cache.get(u.RootID())
}
assert.False(t, c.cache.empty(), "cleared while in use")
require.Eventually(t, c.cache.empty, 2*idle+2*testPollInterval, 5*time.Millisecond, "not cleared")
n := len(polls.get())
time.Sleep(3 * (testPollInterval + testPollJitter))
assert.Equal(t, n, len(polls.get()), "polled after the cache was cleared")
// The next use refills the cache and polls again, from the cursor.
assert.NotEmpty(t, listAll(t, c, u.RootID()))
require.Eventually(t, func() bool { return len(polls.get()) > n }, 5*time.Second, 5*time.Millisecond, "polling didn't resume")
latest, _ := eventRequests(srv)
assert.Equal(t, 1, latest, "the start cursor is fetched again")
// With a subscriber the cache is kept.
unsubscribe := c.events.subscribe(func([]staleNode, bool) {})
defer unsubscribe()
time.Sleep(2*idle + 2*testPollInterval)
assert.False(t, c.cache.empty(), "cleared with a subscriber")
}
// TestPollerNotifiesOutsideCallerLocks checks a subscriber isn't called
// on the goroutine which filled the cache, which may hold a lock the
// subscriber takes, as a dircache lookup holds the dircache's.
func TestPollerNotifiesOutsideCallerLocks(t *testing.T) {
t.Parallel()
c, srv, u, _ := newPollingClient(t)
var callerLock sync.Mutex
notified := make(chan bool, 1)
unsubscribe := c.events.subscribe(func(_ []staleNode, refresh bool) {
callerLock.Lock()
defer callerLock.Unlock()
notified <- refresh
})
defer unsubscribe()
srv.Inject(protontest.Fault{Pattern: latestEventRoute, Status: http.StatusUnprocessableEntity, Code: api.CodeNotExists})
done := make(chan struct{})
callerLock.Lock()
go func() {
defer close(done)
defer callerLock.Unlock()
listAll(t, c, u.RootID())
}()
select {
case <-done:
case <-time.After(10 * time.Second):
t.Fatal("deadlock: the subscriber was called inside the cache fill")
}
select {
case refresh := <-notified:
assert.True(t, refresh)
case <-time.After(5 * time.Second):
t.Fatal("the subscriber wasn't notified")
}
}
+25
View File
@@ -0,0 +1,25 @@
package protondrive
import (
"context"
"fmt"
"time"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/configmap"
)
// ConfigBrowserSignIn runs the browser sign-in of rclone config for
// remote name on m, handing the sign-in page to approve instead of
// printing it.
func ConfigBrowserSignIn(ctx context.Context, name string, m configmap.Mapper, approve func(signInURL string)) error {
out, err := configure(ctx, name, m, fs.ConfigIn{State: "browser"}, signInOptions{
showURL: approve,
pollInterval: 10 * time.Millisecond,
pollTimeout: 10 * time.Second,
})
if err == nil && out != nil {
err = fmt.Errorf("config stopped in state %q", out.State)
}
return err
}
+88
View File
@@ -0,0 +1,88 @@
package protondrive_test
import (
"encoding/base64"
"net/http"
"os"
"testing"
"time"
"github.com/rclone/rclone/backend/protondrive"
"github.com/rclone/rclone/backend/protondrive/api"
"github.com/rclone/rclone/backend/protondrive/protontest"
"github.com/rclone/rclone/fstest"
"github.com/rclone/rclone/fstest/fstests"
"github.com/rclone/rclone/lib/random"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// fakeEnvPrefix starts the environment variables configuring the
// TestProtonDriveFake: remote.
const fakeEnvPrefix = "RCLONE_CONFIG_TESTPROTONDRIVEFAKE_"
// setupFakeRemote starts a fake Proton server with one user and
// configures the TestProtonDriveFake: remote, through environment
// variables, with a saved session of that user. It polls events every
// second, as the remote protonfake configures does.
func setupFakeRemote(t *testing.T) (remoteName string, srv *protontest.Server, user *protontest.User) {
srv = protontest.New(t)
user = srv.AddUser("user", "pass")
sess := srv.NewSession(user)
t.Setenv(fakeEnvPrefix+"TYPE", "protondrive")
t.Setenv(fakeEnvPrefix+"ENDPOINT", srv.URL())
t.Setenv(fakeEnvPrefix+"CLIENT_UID", sess.UID)
t.Setenv(fakeEnvPrefix+"CLIENT_ACCESS_TOKEN", sess.AccessToken)
t.Setenv(fakeEnvPrefix+"CLIENT_REFRESH_TOKEN", sess.RefreshToken)
// The config holds the salted key password base64 encoded.
t.Setenv(fakeEnvPrefix+"CLIENT_SALTED_KEY_PASS", base64.StdEncoding.EncodeToString([]byte(user.KeyPassword())))
t.Setenv(fakeEnvPrefix+"DRAFT_CLIENT_UID", random.String(32))
t.Setenv(fakeEnvPrefix+"EVENTS_MIN_INTERVAL", "1s")
return "TestProtonDriveFake:", srv, user
}
// TestSetupFakeRemote checks the configured session is accepted by the
// fake server.
func TestSetupFakeRemote(t *testing.T) {
remoteName, srv, user := setupFakeRemote(t)
assert.Equal(t, "TestProtonDriveFake:", remoteName)
assert.Equal(t, "protondrive", os.Getenv(fakeEnvPrefix+"TYPE"))
assert.Equal(t, srv.URL(), os.Getenv(fakeEnvPrefix+"ENDPOINT"))
assert.NotEmpty(t, os.Getenv(fakeEnvPrefix+"DRAFT_CLIENT_UID"))
keyPass, err := base64.StdEncoding.DecodeString(os.Getenv(fakeEnvPrefix + "CLIENT_SALTED_KEY_PASS"))
require.NoError(t, err)
assert.Equal(t, user.KeyPassword(), string(keyPass))
require.NotEmpty(t, os.Getenv(fakeEnvPrefix+"CLIENT_REFRESH_TOKEN"))
req, err := http.NewRequest("GET", os.Getenv(fakeEnvPrefix+"ENDPOINT")+"/core/v4/users", nil)
require.NoError(t, err)
req.Header.Set("x-pm-appversion", "external-drive-rclone@1.0.0-stable")
req.Header.Set("x-pm-uid", os.Getenv(fakeEnvPrefix+"CLIENT_UID"))
req.Header.Set("Authorization", "Bearer "+os.Getenv(fakeEnvPrefix+"CLIENT_ACCESS_TOKEN"))
resp, err := srv.Client().Do(req)
require.NoError(t, err)
_ = resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
// TestIntegrationFake runs integration tests against the fake server.
func TestIntegrationFake(t *testing.T) {
if *fstest.RemoteName != "" {
t.Skip("skipping as -remote is set")
}
remoteName, srv, _ := setupFakeRemote(t)
t.Cleanup(func() {
// fstests leaves Fs of the remote behind which go on polling
// events, and logging, into later tests. Gone, the volume
// stops them for good before the server closes.
for _, pattern := range []string{"/drive/volumes/{volumeID}/events/latest", "/drive/v2/volumes/{volumeID}/events/{eventID}"} {
srv.Inject(protontest.Fault{Pattern: pattern, Status: http.StatusUnprocessableEntity, Code: api.CodeNotExists})
}
time.Sleep(3 * time.Second) // an interval of 1s, up to 1s of jitter, and slack
})
fstests.Run(t, &fstests.Opt{
RemoteName: remoteName,
NilObject: (*protondrive.Object)(nil),
QuickTestOK: true,
})
}
+435
View File
@@ -0,0 +1,435 @@
package protondrive
import (
"bytes"
"context"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"unicode/utf16"
"unicode/utf8"
"github.com/ProtonMail/gopenpgp/v3/crypto"
"github.com/rclone/rclone/backend/protondrive/api"
"github.com/rclone/rclone/backend/protondrive/protontest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Fuzz targets for everything which parses bytes the server controls.
// Each is seeded from the old library's fixtures in testdata/oldlib and
// from payloads the fake server makes, and checks invariants of what
// is accepted, not only that nothing panics.
// oldlibFixture is the part of testdata/oldlib/file.json and
// folder.json the fuzz targets use.
type oldlibFixture struct {
NodeKey string
NodePassphrasePlain string
NamePlain string
ChildName string
XAttr string
}
// loadOldlibFixture returns testdata/oldlib/name.
func loadOldlibFixture(t testing.TB, name string) oldlibFixture {
t.Helper()
data, err := os.ReadFile(filepath.Join("testdata/oldlib", name))
require.NoError(t, err)
var fx oldlibFixture
require.NoError(t, json.Unmarshal(data, &fx))
return fx
}
// fuzzLinks returns a client for a user of a fake server holding the
// keys of the old library's v6 file fixture, the root folder's keys,
// and the JSON of a link of each kind the client reads: the fixture
// itself, and nodes the fake makes as SDK clients would.
func fuzzLinks(f *testing.F) (*client, *nodeKeys, [][]byte) {
frozen := loadFrozenFile(f, filepath.Join(frozenFileDir, "v6"))
srv := protontest.New(f)
u := srv.AddUserWithKeys(frozen.Email, "pass", frozen.Keys)
c := newTestClientFor(f, srv, u, clientOptions{})
root := u.RootID()
ids := []string{
u.SeedStoredFile(root, frozen.Stored),
u.SeedFolder(root, "folder"),
u.SeedFile(root, "file.txt", []byte("content"), time.Now()),
u.SeedFile(root, "empty", nil, time.Now()),
u.SeedLegacyFile(root, "legacy", []byte("legacy")),
u.SeedAnonymousFile(root, "anonymous", []byte("anonymous"), time.Now()),
u.SeedAnonymousFolder(root, "anonymous folder"),
u.SeedFileClaimingBlockSizes(root, "claims", []byte("claims"), []int64{1, 2, 3}),
u.SeedDoc(root, "doc"),
}
ctx := context.Background()
links, err := c.fetchLinks(ctx, ids)
require.NoError(f, err)
require.Len(f, links, len(ids))
var seeds [][]byte
for _, d := range links {
data, err := json.Marshal(d)
require.NoError(f, err)
seeds = append(seeds, data)
}
keys, err := c.folderKeysOf(ctx, root)
require.NoError(f, err)
return c, keys, seeds
}
func FuzzValidateName(f *testing.F) {
for _, name := range []string{
loadOldlibFixture(f, "file.json").NamePlain,
loadOldlibFixture(f, "folder.json").NamePlain,
loadOldlibFixture(f, "folder.json").ChildName,
"file.txt", ".", "..", "...", "", "a/b", "/", "a\x00b", " ", "é", "é",
strings.Repeat("a", maxNameLength), strings.Repeat("a", maxNameLength+1),
strings.Repeat("\U0001F600", maxNameLength/2), strings.Repeat("\U0001F600", maxNameLength/2+1),
"\xff\xfe",
} {
f.Add(name)
}
f.Fuzz(func(t *testing.T, name string) {
if validateName(name) != nil {
return
}
assert.NotEmpty(t, name)
assert.True(t, utf8.ValidString(name), "%q", name)
assert.NotEqual(t, ".", name)
assert.NotEqual(t, "..", name)
assert.NotContains(t, name, "/")
assert.NotContains(t, name, "\x00")
assert.LessOrEqual(t, len(utf16.Encode([]rune(name))), maxNameLength)
})
}
func FuzzDecodeLink(f *testing.F) {
c, rootKeys, seeds := fuzzLinks(f)
for _, seed := range seeds {
f.Add(seed)
// Hostile values in links which otherwise decrypt.
var d api.LinkDetails
require.NoError(f, json.Unmarshal(seed, &d))
for _, change := range []func(d *api.LinkDetails){
func(d *api.LinkDetails) { d.Link.LinkID = "" },
func(d *api.LinkDetails) { d.Link.Type = 3 - d.Link.Type },
func(d *api.LinkDetails) {
if d.File != nil && d.File.ActiveRevision != nil {
d.File.ActiveRevision.EncryptedSize = -5
}
},
} {
changed := d
changed.File = cloneFile(d.File)
change(&changed)
data, err := json.Marshal(changed)
require.NoError(f, err)
f.Add(data)
}
}
// Shapes the client can't decrypt.
for _, format := range []string{"legacy", "v6"} {
data, err := os.ReadFile(filepath.Join(frozenFileDir, format, "link.json"))
require.NoError(f, err)
f.Add(data)
}
f.Add([]byte(`{}`))
f.Add([]byte(`{"Link":{"LinkID":"x","Type":1,"ParentLinkID":"p"},"Folder":{}}`))
ctx := context.Background()
f.Fuzz(func(t *testing.T, data []byte) {
var d api.LinkDetails
if json.Unmarshal(data, &d) != nil {
return
}
n, unreadable, err := c.decryptNode(ctx, &d, rootKeys)
if err != nil || unreadable != nil {
assert.Nil(t, n)
return
}
require.NotNil(t, n)
assert.NotEmpty(t, n.ID)
assert.Equal(t, d.Link.LinkID, n.ID)
assert.True(t, utf8.ValidString(n.Name), "%q", n.Name)
if d.Link.ParentLinkID != "" {
assert.NoError(t, validateName(n.Name))
}
assert.True(t, n.IsFolder == (d.Link.Type == api.LinkTypeFolder))
for _, original := range []bool{true, false} {
assert.GreaterOrEqual(t, n.size(original), int64(0), "original=%v", original)
}
if n.X != nil {
checkXAttrs(t, n.X)
}
})
}
// cloneFile returns a copy of file, sharing nothing changeable with it.
func cloneFile(file *api.File) *api.File {
if file == nil {
return nil
}
out := *file
if file.ActiveRevision != nil {
rev := *file.ActiveRevision
out.ActiveRevision = &rev
}
return &out
}
// checkXAttrs checks the invariants of parsed extended attributes.
//
// That the block sizes add up to the size isn't one: as in the SDK
// neither is checked against the other, and a read checks each block
// against its recorded size, so a file whose attributes disagree fails
// to read rather than reading wrong.
func checkXAttrs(t *testing.T, x *xattrs) {
t.Helper()
assert.GreaterOrEqual(t, x.Size, int64(0))
if x.NoSize {
assert.Zero(t, x.Size)
}
if x.BlockSizes != nil {
assert.NotEmpty(t, x.BlockSizes)
}
for i, size := range x.BlockSizes {
assert.Positive(t, size, "block %d", i+1)
}
if !x.ModTime.IsZero() {
assert.Equal(t, time.UTC, x.ModTime.Location())
// RFC 3339 years, moved by a zone offset of under a day.
assert.True(t, x.ModTime.Year() >= -1 && x.ModTime.Year() <= 10000, "%v", x.ModTime)
}
assert.NotContains(t, x.Extra, "Common")
}
func FuzzCheckKeyS2K(f *testing.F) {
f.Add(unarmor(f, loadOldlibFixture(f, "file.json").NodeKey))
now := time.Now()
parent, signer := newTestKeyRing(f, now), newTestKeyRing(f, now)
_, _, _, keys := testNodeKeys(f, parent, signer, true, now)
for _, memExp := range []byte{3, 17, 18, 31} {
f.Add(unarmor(f, argon2Locked(f, keys, 1, 1, memExp)))
}
f.Fuzz(func(t *testing.T, data []byte) {
// Whatever the bytes, the check never runs a KDF and never
// panics; a key it lets through still parses or fails to.
if checkKeyS2K(data) == nil {
_, _ = crypto.NewKey(data)
}
})
}
func FuzzDecryptXAttrs(f *testing.F) {
fx := loadOldlibFixture(f, "file.json")
key, err := unlockKey(fx.NodeKey, []byte(fx.NodePassphrasePlain))
require.NoError(f, err)
nodeKR, err := crypto.NewKeyRing(key)
require.NoError(f, err)
now := time.Now()
// The old library's attributes, encrypted and in plain text.
res, _, _, err := decryptMessage(fx.XAttr, nodeKR, nil, now, maxXAttrsSize)
require.NoError(f, err)
f.Add(fx.XAttr, res.Bytes())
// Attributes as the fake writes them for SDK clients.
c, _, seeds := fuzzLinks(f)
for _, seed := range seeds {
var d api.LinkDetails
require.NoError(f, json.Unmarshal(seed, &d))
if d.File == nil || d.File.ActiveRevision == nil || d.File.ActiveRevision.XAttr == "" {
continue
}
n, err := c.getNode(context.Background(), d.Link.LinkID)
require.NoError(f, err)
res, _, _, err := decryptMessage(d.File.ActiveRevision.XAttr, n.keys.keyRing, nil, now, maxXAttrsSize)
require.NoError(f, err)
f.Add("", res.Bytes())
}
for _, plain := range []string{
`{"Common":{"ModificationTime":"2021-09-16T07:40:54+0000","Size":-1,"BlockSizes":[4194304,-1],"Digests":{"SHA1":"x"}}}`,
`{"Common":{"Size":1152921504606846976,"BlockSizes":[1152921504606846976,1]}}`,
`{"Common":{"ModificationTime":"99999-01-01T00:00:00Z"}}`,
`{"Common":{"ModificationTime":"0000-01-01T00:00:00+23:59"}}`,
`{"Common":{"BlockSizes":[]},"Media":{"Width":1}}`,
`{"Common":{"BlockSizes":[` + strings.Repeat("4194304,", 10000) + `1]}}`,
strings.Repeat(`{"Common":`, 20000) + strings.Repeat("}", 20000),
`{"Common":` + strings.Repeat("[", 100000) + strings.Repeat("]", 100000) + `}`,
} {
f.Add("", []byte(plain))
}
f.Fuzz(func(t *testing.T, enc string, plain []byte) {
if x, _, err := decryptXAttrs(enc, nodeKR, nodeKR, now); err == nil {
checkXAttrs(t, x)
}
if len(plain) > maxXAttrsSize/2 {
return
}
enc, err := encryptArmored(plain, nil, nodeKR, nodeKR, true, now)
require.NoError(t, err)
x, verifyErr, err := decryptXAttrs(enc, nodeKR, nodeKR, now)
if !utf8.Valid(plain) {
assert.Error(t, err)
return
}
require.NoError(t, err)
require.NoError(t, verifyErr)
checkXAttrs(t, x)
})
}
func FuzzDecodeAPIError(f *testing.F) {
for _, seed := range []struct {
status int
body string
retryAfter string
}{
{422, `{"Code":2500,"Error":"A file or folder with that name already exists","Details":{"ConflictLinkID":"abc","ConflictRevisionID":"def"}}`, ""},
{422, `{"Code":2501,"Error":"File or folder not found"}`, ""},
{429, `{"Code":85131,"Error":"Too many recent API requests"}`, "17"},
{429, ``, "Wed, 21 Oct 2015 07:28:00 GMT"},
{429, ``, "99999999999999999999999"},
{401, `{"Code":401,"Error":"Invalid access token"}`, ""},
{400, `{"Code":2064,"Error":"protontest: invalid x-pm-appversion \"x\""}`, ""},
{500, `<html>Internal Server Error</html>`, ""},
{200, `{"Code":"1000"}`, ""},
{0, `{"Error":null,"Details":[1,2,3]}`, "-5"},
} {
f.Add(seed.status, []byte(seed.body), seed.retryAfter)
}
f.Fuzz(func(t *testing.T, status int, body []byte, retryAfterValue string) {
resp := &http.Response{
StatusCode: status,
Status: fmt.Sprintf("%d %s", status, http.StatusText(status)),
Header: http.Header{"Retry-After": {retryAfterValue}},
Body: io.NopCloser(bytes.NewReader(body)),
}
err := errorHandler(resp)
var apiErr *api.Error
require.ErrorAs(t, err, &apiErr)
assert.Equal(t, status, apiErr.Status)
assert.NotEmpty(t, apiErr.Message)
assert.NotEmpty(t, err.Error())
assert.Equal(t, apiErr.Code, apiErrorCode(err))
wait := retryAfter(resp)
assert.Positive(t, wait)
assert.LessOrEqual(t, wait, maxRetryAfter)
})
}
func FuzzDecryptForkPayload(f *testing.F) {
key := bytes.Repeat([]byte{7}, 32)
for _, seed := range []struct{ plain, aad string }{
{`{"type":"default","keyPassword":"the-key-password"}`, forkPayloadAAD},
{`{"keyPassword":""}`, forkPayloadAAD},
{`{"keyPassword":null}`, forkPayloadAAD},
{`{"type":"default"}`, forkPayloadAAD},
{`not json`, forkPayloadAAD},
{`{"keyPassword":"wrong additional data"}`, "other"},
} {
f.Add(sealForkPayload(f, key, seed.plain, seed.aad))
}
f.Add("")
f.Add("not base64!")
f.Add(base64.StdEncoding.EncodeToString(make([]byte, 12)))
f.Fuzz(func(t *testing.T, payload string) {
keyPassword, err := decryptForkPayload(payload, key)
if err != nil {
assert.Empty(t, keyPassword)
return
}
// Success only for a payload sealed with key: open it here
// independently.
blob, err := base64.StdEncoding.DecodeString(payload)
require.NoError(t, err)
block, err := aes.NewCipher(key)
require.NoError(t, err)
gcm, err := cipher.NewGCM(block)
require.NoError(t, err)
require.GreaterOrEqual(t, len(blob), gcm.NonceSize())
plain, err := gcm.Open(nil, blob[:gcm.NonceSize()], blob[gcm.NonceSize():], []byte(forkPayloadAAD))
require.NoError(t, err, "accepted a payload which doesn't authenticate")
var p struct{ KeyPassword *string }
require.NoError(t, json.Unmarshal(plain, &p))
require.NotNil(t, p.KeyPassword)
assert.Equal(t, *p.KeyPassword, keyPassword)
})
}
func FuzzDecodeEvents(f *testing.F) {
// Pages the fake sends for changes of each kind.
srv := protontest.New(f)
u := srv.AddUser("user", "pass")
var (
mu sync.Mutex
pages [][]byte
)
c := newTestClientTransport(f, srv, u, clientOptions{EnableCaching: true}, func(next http.RoundTripper) http.RoundTripper {
return roundTripFunc(func(r *http.Request) (*http.Response, error) {
resp, err := next.RoundTrip(r)
if err != nil || !strings.Contains(r.URL.Path, "/events/") {
return resp, err
}
body, err := io.ReadAll(resp.Body)
_ = resp.Body.Close()
if err != nil {
return nil, err
}
mu.Lock()
pages = append(pages, body)
mu.Unlock()
resp.Body = io.NopCloser(bytes.NewReader(body))
return resp, nil
})
})
srv.SetEventsPageSize(3)
ctx := context.Background()
start, err := c.latestEventID(ctx)
require.NoError(f, err)
dir := u.SeedFolder(u.RootID(), "dir")
file := u.SeedFile(dir, "file", []byte("x"), time.Now())
u.SetParent(file, u.RootID())
srv.InjectEvent(u, protontest.Event{Type: protontest.EventUpdateMetadata, LinkID: dir, ParentLinkID: u.RootID(), IsTrashed: true})
srv.InjectEvent(u, protontest.Event{Type: protontest.EventDelete, LinkID: dir})
_, _, _, err = c.pollEvents(ctx, start)
require.NoError(f, err)
srv.ExpireEvents(u)
_, _, _, err = c.pollEvents(ctx, start)
require.NoError(f, err)
require.NotEmpty(f, pages)
for _, page := range pages {
f.Add(page)
}
f.Add([]byte(`{"Code":1000,"Events":[{"EventID":"e","EventType":0,"Link":{"LinkID":"dir","ParentLinkID":null}}],"EventID":"e","More":true}`))
f.Add([]byte(`{"Events":[{"EventType":99,"Link":{"LinkID":"root","ParentLinkID":"root","IsTrashed":true}}]}`))
f.Add([]byte(`{"Events":[{"EventType":2,"Link":{"LinkID":"a","ParentLinkID":"a"}}]}`))
f.Add([]byte(`{"Events":null,"More":1}`))
f.Fuzz(func(t *testing.T, data []byte) {
var resp api.EventsResponse
if json.Unmarshal(data, &resp) != nil {
return
}
c := eventsTestClient()
c.cache.put(&node{ID: "a", ParentID: "b", IsFolder: true})
c.cache.put(&node{ID: "b", ParentID: "a", IsFolder: true})
changes := eventChanges(resp.Events)
stale := c.applyEvents(changes)
require.Len(t, stale, len(changes))
for i, ch := range changes {
assert.NotEmpty(t, ch.linkID)
assert.Equal(t, ch.linkID, stale[i].id)
assert.Nil(t, c.cache.get(ch.linkID), "changed node %s still cached", ch.linkID)
}
})
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+8
View File
@@ -1,6 +1,7 @@
package protondrive_test package protondrive_test
import ( import (
"flag"
"testing" "testing"
"github.com/rclone/rclone/backend/protondrive" "github.com/rclone/rclone/backend/protondrive"
@@ -9,6 +10,13 @@ import (
// TestIntegration runs integration tests against the remote // TestIntegration runs integration tests against the remote
func TestIntegration(t *testing.T) { func TestIntegration(t *testing.T) {
// A golden capture runs with the real config, where this test would
// write to and delete from the live remote if -run let it through.
for _, name := range []string{"capture", "capture-remote"} {
if f := flag.Lookup(name); f != nil && f.Value.String() != "" {
t.Skipf("-%s is set: not running integration tests during a golden capture", name)
}
}
fstests.Run(t, &fstests.Opt{ fstests.Run(t, &fstests.Opt{
RemoteName: "TestProtonDrive:", RemoteName: "TestProtonDrive:",
NilObject: (*protondrive.Object)(nil), NilObject: (*protondrive.Object)(nil),
+340
View File
@@ -0,0 +1,340 @@
package protontest
import (
"encoding/base64"
"encoding/hex"
"fmt"
"net/http"
"github.com/ProtonMail/go-srp"
"github.com/ProtonMail/gopenpgp/v3/crypto"
"github.com/rclone/rclone/backend/protondrive/api"
)
// userKey is a user key with the password salt which locks it.
type userKey struct {
id string
salt string // base64
keyPassword string // derived from the mailbox password and salt
armored string // private key locked with keyPassword
key *crypto.Key
}
// address is an email address of a user.
type address struct {
id string
email string
keys []*addressKey // the primary key is first
}
// addressKey is an address key, locked with a random passphrase which
// is stored encrypted to and signed by the primary user key.
type addressKey struct {
id string
passphrase []byte
armored string // private key locked with passphrase
token string // armored passphrase encrypted to the user key
signature string // armored detached signature of passphrase by the user key
key *crypto.Key
}
// mustCrypto panics with a message naming what failed if err is not nil.
//
// Key operations on keys the fake generated itself fail only on a bug.
func mustCrypto(what string, err error) {
if err != nil {
panic(fmt.Sprintf("protontest: %s: %v", what, err))
}
}
// generateKey returns a new unlocked Curve25519 key for email, which
// is fast to generate unlike RSA.
func generateKey(name, email string) *crypto.Key {
key, err := crypto.PGP().KeyGeneration().AddUserId(name, email).GenerationTime(signTime()).New().GenerateKey()
mustCrypto("generate key", err)
return key
}
// lockKey returns key armored and locked with passphrase.
func lockKey(key *crypto.Key, passphrase []byte) string {
locked, err := crypto.PGP().LockKey(key, passphrase)
mustCrypto("lock key", err)
armored, err := locked.Armor()
mustCrypto("armor key", err)
return armored
}
// newUserKey returns a new user key for u locked with a key password
// derived from u's mailbox password and a new salt.
func newUserKey(u *User) *userKey {
rawSalt := randomBytes(16)
hashed, err := srp.MailboxPassword([]byte(u.MailboxPassword), rawSalt)
mustCrypto("hash mailbox password", err)
k := &userKey{
id: randomID(),
salt: base64.StdEncoding.EncodeToString(rawSalt),
keyPassword: string(hashed[len(hashed)-31:]),
key: generateKey(u.Username, u.Username),
}
k.armored = lockKey(k.key, []byte(k.keyPassword))
return k
}
// newAddress returns a new address with one address key whose
// passphrase is encrypted to userKey.
func newAddress(email string, userKey *userKey) *address {
passphrase := []byte(hex.EncodeToString(randomBytes(32)))
key := generateKey(email, email)
k := &addressKey{
id: randomID(),
passphrase: passphrase,
armored: lockKey(key, passphrase),
key: key,
}
k.sealPassphrase(userKey)
return &address{id: randomID(), email: email, keys: []*addressKey{k}}
}
// sealPassphrase sets the token and signature of k to its passphrase
// encrypted to and signed by userKey, as Proton stores it.
func (k *addressKey) sealPassphrase(userKey *userKey) {
eh, err := crypto.PGP().Encryption().Recipient(userKey.key).New()
mustCrypto("address key token", err)
msg, err := eh.Encrypt(k.passphrase)
mustCrypto("address key token", err)
token, err := msg.ArmorBytes()
mustCrypto("address key token", err)
sh, err := crypto.PGP().Sign().SigningKey(userKey.key).Detached().SignTime(signTime()).New()
mustCrypto("address key signature", err)
sig, err := sh.Sign(k.passphrase, crypto.Armor)
mustCrypto("address key signature", err)
k.token, k.signature = string(token), string(sig)
}
// KeyPassword returns the password which unlocks u's primary user key:
// the last 31 characters of the bcrypt hash of the mailbox password
// and the key salt, as a client derives it.
func (u *User) KeyPassword() string {
u.srv.mu.Lock()
defer u.srv.mu.Unlock()
return u.keyPassword
}
// Email returns u's primary address.
func (u *User) Email() string {
u.srv.mu.Lock()
defer u.srv.mu.Unlock()
return u.addresses[0].email
}
// AddAddress adds an address with a new address key to u.
func (u *User) AddAddress(email string) {
u.srv.mu.Lock()
primary := u.keys[len(u.keys)-1]
u.srv.mu.Unlock()
addr := newAddress(email, primary)
u.srv.mu.Lock()
defer u.srv.mu.Unlock()
u.addresses = append(u.addresses, addr)
}
// AddUserKey gives u a new primary user key with a new salt, so
// KeySalt and KeyPassword change.
//
// The old keys are listed before the new one and stay locked with
// their old key password, as after a key password change on an account
// with legacy keys. The address key passphrases are re-encrypted to the
// new key.
func (u *User) AddUserKey() {
key := newUserKey(u)
u.srv.mu.Lock()
defer u.srv.mu.Unlock()
u.keys = append(u.keys, key)
u.KeySalt, u.keyPassword = key.salt, key.keyPassword
for _, addr := range u.addresses {
for _, k := range addr.keys {
k.sealPassphrase(key)
}
}
}
// CorruptAddressKeySignature replaces the Signature of every key of u's
// address email, as a malicious server would: with a valid signature
// by an unrelated key, or with nothing if empty is true. AddUserKey
// restores the signatures.
func (s *Server) CorruptAddressKeySignature(u *User, email string, empty bool) {
impostor := generateKey("impostor", "impostor@proton.test")
s.mu.Lock()
defer s.mu.Unlock()
addr := u.addressByEmailLocked(email)
if addr == nil {
panic("protontest: no address " + email)
}
for _, k := range addr.keys {
if empty {
k.signature = ""
continue
}
// Sign the real passphrase, so only the signer is wrong.
sh, err := crypto.PGP().Sign().SigningKey(impostor).Detached().SignTime(signTime()).New()
mustCrypto("impostor signature", err)
sig, err := sh.Sign(k.passphrase, crypto.Armor)
mustCrypto("impostor signature", err)
k.signature = string(sig)
}
}
// EncryptToAddress returns plaintext as an armored message encrypted to
// the primary key of u's address email.
func (u *User) EncryptToAddress(email string, plaintext []byte) string {
u.srv.mu.Lock()
addr := u.addressByEmailLocked(email)
u.srv.mu.Unlock()
if addr == nil {
panic("protontest: no address " + email)
}
eh, err := crypto.PGP().Encryption().Recipient(addr.keys[0].key).New()
mustCrypto("encrypt to address", err)
msg, err := eh.Encrypt(plaintext)
mustCrypto("encrypt to address", err)
armored, err := msg.ArmorBytes()
mustCrypto("encrypt to address", err)
return string(armored)
}
// addressByEmailLocked returns u's address email or nil. srv.mu must
// be held.
func (u *User) addressByEmailLocked(email string) *address {
for _, addr := range u.addresses {
if addr.email == email {
return addr
}
}
return nil
}
// boolInt returns 1 for true and 0 for false, as the API encodes flags.
func boolInt(b bool) int {
if b {
return 1
}
return 0
}
// registerAccount registers the user, address and public key routes.
func (s *Server) registerAccount() {
s.handleAuth("GET", "/core/v4/users", s.handleUser)
s.handleAuth("GET", "/core/v4/addresses", s.handleAddresses)
s.handleAuth("GET", "/core/v4/keys/all", s.handlePublicKeys)
}
// userWithQuota is api.User with the quota fields of Proton's User
// schema (coreTypes.ts).
type userWithQuota struct {
api.User
UsedSpace int64
MaxSpace int64
ProductUsedSpace productUsedSpace
}
// productUsedSpace is the UserUsage schema: used space per product.
type productUsedSpace struct {
Calendar int64
Contact int64
Drive int64
Mail int64
Pass int64
Lumo int64
}
// handleUser returns the user, its keys and its quota, all used space
// being Drive's.
func (s *Server) handleUser(w http.ResponseWriter, r *http.Request, u *User) {
s.mu.Lock()
user := userWithQuota{
User: api.User{ID: u.ID, Name: u.Username, Email: u.addresses[0].email},
MaxSpace: DefaultMaxSpace,
}
for i, key := range u.keys {
user.Keys = append(user.Keys, api.UserKey{
ID: key.id,
Version: 3,
PrivateKey: key.armored,
Primary: boolInt(i == len(u.keys)-1),
Active: 1,
})
}
if u.drive != nil {
user.UsedSpace = u.drive.usedSpace()
user.ProductUsedSpace.Drive = user.UsedSpace
}
s.mu.Unlock()
writeJSON(w, http.StatusOK, &struct {
Code int
User userWithQuota
}{Code: api.CodeOK, User: user})
}
// handleAddresses returns the user's addresses with their keys.
func (s *Server) handleAddresses(w http.ResponseWriter, r *http.Request, u *User) {
s.mu.Lock()
resp := &api.AddressesResponse{Code: api.CodeOK}
for i, addr := range u.addresses {
out := api.Address{ID: addr.id, Email: addr.email, Status: 1, Type: 1, Priority: i + 1}
for j, key := range addr.keys {
out.Keys = append(out.Keys, api.AddressKey{
ID: key.id,
Version: 3,
PrivateKey: key.armored,
Token: key.token,
Signature: key.signature,
Primary: boolInt(j == 0),
Active: 1,
Flags: 3,
})
}
resp.Addresses = append(resp.Addresses, out)
}
s.mu.Unlock()
writeJSON(w, http.StatusOK, resp)
}
// handlePublicKeys returns the public keys of the address in the Email
// query parameter, or HTTP 422 code 33102 if no user has it.
func (s *Server) handlePublicKeys(w http.ResponseWriter, r *http.Request, _ *User) {
email := r.URL.Query().Get("Email")
if email == "" {
writeError(w, http.StatusBadRequest, api.CodeInvalidValue, "protontest: no Email")
return
}
s.mu.Lock()
var addr *address
if owner := s.keysAs[email]; owner != nil {
addr = owner.addresses[0]
}
for _, u := range s.users {
if addr != nil {
break
}
addr = u.addressByEmailLocked(email)
}
var keys []*addressKey
if addr != nil {
keys = addr.keys
}
s.mu.Unlock()
if addr == nil {
writeError(w, http.StatusUnprocessableEntity, api.CodeAddressMissing, "protontest: address does not exist")
return
}
resp := &api.PublicKeysResponse{Code: api.CodeOK}
for i, key := range keys {
pub, err := key.key.GetArmoredPublicKey()
if err != nil {
writeError(w, http.StatusInternalServerError, 0, "protontest: public key: "+err.Error())
return
}
resp.Address.Keys = append(resp.Address.Keys, api.PublicKey{PublicKey: pub, Primary: boolInt(i == 0), Flags: 3})
}
writeJSON(w, http.StatusOK, resp)
}
+555
View File
@@ -0,0 +1,555 @@
package protontest
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"sync"
"github.com/ProtonMail/go-srp"
"github.com/pquerna/otp/totp"
"github.com/rclone/rclone/backend/protondrive/api"
)
// signedModulus is a 2048-bit SRP modulus signed by Proton's modulus
// key, from go-srp's tests.
const signedModulus = `-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
W2z5HBi8RvsfYzZTS7qBaUxxPhsfHJFZpu3Kd6s1JafNrCCH9rfvPLrfuqocxWPgWDH2R8neK7PkNvjxto9TStuY5z7jAzWRvFWN9cQhAKkdWgy0JY6ywVn22+HFpF4cYesHrqFIKUPDMSSIlWjBVmEJZ/MusD44ZT29xcPrOqeZvwtCffKtGAIjLYPZIEbZKnDM1Dm3q2K/xS5h+xdhjnndhsrkwm9U9oyA2wxzSXFL+pdfj2fOdRwuR5nW0J2NFrq3kJjkRmpO/Genq1UW+TEknIWAb6VzJJJA244K/H8cnSx2+nSNZO3bbo6Ys228ruV9A8m6DhxmS+bihN3ttQ==
-----BEGIN PGP SIGNATURE-----
Version: ProtonMail
Comment: https://protonmail.com
wl4EARYIABAFAlwB1j0JEDUFhcTpUY8mAAD8CgEAnsFnF4cF0uSHKkXa1GIa
GO86yMV4zDZEZcDSJo0fgr8A/AlupGN9EdHlsrZLmTA1vhIx+rOgxdEff28N
kvNM7qIK
=q6vu
-----END PGP SIGNATURE-----`
// srpBits is the bit length of the SRP modulus.
const srpBits = 2048
// forkClientID is the client ID rclone's sign-in URL must carry.
const forkClientID = "external-drive"
// User is an account on the fake server.
type User struct {
ID string
Username string
Password string // login password, checked with SRP
MailboxPassword string // derives the key password
KeySalt string // base64 salt of the primary user key's password
srv *Server
srpSalt []byte
// Guarded by srv.mu once the user is added.
keyPassword string
keys []*userKey // the primary key is last
addresses []*address // the primary address is first
drive *drive // nil until the user has a volume
verifierOnce sync.Once
verifier []byte
verifierErr error
// Guarded by srv.mu.
totpSecret string
captcha bool
corruptProof bool
}
// srpVerifier returns the SRP verifier of u's login password, computing
// it on first use since it costs a bcrypt hash.
func (u *User) srpVerifier() ([]byte, error) {
u.verifierOnce.Do(func() {
var auth *srp.Auth
auth, u.verifierErr = srp.NewAuthForVerifier([]byte(u.Password), signedModulus, u.srpSalt)
if u.verifierErr == nil {
u.verifier, u.verifierErr = auth.GenerateVerifier(srpBits)
}
})
return u.verifier, u.verifierErr
}
// EnableTOTP turns on TOTP two-factor authentication for u and returns
// the base32 secret to generate codes from.
func (u *User) EnableTOTP() (secret string) {
key, err := totp.Generate(totp.GenerateOpts{Issuer: "protontest", AccountName: u.Username})
if err != nil {
panic(fmt.Sprintf("protontest: generate TOTP secret: %v", err))
}
u.srv.mu.Lock()
defer u.srv.mu.Unlock()
u.totpSecret = key.Secret()
return u.totpSecret
}
// srpLogin is an SRP challenge issued by POST /core/v4/auth/info.
type srpLogin struct {
user *User
server *srp.Server
}
// fork is a session fork requested by GET /auth/v4/sessions/forks.
type fork struct {
userCode string
key []byte // encryption key from the sign-in URL, once approved
approved *session // child session, once approved
}
// Session holds the tokens of a signed in session.
type Session struct {
UID string
AccessToken string
RefreshToken string
}
// session is the server side state of a Session.
type session struct {
Session
user *User
expired bool // access token no longer accepted
locked bool // waiting for the second factor
}
// randomBytes returns n random bytes.
func randomBytes(n int) []byte {
b := make([]byte, n)
_, _ = rand.Read(b)
return b
}
// randomID returns a random hex string.
func randomID() string {
return hex.EncodeToString(randomBytes(16))
}
// registerAuth registers the session routes.
func (s *Server) registerAuth() {
s.handle("POST", "/auth/v4/refresh", s.handleRefresh)
s.handle("POST", "/core/v4/auth/info", s.handleAuthInfo)
s.handle("POST", "/core/v4/auth", s.handleLogin)
s.handleSession("POST", "/core/v4/auth/2fa", s.handle2FA)
s.handleSession("DELETE", "/core/v4/auth", s.handleLogout)
s.handleAuth("GET", "/core/v4/keys/salts", s.handleKeySalts)
s.handle("GET", "/auth/v4/sessions/forks", s.handleForkInit)
s.handle("GET", "/auth/v4/sessions/forks/{selector}", s.handleForkStatus)
s.handleAuth("GET", "/tests/auth", func(w http.ResponseWriter, r *http.Request, u *User) {
writeJSON(w, http.StatusOK, map[string]any{"Code": api.CodeOK, "UserID": u.ID})
})
s.handleAuth("POST", "/tests/echo", func(w http.ResponseWriter, r *http.Request, u *User) {
var body struct{ Echo string }
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeError(w, http.StatusBadRequest, api.CodeInvalidValue, "protontest: "+err.Error())
return
}
writeJSON(w, http.StatusOK, map[string]any{"Code": api.CodeOK, "Echo": body.Echo})
})
}
// AddUser creates an account whose mailbox password is its password.
func (s *Server) AddUser(username, password string) *User {
return s.AddTwoPasswordUser(username, password, password)
}
// AddTwoPasswordUser creates an account which signs in with password
// and unlocks its keys with mailboxPassword.
//
// The account has a user key and one address, username itself if it
// contains "@" or else username@proton.test, with an address key. Its
// drive has a volume, a main share and an empty root folder.
func (s *Server) AddTwoPasswordUser(username, password, mailboxPassword string) *User {
u := newUser(s, username, password, mailboxPassword)
u.drive = s.newDrive(u.addresses[0])
s.addUser(u)
return u
}
// AddUserWithoutDrive creates an account like AddUser but without a
// volume, as a brand-new Proton account is: its my-files route answers
// HTTP 422 code 2501 until a client creates the volume.
func (s *Server) AddUserWithoutDrive(username, password string) *User {
u := newUser(s, username, password, password)
s.addUser(u)
return u
}
// newUser returns an account with a user key and one address, not yet
// added to s.
func newUser(s *Server, username, password, mailboxPassword string) *User {
u := &User{
ID: randomID(),
Username: username,
Password: password,
MailboxPassword: mailboxPassword,
srv: s,
srpSalt: randomBytes(10),
}
key := newUserKey(u)
u.keys = []*userKey{key}
u.KeySalt, u.keyPassword = key.salt, key.keyPassword
email := username
if !strings.Contains(email, "@") {
email += "@proton.test"
}
u.addresses = []*address{newAddress(email, key)}
return u
}
// addUser adds u to s.
func (s *Server) addUser(u *User) {
s.mu.Lock()
defer s.mu.Unlock()
s.users[u.Username] = u
}
// RequireCaptcha makes password sign-in for u fail with HTTP 422 code
// 9001, as when Proton asks for human verification.
func (s *Server) RequireCaptcha(u *User) {
s.mu.Lock()
defer s.mu.Unlock()
u.captcha = true
}
// CorruptServerProof makes password sign-in for u return a server
// proof with one byte changed, as an impostor server would.
func (s *Server) CorruptServerProof(u *User) {
s.mu.Lock()
defer s.mu.Unlock()
u.corruptProof = true
}
// NewSession signs u in and returns the new session's tokens.
func (s *Server) NewSession(u *User) Session {
s.mu.Lock()
defer s.mu.Unlock()
return s.newSessionLocked(u, false).Session
}
// newSessionLocked creates a session for u. s.mu must be held.
func (s *Server) newSessionLocked(u *User, locked bool) *session {
sess := &session{
Session: Session{UID: randomID(), AccessToken: s.newSecretLocked(SessionToken), RefreshToken: s.newSecretLocked(SessionToken)},
user: u,
locked: locked,
}
s.sessions[sess.UID] = sess
return sess
}
// ApproveFork completes the session fork whose sign-in URL is
// signInURL as if u had signed in on Proton's page: the next poll of
// the fork returns a new session for u and u's key password encrypted
// with the key from the URL.
func (s *Server) ApproveFork(signInURL string, u *User) error {
parsed, err := url.Parse(signInURL)
if err != nil {
return fmt.Errorf("protontest: parse sign-in URL: %w", err)
}
payload, ok := strings.CutPrefix(parsed.Fragment, "payload=")
parts := strings.Split(payload, ":")
if !ok || len(parts) != 4 || parts[0] != "0" || parts[3] != forkClientID {
return fmt.Errorf("protontest: bad sign-in URL payload %q", payload)
}
key, err := base64.StdEncoding.DecodeString(parts[2])
if err != nil || len(key) != 32 {
return fmt.Errorf("protontest: bad key in sign-in URL payload %q", payload)
}
s.mu.Lock()
defer s.mu.Unlock()
for _, f := range s.forks {
if f.userCode == parts[1] {
f.key = key
f.approved = s.newSessionLocked(u, false)
return nil
}
}
return fmt.Errorf("protontest: no fork with user code %q", parts[1])
}
// ExpireAccessToken makes the access token of session uid return 401
// until the session is refreshed.
func (s *Server) ExpireAccessToken(uid string) {
s.mu.Lock()
defer s.mu.Unlock()
if sess := s.sessions[uid]; sess != nil {
sess.expired = true
}
}
// RevokeSession ends session uid, so neither its access token nor its
// refresh token is accepted.
func (s *Server) RevokeSession(uid string) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.sessions, uid)
}
// handleAuth registers h for an authenticated route. Requests without
// a valid x-pm-uid and bearer access token get HTTP 401, and requests
// from a session waiting for its second factor get HTTP 403.
func (s *Server) handleAuth(method, pattern string, h func(w http.ResponseWriter, r *http.Request, u *User)) {
s.handleSession(method, pattern, func(w http.ResponseWriter, r *http.Request, sess *session) {
s.mu.Lock()
locked := sess.locked
s.mu.Unlock()
if locked {
writeError(w, http.StatusForbidden, 0, "protontest: second factor required")
return
}
h(w, r, sess.user)
})
}
// handleSession registers h for a route which needs a session, which
// may be waiting for its second factor. Requests without a valid
// x-pm-uid and bearer access token get HTTP 401.
func (s *Server) handleSession(method, pattern string, h func(w http.ResponseWriter, r *http.Request, sess *session)) {
s.handle(method, pattern, func(w http.ResponseWriter, r *http.Request) {
token, ok := strings.CutPrefix(r.Header.Get("Authorization"), "Bearer ")
s.mu.Lock()
sess := s.sessions[r.Header.Get("x-pm-uid")]
valid := ok && sess != nil && !sess.expired && sess.AccessToken == token
s.mu.Unlock()
if !valid {
writeError(w, http.StatusUnauthorized, 401, "protontest: invalid access token")
return
}
h(w, r, sess)
})
}
// handleRefresh rotates the tokens of the session named by x-pm-uid.
// Refresh tokens are single use.
func (s *Server) handleRefresh(w http.ResponseWriter, r *http.Request) {
var req api.AuthRefreshRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.ResponseType != "token" || req.GrantType != "refresh_token" {
writeError(w, http.StatusBadRequest, api.CodeInvalidValue, "protontest: bad refresh request")
return
}
s.mu.Lock()
defer s.mu.Unlock()
sess := s.sessions[r.Header.Get("x-pm-uid")]
if sess == nil || req.RefreshToken == "" || sess.RefreshToken != req.RefreshToken {
writeError(w, http.StatusUnprocessableEntity, api.CodeRefreshTokenInvalid, "protontest: invalid refresh token")
return
}
sess.AccessToken = s.newSecretLocked(SessionToken)
sess.RefreshToken = s.newSecretLocked(SessionToken)
sess.expired = false
writeJSON(w, http.StatusOK, &api.AuthRefreshResponse{
Code: api.CodeOK,
UID: sess.UID,
AccessToken: sess.AccessToken,
RefreshToken: sess.RefreshToken,
})
}
// handleAuthInfo issues an SRP challenge for the user named in the
// request.
func (s *Server) handleAuthInfo(w http.ResponseWriter, r *http.Request) {
var req api.AuthInfoRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.Intent != "Proton" {
writeError(w, http.StatusBadRequest, api.CodeInvalidValue, "protontest: bad auth info request")
return
}
s.mu.Lock()
u := s.users[req.Username]
s.mu.Unlock()
if u == nil {
writeError(w, http.StatusUnprocessableEntity, api.CodePasswordWrong, "protontest: unknown user")
return
}
verifier, err := u.srpVerifier()
if err != nil {
writeError(w, http.StatusInternalServerError, 0, "protontest: SRP verifier: "+err.Error())
return
}
server, err := srp.NewServerFromSigned(signedModulus, verifier, srpBits)
if err != nil {
writeError(w, http.StatusInternalServerError, 0, "protontest: SRP server: "+err.Error())
return
}
challenge, err := server.GenerateChallenge()
if err != nil {
writeError(w, http.StatusInternalServerError, 0, "protontest: SRP challenge: "+err.Error())
return
}
srpSession := randomID()
s.mu.Lock()
s.srp[srpSession] = &srpLogin{user: u, server: server}
s.mu.Unlock()
writeJSON(w, http.StatusOK, &api.AuthInfoResponse{
Code: api.CodeOK,
Version: 4,
Modulus: signedModulus,
ServerEphemeral: base64.StdEncoding.EncodeToString(challenge),
Salt: base64.StdEncoding.EncodeToString(u.srpSalt),
SRPSession: srpSession,
Username: u.Username,
})
}
// handleLogin checks the SRP proof for a challenge and creates a
// session, locked until the second factor if the user has TOTP.
// Challenges are single use.
func (s *Server) handleLogin(w http.ResponseWriter, r *http.Request) {
var req api.AuthRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, api.CodeInvalidValue, "protontest: bad auth request")
return
}
ephemeral, err1 := base64.StdEncoding.DecodeString(req.ClientEphemeral)
proof, err2 := base64.StdEncoding.DecodeString(req.ClientProof)
if err := errors.Join(err1, err2); err != nil {
writeError(w, http.StatusBadRequest, api.CodeInvalidValue, "protontest: bad auth request: "+err.Error())
return
}
s.mu.Lock()
defer s.mu.Unlock()
login := s.srp[req.SRPSession]
delete(s.srp, req.SRPSession)
if login == nil || login.user.Username != req.Username {
writeError(w, http.StatusUnprocessableEntity, api.CodeInvalidValue, "protontest: unknown SRP session")
return
}
u := login.user
if u.captcha {
writeError(w, http.StatusUnprocessableEntity, api.CodeHumanVerification, "protontest: human verification required")
return
}
serverProof, err := login.server.VerifyProofs(ephemeral, proof)
if err != nil {
writeError(w, http.StatusUnprocessableEntity, api.CodePasswordWrong, "protontest: incorrect login credentials")
return
}
if u.corruptProof {
serverProof[0] ^= 1
}
resp := &api.AuthResponse{
Code: api.CodeOK,
UserID: u.ID,
TokenType: "Bearer",
ServerProof: base64.StdEncoding.EncodeToString(serverProof),
Scopes: []string{"full"},
PasswordMode: api.PasswordModeOne,
}
if u.MailboxPassword != u.Password {
resp.PasswordMode = api.PasswordModeTwo
}
if u.totpSecret != "" {
resp.TwoFA.Enabled = api.TwoFATOTP
resp.Scopes = []string{"twofactor"}
}
sess := s.newSessionLocked(u, u.totpSecret != "")
resp.UID, resp.AccessToken, resp.RefreshToken = sess.UID, sess.AccessToken, sess.RefreshToken
writeJSON(w, http.StatusOK, resp)
}
// handle2FA unlocks a session with a TOTP code.
func (s *Server) handle2FA(w http.ResponseWriter, r *http.Request, sess *session) {
var req api.Auth2FARequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, api.CodeInvalidValue, "protontest: bad 2FA request")
return
}
s.mu.Lock()
defer s.mu.Unlock()
secret := sess.user.totpSecret
if secret == "" || !totp.Validate(req.TwoFactorCode, secret) {
writeError(w, http.StatusUnprocessableEntity, api.CodeInvalidValue, "protontest: incorrect 2FA code")
return
}
sess.locked = false
writeJSON(w, http.StatusOK, map[string]any{"Code": api.CodeOK, "Scopes": []string{"full"}})
}
// handleLogout ends the session.
func (s *Server) handleLogout(w http.ResponseWriter, r *http.Request, sess *session) {
s.mu.Lock()
delete(s.sessions, sess.UID)
s.mu.Unlock()
writeJSON(w, http.StatusOK, map[string]any{"Code": api.CodeOK})
}
// handleKeySalts returns the password salt of each user key.
func (s *Server) handleKeySalts(w http.ResponseWriter, r *http.Request, u *User) {
resp := &api.KeySaltsResponse{Code: api.CodeOK}
s.mu.Lock()
for _, key := range u.keys {
resp.KeySalts = append(resp.KeySalts, api.KeySalt{ID: key.id, KeySalt: key.salt})
}
s.mu.Unlock()
writeJSON(w, http.StatusOK, resp)
}
// handleForkInit starts a session fork for a client to sign in with.
func (s *Server) handleForkInit(w http.ResponseWriter, r *http.Request) {
f := &fork{userCode: strings.ToUpper(hex.EncodeToString(randomBytes(4)))}
s.mu.Lock()
selector := s.newSecretLocked(ForkSelector)
s.forks[selector] = f
s.mu.Unlock()
writeJSON(w, http.StatusOK, &api.ForkInitResponse{Code: api.CodeOK, Selector: selector, UserCode: f.userCode})
}
// handleForkStatus returns the child session of an approved fork, once,
// or HTTP 422 while the fork is waiting for approval.
func (s *Server) handleForkStatus(w http.ResponseWriter, r *http.Request) {
selector := r.PathValue("selector")
s.mu.Lock()
f := s.forks[selector]
if f == nil || f.approved == nil {
s.mu.Unlock()
writeError(w, http.StatusUnprocessableEntity, api.CodeInvalidValue, "protontest: fork not ready")
return
}
delete(s.forks, selector)
sess := f.approved.Session
user := f.approved.user
s.mu.Unlock()
payload, err := sealForkPayload(f.key, user.KeyPassword())
if err != nil {
writeError(w, http.StatusInternalServerError, 0, "protontest: fork payload: "+err.Error())
return
}
writeJSON(w, http.StatusOK, &api.ForkStatusResponse{
Code: api.CodeOK,
Payload: payload,
UID: sess.UID,
AccessToken: sess.AccessToken,
RefreshToken: sess.RefreshToken,
})
}
// sealForkPayload encrypts keyPassword for a fork as Proton's sign-in
// page does: base64 of nonce, AES-256-GCM ciphertext and tag, with
// additional data "fork".
func sealForkPayload(key []byte, keyPassword string) (string, error) {
plain, err := json.Marshal(map[string]string{"keyPassword": keyPassword})
if err != nil {
return "", err
}
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
nonce := randomBytes(gcm.NonceSize())
return base64.StdEncoding.EncodeToString(gcm.Seal(nonce, nonce, plain, []byte("fork"))), nil
}
+139
View File
@@ -0,0 +1,139 @@
package protontest
import (
"bytes"
"encoding/base64"
"encoding/json"
"io"
"net/http"
"testing"
"time"
"github.com/ProtonMail/go-srp"
"github.com/pquerna/otp/totp"
"github.com/rclone/rclone/backend/protondrive/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// refresh calls POST /auth/v4/refresh for uid with refreshToken.
func refresh(t *testing.T, s *Server, uid, refreshToken string) (*http.Response, []byte) {
t.Helper()
body, err := json.Marshal(&api.AuthRefreshRequest{ResponseType: "token", GrantType: "refresh_token", RefreshToken: refreshToken})
require.NoError(t, err)
req, err := http.NewRequest("POST", s.URL()+"/auth/v4/refresh", bytes.NewReader(body))
require.NoError(t, err)
req.Header.Set("x-pm-appversion", testAppVersion)
req.Header.Set("x-pm-uid", uid)
resp, err := s.Client().Do(req)
require.NoError(t, err)
defer func() { _ = resp.Body.Close() }()
out, err := io.ReadAll(resp.Body)
require.NoError(t, err)
return resp, out
}
func authed(t *testing.T, s *Server, sess Session) int {
t.Helper()
resp, _ := do(t, s, "GET", "/tests/auth", map[string]string{
"x-pm-appversion": testAppVersion,
"x-pm-uid": sess.UID,
"Authorization": "Bearer " + sess.AccessToken,
})
return resp.StatusCode
}
func TestAuth(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
assert.Equal(t, "pass", u.MailboxPassword)
sess := s.NewSession(u)
assert.Equal(t, http.StatusOK, authed(t, s, sess))
assert.Equal(t, http.StatusUnauthorized, authed(t, s, Session{UID: sess.UID, AccessToken: "wrong"}))
assert.Equal(t, http.StatusUnauthorized, authed(t, s, Session{UID: "unknown", AccessToken: sess.AccessToken}))
s.ExpireAccessToken(sess.UID)
assert.Equal(t, http.StatusUnauthorized, authed(t, s, sess))
resp, body := refresh(t, s, sess.UID, sess.RefreshToken)
require.Equal(t, http.StatusOK, resp.StatusCode, "body: %s", body)
var out api.AuthRefreshResponse
require.NoError(t, json.Unmarshal(body, &out))
assert.Equal(t, sess.UID, out.UID)
next := Session{UID: out.UID, AccessToken: out.AccessToken, RefreshToken: out.RefreshToken}
assert.Equal(t, http.StatusOK, authed(t, s, next))
assert.Equal(t, http.StatusUnauthorized, authed(t, s, sess), "old access token")
// Refresh tokens are single use.
resp, body = refresh(t, s, sess.UID, sess.RefreshToken)
assert.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
assert.Equal(t, api.CodeRefreshTokenInvalid, decodeError(t, body).Code)
s.RevokeSession(sess.UID)
assert.Equal(t, http.StatusUnauthorized, authed(t, s, next))
resp, body = refresh(t, s, next.UID, next.RefreshToken)
assert.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
assert.Equal(t, api.CodeRefreshTokenInvalid, decodeError(t, body).Code)
}
// postJSON sends in as JSON to path with the given extra headers and
// decodes the response into out, returning the HTTP status.
func postJSON(t *testing.T, s *Server, path string, header map[string]string, in, out any) int {
t.Helper()
body, err := json.Marshal(in)
require.NoError(t, err)
req, err := http.NewRequest("POST", s.URL()+path, bytes.NewReader(body))
require.NoError(t, err)
req.Header.Set("x-pm-appversion", testAppVersion)
for k, v := range header {
req.Header.Set(k, v)
}
resp, err := s.Client().Do(req)
require.NoError(t, err)
defer func() { _ = resp.Body.Close() }()
require.NoError(t, json.NewDecoder(resp.Body).Decode(out))
return resp.StatusCode
}
func TestLoginTOTPLocksSession(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
secret := u.EnableTOTP()
var info api.AuthInfoResponse
require.Equal(t, http.StatusOK, postJSON(t, s, "/core/v4/auth/info", nil, &api.AuthInfoRequest{Intent: "Proton", Username: "user"}, &info))
auth, err := srp.NewAuth(info.Version, "user", []byte("pass"), info.Salt, info.Modulus, info.ServerEphemeral)
require.NoError(t, err)
proofs, err := auth.GenerateProofs(srpBits)
require.NoError(t, err)
var out api.AuthResponse
require.Equal(t, http.StatusOK, postJSON(t, s, "/core/v4/auth", nil, &api.AuthRequest{
Username: "user",
ClientEphemeral: base64.StdEncoding.EncodeToString(proofs.ClientEphemeral),
ClientProof: base64.StdEncoding.EncodeToString(proofs.ClientProof),
SRPSession: info.SRPSession,
}, &out))
assert.Equal(t, base64.StdEncoding.EncodeToString(proofs.ExpectedServerProof), out.ServerProof)
assert.Equal(t, api.TwoFATOTP, out.TwoFA.Enabled)
sess := Session{UID: out.UID, AccessToken: out.AccessToken, RefreshToken: out.RefreshToken}
assert.Equal(t, http.StatusForbidden, authed(t, s, sess))
headers := map[string]string{"x-pm-uid": sess.UID, "Authorization": "Bearer " + sess.AccessToken}
var res api.Error
assert.Equal(t, http.StatusUnprocessableEntity, postJSON(t, s, "/core/v4/auth/2fa", headers, &api.Auth2FARequest{TwoFactorCode: "000000x"}, &res))
assert.Equal(t, http.StatusForbidden, authed(t, s, sess))
code, err := totp.GenerateCode(secret, time.Now())
require.NoError(t, err)
assert.Equal(t, http.StatusOK, postJSON(t, s, "/core/v4/auth/2fa", headers, &api.Auth2FARequest{TwoFactorCode: code}, &res))
assert.Equal(t, http.StatusOK, authed(t, s, sess))
// SRP challenges are single use.
assert.Equal(t, http.StatusUnprocessableEntity, postJSON(t, s, "/core/v4/auth", nil, &api.AuthRequest{
Username: "user",
ClientEphemeral: base64.StdEncoding.EncodeToString(proofs.ClientEphemeral),
ClientProof: base64.StdEncoding.EncodeToString(proofs.ClientProof),
SRPSession: info.SRPSession,
}, &res))
}
@@ -0,0 +1,58 @@
// Command protonfake serves a fake Proton Drive API for integration
// tests.
//
// It prints the rclone config of a signed in user with a Drive volume
// as key=value lines on stdout, then serves until interrupted.
//
// go run ./backend/protondrive/protontest/cmd/protonfake -addr 127.0.0.1:0
package main
import (
"encoding/base64"
"flag"
"fmt"
"io"
"os"
"os/signal"
"syscall"
"github.com/rclone/rclone/backend/protondrive/protontest"
)
func main() {
addr := flag.String("addr", "127.0.0.1:0", "address to listen on")
flag.Parse()
stop := make(chan struct{})
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
go func() {
<-sig
close(stop)
}()
if err := run(*addr, os.Stdout, stop); err != nil {
fmt.Fprintln(os.Stderr, "protonfake:", err)
os.Exit(1)
}
}
// run serves a fake server on addr until stop is closed, after writing
// its endpoint and a fresh session's config to out.
func run(addr string, out io.Writer, stop <-chan struct{}) error {
srv, err := protontest.NewStandalone(addr)
if err != nil {
return err
}
defer srv.Close()
user := srv.AddUser("protonfake", "protonfake")
sess := srv.NewSession(user)
// The config holds the salted key password base64 encoded.
keyPass := base64.StdEncoding.EncodeToString([]byte(user.KeyPassword()))
// Events are polled every second, so tests see changes quickly.
_, err = fmt.Fprintf(out, "endpoint=%s\nclient_uid=%s\nclient_access_token=%s\nclient_refresh_token=%s\nclient_salted_key_pass=%s\nevents_min_interval=1s\ntype=protondrive\n",
srv.URL(), sess.UID, sess.AccessToken, sess.RefreshToken, keyPass)
if err != nil {
return fmt.Errorf("write config: %w", err)
}
<-stop
return nil
}
@@ -0,0 +1,77 @@
package main
import (
"bufio"
"encoding/base64"
"io"
"net/http"
"strings"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRun(t *testing.T) {
pr, pw := io.Pipe()
stop := make(chan struct{})
var stopOnce sync.Once
stopRun := func() { stopOnce.Do(func() { close(stop) }) }
done := make(chan error, 1)
finished := make(chan struct{})
go func() {
defer close(finished)
err := run("127.0.0.1:0", pw, stop)
_ = pw.Close()
done <- err
}()
// However the test ends, run is stopped and waited for.
t.Cleanup(func() {
stopRun()
_ = pr.Close()
<-finished
})
wantKeys := []string{"endpoint", "client_uid", "client_access_token", "client_refresh_token", "client_salted_key_pass", "events_min_interval", "type"}
values := make(map[string]string)
lines := bufio.NewScanner(pr)
for _, key := range wantKeys {
require.True(t, lines.Scan(), "missing line for %s", key)
k, v, ok := strings.Cut(lines.Text(), "=")
require.True(t, ok, "line %q is not key=value", lines.Text())
require.Equal(t, key, k)
require.NotEmpty(t, v, key)
values[k] = v
}
assert.Equal(t, "protondrive", values["type"])
assert.Equal(t, "1s", values["events_min_interval"])
assert.False(t, strings.HasSuffix(values["endpoint"], ":0"), "endpoint %q must carry the bound port", values["endpoint"])
keyPass, err := base64.StdEncoding.DecodeString(values["client_salted_key_pass"])
require.NoError(t, err)
assert.Len(t, keyPass, 31, "a key password is the last 31 characters of a bcrypt hash")
req, err := http.NewRequest("GET", values["endpoint"]+"/core/v4/users", nil)
require.NoError(t, err)
req.Header.Set("x-pm-appversion", "external-drive-rclone@1.0.0-stable")
req.Header.Set("x-pm-uid", values["client_uid"])
req.Header.Set("Authorization", "Bearer "+values["client_access_token"])
resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
_ = resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
stopRun()
extra := lines.Scan()
assert.False(t, extra, "unexpected output %q", lines.Text())
require.NoError(t, <-done)
_, err = http.Get(values["endpoint"] + "/tests/ping")
assert.Error(t, err, "server still answering after stop")
}
func TestRunBadAddress(t *testing.T) {
var out strings.Builder
err := run("not an address", &out, make(chan struct{}))
assert.Error(t, err)
assert.Empty(t, out.String())
}
+339
View File
@@ -0,0 +1,339 @@
package protontest
// The Drive cryptography of the fake, written from Proton's Drive SDK
// (client/js/src/crypto/driveCrypto.ts and the nodes, shares and upload
// crypto services) independently of the protondrive backend, so a bug
// in one is unlikely to be mirrored in the other.
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"time"
"github.com/ProtonMail/go-crypto/openpgp/packet"
"github.com/ProtonMail/gopenpgp/v3/constants"
"github.com/ProtonMail/gopenpgp/v3/crypto"
"github.com/ProtonMail/gopenpgp/v3/profile"
)
// driveKeyName is the user ID of every node and share key, as the SDK
// generates them.
const driveKeyName = "Drive key"
// aeadPGP returns a handle whose keys advertise SEIPDv2 and whose
// session-key encryption uses AES-256 GCM, as the SDK's file content
// does (openpgp.js aeadProtect with Proton's GCM preference).
//
// Everything that is not file content goes through encryptArmored,
// which never produces SEIPDv2 whatever the recipient advertises.
func aeadPGP() *crypto.PGPHandle {
p := profile.Default()
p.AeadEncryption = &packet.AEADConfig{DefaultMode: packet.AEADModeGCM}
return crypto.PGPWithProfile(p)
}
// signTime returns when the fake generates keys and makes signatures: a
// day before the local clock, so that what it writes is valid at the
// server clock whatever skew within a day a test sets.
func signTime() int64 {
return time.Now().Add(-24 * time.Hour).Unix()
}
// signTimeOf returns when the fake signs with signer: signTime, or
// signer's creation time if later, as for a key a test supplied which
// was generated less than a day ago.
func signTimeOf(signer *crypto.Key) int64 {
return max(signTime(), signer.GetEntity().PrimaryKey.CreationTime.Unix())
}
// newPassphrase returns base64 of 32 random bytes, the SDK's
// generatePassphrase.
func newPassphrase() []byte {
return []byte(base64.StdEncoding.EncodeToString(randomBytes(32)))
}
// lockedKey is a generated key with its passphrase and armored locked
// form.
type lockedKey struct {
key *crypto.Key
passphrase []byte
armored string
}
// newDriveKey generates a node or share key locked with a new
// passphrase. aead makes the key advertise SEIPDv2, as file node keys
// do; folder and share keys do not.
func newDriveKey(aead bool) lockedKey {
pgp := crypto.PGP()
if aead {
pgp = aeadPGP()
}
key, err := pgp.KeyGeneration().AddUserId(driveKeyName, "").GenerationTime(signTime()).New().GenerateKey()
mustCrypto("generate drive key", err)
passphrase := newPassphrase()
return lockedKey{key: key, passphrase: passphrase, armored: lockKey(key, passphrase)}
}
// sealedPassphrase is a node or share passphrase as the server stores it.
type sealedPassphrase struct {
armored string // encrypted to the parent key
signature string // armored detached signature of the plaintext
}
// sealPassphrase encrypts passphrase to parent without AEAD and signs it
// with signer in a detached signature, the SDK's encryptPassphrase.
func sealPassphrase(passphrase []byte, parent, signer *crypto.Key) sealedPassphrase {
return sealedPassphrase{
armored: encryptArmored(passphrase, parent, nil, false),
signature: signArmored(passphrase, signer),
}
}
// encryptArmored encrypts data to key without AEAD, signed inline by
// signer if not nil, compressed if compress is set.
//
// Encrypting to recipients picks SEIPDv2 whenever every recipient key
// advertises it, as file node keys do, so the session key is generated
// and wrapped separately: the SDK's ignoreSEIPDv2FeatureFlag.
func encryptArmored(data []byte, key, signer *crypto.Key, compress bool) string {
sk, err := crypto.GenerateSessionKeyAlgo(constants.AES256)
mustCrypto("session key", err)
kh, err := crypto.PGP().Encryption().Recipient(key).New()
mustCrypto("encrypt session key", err)
keyPacket, err := kh.EncryptSessionKey(sk)
mustCrypto("encrypt session key", err)
b := crypto.PGP().Encryption().SessionKey(sk)
if signer != nil {
b = b.SigningKey(signer).SignTime(signTimeOf(signer))
}
if compress {
b = b.Compress()
}
eh, err := b.New()
mustCrypto("encrypt", err)
msg, err := eh.Encrypt(data)
mustCrypto("encrypt", err)
armored, err := crypto.NewPGPMessage(append(keyPacket, msg.DataPacket...)).Armor()
mustCrypto("armor message", err)
return armored
}
// signArmored returns an armored detached signature of data by signer.
func signArmored(data []byte, signer *crypto.Key) string {
sh, err := crypto.PGP().Sign().SigningKey(signer).Detached().SignTime(signTimeOf(signer)).New()
mustCrypto("sign", err)
sig, err := sh.Sign(data, crypto.Armor)
mustCrypto("sign", err)
return string(sig)
}
// signBinary returns a binary detached signature of data by signer.
func signBinary(data []byte, signer *crypto.Key) []byte {
sh, err := crypto.PGP().Sign().SigningKey(signer).Detached().SignTime(signTimeOf(signer)).New()
mustCrypto("sign", err)
sig, err := sh.Sign(data, crypto.Bytes)
mustCrypto("sign", err)
return sig
}
// newHashKey returns a new folder hash key and its armored form,
// encrypted to and signed by nodeKey, the SDK's generateHashKey.
func newHashKey(nodeKey *crypto.Key) (hashKey []byte, armored string) {
hashKey = newPassphrase()
return hashKey, encryptArmored(hashKey, nodeKey, nodeKey, false)
}
// lookupHash returns the hex HMAC-SHA256 of name keyed with the parent
// folder's hash key, the SDK's generateLookupHash.
func lookupHash(parentHashKey []byte, name string) string {
mac := hmac.New(sha256.New, parentHashKey)
mac.Write([]byte(name))
return hex.EncodeToString(mac.Sum(nil))
}
// contentKey is a file's content session key with its key packet.
type contentKey struct {
sk *crypto.SessionKey
packet string // base64 PKESK to the node key
signature string // armored detached signature of the session key by the node key
aead bool
}
// newContentKey generates a content session key for nodeKey, the SDK's
// generateContentKey. With aead the key packet is a v6 PKESK and blocks
// are SEIPDv2; without, the legacy v3 PKESK and SEIPDv1.
func newContentKey(nodeKey *crypto.Key, aead bool) contentKey {
token := randomBytes(32)
sk := crypto.NewSessionKeyFromTokenWithAead(token, constants.AES256, aead)
eh, err := crypto.PGP().Encryption().Recipient(nodeKey).New()
mustCrypto("content key packet", err)
keyPacket, err := eh.EncryptSessionKey(sk)
mustCrypto("content key packet", err)
return contentKey{
sk: sk,
packet: base64.StdEncoding.EncodeToString(keyPacket),
signature: signArmored(sk.Key, nodeKey),
aead: aead,
}
}
// pgp returns the handle for encrypting with ck.
func (ck contentKey) pgp() *crypto.PGPHandle {
if ck.aead {
return aeadPGP()
}
return crypto.PGP()
}
// encryptBlock encrypts one block of plaintext with the content key and
// returns the data packet and the armored block signature: a detached
// signature by signer encrypted to nodeKey with the content key, the
// SDK's encryptBlock.
func (ck contentKey) encryptBlock(plain []byte, nodeKey, signer *crypto.Key) (data []byte, encSignature string) {
eh, err := ck.pgp().Encryption().SessionKey(ck.sk).New()
mustCrypto("encrypt block", err)
msg, err := eh.Encrypt(plain)
mustCrypto("encrypt block", err)
sigHandle, err := ck.pgp().Encryption().Recipient(nodeKey).SessionKey(ck.sk).New()
mustCrypto("encrypt block signature", err)
sigMsg, err := sigHandle.Encrypt(signBinary(plain, signer))
mustCrypto("encrypt block signature", err)
encSignature, err = sigMsg.Armor()
mustCrypto("armor block signature", err)
return msg.DataPacket, encSignature
}
// keyRing returns a key ring of keys, which must not be empty.
func keyRing(keys ...*crypto.Key) *crypto.KeyRing {
kr, err := crypto.NewKeyRing(nil)
mustCrypto("key ring", err)
for _, k := range keys {
mustCrypto("key ring", kr.AddKey(k))
}
return kr
}
// errSignature wraps every signature verification failure.
var errSignature = errors.New("protontest: bad signature")
// decryptArmored decrypts an armored message with key. If verify is not
// nil the embedded signature must be valid for one of its keys at time
// at, or now if at is zero.
func decryptArmored(armored string, key *crypto.Key, verify *crypto.KeyRing, at time.Time) ([]byte, error) {
b := crypto.PGP().Decryption().DecryptionKey(key)
if verify != nil {
b = b.VerificationKeys(verify)
if !at.IsZero() {
b = b.VerifyTime(at.Unix())
}
}
dh, err := b.New()
if err != nil {
return nil, err
}
res, err := dh.Decrypt([]byte(armored), crypto.Armor)
if err != nil {
return nil, err
}
if verify != nil {
if err := res.SignatureError(); err != nil {
return res.Bytes(), fmt.Errorf("%w: %v", errSignature, err)
}
}
return res.Bytes(), nil
}
// verifyDetached checks a detached signature, armored or binary per
// encoding, of data against verify at time at, or now if at is zero.
func verifyDetached(data, sig []byte, encoding int8, verify *crypto.KeyRing, at time.Time) error {
b := crypto.PGP().Verify().VerificationKeys(verify)
if !at.IsZero() {
b = b.VerifyTime(at.Unix())
}
vh, err := b.New()
if err != nil {
return err
}
res, err := vh.VerifyDetached(data, sig, encoding)
if err != nil {
return fmt.Errorf("%w: %v", errSignature, err)
}
if err := res.SignatureError(); err != nil {
return fmt.Errorf("%w: %v", errSignature, err)
}
return nil
}
// unlockFunc unlocks an armored private key with a passphrase.
type unlockFunc func(armoredKey string, passphrase []byte) (*crypto.Key, error)
// unlockDriveKey decrypts a node or share key: the passphrase with
// parent, its detached signature against verify, then the key itself
// with unlock. A bad signature is returned alongside the unlocked key.
// Signatures are checked at time at, or now if it is zero.
func unlockDriveKey(armoredKey string, sealed sealedPassphrase, parent *crypto.Key, verify *crypto.KeyRing, unlock unlockFunc, at time.Time) (*crypto.Key, error) {
passphrase, err := decryptArmored(sealed.armored, parent, nil, at)
if err != nil {
return nil, fmt.Errorf("decrypt passphrase: %w", err)
}
key, err := unlock(armoredKey, passphrase)
if err != nil {
return nil, fmt.Errorf("unlock key: %w", err)
}
if err := verifyDetached(passphrase, []byte(sealed.signature), crypto.Armor, verify, at); err != nil {
return key, fmt.Errorf("passphrase: %w", err)
}
return key, nil
}
// decryptContentKey decrypts a base64 content key packet with nodeKey
// and checks its signature against nodeKey and verify at time at.
func decryptContentKey(packet64, signature string, nodeKey *crypto.Key, verify *crypto.KeyRing, at time.Time) (*crypto.SessionKey, error) {
keyPacket, err := base64.StdEncoding.DecodeString(packet64)
if err != nil {
return nil, fmt.Errorf("content key packet: %w", err)
}
dh, err := crypto.PGP().Decryption().DecryptionKey(nodeKey).New()
if err != nil {
return nil, err
}
sk, err := dh.DecryptSessionKey(keyPacket)
if err != nil {
return nil, fmt.Errorf("content key packet: %w", err)
}
kr := keyRing(nodeKey)
for _, k := range verify.GetKeys() {
mustCrypto("key ring", kr.AddKey(k))
}
if err := verifyDetached(sk.Key, []byte(signature), crypto.Armor, kr, at); err != nil {
return sk, fmt.Errorf("content key packet: %w", err)
}
return sk, nil
}
// decryptBlock decrypts a block's data packet with sk and checks its
// encrypted signature, decrypted with nodeKey, against verify at time
// at.
func decryptBlock(data []byte, encSignature string, sk *crypto.SessionKey, nodeKey *crypto.Key, verify *crypto.KeyRing, at time.Time) ([]byte, error) {
dh, err := crypto.PGP().Decryption().SessionKey(sk).New()
if err != nil {
return nil, err
}
res, err := dh.Decrypt(data, crypto.Bytes)
if err != nil {
return nil, fmt.Errorf("decrypt block: %w", err)
}
sig, err := decryptArmored(encSignature, nodeKey, nil, at)
if err != nil {
return nil, fmt.Errorf("decrypt block signature: %w", err)
}
if err := verifyDetached(res.Bytes(), sig, crypto.Bytes, verify, at); err != nil {
return res.Bytes(), fmt.Errorf("block: %w", err)
}
return res.Bytes(), nil
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,716 @@
package protontest
import (
"bytes"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"strings"
"testing"
"time"
"github.com/ProtonMail/go-crypto/openpgp/armor"
"github.com/ProtonMail/go-crypto/openpgp/packet"
"github.com/ProtonMail/gopenpgp/v3/crypto"
"github.com/rclone/rclone/backend/protondrive/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// sessionHeader returns the headers of an authenticated request.
func sessionHeader(sess Session) map[string]string {
return map[string]string{
"x-pm-appversion": testAppVersion,
"x-pm-uid": sess.UID,
"Authorization": "Bearer " + sess.AccessToken,
}
}
// getJSON calls GET path as sess, decodes the response into out if it
// is not nil and returns the HTTP status and body.
func getJSON(t *testing.T, s *Server, sess Session, path string, out any) (int, []byte) {
t.Helper()
resp, body := do(t, s, "GET", path, sessionHeader(sess))
if out != nil && resp.StatusCode == http.StatusOK {
require.NoError(t, json.Unmarshal(body, out), "body: %s", body)
}
return resp.StatusCode, body
}
// patterned returns content of n bytes which differs per block.
func patterned(n int) []byte {
b := make([]byte, n)
for i := range b {
b[i] = byte(i*7 + i/blockSize)
}
return b
}
func sha1Hex(b []byte) string {
sum := sha1.Sum(b)
return hex.EncodeToString(sum[:])
}
func TestSeedAndDump(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
modTime := time.Date(2024, 5, 6, 7, 8, 9, 123e6, time.FixedZone("x", 3600))
docs := u.SeedFolder(u.RootID(), "Documents")
sub := u.SeedFolder(docs, "Sub")
empty := u.SeedFile(u.RootID(), "empty.txt", nil, modTime)
small := u.SeedFile(docs, "small.bin", []byte("hello"), modTime)
exact := u.SeedFile(sub, "exact.bin", patterned(blockSize), modTime)
big := u.SeedFile(sub, "big.bin", patterned(2*blockSize+123), modTime)
before := time.Now().Truncate(time.Second)
legacy := u.SeedLegacyFile(docs, "legacy.txt", []byte("old format"))
doc := u.SeedDoc(u.RootID(), "Notes")
type want struct {
id, path string
folder bool
content []byte
hasTime bool
}
wants := []want{
{docs, "Documents", true, nil, false},
{sub, "Documents/Sub", true, nil, false},
{big, "Documents/Sub/big.bin", false, patterned(2*blockSize + 123), true},
{exact, "Documents/Sub/exact.bin", false, patterned(blockSize), true},
{legacy, "Documents/legacy.txt", false, []byte("old format"), true},
{small, "Documents/small.bin", false, []byte("hello"), true},
{doc, "Notes", false, nil, false},
{empty, "empty.txt", false, nil, true},
}
dump := u.Dump()
require.Len(t, dump, len(wants))
for i, w := range wants {
n := dump[i]
assert.NoError(t, n.Err, w.path)
assert.Equal(t, w.id, n.ID, w.path)
assert.Equal(t, w.path, n.Path)
assert.Equal(t, w.folder, n.IsFolder, w.path)
assert.Equal(t, linkStateActive, n.State, w.path)
assert.True(t, bytes.Equal(w.content, n.Content), "%s: content", w.path)
switch {
case !w.hasTime:
assert.True(t, n.ModTime.IsZero(), w.path)
assert.Empty(t, n.SHA1, w.path)
case w.id == legacy:
assert.False(t, n.ModTime.Before(before), w.path)
assert.False(t, n.ModTime.After(time.Now()), w.path)
assert.Equal(t, sha1Hex(w.content), n.SHA1, w.path)
default:
assert.True(t, modTime.Equal(n.ModTime), "%s: modtime %v", w.path, n.ModTime)
assert.Equal(t, sha1Hex(w.content), n.SHA1, w.path)
}
}
assert.Empty(t, u.Drafts())
}
// packetVersion returns the tag and version byte of the first packet
// of a binary OpenPGP message.
func packetVersion(t *testing.T, msg []byte) (tag packet.Packet, version byte) {
t.Helper()
p, err := packet.NewReader(bytes.NewReader(msg)).Next()
require.NoError(t, err)
switch p := p.(type) {
case *packet.EncryptedKey:
return p, byte(p.Version)
case *packet.SymmetricallyEncrypted:
return p, byte(p.Version)
}
t.Fatalf("unexpected packet %T", p)
return nil, 0
}
// dataPacketVersion returns the SEIPD version of an armored message.
func dataPacketVersion(t *testing.T, armored string) byte {
t.Helper()
block, err := armor.Decode(strings.NewReader(armored))
require.NoError(t, err)
packets := packet.NewReader(block.Body)
for {
p, err := packets.Next()
require.NoError(t, err)
if p, ok := p.(*packet.SymmetricallyEncrypted); ok {
return byte(p.Version)
}
}
}
func TestSeedFormats(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
current := u.SeedFile(u.RootID(), "current", []byte("new"), time.Now())
legacy := u.SeedLegacyFile(u.RootID(), "legacy", []byte("old"))
folder := u.SeedFolder(u.RootID(), "folder")
v := u.view()
for _, tc := range []struct {
id string
pkesk byte
seipd byte
aeadKey bool
mediaType string
blockCount int
}{
{current, 6, 2, true, "application/octet-stream", 1},
{legacy, 3, 1, false, "application/octet-stream", 1},
} {
l := v.d.links[tc.id]
keyPacket, err := base64.StdEncoding.DecodeString(l.contentKeyPacket)
require.NoError(t, err)
_, version := packetVersion(t, keyPacket)
assert.Equal(t, tc.pkesk, version, "content key packet version")
rev := l.activeRevision()
require.Len(t, rev.blocks, tc.blockCount)
_, version = packetVersion(t, rev.blocks[0].data)
assert.Equal(t, tc.seipd, version, "block SEIPD version")
assert.Equal(t, tc.seipd, dataPacketVersion(t, rev.blocks[0].encSignature), "block signature SEIPD version")
assert.Equal(t, tc.mediaType, l.mediaType)
key, err := crypto.NewKeyFromArmored(l.nodeKey)
require.NoError(t, err)
sig, err := key.GetEntity().PrimarySelfSignature(time.Now(), nil)
require.NoError(t, err)
assert.Equal(t, tc.aeadKey, sig.SEIPDv2, "node key advertises SEIPDv2")
// Everything but content avoids AEAD whatever the node key
// advertises.
assert.Equal(t, byte(1), dataPacketVersion(t, l.name), "name")
assert.Equal(t, byte(1), dataPacketVersion(t, l.nodePass.armored), "passphrase")
assert.Equal(t, byte(1), dataPacketVersion(t, rev.xattr), "extended attributes")
}
l := v.d.links[folder]
key, err := crypto.NewKeyFromArmored(l.nodeKey)
require.NoError(t, err)
sig, err := key.GetEntity().PrimarySelfSignature(time.Now(), nil)
require.NoError(t, err)
assert.False(t, sig.SEIPDv2, "folder key advertises SEIPDv2")
assert.Equal(t, byte(1), dataPacketVersion(t, l.hashKey), "hash key")
}
func TestCorrupt(t *testing.T) {
for _, what := range []string{"name", "name-signature", "passphrase-signature", "block-hash", "manifest", "swap-blocks", "truncate-blocks", "block-rollback", "foreign-block"} {
t.Run(what, func(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
victim := u.SeedFile(u.RootID(), "victim", patterned(blockSize+10), time.Now())
if what == "block-rollback" {
u.SeedRevision(victim, patterned(blockSize+20), time.Now())
}
bystander := u.SeedFile(u.RootID(), "bystander", []byte("fine"), time.Now())
u.Corrupt(victim, what)
for _, n := range u.Dump() {
switch n.ID {
case victim:
assert.Error(t, n.Err)
case bystander:
assert.NoError(t, n.Err)
}
}
})
}
}
func TestBootstrapRoutes(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
var myFiles myFilesResponse
status, body := getJSON(t, s, sess, "/drive/v2/shares/my-files", &myFiles)
require.Equal(t, http.StatusOK, status, "body: %s", body)
assert.Equal(t, u.VolumeID(), myFiles.Volume.VolumeID)
assert.Equal(t, u.ShareID(), myFiles.Share.ShareID)
assert.Equal(t, u.RootID(), myFiles.Link.Link.LinkID)
assert.Equal(t, u.Email(), myFiles.Share.CreatorEmail)
assert.Nil(t, myFiles.Link.Link.ParentLinkID)
var volume getVolumeResponse
status, body = getJSON(t, s, sess, "/drive/volumes/"+u.VolumeID(), &volume)
require.Equal(t, http.StatusOK, status, "body: %s", body)
assert.Equal(t, u.ShareID(), volume.Volume.Share.ShareID)
assert.Equal(t, u.RootID(), volume.Volume.Share.LinkID)
var share shareResponse
status, body = getJSON(t, s, sess, "/drive/shares/"+u.ShareID(), &share)
require.Equal(t, http.StatusOK, status, "body: %s", body)
assert.Equal(t, u.RootID(), share.LinkID)
assert.Equal(t, myFiles.Share.Key, share.Key)
require.NotNil(t, share.AddressID)
assert.Equal(t, myFiles.Share.AddressID, *share.AddressID)
// Another user's IDs are not found.
other := s.AddUser("other", "pass")
status, body = getJSON(t, s, sess, "/drive/volumes/"+other.VolumeID(), nil)
assert.Equal(t, http.StatusUnprocessableEntity, status)
assert.Equal(t, api.CodeNotExists, decodeError(t, body).Code)
status, _ = getJSON(t, s, sess, "/drive/shares/"+other.ShareID(), nil)
assert.Equal(t, http.StatusUnprocessableEntity, status)
}
// keysOf returns the keys of a JSON object.
func keysOf(t *testing.T, v any) []string {
t.Helper()
m, ok := v.(map[string]any)
require.True(t, ok, "not an object: %v", v)
var keys []string
for k := range m {
keys = append(keys, k)
}
return keys
}
// TestWireFieldNames checks the link metadata response carries the
// fields the SDK's nodes/apiService.ts reads, by name.
func TestWireFieldNames(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
folder := u.SeedFolder(u.RootID(), "folder")
file := u.SeedFile(u.RootID(), "file", []byte("x"), time.Now())
var resp struct{ Links []map[string]any }
status := postJSON(t, s, "/drive/v2/volumes/"+u.VolumeID()+"/links", sessionHeader(sess), &linksRequest{LinkIDs: []string{folder, file}}, &resp)
require.Equal(t, http.StatusOK, status)
require.Len(t, resp.Links, 2)
for _, l := range resp.Links {
assert.Subset(t, keysOf(t, l), []string{"Link", "Folder", "File", "Sharing", "Membership"})
assert.Subset(t, keysOf(t, l["Link"]), []string{
"LinkID", "Type", "ParentLinkID", "State", "CreateTime", "ModifyTime", "TrashTime",
"Name", "NameHash", "NodeKey", "NodePassphrase", "NodePassphraseSignature",
"SignatureEmail", "NameSignatureEmail", "OwnedBy", "ThirdParty", "Sdk",
})
if l["File"] == nil {
assert.Subset(t, keysOf(t, l["Folder"]), []string{"NodeHashKey", "XAttr", "IsImported"})
continue
}
f := l["File"]
assert.Subset(t, keysOf(t, f), []string{"ActiveRevision", "TotalEncryptedSize", "ContentKeyPacket", "MediaType", "ContentKeyPacketSignature"})
assert.Subset(t, keysOf(t, f.(map[string]any)["ActiveRevision"]), []string{
"RevisionID", "CreateTime", "EncryptedSize", "SignatureEmail", "XAttr", "IsImported",
"Thumbnails", "ThirdParty", "Sdk", "ManifestSignature",
})
}
}
func TestChildrenPaging(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
s.SetChildrenPageSize(100)
parent := u.SeedFolder(u.RootID(), "parent")
want := make(map[string]bool)
for i := range 250 {
want[u.SeedFolder(parent, fmt.Sprintf("child %03d", i))] = true
}
trashed := u.SeedFolder(parent, "trashed")
s.mu.Lock()
u.drive.links[trashed].state = linkStateTrashed
s.mu.Unlock()
s.ResetRequests()
got := make(map[string]bool)
anchor, pages := "", 0
for {
path := "/drive/v2/volumes/" + u.VolumeID() + "/folders/" + parent + "/children"
if anchor != "" {
path += "?AnchorID=" + anchor
}
var page childrenResponse
status, body := getJSON(t, s, sess, path, &page)
require.Equal(t, http.StatusOK, status, "body: %s", body)
pages++
for _, id := range page.LinkIDs {
assert.False(t, got[id], "duplicate %s", id)
got[id] = true
}
if !page.More {
assert.Nil(t, page.AnchorID)
break
}
require.NotNil(t, page.AnchorID)
require.Len(t, page.LinkIDs, 100)
anchor = *page.AnchorID
}
assert.Equal(t, 3, pages)
assert.Equal(t, want, got)
assert.Equal(t, 3, s.Requests()["GET /drive/v2/volumes/{volumeID}/folders/{linkID}/children"])
}
func TestLinksBatchLimit(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
a := u.SeedFolder(u.RootID(), "a")
b := u.SeedFile(u.RootID(), "b", []byte("b"), time.Now())
path := "/drive/v2/volumes/" + u.VolumeID() + "/links"
ids := []string{a, b}
for len(ids) < maxLinksPerRequest {
ids = append(ids, randomID())
}
var resp linksResponse
require.Equal(t, http.StatusOK, postJSON(t, s, path, sessionHeader(sess), &linksRequest{LinkIDs: ids}, &resp))
require.Len(t, resp.Links, 2, "unknown IDs are left out")
assert.Equal(t, a, resp.Links[0].Link.LinkID)
assert.NotNil(t, resp.Links[0].Folder)
assert.Equal(t, b, resp.Links[1].Link.LinkID)
require.NotNil(t, resp.Links[1].File)
assert.NotNil(t, resp.Links[1].File.ActiveRevision)
var apiErr api.Error
ids = append(ids, randomID())
assert.Equal(t, http.StatusUnprocessableEntity, postJSON(t, s, path, sessionHeader(sess), &linksRequest{LinkIDs: ids}, &apiErr))
assert.Equal(t, api.CodeInvalidValue, apiErr.Code)
}
// getBlock fetches a block URL with the given storage token, sent
// unless empty.
func getBlock(t *testing.T, s *Server, bareURL, token string) (int, []byte) {
t.Helper()
header := map[string]string{"x-pm-appversion": testAppVersion}
if token != "" {
header["pm-storage-token"] = token
}
require.True(t, strings.HasPrefix(bareURL, s.URL()+"/"), "block URL %s is not on the test server", bareURL)
resp, body := do(t, s, "GET", strings.TrimPrefix(bareURL, s.URL()), header)
return resp.StatusCode, body
}
func TestBlockDownloadNeedsToken(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
content := patterned(blockSize + 100)
file := u.SeedFile(u.RootID(), "file", content, time.Now())
var links linksResponse
require.Equal(t, http.StatusOK, postJSON(t, s, "/drive/v2/volumes/"+u.VolumeID()+"/links", sessionHeader(sess), &linksRequest{LinkIDs: []string{file}}, &links))
revisionPath := "/drive/v2/volumes/" + u.VolumeID() + "/files/" + file + "/revisions/" + links.Links[0].File.ActiveRevision.RevisionID
var rev revisionResponse
status, body := getJSON(t, s, sess, revisionPath+"?NoBlockUrls=true", &rev)
require.Equal(t, http.StatusOK, status, "body: %s", body)
require.Len(t, rev.Revision.Blocks, 2)
for _, b := range rev.Revision.Blocks {
assert.Nil(t, b.Token)
assert.Nil(t, b.BareURL)
}
require.NotNil(t, rev.Revision.ManifestSignature)
status, body = getJSON(t, s, sess, revisionPath+"?PageSize=1&FromBlockIndex=2", &rev)
require.Equal(t, http.StatusOK, status, "body: %s", body)
require.Len(t, rev.Revision.Blocks, 1)
b := rev.Revision.Blocks[0]
assert.Equal(t, 2, b.Index)
require.NotNil(t, b.Token)
require.NotNil(t, b.BareURL)
status, _ = getBlock(t, s, *b.BareURL, "")
assert.Equal(t, http.StatusForbidden, status)
status, _ = getBlock(t, s, *b.BareURL, "wrong")
assert.Equal(t, http.StatusForbidden, status)
status, data := getBlock(t, s, *b.BareURL, *b.Token)
require.Equal(t, http.StatusOK, status)
sum := sha256.Sum256(data)
assert.Equal(t, b.Hash, base64.StdEncoding.EncodeToString(sum[:]))
status, _ = getBlock(t, s, s.URL()+"/storage/blocks/unknown", *b.Token)
assert.Equal(t, http.StatusNotFound, status)
}
func TestQuota(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
u.SeedFile(u.RootID(), "a", patterned(1000), time.Now())
u.SeedFile(u.RootID(), "b", patterned(10), time.Now())
var want int64
v := u.view()
for _, l := range v.d.links {
for _, r := range l.revisions {
want += r.size()
}
}
require.NotZero(t, want)
var resp struct {
User struct {
UsedSpace int64
MaxSpace int64
ProductUsedSpace struct{ Drive int64 }
}
}
status, body := getJSON(t, s, sess, "/core/v4/users", &resp)
require.Equal(t, http.StatusOK, status, "body: %s", body)
assert.Equal(t, want, resp.User.UsedSpace)
assert.Equal(t, want, resp.User.ProductUsedSpace.Drive)
assert.Equal(t, DefaultMaxSpace, resp.User.MaxSpace)
}
// volumeBootstrap returns a create volume request for the primary
// address of u made as the SDK's generateVolumeBootstrap does.
func volumeBootstrap(u *User) *createVolumeRequest {
addr := u.addresses[0]
addrKey := addr.keys[0].key
share := newDriveKey(false)
sharePass := sealPassphrase(share.passphrase, addrKey, addrKey)
root := newDriveKey(false)
rootPass := sealPassphrase(root.passphrase, share.key, addrKey)
_, hashKey := newHashKey(root.key)
return &createVolumeRequest{
AddressID: addr.id,
AddressKeyID: &addr.keys[0].id,
ShareKey: share.armored,
SharePassphrase: sharePass.armored,
SharePassphraseSignature: sharePass.signature,
FolderName: encryptArmored([]byte("root"), share.key, addrKey, false),
FolderKey: root.armored,
FolderPassphrase: rootPass.armored,
FolderPassphraseSignature: rootPass.signature,
FolderHashKey: hashKey,
}
}
func TestAccountWithoutDrive(t *testing.T) {
s := New(t)
u := s.AddUserWithoutDrive("new", "pass")
sess := s.NewSession(u)
status, body := getJSON(t, s, sess, "/drive/v2/shares/my-files", nil)
assert.Equal(t, http.StatusUnprocessableEntity, status)
assert.Equal(t, api.CodeNotExists, decodeError(t, body).Code)
assert.Equal(t, "Volume not found", decodeError(t, body).Message)
impostor := generateKey("impostor", "impostor@proton.test")
for name, spoil := range map[string]func(req *createVolumeRequest){
"share passphrase to another key": func(req *createVolumeRequest) {
req.SharePassphrase = encryptArmored([]byte("x"), impostor, nil, false)
},
"share passphrase signed by another key": func(req *createVolumeRequest) {
other := volumeBootstrap(u)
req.SharePassphraseSignature = other.SharePassphraseSignature
},
"folder name missing": func(req *createVolumeRequest) {
req.FolderName = ""
},
"hash key missing": func(req *createVolumeRequest) {
req.FolderHashKey = ""
},
"unknown address": func(req *createVolumeRequest) {
req.AddressID = "unknown"
},
} {
req := volumeBootstrap(u)
spoil(req)
var apiErr api.Error
assert.Equal(t, http.StatusUnprocessableEntity, postJSON(t, s, "/drive/volumes", sessionHeader(sess), req, &apiErr), name)
assert.Equal(t, api.CodeInvalidValue, apiErr.Code, name)
}
var created getVolumeResponse
status = postJSON(t, s, "/drive/volumes", sessionHeader(sess), volumeBootstrap(u), &created)
require.Equal(t, http.StatusOK, status)
assert.Equal(t, u.VolumeID(), created.Volume.ID)
assert.Equal(t, u.ShareID(), created.Volume.Share.ShareID)
assert.Equal(t, u.RootID(), created.Volume.Share.LinkID)
var apiErr api.Error
assert.Equal(t, http.StatusUnprocessableEntity, postJSON(t, s, "/drive/volumes", sessionHeader(sess), volumeBootstrap(u), &apiErr))
assert.Equal(t, api.CodeAlreadyExists, apiErr.Code)
var myFiles myFilesResponse
status, body = getJSON(t, s, sess, "/drive/v2/shares/my-files", &myFiles)
require.Equal(t, http.StatusOK, status, "body: %s", body)
assert.Equal(t, u.RootID(), myFiles.Link.Link.LinkID)
assert.Empty(t, u.Dump())
id := u.SeedFile(u.RootID(), "after", []byte("works"), time.Now())
dump := u.Dump()
require.Len(t, dump, 1)
assert.Equal(t, id, dump[0].ID)
assert.Equal(t, "after", dump[0].Path)
assert.NoError(t, dump[0].Err)
}
// TestBlockBodyIsReadable checks a downloaded block is the exact data
// packet that decrypts with the content key.
func TestBlockBodyIsReadable(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
file := u.SeedFile(u.RootID(), "file", []byte("block body"), time.Now())
v := u.view()
l := v.d.links[file]
key, err := v.unlock(file)
require.NoError(t, err)
sk, err := decryptContentKey(l.contentKeyPacket, l.contentKeyPacketSignature, key, keyRing(key), time.Time{})
require.NoError(t, err)
var rev revisionResponse
status, body := getJSON(t, s, sess, "/drive/v2/volumes/"+u.VolumeID()+"/files/"+file+"/revisions/"+l.activeRevision().id, &rev)
require.Equal(t, http.StatusOK, status, "body: %s", body)
b := rev.Revision.Blocks[0]
status, data := getBlock(t, s, *b.BareURL, *b.Token)
require.Equal(t, http.StatusOK, status)
plain, err := decryptBlock(data, *b.EncSignature, sk, key, keyRing(u.addresses[0].keys[0].key), time.Time{})
require.NoError(t, err)
assert.Equal(t, "block body", string(plain))
}
func TestSeedRevision(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
ids := map[string]string{
"current": u.SeedFile(u.RootID(), "current", []byte("old"), time.Now()),
"legacy": u.SeedLegacyFile(u.RootID(), "legacy", []byte("old")),
}
content := patterned(blockSize + 5)
for name, id := range ids {
old := u.view().d.links[id].activeRevision().id
rev := u.SeedRevision(id, content, time.Now())
l := u.view().d.links[id]
assert.Equal(t, rev, l.activeRevision().id, name)
require.Len(t, l.revisions, 2, name)
assert.Equal(t, old, l.revisions[0].id, name)
assert.Equal(t, revisionStateObsolete, l.revisions[0].state, name)
}
dump := u.Dump()
require.Len(t, dump, 2)
for _, d := range dump {
assert.NoError(t, d.Err, d.Path)
assert.True(t, bytes.Equal(content, d.Content), d.Path)
}
}
func TestSeedForgedFolder(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
forged := u.SeedForgedFolder(u.RootID(), "forged")
nested := u.SeedAnonymousFolder(forged, "nested")
file := u.SeedAnonymousFile(nested, "file", []byte("forged content"), time.Now())
for _, n := range u.Dump() {
switch n.ID {
case forged:
assert.Error(t, n.Err, "the forged key's signature verifies")
case nested, file:
assert.NoError(t, n.Err, n.Path)
}
if n.ID == file {
assert.Equal(t, "forged/nested/file", n.Path)
assert.Equal(t, "forged content", string(n.Content))
}
}
}
func TestForgeShareAndRoot(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
other := s.AddUser("other", "pass")
u.ForgeShareCreator(other, other.Email())
u.ForgeRootSigner(other, other.Email())
v := u.view()
assert.Equal(t, other.Email(), v.d.creatorEmail)
_, err := v.unlockShare()
assert.NoError(t, err, "the share verifies against the other user's key")
root := v.d.links[v.d.rootID]
assert.Equal(t, other.Email(), root.signatureEmail)
_, err = v.unlock(v.d.rootID)
assert.NoError(t, err, "the root verifies against the other user's key")
id := u.SeedFile(u.RootID(), "file", []byte("x"), time.Now())
for _, n := range u.Dump() {
if n.ID == id {
assert.NoError(t, n.Err)
}
}
}
func TestServeKeysAs(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
other := s.AddUser("other", "pass")
sess := s.NewSession(u)
s.ServeKeysAs("USER@proton.test", other)
var resp api.PublicKeysResponse
status, body := getJSON(t, s, sess, "/core/v4/keys/all?Email=USER@proton.test", &resp)
require.Equal(t, http.StatusOK, status, "body: %s", body)
require.NotEmpty(t, resp.Address.Keys)
want, err := other.addresses[0].keys[0].key.GetArmoredPublicKey()
require.NoError(t, err)
assert.Equal(t, want, resp.Address.Keys[0].PublicKey)
}
func TestNodeSecrets(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
id := u.SeedFile(u.RootID(), "file", []byte("secret"), time.Now())
passphrase, sessionKey := u.NodeSecrets(id)
v := u.view()
l := v.d.links[id]
key, err := crypto.NewPrivateKeyFromArmored(l.nodeKey, passphrase)
require.NoError(t, err, "the passphrase unlocks the node key")
sk, err := decryptContentKey(l.contentKeyPacket, l.contentKeyPacketSignature, key, keyRing(key), time.Time{})
require.NoError(t, err)
assert.Equal(t, sk.Key, sessionKey)
}
func TestIssuedSecrets(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
file := u.SeedFile(u.RootID(), "file", []byte("x"), time.Now())
assert.Equal(t, []string{sess.AccessToken, sess.RefreshToken}, s.IssuedSecrets())
var rev revisionResponse
status, body := getJSON(t, s, sess, "/drive/v2/volumes/"+u.VolumeID()+"/files/"+file+"/revisions/"+u.view().d.links[file].activeRevision().id, &rev)
require.Equal(t, http.StatusOK, status, "body: %s", body)
token := *rev.Revision.Blocks[0].Token
assert.Equal(t, []string{token}, s.IssuedSecrets(StorageToken))
assert.Equal(t, []string{sess.AccessToken, sess.RefreshToken}, s.IssuedSecrets(SessionToken))
assert.Equal(t, []string{sess.AccessToken, sess.RefreshToken, token}, s.IssuedSecrets())
assert.Empty(t, s.IssuedSecrets(ForkSelector))
}
func TestStoredFileRoundTrip(t *testing.T) {
s := New(t)
addrPass := newPassphrase()
root := newDriveKey(false)
_, hashKey := newHashKey(root.key)
keys := FixedKeys{
AddressKey: lockKey(generateKey("fixed", "fixed@proton.test"), addrPass),
AddressKeyPassphrase: string(addrPass),
RootKey: root.armored,
RootKeyPassphrase: string(root.passphrase),
RootHashKey: hashKey,
}
// Two users holding the same keys can read each other's stored files.
one := s.AddUserWithKeys("one", "pass", keys)
two := s.AddUserWithKeys("two", "pass", keys)
content := patterned(blockSize + 5)
id := one.SeedFile(one.RootID(), "f", content, time.Now())
f := one.StoredFile(id)
assert.Equal(t, "one@proton.test", f.Link.SignatureEmail)
require.Len(t, f.Revision.Blocks, 2)
for _, b := range f.Revision.Blocks {
sum := sha256.Sum256(b.Data)
assert.Equal(t, sum[:], b.Hash)
}
two.SeedStoredFile(two.RootID(), f)
for _, u := range []*User{one, two} {
dump := u.Dump()
require.Len(t, dump, 1)
assert.NoError(t, dump[0].Err)
assert.Equal(t, "f", dump[0].Path)
assert.True(t, bytes.Equal(content, dump[0].Content))
}
}
+208
View File
@@ -0,0 +1,208 @@
package protontest
import (
"net/http"
"github.com/rclone/rclone/backend/protondrive/api"
)
// Volume event types as the API encodes them.
const (
EventDelete = 0
EventCreate = 1
EventUpdate = 2
EventUpdateMetadata = 3
)
// defaultEventsPageSize is the number of events per page unless changed
// by SetEventsPageSize.
const defaultEventsPageSize = 50
// Event is a change to a link as the volume events route reports it.
// Every change through the API or a test mutator such as SeedFile or
// SetParent appends one, as Proton does: Create when a link becomes
// active (an upload committed, a folder made or copied), Update when a
// new revision is committed or a test mutator changes the link's
// content or keys, UpdateMetadata when the link is renamed, moved or
// trashed, and Delete for every link emptying the trash removes,
// descendants included.
//
// As on Proton, ParentLinkID, IsShared and IsTrashed are the link's
// state when the events are read, not when the change was made: a link
// deleted for good has no parent and isn't trashed. Events added by
// InjectEvent keep their own fields.
type Event struct {
Type int
LinkID string
ParentLinkID string // empty for none
IsShared bool
IsTrashed bool
}
// loggedEvent is an Event with its ID.
type loggedEvent struct {
id string
Event
injected bool // whether Event is reported as given, not with the link's state
}
// eventLog is a volume's events, oldest first.
type eventLog struct {
entries []loggedEvent
latest string // ID of the newest event, or the initial ID
cursors map[string]int // events covered by each ID still known
}
// newEventLog returns an empty log with an initial ID.
func newEventLog() *eventLog {
id := randomID()
return &eventLog{latest: id, cursors: map[string]int{id: 0}}
}
// add appends e with a new ID.
func (l *eventLog) add(e loggedEvent) {
id := randomID()
e.id = id
l.entries = append(l.entries, e)
l.latest = id
l.cursors[id] = len(l.entries)
}
// logLink appends an event of type typ for link l.
func (d *drive) logLink(typ int, l *link) {
d.events.add(loggedEvent{Event: Event{Type: typ, LinkID: l.id}})
}
// reported returns e as the events route reports it now.
func (d *drive) reported(e loggedEvent) Event {
if e.injected {
return e.Event
}
out := Event{Type: e.Type, LinkID: e.LinkID}
if l := d.links[e.LinkID]; l != nil {
out.ParentLinkID = l.parentID
out.IsTrashed = l.state == linkStateTrashed
}
return out
}
type latestEventResponse struct {
EventID string
Code int
}
type eventLinkJSON struct {
LinkID string
ParentLinkID *string
IsShared bool
IsTrashed bool
}
type eventJSON struct {
EventID string
EventType int
Link eventLinkJSON
}
type invitationJSON struct {
LinkID string
}
type eventsResponse struct {
Events []eventJSON
ConvertibleExternalInvitations []invitationJSON
EventID string
More bool
Refresh bool
DisableSdkQuotaChecks bool
Code int
}
// registerEvents registers the volume events routes.
func (s *Server) registerEvents() {
s.handleDrive("GET", "/drive/volumes/{volumeID}/events/latest", s.handleLatestEvent)
s.handleDrive("GET", "/drive/v2/volumes/{volumeID}/events/{eventID}", s.handleEvents)
}
// handleLatestEvent returns the ID of the volume's newest event.
func (s *Server) handleLatestEvent(w http.ResponseWriter, r *http.Request, u *User, d *drive) {
writeJSON(w, http.StatusOK, &latestEventResponse{EventID: d.events.latest, Code: api.CodeOK})
}
// handleEvents returns a page of the volume's events after an event
// ID, or Refresh with the newest ID if that ID is unknown or forgotten.
func (s *Server) handleEvents(w http.ResponseWriter, r *http.Request, u *User, d *drive) {
log := d.events
resp := &eventsResponse{
Events: []eventJSON{},
ConvertibleExternalInvitations: []invitationJSON{},
EventID: log.latest,
Code: api.CodeOK,
}
start, ok := log.cursors[r.PathValue("eventID")]
if !ok {
resp.Refresh = true
writeJSON(w, http.StatusOK, resp)
return
}
end := min(start+s.eventsPageSize, len(log.entries))
for _, logged := range log.entries[start:end] {
e := d.reported(logged)
resp.Events = append(resp.Events, eventJSON{
EventID: logged.id,
EventType: e.Type,
Link: eventLinkJSON{
LinkID: e.LinkID,
ParentLinkID: nullable(e.ParentLinkID),
IsShared: e.IsShared,
IsTrashed: e.IsTrashed,
},
})
}
if end > start {
resp.EventID = log.entries[end-1].id
}
resp.More = end < len(log.entries)
writeJSON(w, http.StatusOK, resp)
}
// SetEventsPageSize sets the number of events per page of the volume
// events route; n of 0 or less restores the default.
func (s *Server) SetEventsPageSize(n int) {
if n <= 0 {
n = defaultEventsPageSize
}
s.mu.Lock()
defer s.mu.Unlock()
s.eventsPageSize = n
}
// ExpireEvents forgets every event ID of u's volume but the newest, so
// a client polling from an older one is told to refresh.
func (s *Server) ExpireEvents(u *User) {
s.mu.Lock()
defer s.mu.Unlock()
log := u.mustDriveLocked().events
log.cursors = map[string]int{log.latest: len(log.entries)}
}
// InjectEvent appends e to the events of u's volume. It is reported
// with its own fields, whatever the state of its link when read.
func (s *Server) InjectEvent(u *User, e Event) {
s.mu.Lock()
defer s.mu.Unlock()
u.mustDriveLocked().events.add(loggedEvent{Event: e, injected: true})
}
// Events returns every event of u's volume, oldest first, including
// those ExpireEvents forgot, as the events route would report them now.
func (s *Server) Events(u *User) []Event {
s.mu.Lock()
defer s.mu.Unlock()
d := u.mustDriveLocked()
out := make([]Event, 0, len(d.events.entries))
for _, e := range d.events.entries {
out = append(out, d.reported(e))
}
return out
}
@@ -0,0 +1,423 @@
package protontest
import (
"encoding/json"
"net/http"
"testing"
"time"
"github.com/rclone/rclone/backend/protondrive/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// latestEvent returns the start cursor of u's volume as sess.
func latestEvent(t *testing.T, s *Server, sess Session, u *User) string {
t.Helper()
var resp latestEventResponse
status, body := getJSON(t, s, sess, "/drive/volumes/"+u.VolumeID()+"/events/latest", &resp)
require.Equal(t, http.StatusOK, status, "body: %s", body)
require.Equal(t, api.CodeOK, resp.Code)
require.NotEmpty(t, resp.EventID)
return resp.EventID
}
// eventsPage returns the page of u's volume events after cursor.
func eventsPage(t *testing.T, s *Server, sess Session, u *User, cursor string) eventsResponse {
t.Helper()
var resp eventsResponse
status, body := getJSON(t, s, sess, "/drive/v2/volumes/"+u.VolumeID()+"/events/"+cursor, &resp)
require.Equal(t, http.StatusOK, status, "body: %s", body)
require.Equal(t, api.CodeOK, resp.Code)
return resp
}
// eventsSince returns every event of u's volume after cursor, following
// More.
func eventsSince(t *testing.T, s *Server, sess Session, u *User, cursor string) []eventJSON {
t.Helper()
var out []eventJSON
for {
resp := eventsPage(t, s, sess, u, cursor)
require.False(t, resp.Refresh)
out = append(out, resp.Events...)
cursor = resp.EventID
if !resp.More {
return out
}
}
}
func TestEventsLatestAndPage(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
start := latestEvent(t, s, sess, u)
resp := eventsPage(t, s, sess, u, start)
assert.Empty(t, resp.Events)
assert.Equal(t, start, resp.EventID)
assert.False(t, resp.More)
assert.False(t, resp.Refresh)
status, body := sendJSON(t, s, sess, "POST", "/drive/v2/volumes/"+u.VolumeID()+"/folders", newFolderRequest(u, u.RootID(), "dir"))
require.Equal(t, http.StatusOK, status, "body: %s", body)
var created createFolderResponse
require.NoError(t, json.Unmarshal(body, &created))
resp = eventsPage(t, s, sess, u, start)
require.Len(t, resp.Events, 1)
e := resp.Events[0]
assert.Equal(t, EventCreate, e.EventType)
assert.Equal(t, created.Folder.ID, e.Link.LinkID)
require.NotNil(t, e.Link.ParentLinkID)
assert.Equal(t, u.RootID(), *e.Link.ParentLinkID)
assert.False(t, e.Link.IsShared)
assert.False(t, e.Link.IsTrashed)
assert.NotEqual(t, start, e.EventID)
assert.Equal(t, e.EventID, resp.EventID)
assert.False(t, resp.More)
assert.False(t, resp.Refresh)
assert.Equal(t, e.EventID, latestEvent(t, s, sess, u))
resp = eventsPage(t, s, sess, u, e.EventID)
assert.Empty(t, resp.Events)
assert.Equal(t, e.EventID, resp.EventID)
// Field names are Proton's.
var raw map[string]any
_, body = getJSON(t, s, sess, "/drive/v2/volumes/"+u.VolumeID()+"/events/"+start, &raw)
assert.Subset(t, keysOf(t, raw), []string{"Code", "Events", "EventID", "More", "Refresh"})
events, ok := raw["Events"].([]any)
require.True(t, ok, "body: %s", body)
require.Len(t, events, 1)
assert.ElementsMatch(t, []string{"EventID", "EventType", "Link"}, keysOf(t, events[0]))
assert.ElementsMatch(t, []string{"LinkID", "ParentLinkID", "IsShared", "IsTrashed"}, keysOf(t, events[0].(map[string]any)["Link"]))
}
func TestEventsPerMutation(t *testing.T) {
type event struct {
Type int
LinkID string
ParentID string
IsTrashed bool
}
for _, tc := range []struct {
name string
// do makes a change and returns the events it expects.
do func(t *testing.T, x *renameMoveCopy) []event
}{
{"create folder", func(t *testing.T, x *renameMoveCopy) []event {
status, body := sendJSON(t, x.s, x.sess, "POST", "/drive/v2/volumes/"+x.volume+"/folders", newFolderRequest(x.u, x.a, "new"))
require.Equal(t, http.StatusOK, status, "body: %s", body)
var resp createFolderResponse
require.NoError(t, json.Unmarshal(body, &resp))
return []event{{EventCreate, resp.Folder.ID, x.a, false}}
}},
{"upload commit", func(t *testing.T, x *renameMoveCopy) []event {
up := &uploader{t: t, s: x.s, u: x.u, sess: x.sess, volume: x.volume}
f := up.newFileRequest("up")
up.create(f)
blocks := f.encrypt([]byte("content"))
up.uploadAll(f, blocks)
status, body := up.commit(f, f.commitRequest(blocks))
require.Equal(t, http.StatusOK, status, "body: %s", body)
return []event{{EventCreate, f.linkID, x.u.RootID(), false}}
}},
{"new revision", func(t *testing.T, x *renameMoveCopy) []event {
up := &uploader{t: t, s: x.s, u: x.u, sess: x.sess, volume: x.volume}
f := up.newFileRequest("up")
up.create(f)
blocks := f.encrypt([]byte("one"))
up.uploadAll(f, blocks)
status, body := up.commit(f, f.commitRequest(blocks))
require.Equal(t, http.StatusOK, status, "body: %s", body)
current := f.revID
uid := testClientUID
status, body = up.send("POST", "/drive/v2/volumes/"+x.volume+"/files/"+f.linkID+"/revisions", &createRevisionRequest{CurrentRevisionID: &current, ClientUID: &uid})
require.Equal(t, http.StatusOK, status, "body: %s", body)
var resp createRevisionResponse
require.NoError(t, json.Unmarshal(body, &resp))
f.revID = resp.Revision.ID
up.loadCode(f)
blocks = f.encrypt([]byte("two"))
up.uploadAll(f, blocks)
status, body = up.commit(f, f.commitRequest(blocks))
require.Equal(t, http.StatusOK, status, "body: %s", body)
return []event{
{EventCreate, f.linkID, x.u.RootID(), false},
{EventUpdate, f.linkID, x.u.RootID(), false},
}
}},
{"rename", func(t *testing.T, x *renameMoveCopy) []event {
email, hash, original := x.u.Email(), lookupHash([]byte("k"), "new"), x.hash(x.f)
status, body := sendJSON(t, x.s, x.sess, "PUT", "/drive/v2/volumes/"+x.volume+"/links/"+x.f+"/rename",
&renameRequest{Name: x.validMsg, Hash: &hash, NameSignatureEmail: &email, OriginalHash: &original})
require.Equal(t, http.StatusOK, status, "body: %s", body)
return []event{{EventUpdateMetadata, x.f, x.a, false}}
}},
{"move", func(t *testing.T, x *renameMoveCopy) []event {
email := x.u.Email()
status, body := sendJSON(t, x.s, x.sess, "PUT", "/drive/v2/volumes/"+x.volume+"/links/"+x.f+"/move", &moveRequest{
Name: x.validMsg, NodePassphrase: x.validMsg, Hash: lookupHash([]byte("k"), "new"),
ParentLinkID: x.b, OriginalHash: x.hash(x.f), NameSignatureEmail: &email,
})
require.Equal(t, http.StatusOK, status, "body: %s", body)
return []event{{EventUpdateMetadata, x.f, x.b, false}}
}},
{"copy", func(t *testing.T, x *renameMoveCopy) []event {
email := x.u.Email()
status, body := sendJSON(t, x.s, x.sess, "POST", "/drive/volumes/"+x.volume+"/links/"+x.f+"/copy", &copyRequest{
Name: x.validMsg, NodePassphrase: x.validMsg, Hash: lookupHash([]byte("k"), "copy"),
TargetVolumeID: x.volume, TargetParentLinkID: x.b, NameSignatureEmail: &email,
})
require.Equal(t, http.StatusOK, status, "body: %s", body)
var resp copyResponse
require.NoError(t, json.Unmarshal(body, &resp))
return []event{{EventCreate, resp.LinkID, x.b, false}}
}},
{"trash", func(t *testing.T, x *renameMoveCopy) []event {
var resp multiResponse
status := postJSON(t, x.s, "/drive/v2/volumes/"+x.volume+"/trash_multiple", sessionHeader(x.sess), &linksRequest{LinkIDs: []string{x.f}}, &resp)
require.Equal(t, http.StatusOK, status)
return []event{{EventUpdateMetadata, x.f, x.a, true}}
}},
{"create then trash", func(t *testing.T, x *renameMoveCopy) []event {
status, body := sendJSON(t, x.s, x.sess, "POST", "/drive/v2/volumes/"+x.volume+"/folders", newFolderRequest(x.u, x.a, "new"))
require.Equal(t, http.StatusOK, status, "body: %s", body)
var created createFolderResponse
require.NoError(t, json.Unmarshal(body, &created))
var resp multiResponse
status = postJSON(t, x.s, "/drive/v2/volumes/"+x.volume+"/trash_multiple", sessionHeader(x.sess), &linksRequest{LinkIDs: []string{created.Folder.ID}}, &resp)
require.Equal(t, http.StatusOK, status)
// Both report the link as it is when read.
return []event{{EventCreate, created.Folder.ID, x.a, true}, {EventUpdateMetadata, created.Folder.ID, x.a, true}}
}},
{"empty trash", func(t *testing.T, x *renameMoveCopy) []event {
var resp multiResponse
status := postJSON(t, x.s, "/drive/v2/volumes/"+x.volume+"/trash_multiple", sessionHeader(x.sess), &linksRequest{LinkIDs: []string{x.f}}, &resp)
require.Equal(t, http.StatusOK, status)
status, body := sendJSON(t, x.s, x.sess, "DELETE", "/drive/volumes/"+x.volume+"/trash", nil)
require.Equal(t, http.StatusOK, status, "body: %s", body)
// A link deleted for good has no parent and isn't trashed.
return []event{{EventUpdateMetadata, x.f, "", false}, {EventDelete, x.f, "", false}}
}},
{"empty trash of a folder", func(t *testing.T, x *renameMoveCopy) []event {
var resp multiResponse
status := postJSON(t, x.s, "/drive/v2/volumes/"+x.volume+"/trash_multiple", sessionHeader(x.sess), &linksRequest{LinkIDs: []string{x.a}}, &resp)
require.Equal(t, http.StatusOK, status)
status, body := sendJSON(t, x.s, x.sess, "DELETE", "/drive/volumes/"+x.volume+"/trash", nil)
require.Equal(t, http.StatusOK, status, "body: %s", body)
return []event{
{EventUpdateMetadata, x.a, "", false},
{EventDelete, x.a, "", false},
{EventDelete, x.f, "", false},
{EventDelete, x.anon, "", false},
}
}},
{"delete draft", func(t *testing.T, x *renameMoveCopy) []event {
up := &uploader{t: t, s: x.s, u: x.u, sess: x.sess, volume: x.volume}
f := up.newFileRequest("draft")
up.create(f)
var resp multiResponse
status := postJSON(t, x.s, "/drive/v2/volumes/"+x.volume+"/delete_multiple", sessionHeader(x.sess), &linksRequest{LinkIDs: []string{f.linkID}}, &resp)
require.Equal(t, http.StatusOK, status)
require.Equal(t, api.CodeOK, resp.Responses[0].Response.Code)
return nil
}},
{"delete draft revision", func(t *testing.T, x *renameMoveCopy) []event {
up := &uploader{t: t, s: x.s, u: x.u, sess: x.sess, volume: x.volume}
f := up.newFileRequest("up")
up.create(f)
blocks := f.encrypt([]byte("one"))
up.uploadAll(f, blocks)
status, body := up.commit(f, f.commitRequest(blocks))
require.Equal(t, http.StatusOK, status, "body: %s", body)
current := f.revID
uid := testClientUID
path := "/drive/v2/volumes/" + x.volume + "/files/" + f.linkID + "/revisions"
status, body = up.send("POST", path, &createRevisionRequest{CurrentRevisionID: &current, ClientUID: &uid})
require.Equal(t, http.StatusOK, status, "body: %s", body)
var resp createRevisionResponse
require.NoError(t, json.Unmarshal(body, &resp))
status, body = up.send("DELETE", path+"/"+resp.Revision.ID, nil)
require.Equal(t, http.StatusOK, status, "body: %s", body)
return []event{{EventCreate, f.linkID, x.u.RootID(), false}}
}},
} {
t.Run(tc.name, func(t *testing.T) {
x := newRenameMoveCopy(t)
start := latestEvent(t, x.s, x.sess, x.u)
want := tc.do(t, x)
var got []event
for _, e := range eventsSince(t, x.s, x.sess, x.u, start) {
parent := ""
if e.Link.ParentLinkID != nil {
parent = *e.Link.ParentLinkID
}
got = append(got, event{e.EventType, e.Link.LinkID, parent, e.Link.IsTrashed})
}
assert.Equal(t, want, got)
})
}
}
func TestEventsCopyFolder(t *testing.T) {
x := newRenameMoveCopy(t)
start := latestEvent(t, x.s, x.sess, x.u)
email := x.u.Email()
status, body := sendJSON(t, x.s, x.sess, "POST", "/drive/volumes/"+x.volume+"/links/"+x.a+"/copy", &copyRequest{
Name: x.validMsg, NodePassphrase: x.validMsg, Hash: lookupHash([]byte("k"), "copy"),
TargetVolumeID: x.volume, TargetParentLinkID: x.b, NameSignatureEmail: &email,
})
require.Equal(t, http.StatusOK, status, "body: %s", body)
var resp copyResponse
require.NoError(t, json.Unmarshal(body, &resp))
// Every link the copy makes is created.
events := eventsSince(t, x.s, x.sess, x.u, start)
require.Len(t, events, 3)
parents := map[string]string{}
for _, e := range events {
assert.Equal(t, EventCreate, e.EventType)
parents[e.Link.LinkID] = *e.Link.ParentLinkID
}
assert.Equal(t, x.b, parents[resp.LinkID])
for id, parent := range parents {
if id != resp.LinkID {
assert.Equal(t, resp.LinkID, parent)
}
}
}
func TestEventsPaging(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
s.SetEventsPageSize(2)
cursor := latestEvent(t, s, sess, u)
var want []string
for _, name := range []string{"a", "b", "c", "d", "e"} {
want = append(want, u.SeedFolder(u.RootID(), name))
}
var got []string
seen := map[string]bool{cursor: true}
for i, wantMore := range []bool{true, true, false} {
resp := eventsPage(t, s, sess, u, cursor)
require.False(t, resp.Refresh)
assert.Equal(t, wantMore, resp.More, "call %d", i)
require.Len(t, resp.Events, min(2, len(want)-len(got)), "call %d", i)
for _, e := range resp.Events {
assert.False(t, seen[e.EventID], "duplicate event ID %s", e.EventID)
seen[e.EventID] = true
got = append(got, e.Link.LinkID)
}
assert.Equal(t, resp.Events[len(resp.Events)-1].EventID, resp.EventID)
cursor = resp.EventID
}
assert.Equal(t, want, got)
assert.Equal(t, cursor, latestEvent(t, s, sess, u))
}
// TestEventsPageSizeDefault checks a page size of 0 or less restores
// the default rather than serving empty pages with More set.
func TestEventsPageSizeDefault(t *testing.T) {
for _, n := range []int{0, -1} {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
s.SetEventsPageSize(n)
cursor := latestEvent(t, s, sess, u)
id := u.SeedFolder(u.RootID(), "a")
resp := eventsPage(t, s, sess, u, cursor)
assert.False(t, resp.More, "page size %d", n)
require.Len(t, resp.Events, 1, "page size %d", n)
assert.Equal(t, id, resp.Events[0].Link.LinkID)
}
}
func TestEventsRefresh(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
old := latestEvent(t, s, sess, u)
u.SeedFolder(u.RootID(), "a")
s.ExpireEvents(u)
newest := latestEvent(t, s, sess, u)
assert.NotEqual(t, old, newest)
resp := eventsPage(t, s, sess, u, old)
assert.True(t, resp.Refresh)
assert.Equal(t, newest, resp.EventID)
assert.Empty(t, resp.Events)
resp = eventsPage(t, s, sess, u, "unknown")
assert.True(t, resp.Refresh)
assert.Equal(t, newest, resp.EventID)
// The newest cursor still works, and so do the ones after it.
resp = eventsPage(t, s, sess, u, newest)
assert.False(t, resp.Refresh)
assert.Empty(t, resp.Events)
id := u.SeedFolder(u.RootID(), "b")
events := eventsSince(t, s, sess, u, newest)
require.Len(t, events, 1)
assert.Equal(t, id, events[0].Link.LinkID)
}
func TestEventsVolumeGone(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
cursor := latestEvent(t, s, sess, u)
for _, path := range []string{
"/drive/volumes/unknown/events/latest",
"/drive/v2/volumes/unknown/events/" + cursor,
} {
status, body := getJSON(t, s, sess, path, nil)
requireRejected(t, api.CodeNotExists, status, body)
}
}
func TestEventsSeedAppends(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
assert.Empty(t, s.Events(u))
dir := u.SeedFolder(u.RootID(), "dir")
id := u.SeedFile(u.RootID(), "f", []byte("f"), time.Now())
assert.Equal(t, []Event{
{Type: EventCreate, LinkID: dir, ParentLinkID: u.RootID()},
{Type: EventCreate, LinkID: id, ParentLinkID: u.RootID()},
}, s.Events(u))
u.SeedRevision(id, []byte("g"), time.Now())
u.ClaimSize(id, 10)
u.Corrupt(id, "name")
u.SetParent(id, dir)
// Every event reports the link as it is now.
assert.Equal(t, []Event{
{Type: EventCreate, LinkID: id, ParentLinkID: dir},
{Type: EventUpdate, LinkID: id, ParentLinkID: dir},
{Type: EventUpdate, LinkID: id, ParentLinkID: dir},
{Type: EventUpdate, LinkID: id, ParentLinkID: dir},
{Type: EventUpdateMetadata, LinkID: id, ParentLinkID: dir},
}, s.Events(u)[1:])
u.ForgeRootSigner(u, u.Email())
assert.Equal(t, Event{Type: EventUpdate, LinkID: u.RootID()}, s.Events(u)[6])
// An injected event is served as given, not with its link's state.
sess := s.NewSession(u)
cursor := latestEvent(t, s, sess, u)
s.InjectEvent(u, Event{Type: EventUpdateMetadata, LinkID: dir, IsShared: true, IsTrashed: true})
events := eventsSince(t, s, sess, u, cursor)
require.Len(t, events, 1)
assert.Equal(t, EventUpdateMetadata, events[0].EventType)
assert.Equal(t, dir, events[0].Link.LinkID)
assert.Nil(t, events[0].Link.ParentLinkID)
assert.True(t, events[0].Link.IsShared)
assert.True(t, events[0].Link.IsTrashed)
assert.Equal(t, Event{Type: EventUpdateMetadata, LinkID: dir, IsShared: true, IsTrashed: true}, s.Events(u)[len(s.Events(u))-1])
}
+767
View File
@@ -0,0 +1,767 @@
package protontest
import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"slices"
"sort"
"strings"
"time"
"github.com/ProtonMail/go-crypto/openpgp/armor"
"github.com/ProtonMail/go-crypto/openpgp/packet"
"github.com/ProtonMail/go-crypto/openpgp/s2k"
"github.com/rclone/rclone/backend/protondrive/api"
)
// OpenPGP packet tags checked in client-written values.
const (
tagPKESK = 1
tagSignature = 2
tagSecretKey = 5
tagSecretSubkey = 7
tagSEIPD = 18
)
// s2kUsageSHA1 is the S2K usage octet of a secret key locked with CFB
// and a SHA-1 check, as ordinary (not AEAD) key locking writes.
const s2kUsageSHA1 = 254
// Wire types of the mutating routes, named after and shaped like the
// schemas in the SDK's driveTypes.ts.
type createFolderRequest struct {
ParentLinkID string
NodeKey string
NodeHashKey string
NodePassphrase string
NodePassphraseSignature string
SignatureEmail *string
Name string
Hash string
XAttr *string
}
type folderIDJSON struct {
ID string
}
type createFolderResponse struct {
Folder folderIDJSON
Code int
}
type renameRequest struct {
Name string
Hash *string
NameSignatureEmail *string
OriginalHash *string
}
type moveRequest struct {
Name string
NodePassphrase string
Hash string
ParentLinkID string
OriginalHash string
NameSignatureEmail *string
ContentHash *string
NodePassphraseSignature *string
SignatureEmail *string
}
type copyRequest struct {
Name string
NodePassphrase string
Hash string
TargetVolumeID string
TargetParentLinkID string
NameSignatureEmail *string
NodePassphraseSignature *string
SignatureEmail *string
}
type copyResponse struct {
LinkID string
Code int
}
type codeJSON struct {
Code int
Error string `json:",omitempty"`
}
type linkResultJSON struct {
LinkID string
Response codeJSON
}
type multiResponse struct {
Code int
Responses []linkResultJSON
}
type checkHashesRequest struct {
Hashes []string
ClientUID []string
}
type pendingHashJSON struct {
Hash string
RevisionID string
LinkID string
ClientUID *string
}
type availableHashesResponse struct {
AvailableHashes []string
PendingHashes []pendingHashJSON
Code int
}
type conflictDetailsJSON struct {
ConflictLinkID string
ConflictRevisionID *string
ConflictDraftRevisionID *string
ConflictDraftClientUID *string
}
type conflictResponse struct {
Code int
Error string
Details conflictDetailsJSON
}
// registerMutations registers the routes which change a drive.
func (s *Server) registerMutations() {
s.handleDrive("POST", "/drive/v2/volumes/{volumeID}/folders", s.handleCreateFolder)
s.handleDrive("PUT", "/drive/v2/volumes/{volumeID}/links/{linkID}/rename", s.handleRename)
s.handleDrive("PUT", "/drive/v2/volumes/{volumeID}/links/{linkID}/move", s.handleMove)
s.handleDrive("POST", "/drive/volumes/{volumeID}/links/{linkID}/copy", s.handleCopy)
s.handleDrive("POST", "/drive/v2/volumes/{volumeID}/trash_multiple", s.handleTrash)
s.handleDrive("DELETE", "/drive/volumes/{volumeID}/trash", s.handleEmptyTrash)
s.handleDrive("POST", "/drive/v2/volumes/{volumeID}/links/{linkID}/checkAvailableHashes", s.handleCheckAvailableHashes)
}
// decodeRequest decodes the JSON body of r into v, or answers HTTP 400
// and returns false.
func decodeRequest(w http.ResponseWriter, r *http.Request, v any) bool {
if err := json.NewDecoder(r.Body).Decode(v); err != nil {
writeError(w, http.StatusBadRequest, api.CodeInvalidValue, "protontest: bad request body: "+err.Error())
return false
}
return true
}
// writeInvalid answers HTTP 422 code 2001 for a request failing check.
func writeInvalid(w http.ResponseWriter, err error) {
writeError(w, http.StatusUnprocessableEntity, api.CodeInvalidValue, "protontest: "+err.Error())
}
// writeConflict answers HTTP 422 code 2500 for a request whose name
// hash is taken by l.
func writeConflict(w http.ResponseWriter, l *link) {
details := conflictDetailsJSON{ConflictLinkID: l.id}
for _, r := range l.revisions {
switch r.state {
case revisionStateActive:
details.ConflictRevisionID = nullable(r.id)
case revisionStateDraft:
details.ConflictDraftRevisionID = nullable(r.id)
details.ConflictDraftClientUID = nullable(r.clientUID)
}
}
writeJSON(w, http.StatusUnprocessableEntity, &conflictResponse{
Code: api.CodeAlreadyExists,
Error: "A file or folder with that name already exists",
Details: details,
})
}
// activeLink returns the active link id, or nil.
func (d *drive) activeLink(id string) *link {
if l := d.links[id]; l != nil && l.state == linkStateActive {
return l
}
return nil
}
// activeFolder returns the active folder id, or nil.
func (d *drive) activeFolder(id string) *link {
if l := d.activeLink(id); l != nil && l.typ == linkTypeFolder {
return l
}
return nil
}
// conflict returns the link other than except in folder parentID
// which is not trashed and has the name hash hash, or nil.
func (d *drive) conflict(parentID, hash, except string) *link {
for _, l := range d.links {
if l.parentID == parentID && l.nameHash == hash && l.state != linkStateTrashed && l.id != except {
return l
}
}
return nil
}
// within reports whether link id is ancestorID or below it.
func (d *drive) within(id, ancestorID string) bool {
for l := d.links[id]; l != nil; l = d.links[l.parentID] {
if l.id == ancestorID {
return true
}
}
return false
}
// inTrash reports whether l or one of its ancestors is trashed.
func (d *drive) inTrash(l *link) bool {
for ; l != nil; l = d.links[l.parentID] {
if l.state == linkStateTrashed {
return true
}
}
return false
}
// ownsEmailLocked reports whether email is one of u's addresses.
// srv.mu must be held.
func (u *User) ownsEmailLocked(email string) bool {
return u.addressByEmailLocked(email) != nil
}
// checkHash returns an error unless hash is a lower-case hex SHA-256.
func checkHash(hash string) error {
if len(hash) != 64 || strings.ToLower(hash) != hash {
return fmt.Errorf("hash %q is not lower-case hex SHA-256", hash)
}
if _, err := hex.DecodeString(hash); err != nil {
return fmt.Errorf("hash %q is not lower-case hex SHA-256", hash)
}
return nil
}
// checkSigner returns an error unless email is one of u's addresses.
func checkSigner(u *User, field string, email *string) error {
if email == nil || !u.ownsEmailLocked(*email) {
return fmt.Errorf("%s must be an address of the user", field)
}
return nil
}
// checkMessages checks every named armored message with checkMessage.
func checkMessages(messages map[string]string) error {
for field, armored := range messages {
if err := checkMessage(armored); err != nil {
return fmt.Errorf("%s: %w", field, err)
}
}
return nil
}
// checkMessage returns an error unless armored is an OpenPGP message of
// v3 PKESKs followed by one SEIPDv1 packet.
//
// Official clients write names, passphrases, hash keys and extended
// attributes in that format and not all of them read SEIPDv2 or v6
// PKESKs there, whatever the recipient key accepts.
func checkMessage(armored string) error {
if armored == "" {
return errors.New("missing")
}
block, err := armor.Decode(strings.NewReader(armored))
if err != nil {
return fmt.Errorf("not armored: %w", err)
}
if block.Type != "PGP MESSAGE" {
return fmt.Errorf("armor type %q, want PGP MESSAGE", block.Type)
}
packets := packet.NewOpaqueReader(block.Body)
keyPackets, dataPackets := 0, 0
for {
op, err := packets.Next()
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("malformed packet: %w", err)
}
if dataPackets > 0 {
return errors.New("packet after the encrypted data")
}
version := -1
if len(op.Contents) > 0 {
version = int(op.Contents[0])
}
switch op.Tag {
case tagPKESK:
if version != 3 {
return fmt.Errorf("v%d PKESK, want v3", version)
}
keyPackets++
case tagSEIPD:
if version != 1 {
return fmt.Errorf("SEIPDv%d, want SEIPDv1", version)
}
dataPackets++
default:
return fmt.Errorf("unexpected packet with tag %d", op.Tag)
}
}
if keyPackets == 0 || dataPackets == 0 {
return errors.New("want PKESKs and an encrypted data packet")
}
return nil
}
// checkFolderKey returns an error unless armored is a v4 secret key
// whose signatures don't advertise AEAD, with every secret key packet
// locked with CFB and an iterated and salted S2K, as official clients
// generate folder keys. The official web app can't use a node key
// locked with Argon2 or AEAD.
func checkFolderKey(armored string) error {
return checkNodeKey(armored, false)
}
// checkFileKey returns an error unless armored is a v4 secret key
// locked as checkFolderKey requires. Unlike a folder key, a file key
// advertises AEAD, for its content.
func checkFileKey(armored string) error {
return checkNodeKey(armored, true)
}
// checkNodeKey does the work of checkFolderKey and checkFileKey; aead
// allows the key to advertise AEAD.
func checkNodeKey(armored string, aead bool) error {
block, err := armor.Decode(strings.NewReader(armored))
if err != nil {
return fmt.Errorf("not armored: %w", err)
}
if block.Type != "PGP PRIVATE KEY BLOCK" {
return fmt.Errorf("armor type %q, want PGP PRIVATE KEY BLOCK", block.Type)
}
packets := packet.NewOpaqueReader(block.Body)
secretKeys := 0
for {
op, err := packets.Next()
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("malformed packet: %w", err)
}
switch op.Tag {
case tagSecretKey, tagSecretSubkey:
if err := checkSecretKeyPacket(op); err != nil {
return err
}
secretKeys++
case tagSignature:
p, err := op.Parse()
sig, ok := p.(*packet.Signature)
if err != nil || !ok {
return fmt.Errorf("malformed signature: %v", err)
}
if !aead && (sig.SEIPDv2 || len(sig.PreferredCipherSuites) > 0) {
return errors.New("key advertises AEAD (SEIPDv2)")
}
}
}
if secretKeys == 0 {
return errors.New("no secret key")
}
return nil
}
// checkSecretKeyPacket checks one secret key or subkey packet for
// checkNodeKey.
func checkSecretKeyPacket(op *packet.OpaquePacket) error {
if len(op.Contents) == 0 || op.Contents[0] != 4 {
return errors.New("not a v4 key")
}
p, err := op.Parse()
pk, ok := p.(*packet.PrivateKey)
if err != nil || !ok {
return fmt.Errorf("malformed secret key: %v", err)
}
// The packet starts with the public key body, which a v4 key hashes
// after 0x99 and a two-octet length.
var pub bytes.Buffer
if err := pk.PublicKey.SerializeForHash(&pub); err != nil {
return fmt.Errorf("malformed secret key: %w", err)
}
rest := op.Contents[pub.Len()-3:]
if len(rest) < 3 {
return errors.New("malformed secret key: truncated")
}
switch usage := rest[0]; usage {
case s2kUsageSHA1:
case 0:
return errors.New("secret key is not locked")
case 253:
return errors.New("secret key is locked with AEAD")
default:
return fmt.Errorf("secret key S2K usage %d, want %d", usage, s2kUsageSHA1)
}
if mode := s2k.Mode(rest[2]); mode != s2k.IteratedSaltedS2K {
return fmt.Errorf("secret key S2K mode %d, want iterated and salted", mode)
}
return nil
}
// keySignature returns the node passphrase signature and its signer
// that l has once moved or copied, given the request's sig and email.
//
// As the SDK's moveNode and copyNode, a request gives them if and only
// if l's key is anonymous: an anonymous key is then signed by the
// user, and a signed one keeps its signature, as the passphrase is
// unchanged.
func keySignature(u *User, l *link, sig, email *string) (string, string, error) {
if l.signatureEmail != "" {
if (sig != nil && *sig != "") || (email != nil && *email != "") {
return "", "", errors.New("NodePassphraseSignature and SignatureEmail must not be given for a signed node")
}
return l.nodePass.signature, l.signatureEmail, nil
}
if sig == nil || *sig == "" {
return "", "", errors.New("NodePassphraseSignature is required for an anonymous node")
}
if err := checkSigner(u, "SignatureEmail", email); err != nil {
return "", "", err
}
return *sig, *email, nil
}
// handleCreateFolder creates a folder from key material the client
// generated.
func (s *Server) handleCreateFolder(w http.ResponseWriter, r *http.Request, u *User, d *drive) {
var req createFolderRequest
if !decodeRequest(w, r, &req) {
return
}
parent := d.activeFolder(req.ParentLinkID)
if parent == nil {
writeError(w, http.StatusUnprocessableEntity, api.CodeNotExists, "Parent folder not found")
return
}
messages := map[string]string{
"Name": req.Name,
"NodePassphrase": req.NodePassphrase,
"NodeHashKey": req.NodeHashKey,
}
if req.XAttr != nil {
messages["XAttr"] = *req.XAttr
}
err := errors.Join(
checkHash(req.Hash),
checkSigner(u, "SignatureEmail", req.SignatureEmail),
checkMessages(messages),
)
if err == nil && req.NodePassphraseSignature == "" {
err = errors.New("NodePassphraseSignature is required")
}
if err == nil {
err = annotateField("NodeKey", checkFolderKey(req.NodeKey))
}
if err != nil {
writeInvalid(w, err)
return
}
if other := d.conflict(parent.id, req.Hash, ""); other != nil {
writeConflict(w, other)
return
}
now := time.Now().Unix()
l := &link{
id: randomID(),
parentID: parent.id,
typ: linkTypeFolder,
state: linkStateActive,
createTime: now,
modifyTime: now,
name: req.Name,
nameHash: req.Hash,
nodeKey: req.NodeKey,
nodePass: sealedPassphrase{armored: req.NodePassphrase, signature: req.NodePassphraseSignature},
signatureEmail: *req.SignatureEmail,
nameSignatureEmail: *req.SignatureEmail,
hashKey: req.NodeHashKey,
}
if req.XAttr != nil {
l.xattr = *req.XAttr
}
d.addLink(l)
d.logLink(EventCreate, l)
s.lagLocked(l.id, nil)
writeJSON(w, http.StatusOK, &createFolderResponse{Folder: folderIDJSON{ID: l.id}, Code: api.CodeOK})
}
// annotateField returns err prefixed with field, or nil.
func annotateField(field string, err error) error {
if err == nil {
return nil
}
return fmt.Errorf("%s: %w", field, err)
}
// handleRename renames a link within its folder.
func (s *Server) handleRename(w http.ResponseWriter, r *http.Request, u *User, d *drive) {
var req renameRequest
if !decodeRequest(w, r, &req) {
return
}
l := d.activeLink(r.PathValue("linkID"))
switch {
case l == nil:
writeError(w, http.StatusUnprocessableEntity, api.CodeNotExists, "File or folder not found")
return
case l.parentID == "":
writeError(w, http.StatusUnprocessableEntity, api.CodeInvalidRequirements, "protontest: the root can't be renamed")
return
case req.OriginalHash == nil || *req.OriginalHash != l.nameHash:
writeError(w, http.StatusUnprocessableEntity, api.CodeInvalidRequirements, "protontest: OriginalHash doesn't match: the node changed")
return
}
hash := ""
if req.Hash != nil {
hash = *req.Hash
}
err := errors.Join(
checkHash(hash),
checkSigner(u, "NameSignatureEmail", req.NameSignatureEmail),
checkMessages(map[string]string{"Name": req.Name}),
)
if err != nil {
writeInvalid(w, err)
return
}
if other := d.conflict(l.parentID, hash, l.id); other != nil {
writeConflict(w, other)
return
}
l.name, l.nameHash, l.nameSignatureEmail = req.Name, hash, *req.NameSignatureEmail
l.modifyTime = time.Now().Unix()
d.logLink(EventUpdateMetadata, l)
writeJSON(w, http.StatusOK, &codeJSON{Code: api.CodeOK})
}
// handleMove moves a link to another folder, possibly renaming it.
func (s *Server) handleMove(w http.ResponseWriter, r *http.Request, u *User, d *drive) {
var req moveRequest
if !decodeRequest(w, r, &req) {
return
}
l := d.activeLink(r.PathValue("linkID"))
parent := d.activeFolder(req.ParentLinkID)
switch {
case l == nil:
writeError(w, http.StatusUnprocessableEntity, api.CodeNotExists, "File or folder not found")
return
case l.parentID == "":
writeError(w, http.StatusUnprocessableEntity, api.CodeInvalidRequirements, "protontest: the root can't be moved")
return
case parent == nil:
writeError(w, http.StatusUnprocessableEntity, api.CodeNotExists, "Parent folder not found")
return
case d.within(parent.id, l.id):
writeError(w, http.StatusUnprocessableEntity, api.CodeInvalidRequirements, "protontest: a folder can't be moved into itself")
return
case req.OriginalHash != l.nameHash:
writeError(w, http.StatusUnprocessableEntity, api.CodeInvalidRequirements, "protontest: OriginalHash doesn't match: the node changed")
return
}
err := errors.Join(
checkHash(req.Hash),
checkSigner(u, "NameSignatureEmail", req.NameSignatureEmail),
checkMessages(map[string]string{"Name": req.Name, "NodePassphrase": req.NodePassphrase}),
)
if err != nil {
writeInvalid(w, err)
return
}
sig, signer, err := keySignature(u, l, req.NodePassphraseSignature, req.SignatureEmail)
if err != nil {
writeInvalid(w, err)
return
}
if other := d.conflict(parent.id, req.Hash, l.id); other != nil {
writeConflict(w, other)
return
}
l.parentID, l.name, l.nameHash, l.nameSignatureEmail = parent.id, req.Name, req.Hash, *req.NameSignatureEmail
l.nodePass = sealedPassphrase{armored: req.NodePassphrase, signature: sig}
l.signatureEmail = signer
l.modifyTime = time.Now().Unix()
d.logLink(EventUpdateMetadata, l)
writeJSON(w, http.StatusOK, &codeJSON{Code: api.CodeOK})
}
// handleCopy copies a link, and a folder's active descendants, into a
// folder. Only the name and passphrase of the copy come from the
// client; its key, content and descendants are the server's copies.
func (s *Server) handleCopy(w http.ResponseWriter, r *http.Request, u *User, d *drive) {
var req copyRequest
if !decodeRequest(w, r, &req) {
return
}
l := d.activeLink(r.PathValue("linkID"))
parent := d.activeFolder(req.TargetParentLinkID)
switch {
case l == nil:
writeError(w, http.StatusUnprocessableEntity, api.CodeNotExists, "File or folder not found")
return
case l.parentID == "":
writeError(w, http.StatusUnprocessableEntity, api.CodeInvalidRequirements, "protontest: the root can't be copied")
return
case req.TargetVolumeID != d.volumeID:
writeError(w, http.StatusUnprocessableEntity, api.CodeNotExists, "Volume not found")
return
case parent == nil:
writeError(w, http.StatusUnprocessableEntity, api.CodeNotExists, "Parent folder not found")
return
case d.within(parent.id, l.id):
writeError(w, http.StatusUnprocessableEntity, api.CodeInvalidRequirements, "protontest: a folder can't be copied into itself")
return
}
err := errors.Join(
checkHash(req.Hash),
checkSigner(u, "NameSignatureEmail", req.NameSignatureEmail),
checkMessages(map[string]string{"Name": req.Name, "NodePassphrase": req.NodePassphrase}),
)
if err != nil {
writeInvalid(w, err)
return
}
sig, signer, err := keySignature(u, l, req.NodePassphraseSignature, req.SignatureEmail)
if err != nil {
writeInvalid(w, err)
return
}
if other := d.conflict(parent.id, req.Hash, ""); other != nil {
writeConflict(w, other)
return
}
c := d.copyLink(l, parent.id, time.Now().Unix())
c.name, c.nameHash, c.nameSignatureEmail = req.Name, req.Hash, *req.NameSignatureEmail
c.nodePass = sealedPassphrase{armored: req.NodePassphrase, signature: sig}
c.signatureEmail = signer
s.lagLocked(c.id, nil)
writeJSON(w, http.StatusOK, &copyResponse{LinkID: c.id, Code: api.CodeOK})
}
// copyLink stores and returns a copy of l in folder parentID with its
// active revision and, for a folder, copies of its active descendants.
// Blocks are shared, as they are immutable.
func (d *drive) copyLink(l *link, parentID string, now int64) *link {
c := *l
c.id, c.parentID = randomID(), parentID
c.state, c.createTime, c.modifyTime, c.trashTime = linkStateActive, now, now, 0
c.revisions = nil
if r := l.activeRevision(); r != nil {
rc := *r
rc.id = randomID()
rc.blocks = slices.Clone(r.blocks)
c.revisions = []*revision{&rc}
}
var children []*link
for _, child := range d.links {
if child.parentID == l.id && child.state == linkStateActive {
children = append(children, child)
}
}
sort.Slice(children, func(i, j int) bool { return children[i].seq < children[j].seq })
d.addLink(&c)
d.logLink(EventCreate, &c)
for _, child := range children {
d.copyLink(child, c.id, now)
}
return &c
}
// handleTrash trashes up to 100 links, answering for each.
func (s *Server) handleTrash(w http.ResponseWriter, r *http.Request, u *User, d *drive) {
var req linksRequest
if !decodeRequest(w, r, &req) {
return
}
if len(req.LinkIDs) > maxLinksPerRequest {
writeError(w, http.StatusUnprocessableEntity, api.CodeInvalidValue, fmt.Sprintf("protontest: %d link IDs, at most %d allowed", len(req.LinkIDs), maxLinksPerRequest))
return
}
now := time.Now().Unix()
resp := &multiResponse{Code: api.CodeOKMany, Responses: []linkResultJSON{}}
for _, id := range req.LinkIDs {
result := linkResultJSON{LinkID: id, Response: codeJSON{Code: api.CodeOK}}
l := d.activeLink(id)
switch {
case l == nil:
result.Response = codeJSON{Code: api.CodeNotExists, Error: "File or folder not found"}
case l.parentID == "":
result.Response = codeJSON{Code: api.CodeInvalidRequirements, Error: "protontest: the root can't be trashed"}
default:
l.state, l.trashTime, l.modifyTime = linkStateTrashed, now, now
d.logLink(EventUpdateMetadata, l)
}
resp.Responses = append(resp.Responses, result)
}
writeJSON(w, http.StatusOK, resp)
}
// handleEmptyTrash deletes every trashed link with its descendants.
func (s *Server) handleEmptyTrash(w http.ResponseWriter, r *http.Request, u *User, d *drive) {
var doomed []*link
for _, l := range d.links {
if d.inTrash(l) {
doomed = append(doomed, l)
}
}
sort.Slice(doomed, func(i, j int) bool { return doomed[i].seq < doomed[j].seq })
for _, l := range doomed {
d.logLink(EventDelete, l)
delete(d.links, l.id)
}
writeJSON(w, http.StatusOK, &codeJSON{Code: api.CodeOK})
}
// handleCheckAvailableHashes reports which name hashes are free in a
// folder, and the drafts holding the others unless they belong to one
// of the given ClientUIDs.
func (s *Server) handleCheckAvailableHashes(w http.ResponseWriter, r *http.Request, u *User, d *drive) {
var req checkHashesRequest
if !decodeRequest(w, r, &req) {
return
}
parent := d.activeFolder(r.PathValue("linkID"))
if parent == nil {
writeError(w, http.StatusUnprocessableEntity, api.CodeNotExists, "Folder not found")
return
}
resp := &availableHashesResponse{AvailableHashes: []string{}, PendingHashes: []pendingHashJSON{}, Code: api.CodeOK}
for _, hash := range req.Hashes {
other := d.conflict(parent.id, hash, "")
if other == nil {
resp.AvailableHashes = append(resp.AvailableHashes, hash)
continue
}
if other.state != linkStateDraft {
continue
}
for _, rev := range other.revisions {
if rev.state == revisionStateDraft && !slices.Contains(req.ClientUID, rev.clientUID) {
resp.PendingHashes = append(resp.PendingHashes, pendingHashJSON{
Hash: hash,
RevisionID: rev.id,
LinkID: other.id,
ClientUID: nullable(rev.clientUID),
})
}
}
}
writeJSON(w, http.StatusOK, resp)
}
@@ -0,0 +1,542 @@
package protontest
import (
"bytes"
"encoding/json"
"io"
"net/http"
"strings"
"testing"
"time"
"github.com/ProtonMail/go-crypto/openpgp/armor"
"github.com/ProtonMail/go-crypto/openpgp/packet"
"github.com/ProtonMail/go-crypto/openpgp/s2k"
"github.com/ProtonMail/gopenpgp/v3/crypto"
"github.com/ProtonMail/gopenpgp/v3/profile"
"github.com/rclone/rclone/backend/protondrive/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// sendJSON sends in as JSON with method to path as sess and returns
// the HTTP status and body.
func sendJSON(t *testing.T, s *Server, sess Session, method, path string, in any) (int, []byte) {
t.Helper()
body, err := json.Marshal(in)
require.NoError(t, err)
req, err := http.NewRequest(method, s.URL()+path, bytes.NewReader(body))
require.NoError(t, err)
for k, v := range sessionHeader(sess) {
req.Header.Set(k, v)
}
resp, err := s.Client().Do(req)
require.NoError(t, err)
defer func() { _ = resp.Body.Close() }()
out, err := io.ReadAll(resp.Body)
require.NoError(t, err)
return resp.StatusCode, out
}
// requireRejected fails the test unless the response is HTTP 422 with
// code, and its message contains each of want.
func requireRejected(t *testing.T, code, status int, body []byte, want ...string) {
t.Helper()
require.Equal(t, http.StatusUnprocessableEntity, status, "body: %s", body)
e := decodeError(t, body)
assert.Equal(t, code, e.Code, "body: %s", body)
for _, w := range want {
assert.Contains(t, e.Message, w)
}
}
// newFolderRequest returns a create folder request for name in
// parentID made as the SDK's createFolder does.
func newFolderRequest(u *User, parentID, name string) *createFolderRequest {
v := u.view()
parentKey, parentHashKey := v.folderKeys(parentID)
node := newDriveKey(false)
pass := sealPassphrase(node.passphrase, parentKey, v.addrKey)
_, hashKey := newHashKey(node.key)
xattr := encryptArmored([]byte(`{"Common":{"ModificationTime":"2024-01-02T03:04:05.000Z"}}`), node.key, v.addrKey, true)
return &createFolderRequest{
ParentLinkID: parentID,
NodeKey: node.armored,
NodeHashKey: hashKey,
NodePassphrase: pass.armored,
NodePassphraseSignature: pass.signature,
SignatureEmail: &v.email,
Name: encryptArmored([]byte(name), parentKey, v.addrKey, false),
Hash: lookupHash(parentHashKey, name),
XAttr: &xattr,
}
}
// messagePackets returns the packets of an armored message.
func messagePackets(t *testing.T, armored string) []*packet.OpaquePacket {
t.Helper()
block, err := armor.Decode(strings.NewReader(armored))
require.NoError(t, err)
var out []*packet.OpaquePacket
r := packet.NewOpaqueReader(block.Body)
for {
op, err := r.Next()
if err == io.EOF {
return out
}
require.NoError(t, err)
out = append(out, op)
}
}
// armorPackets returns packets as an armored message.
func armorPackets(t *testing.T, packets ...*packet.OpaquePacket) string {
t.Helper()
var out bytes.Buffer
w, err := armor.Encode(&out, "PGP MESSAGE", nil)
require.NoError(t, err)
for _, op := range packets {
require.NoError(t, op.Serialize(w))
}
require.NoError(t, w.Close())
return out.String()
}
// aeadMessages returns messages in the formats clients must not write
// for names, passphrases, hash keys and extended attributes, by name.
func aeadMessages(t *testing.T) map[string]string {
t.Helper()
// Encrypting to a file key, which advertises AEAD, gives a v6 PKESK
// and SEIPDv2, as gopenpgp does unless told otherwise.
eh, err := aeadPGP().Encryption().Recipient(newDriveKey(true).key).New()
require.NoError(t, err)
msg, err := eh.Encrypt([]byte("value"))
require.NoError(t, err)
armored, err := msg.Armor()
require.NoError(t, err)
v6 := messagePackets(t, armored)
require.Len(t, v6, 2)
require.Equal(t, []byte{6, 2}, []byte{v6[0].Contents[0], v6[1].Contents[0]})
v3 := messagePackets(t, encryptArmored([]byte("value"), newDriveKey(false).key, nil, false))
require.Len(t, v3, 2)
require.Equal(t, []byte{3, 1}, []byte{v3[0].Contents[0], v3[1].Contents[0]})
return map[string]string{
"v6 PKESK and SEIPDv2": armored,
"v3 PKESK and SEIPDv2": armorPackets(t, v3[0], v6[1]),
"v6 PKESK and SEIPDv1": armorPackets(t, v6[0], v3[1]),
}
}
// lockedWith returns a new v4 node key locked by a handle of p.
func lockedWith(t *testing.T, p *profile.Custom) string {
t.Helper()
key, err := crypto.PGP().KeyGeneration().AddUserId(driveKeyName, "").New().GenerateKey()
require.NoError(t, err)
locked, err := crypto.PGPWithProfile(p).LockKey(key, newPassphrase())
require.NoError(t, err)
armored, err := locked.Armor()
require.NoError(t, err)
return armored
}
func TestCreateFolderFormats(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
path := "/drive/v2/volumes/" + u.VolumeID() + "/folders"
post := func(req *createFolderRequest) (int, []byte) {
return sendJSON(t, s, sess, "POST", path, req)
}
for variant, bad := range aeadMessages(t) {
for field, spoil := range map[string]func(req *createFolderRequest){
"Name": func(req *createFolderRequest) { req.Name = bad },
"NodePassphrase": func(req *createFolderRequest) { req.NodePassphrase = bad },
"NodeHashKey": func(req *createFolderRequest) { req.NodeHashKey = bad },
"XAttr": func(req *createFolderRequest) { req.XAttr = &bad },
} {
t.Run(field+" "+variant, func(t *testing.T) {
req := newFolderRequest(u, u.RootID(), "dir")
spoil(req)
status, body := post(req)
requireRejected(t, api.CodeInvalidValue, status, body, field)
})
}
}
aeadLocked := profile.Default()
aeadLocked.AeadKeyEncryption = &packet.AEADConfig{}
argon2 := profile.Default()
argon2.AeadKeyEncryption = &packet.AEADConfig{}
argon2.S2kKeyEncryption = &s2k.Config{S2KMode: s2k.Argon2S2K, Argon2Config: &s2k.Argon2Config{}}
salted := profile.Default()
salted.S2kKeyEncryption = &s2k.Config{S2KMode: s2k.SaltedS2K, PassphraseIsHighEntropy: true}
v6Key, err := crypto.PGPWithProfile(profile.RFC9580()).KeyGeneration().AddUserId(driveKeyName, "").New().GenerateKey()
require.NoError(t, err)
v6Locked, err := crypto.PGPWithProfile(profile.RFC9580()).LockKey(v6Key, newPassphrase())
require.NoError(t, err)
v6Armored, err := v6Locked.Armor()
require.NoError(t, err)
unlocked, err := generateKey(driveKeyName, "").Armor()
require.NoError(t, err)
for name, tc := range map[string]struct {
key string
want string
}{
"advertises AEAD": {newDriveKey(true).armored, "advertises AEAD"},
"v6": {v6Armored, "not a v4 key"},
"AEAD locked": {lockedWith(t, aeadLocked), "locked with AEAD"},
"Argon2": {lockedWith(t, argon2), "locked with AEAD"},
"salted S2K": {lockedWith(t, salted), "S2K mode 1"},
"not locked": {unlocked, "not locked"},
} {
t.Run("NodeKey "+name, func(t *testing.T) {
req := newFolderRequest(u, u.RootID(), "dir")
req.NodeKey = tc.key
status, body := post(req)
requireRejected(t, api.CodeInvalidValue, status, body, "NodeKey", tc.want)
})
}
for name, spoil := range map[string]func(req *createFolderRequest){
"no hash key": func(req *createFolderRequest) { req.NodeHashKey = "" },
"no signature": func(req *createFolderRequest) { req.NodePassphraseSignature = "" },
"bad hash": func(req *createFolderRequest) { req.Hash = "ABC" },
"no signature email": func(req *createFolderRequest) { req.SignatureEmail = nil },
"someone else's email": func(req *createFolderRequest) { other := "other@proton.test"; req.SignatureEmail = &other },
} {
t.Run(name, func(t *testing.T) {
req := newFolderRequest(u, u.RootID(), "dir")
spoil(req)
status, body := post(req)
requireRejected(t, api.CodeInvalidValue, status, body)
})
}
assert.Empty(t, u.Dump())
req := newFolderRequest(u, u.RootID(), "dir")
req.ParentLinkID = "unknown"
status, body := post(req)
requireRejected(t, api.CodeNotExists, status, body)
var created createFolderResponse
status, body = post(newFolderRequest(u, u.RootID(), "dir"))
require.Equal(t, http.StatusOK, status, "body: %s", body)
require.NoError(t, json.Unmarshal(body, &created))
dump := u.Dump()
require.Len(t, dump, 1)
assert.Equal(t, created.Folder.ID, dump[0].ID)
assert.Equal(t, "dir", dump[0].Path)
assert.NoError(t, dump[0].Err)
assert.True(t, time.Date(2024, 1, 2, 3, 4, 5, 0, time.UTC).Equal(dump[0].ModTime))
// The name hash must be unique among the folder's untrashed nodes.
fileID := u.SeedFile(u.RootID(), "file", []byte("x"), time.Now())
status, body = post(newFolderRequest(u, u.RootID(), "file"))
requireRejected(t, api.CodeAlreadyExists, status, body)
var conflict conflictResponse
require.NoError(t, json.Unmarshal(body, &conflict))
assert.Equal(t, fileID, conflict.Details.ConflictLinkID)
require.NotNil(t, conflict.Details.ConflictRevisionID)
assert.Equal(t, u.view().d.links[fileID].activeRevision().id, *conflict.Details.ConflictRevisionID)
}
// renameMoveCopy holds a user with folders a and b, a signed file f in
// a and an anonymous file anon in a, and a session.
type renameMoveCopy struct {
s *Server
u *User
sess Session
a, b, f, anon string
volume string
validMsg, aeadMsg string
}
func newRenameMoveCopy(t *testing.T) *renameMoveCopy {
s := New(t)
u := s.AddUser("user", "pass")
x := &renameMoveCopy{s: s, u: u, sess: s.NewSession(u), volume: u.VolumeID()}
x.a = u.SeedFolder(u.RootID(), "a")
x.b = u.SeedFolder(u.RootID(), "b")
x.f = u.SeedFile(x.a, "f", []byte("f"), time.Now())
x.anon = u.SeedAnonymousFile(x.a, "anon", []byte("anon"), time.Now())
// The fake doesn't decrypt what it is sent, so any message in the
// right format will do.
x.validMsg = encryptArmored([]byte("value"), newDriveKey(false).key, nil, false)
x.aeadMsg = aeadMessages(t)["v6 PKESK and SEIPDv2"]
return x
}
// hash returns the stored name hash of link id.
func (x *renameMoveCopy) hash(id string) string {
return x.u.view().d.links[id].nameHash
}
func TestRenameRules(t *testing.T) {
x := newRenameMoveCopy(t)
path := func(id string) string { return "/drive/v2/volumes/" + x.volume + "/links/" + id + "/rename" }
email := x.u.Email()
newHash := lookupHash([]byte("k"), "new")
req := func(original string) *renameRequest {
return &renameRequest{Name: x.validMsg, Hash: &newHash, NameSignatureEmail: &email, OriginalHash: &original}
}
status, body := sendJSON(t, x.s, x.sess, "PUT", path("unknown"), req(""))
requireRejected(t, api.CodeNotExists, status, body)
status, body = sendJSON(t, x.s, x.sess, "PUT", path(x.f), req("stale"))
requireRejected(t, api.CodeInvalidRequirements, status, body, "OriginalHash")
status, body = sendJSON(t, x.s, x.sess, "PUT", path(x.u.RootID()), req(""))
requireRejected(t, api.CodeInvalidRequirements, status, body)
bad := req(x.hash(x.f))
bad.Name = x.aeadMsg
status, body = sendJSON(t, x.s, x.sess, "PUT", path(x.f), bad)
requireRejected(t, api.CodeInvalidValue, status, body, "Name")
taken := req(x.hash(x.f))
anonHash := x.hash(x.anon)
taken.Hash = &anonHash
status, body = sendJSON(t, x.s, x.sess, "PUT", path(x.f), taken)
requireRejected(t, api.CodeAlreadyExists, status, body)
// Renaming to its own name is no conflict.
same := req(x.hash(x.f))
ownHash := x.hash(x.f)
same.Hash = &ownHash
status, body = sendJSON(t, x.s, x.sess, "PUT", path(x.f), same)
require.Equal(t, http.StatusOK, status, "body: %s", body)
status, body = sendJSON(t, x.s, x.sess, "PUT", path(x.f), req(x.hash(x.f)))
require.Equal(t, http.StatusOK, status, "body: %s", body)
assert.Equal(t, newHash, x.hash(x.f))
}
func TestMoveRules(t *testing.T) {
x := newRenameMoveCopy(t)
path := func(id string) string { return "/drive/v2/volumes/" + x.volume + "/links/" + id + "/move" }
email := x.u.Email()
other := "other@proton.test"
sig := "signature"
req := func(id, parentID string) *moveRequest {
return &moveRequest{
Name: x.validMsg,
NodePassphrase: x.validMsg,
Hash: lookupHash([]byte("k"), "new"),
ParentLinkID: parentID,
OriginalHash: x.hash(id),
NameSignatureEmail: &email,
}
}
sub := x.u.SeedFolder(x.a, "sub")
for name, tc := range map[string]struct {
id string
req *moveRequest
code int
reason string
}{
"unknown link": {"unknown", req(x.f, x.b), api.CodeNotExists, ""},
"unknown parent": {x.f, req(x.f, "unknown"), api.CodeNotExists, ""},
"parent is a file": {x.anon, req(x.anon, x.f), api.CodeNotExists, ""},
"into itself": {x.a, req(x.a, x.a), api.CodeInvalidRequirements, "into itself"},
"into a descendant": {x.a, req(x.a, sub), api.CodeInvalidRequirements, "into itself"},
"the root": {x.u.RootID(), req(x.u.RootID(), x.b), api.CodeInvalidRequirements, ""},
"stale original hash": {x.f, func() *moveRequest { r := req(x.f, x.b); r.OriginalHash = "stale"; return r }(), api.CodeInvalidRequirements, "OriginalHash"},
"AEAD name": {x.f, func() *moveRequest { r := req(x.f, x.b); r.Name = x.aeadMsg; return r }(), api.CodeInvalidValue, "Name"},
"AEAD passphrase": {x.f, func() *moveRequest { r := req(x.f, x.b); r.NodePassphrase = x.aeadMsg; return r }(), api.CodeInvalidValue, "NodePassphrase"},
"name signed by another": {x.f, func() *moveRequest { r := req(x.f, x.b); r.NameSignatureEmail = &other; return r }(), api.CodeInvalidValue, "NameSignatureEmail"},
"signed node re-signed": {x.f, func() *moveRequest {
r := req(x.f, x.b)
r.NodePassphraseSignature, r.SignatureEmail = &sig, &email
return r
}(), api.CodeInvalidValue, "must not be given"},
"anonymous node unsigned": {x.anon, req(x.anon, x.b), api.CodeInvalidValue, "anonymous"},
"anonymous node signed by another": {x.anon, func() *moveRequest {
r := req(x.anon, x.b)
r.NodePassphraseSignature, r.SignatureEmail = &sig, &other
return r
}(), api.CodeInvalidValue, "SignatureEmail"},
"name taken": {x.f, func() *moveRequest { r := req(x.f, x.a); r.Hash = x.hash(x.anon); return r }(), api.CodeAlreadyExists, ""},
} {
status, body := sendJSON(t, x.s, x.sess, "PUT", path(tc.id), tc.req)
t.Run(name, func(t *testing.T) {
requireRejected(t, tc.code, status, body, tc.reason)
})
}
// A signed node keeps its passphrase signature; an anonymous one
// takes the one given.
before := *x.u.view().d.links[x.f]
status, body := sendJSON(t, x.s, x.sess, "PUT", path(x.f), req(x.f, x.b))
require.Equal(t, http.StatusOK, status, "body: %s", body)
after := x.u.view().d.links[x.f]
assert.Equal(t, x.b, after.parentID)
assert.Equal(t, before.nodePass.signature, after.nodePass.signature)
assert.Equal(t, before.signatureEmail, after.signatureEmail)
assert.Equal(t, x.validMsg, after.nodePass.armored)
r := req(x.anon, x.b)
r.Hash = lookupHash([]byte("k"), "anon")
r.NodePassphraseSignature, r.SignatureEmail = &sig, &email
status, body = sendJSON(t, x.s, x.sess, "PUT", path(x.anon), r)
require.Equal(t, http.StatusOK, status, "body: %s", body)
anon := x.u.view().d.links[x.anon]
assert.Equal(t, sig, anon.nodePass.signature)
assert.Equal(t, email, anon.signatureEmail)
assert.Equal(t, email, anon.nameSignatureEmail)
}
func TestCopyRules(t *testing.T) {
x := newRenameMoveCopy(t)
path := func(id string) string { return "/drive/volumes/" + x.volume + "/links/" + id + "/copy" }
email := x.u.Email()
req := func(parentID string) *copyRequest {
return &copyRequest{
Name: x.validMsg,
NodePassphrase: x.validMsg,
Hash: lookupHash([]byte("k"), "copy"),
TargetVolumeID: x.volume,
TargetParentLinkID: parentID,
NameSignatureEmail: &email,
}
}
status, body := sendJSON(t, x.s, x.sess, "POST", path("unknown"), req(x.b))
requireRejected(t, api.CodeNotExists, status, body)
wrongVolume := req(x.b)
wrongVolume.TargetVolumeID = "unknown"
status, body = sendJSON(t, x.s, x.sess, "POST", path(x.f), wrongVolume)
requireRejected(t, api.CodeNotExists, status, body)
status, body = sendJSON(t, x.s, x.sess, "POST", path(x.a), req(x.a))
requireRejected(t, api.CodeInvalidRequirements, status, body, "into itself")
status, body = sendJSON(t, x.s, x.sess, "POST", path(x.anon), req(x.b))
requireRejected(t, api.CodeInvalidValue, status, body, "anonymous")
aead := req(x.b)
aead.NodePassphrase = x.aeadMsg
status, body = sendJSON(t, x.s, x.sess, "POST", path(x.f), aead)
requireRejected(t, api.CodeInvalidValue, status, body, "NodePassphrase")
taken := req(x.a)
taken.Hash = x.hash(x.f)
status, body = sendJSON(t, x.s, x.sess, "POST", path(x.f), taken)
requireRejected(t, api.CodeAlreadyExists, status, body)
status, body = sendJSON(t, x.s, x.sess, "POST", path(x.f), req(x.b))
require.Equal(t, http.StatusOK, status, "body: %s", body)
var resp copyResponse
require.NoError(t, json.Unmarshal(body, &resp))
v := x.u.view()
src, cp := v.d.links[x.f], v.d.links[resp.LinkID]
require.NotNil(t, cp)
assert.Equal(t, x.b, cp.parentID)
assert.Equal(t, src.nodeKey, cp.nodeKey)
assert.Equal(t, src.contentKeyPacket, cp.contentKeyPacket)
assert.Equal(t, src.nodePass.signature, cp.nodePass.signature)
assert.NotEqual(t, src.activeRevision().id, cp.activeRevision().id)
assert.Equal(t, src.activeRevision().blocks, cp.activeRevision().blocks)
}
func TestTrashAndEmptyTrash(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
dir := u.SeedFolder(u.RootID(), "dir")
child := u.SeedFile(dir, "child", []byte("c"), time.Now())
kept := u.SeedFile(u.RootID(), "kept", []byte("k"), time.Now())
var resp multiResponse
status := postJSON(t, s, "/drive/v2/volumes/"+u.VolumeID()+"/trash_multiple", sessionHeader(sess),
&linksRequest{LinkIDs: []string{dir, "unknown", u.RootID()}}, &resp)
require.Equal(t, http.StatusOK, status)
assert.Equal(t, api.CodeOKMany, resp.Code)
require.Len(t, resp.Responses, 3)
assert.Equal(t, linkResultJSON{LinkID: dir, Response: codeJSON{Code: api.CodeOK}}, resp.Responses[0])
assert.Equal(t, api.CodeNotExists, resp.Responses[1].Response.Code)
assert.NotEmpty(t, resp.Responses[1].Response.Error)
assert.Equal(t, api.CodeInvalidRequirements, resp.Responses[2].Response.Code)
states := map[string]int{}
for _, n := range u.Dump() {
require.NoError(t, n.Err)
states[n.ID] = n.State
}
assert.Equal(t, map[string]int{dir: linkStateTrashed, child: linkStateActive, kept: linkStateActive}, states)
status, body := sendJSON(t, s, sess, "DELETE", "/drive/volumes/"+u.VolumeID()+"/trash", nil)
require.Equal(t, http.StatusOK, status, "body: %s", body)
dump := u.Dump()
require.Len(t, dump, 1)
assert.Equal(t, kept, dump[0].ID)
}
func TestCheckAvailableHashes(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
sess := s.NewSession(u)
root := u.RootID()
u.SeedFile(root, "taken", []byte("x"), time.Now())
trashed := u.SeedFile(root, "trashed", []byte("x"), time.Now())
_, hashKey := u.view().folderKeys(root)
draftID, draftRev := randomID(), randomID()
s.mu.Lock()
d := u.mustDriveLocked()
d.links[trashed].state = linkStateTrashed
d.addLink(&link{
id: draftID,
parentID: root,
typ: linkTypeFile,
state: linkStateDraft,
nameHash: lookupHash(hashKey, "draft"),
revisions: []*revision{{id: draftRev, state: revisionStateDraft, clientUID: "mine"}},
})
s.mu.Unlock()
hashes := []string{lookupHash(hashKey, "free"), lookupHash(hashKey, "taken"), lookupHash(hashKey, "trashed"), lookupHash(hashKey, "draft")}
path := "/drive/v2/volumes/" + u.VolumeID() + "/links/" + root + "/checkAvailableHashes"
var resp availableHashesResponse
require.Equal(t, http.StatusOK, postJSON(t, s, path, sessionHeader(sess), &checkHashesRequest{Hashes: hashes}, &resp))
assert.Equal(t, []string{hashes[0], hashes[2]}, resp.AvailableHashes)
mine := "mine"
assert.Equal(t, []pendingHashJSON{{Hash: hashes[3], RevisionID: draftRev, LinkID: draftID, ClientUID: &mine}}, resp.PendingHashes)
resp = availableHashesResponse{}
require.Equal(t, http.StatusOK, postJSON(t, s, path, sessionHeader(sess), &checkHashesRequest{Hashes: hashes, ClientUID: []string{"mine"}}, &resp))
assert.Equal(t, []string{hashes[0], hashes[2]}, resp.AvailableHashes)
assert.Empty(t, resp.PendingHashes)
var apiErr api.Error
assert.Equal(t, http.StatusUnprocessableEntity, postJSON(t, s, "/drive/v2/volumes/"+u.VolumeID()+"/links/unknown/checkAvailableHashes", sessionHeader(sess), &checkHashesRequest{}, &apiErr))
assert.Equal(t, api.CodeNotExists, apiErr.Code)
}
func TestDumpChecksNameHash(t *testing.T) {
s := New(t)
u := s.AddUser("user", "pass")
id := u.SeedFile(u.RootID(), "file", []byte("x"), time.Now())
require.NoError(t, u.Dump()[0].Err)
s.mu.Lock()
u.mustDriveLocked().links[id].nameHash = lookupHash([]byte("other key"), "file")
s.mu.Unlock()
err := u.Dump()[0].Err
require.Error(t, err)
assert.Contains(t, err.Error(), "name hash")
}
// TestMutationWireNames checks the backend's request types decode into
// the fake's without unknown fields.
func TestMutationWireNames(t *testing.T) {
for _, tc := range []struct {
client any
fake any
}{
{&api.CreateFolderRequest{XAttr: "x"}, &createFolderRequest{}},
{&api.RenameRequest{}, &renameRequest{}},
{&api.MoveRequest{NodePassphraseSignature: "x", SignatureEmail: "x"}, &moveRequest{}},
{&api.CopyRequest{NodePassphraseSignature: "x", SignatureEmail: "x"}, &copyRequest{}},
{&api.LinksRequest{}, &linksRequest{}},
{&api.MultiLinkResponse{Responses: []api.LinkResult{{}}}, &multiResponse{}},
{&api.CreateFolderResponse{}, &createFolderResponse{}},
{&api.CopyResponse{}, &copyResponse{}},
} {
data, err := json.Marshal(tc.client)
require.NoError(t, err)
dec := json.NewDecoder(bytes.NewReader(data))
dec.DisallowUnknownFields()
assert.NoError(t, dec.Decode(tc.fake), "%T", tc.client)
}
}
+440
View File
@@ -0,0 +1,440 @@
// Package protontest is a fake Proton Drive API server for tests.
//
// It must not import the protondrive backend, whose tests use it.
package protontest
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/http/httptest"
"regexp"
"slices"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/ProtonMail/gopenpgp/v3/crypto"
"github.com/rclone/rclone/backend/protondrive/api"
)
var (
// appVersionRe is Proton's published x-pm-appversion grammar
// (SDK README, "Usage Guidelines") with the name fixed to rclone.
appVersionRe = regexp.MustCompile(`^external-drive-rclone@[0-9]+\.[0-9]+\.[0-9]+(-(stable|beta|alpha))?(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$`)
// legacyRouteRe matches the removed share-scoped routes and
// everything beneath them.
legacyRouteRe = regexp.MustCompile(`^/drive/shares/[^/]+/(links|folders|files|events)(/|$)`)
)
// legacyRequests counts the requests for legacy routes, per "METHOD
// path", made to every server in the process, so a test binary can
// check none was made after all its tests have run.
var legacyRequests = struct {
mu sync.Mutex
counts map[string]int
}{counts: make(map[string]int)}
// LegacyRequests returns a copy of the count, per "METHOD path", of
// the requests for removed share-scoped routes made to every server in
// the process since it started. ResetRequests doesn't change it.
func LegacyRequests() map[string]int {
legacyRequests.mu.Lock()
defer legacyRequests.mu.Unlock()
out := make(map[string]int, len(legacyRequests.counts))
for k, v := range legacyRequests.counts {
out[k] = v
}
return out
}
// Fault describes a scripted failure for requests to one route.
type Fault struct {
Method string // HTTP method to match; empty matches any
Pattern string // route pattern as registered, e.g. "/tests/ping"
Skip int // matching requests let through before faulting
Times int // matching requests faulted after Skip; 0 means forever
Status int // HTTP status to return; 0 with Drop false only holds the request
Code int // Proton code in the error body
RetryAfter int // Retry-After header in seconds, if non-zero
Drop bool // close the connection instead of responding
Delay time.Duration // wait before handling the request
Gather bool // hold each faulted request until all Times have arrived
AfterHandler bool // run the real handler, then replace its response
// Hold, if set, makes each faulted request run the real handler on
// arrival and get its response only once Hold is closed, so the
// response is as of arrival. Held, if set, is sent to once the
// response is ready and held.
Hold <-chan struct{}
Held chan<- struct{}
}
// fault is an injected Fault with the count of requests it has matched.
type fault struct {
Fault
seen int
gathered chan struct{} // closed when the last of Times has arrived, if Gather
}
// fires records a matching request and reports whether it is faulted.
func (f *fault) fires() bool {
f.seen++
n := f.seen - f.Skip
if f.Gather && n == f.Times {
close(f.gathered)
}
return n > 0 && (f.Times == 0 || n <= f.Times)
}
// Server is a fake Proton API server.
type Server struct {
srv *httptest.Server
mux *http.ServeMux
mu sync.Mutex
requests map[string]int // count per "METHOD pattern"
faults []*fault
clockSkew time.Duration
users map[string]*User // by username
sessions map[string]*session // by UID
srp map[string]*srpLogin // by SRPSession
forks map[string]*fork // by Selector
childrenPageSize int
eventsPageSize int
downloads map[string]*download // block URLs by ID
unlockedKeys map[string]*crypto.Key // by keyCacheID
uploads map[string]*uploadURL // block upload URLs by ID
storageUploads int // block uploads in progress
storagePeak int // most block uploads in progress at once
secrets []issuedSecret // every secret issued, in order
keysAs map[string]*User // whose keys GET /core/v4/keys/all serves for an email, overriding the owner's
readLag int // reads of a node after a change which don't see it; see SetReadLag
readLagView LagView // what lagging reads of a changed node see
lagging map[string]*lagView // nodes whose reads lag, by ID
// driveRequirements is sent as x-pm-drive-requirements headers if
// not empty; see SetDriveRequirements.
driveRequirements []string
}
// New starts a fake server which is closed when the test ends.
func New(t testing.TB) *Server {
s := newServer()
s.srv = httptest.NewServer(http.HandlerFunc(s.serveHTTP))
t.Cleanup(s.Close)
return s
}
// NewStandalone starts a fake server listening on addr, such as
// "127.0.0.1:0", for use outside tests. Call Close to stop it.
func NewStandalone(addr string) (*Server, error) {
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, fmt.Errorf("protontest: listen: %w", err)
}
s := newServer()
s.srv = httptest.NewUnstartedServer(http.HandlerFunc(s.serveHTTP))
_ = s.srv.Listener.Close()
s.srv.Listener = l
s.srv.Start()
return s, nil
}
// newServer returns a server with its routes registered, not yet
// listening.
func newServer() *Server {
s := &Server{
mux: http.NewServeMux(),
requests: make(map[string]int),
users: make(map[string]*User),
sessions: make(map[string]*session),
srp: make(map[string]*srpLogin),
forks: make(map[string]*fork),
childrenPageSize: defaultChildrenPageSize,
eventsPageSize: defaultEventsPageSize,
downloads: make(map[string]*download),
unlockedKeys: make(map[string]*crypto.Key),
uploads: make(map[string]*uploadURL),
lagging: make(map[string]*lagView),
}
s.handle("GET", "/tests/ping", func(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, map[string]int{"Code": api.CodeOK})
})
s.registerAuth()
s.registerAccount()
s.registerDrive()
return s
}
// Close stops the server and blocks until all its requests are done.
func (s *Server) Close() {
s.srv.Close()
}
// URL returns the base URL of the server.
func (s *Server) URL() string {
return s.srv.URL
}
// Client returns an HTTP client configured for the server.
func (s *Server) Client() *http.Client {
return s.srv.Client()
}
// Requests returns a copy of the request count per "METHOD pattern",
// e.g. "GET /drive/v2/volumes/{volumeID}/folders/{linkID}/children".
//
// Requests for unregistered or legacy routes are counted under their
// path.
func (s *Server) Requests() map[string]int {
s.mu.Lock()
defer s.mu.Unlock()
out := make(map[string]int, len(s.requests))
for k, v := range s.requests {
out[k] = v
}
return out
}
// ResetRequests zeroes the request counts and the peak of
// StorageUploadPeak.
func (s *Server) ResetRequests() {
s.mu.Lock()
defer s.mu.Unlock()
clear(s.requests)
s.storagePeak = s.storageUploads
}
// ServeKeysAs makes GET /core/v4/keys/all serve owner's primary
// address keys for email, as a hostile server could, whoever owns
// email.
func (s *Server) ServeKeysAs(email string, owner *User) {
s.mu.Lock()
defer s.mu.Unlock()
if s.keysAs == nil {
s.keysAs = make(map[string]*User)
}
s.keysAs[email] = owner
}
// SecretKind is a kind of secret the server issues.
type SecretKind int
// Kinds of secret the server issues.
const (
SessionToken SecretKind = iota + 1 // an access or refresh token
ForkSelector // the selector polling a session fork
StorageToken // a block upload or download token
)
// issuedSecret is a secret the server handed to a client.
type issuedSecret struct {
kind SecretKind
value string
}
// newSecretLocked returns a new random secret of kind and records it
// for IssuedSecrets. s.mu must be held.
func (s *Server) newSecretLocked(kind SecretKind) string {
secret := randomID()
s.secrets = append(s.secrets, issuedSecret{kind: kind, value: secret})
return secret
}
// IssuedSecrets returns every secret of the kinds given, or of any kind
// if none is, which the server has handed to a client, in order.
func (s *Server) IssuedSecrets(kinds ...SecretKind) []string {
s.mu.Lock()
defer s.mu.Unlock()
var out []string
for _, secret := range s.secrets {
if len(kinds) == 0 || slices.Contains(kinds, secret.kind) {
out = append(out, secret.value)
}
}
return out
}
// Inject adds a fault. Faults are checked in the order they were added
// and the first one that fires is applied. A Gather fault needs Times.
func (s *Server) Inject(f Fault) {
if f.Gather && f.Times <= 0 {
panic("protontest: a Gather fault needs Times")
}
s.mu.Lock()
defer s.mu.Unlock()
s.faults = append(s.faults, &fault{Fault: f, gathered: make(chan struct{})})
}
// SetClockSkew shifts the Date header of every response by d.
func (s *Server) SetClockSkew(d time.Duration) {
s.mu.Lock()
defer s.mu.Unlock()
s.clockSkew = d
}
// SetDriveRequirements makes every response carry an
// x-pm-drive-requirements header with each of values, as Proton
// announces upcoming requirements; none if values is empty.
func (s *Server) SetDriveRequirements(values ...string) {
s.mu.Lock()
defer s.mu.Unlock()
s.driveRequirements = values
}
// handle registers h for method and an http.ServeMux path pattern.
func (s *Server) handle(method, pattern string, h http.HandlerFunc) {
s.mux.HandleFunc(method+" "+pattern, h)
}
// serveHTTP checks, counts and dispatches every request.
func (s *Server) serveHTTP(w http.ResponseWriter, r *http.Request) {
_, pattern := s.mux.Handler(r)
// Registered patterns are "METHOD /path"; keep the path part.
_, pattern, _ = strings.Cut(pattern, " ")
key := r.Method + " " + pattern
if pattern == "" {
key = r.Method + " " + r.URL.Path
}
s.mu.Lock()
s.requests[key]++
w.Header().Set("Date", time.Now().Add(s.clockSkew).UTC().Format(http.TimeFormat))
for _, v := range s.driveRequirements {
w.Header().Add("x-pm-drive-requirements", v)
}
if pattern == storageUploadPattern {
s.storageUploads++
s.storagePeak = max(s.storagePeak, s.storageUploads)
defer func() {
s.mu.Lock()
s.storageUploads--
s.mu.Unlock()
}()
}
s.mu.Unlock()
switch {
case legacyRouteRe.MatchString(r.URL.Path):
legacyRequests.mu.Lock()
legacyRequests.counts[r.Method+" "+r.URL.Path]++
legacyRequests.mu.Unlock()
writeError(w, http.StatusGone, 0, "protontest: legacy route "+r.URL.Path+" was removed")
return
case r.Header.Get("x-pm-drive-sdk-version") != "":
writeError(w, http.StatusBadRequest, api.CodeInvalidSection, "protontest: x-pm-drive-sdk-version must not be sent")
return
case !appVersionRe.MatchString(r.Header.Get("x-pm-appversion")):
writeError(w, http.StatusBadRequest, api.CodeInvalidSection, fmt.Sprintf("protontest: invalid x-pm-appversion %q", r.Header.Get("x-pm-appversion")))
return
case pattern == "":
writeError(w, http.StatusNotFound, 0, "protontest: no route for "+r.Method+" "+r.URL.Path)
return
}
if f := s.activeFault(r.Method, pattern); f != nil {
s.serveFault(w, r, f)
return
}
s.mux.ServeHTTP(w, r)
}
// activeFault records the request against every matching fault and
// returns the first one that fires, or nil.
func (s *Server) activeFault(method, pattern string) *fault {
s.mu.Lock()
defer s.mu.Unlock()
var active *fault
for _, f := range s.faults {
if f.Pattern != pattern || (f.Method != "" && f.Method != method) {
continue
}
if f.fires() && active == nil {
active = f
}
}
return active
}
// serveFault applies f to a request for a registered route.
func (s *Server) serveFault(w http.ResponseWriter, r *http.Request, f *fault) {
if f.Delay > 0 || f.Gather {
// The server notices a client giving up, and cancels the request
// context, only once the request body has been read.
if body, err := io.ReadAll(r.Body); err == nil {
r.Body = io.NopCloser(bytes.NewReader(body))
}
}
if f.Gather {
select {
case <-f.gathered:
case <-r.Context().Done():
return
}
}
if f.Delay > 0 {
select {
case <-time.After(f.Delay):
case <-r.Context().Done():
return
}
}
if f.Hold != nil {
rec := httptest.NewRecorder()
s.mux.ServeHTTP(rec, r)
if f.Held != nil {
f.Held <- struct{}{}
}
select {
case <-f.Hold:
case <-r.Context().Done():
return
}
for k, v := range rec.Header() {
w.Header()[k] = v
}
w.WriteHeader(rec.Code)
_, _ = w.Write(rec.Body.Bytes())
return
}
if f.Status == 0 && !f.Drop {
s.mux.ServeHTTP(w, r)
return
}
if f.AfterHandler {
s.mux.ServeHTTP(httptest.NewRecorder(), r)
}
if f.Drop {
conn, _, err := http.NewResponseController(w).Hijack()
if err != nil {
panic(fmt.Sprintf("protontest: drop connection: %v", err))
}
_ = conn.Close()
return
}
if f.RetryAfter > 0 {
w.Header().Set("Retry-After", strconv.Itoa(f.RetryAfter))
}
writeError(w, f.Status, f.Code, "protontest: injected fault")
}
// writeJSON writes v as a JSON response with the given HTTP status.
func writeJSON(w http.ResponseWriter, status int, v any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(v)
}
// writeError writes an api.Error response.
func writeError(w http.ResponseWriter, status, code int, msg string) {
writeJSON(w, status, &api.Error{Code: code, Message: msg})
}
@@ -0,0 +1,332 @@
package protontest
import (
"encoding/json"
"io"
"net/http"
"strings"
"testing"
"time"
"github.com/rclone/rclone/backend/protondrive/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const testAppVersion = "external-drive-rclone@1.77.0-stable"
// do sends a request with the given headers and returns the response
// and its body.
func do(t *testing.T, s *Server, method, path string, header map[string]string) (*http.Response, []byte) {
t.Helper()
req, err := http.NewRequest(method, s.URL()+path, nil)
require.NoError(t, err)
for k, v := range header {
req.Header.Set(k, v)
}
resp, err := s.Client().Do(req)
require.NoError(t, err)
defer func() { _ = resp.Body.Close() }()
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
return resp, body
}
// ping calls GET /tests/ping with a valid app version.
func ping(t *testing.T, s *Server) (*http.Response, []byte) {
t.Helper()
return do(t, s, "GET", "/tests/ping", map[string]string{"x-pm-appversion": testAppVersion})
}
func decodeError(t *testing.T, body []byte) api.Error {
t.Helper()
var e api.Error
require.NoError(t, json.Unmarshal(body, &e), "body: %s", body)
return e
}
func TestLegacyRoutesGone(t *testing.T) {
s := New(t)
for _, path := range []string{
"/drive/shares/abc/links/def",
"/drive/shares/abc/folders/def/children",
"/drive/shares/abc/files",
"/drive/shares/abc/events/latest",
} {
for _, method := range []string{"GET", "POST"} {
resp, _ := do(t, s, method, path, map[string]string{"x-pm-appversion": testAppVersion})
assert.Equal(t, http.StatusGone, resp.StatusCode, "%s %s", method, path)
assert.Equal(t, 1, s.Requests()[method+" "+path], "%s %s counted", method, path)
}
}
// The share itself is not a legacy route.
resp, _ := do(t, s, "GET", "/drive/shares/abc", map[string]string{"x-pm-appversion": testAppVersion})
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
}
func TestLegacyRequestsProcessWide(t *testing.T) {
before := LegacyRequests()
s1, s2 := New(t), New(t)
do(t, s1, "GET", "/drive/shares/abc/links/def", map[string]string{"x-pm-appversion": testAppVersion})
do(t, s2, "GET", "/drive/shares/abc/links/def", map[string]string{"x-pm-appversion": testAppVersion})
do(t, s2, "POST", "/drive/shares/abc/files", nil)
do(t, s2, "GET", "/drive/shares/abc", nil)
s1.ResetRequests()
s2.ResetRequests()
after := LegacyRequests()
assert.Equal(t, before["GET /drive/shares/abc/links/def"]+2, after["GET /drive/shares/abc/links/def"])
assert.Equal(t, before["POST /drive/shares/abc/files"]+1, after["POST /drive/shares/abc/files"], "counted whatever its headers")
assert.NotContains(t, after, "GET /drive/shares/abc")
after["changed"] = 1
assert.NotContains(t, LegacyRequests(), "changed", "a copy is returned")
}
func TestUnknownRoute(t *testing.T) {
s := New(t)
resp, body := do(t, s, "GET", "/core/v4/nothing", map[string]string{"x-pm-appversion": testAppVersion})
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
assert.Contains(t, decodeError(t, body).Message, "GET /core/v4/nothing")
}
func TestAppVersionEnforced(t *testing.T) {
s := New(t)
for _, version := range []string{
"",
"web-drive@5.2.0",
"external-drive-rclone@1.77.0-dev",
"external-drive-other@1.77.0-stable",
"external-drive-rclone@1.77-stable",
"external-drive-rclone@1.77.0-stable+",
} {
header := map[string]string{}
if version != "" {
header["x-pm-appversion"] = version
}
resp, body := do(t, s, "GET", "/tests/ping", header)
assert.Equal(t, http.StatusBadRequest, resp.StatusCode, "version %q", version)
assert.Equal(t, api.CodeInvalidSection, decodeError(t, body).Code, "version %q", version)
}
for _, version := range []string{
"external-drive-rclone@1.77.0-stable",
"external-drive-rclone@1.77.0-beta+abc",
"external-drive-rclone@1.77.0-alpha",
"external-drive-rclone@1.77.0",
"external-drive-rclone@1.77.0-alpha+abc123f.dirty",
} {
resp, body := do(t, s, "GET", "/tests/ping", map[string]string{"x-pm-appversion": version})
assert.Equal(t, http.StatusOK, resp.StatusCode, "version %q", version)
assert.JSONEq(t, `{"Code":1000}`, string(body), "version %q", version)
}
resp, _ := do(t, s, "GET", "/tests/ping", map[string]string{
"x-pm-appversion": testAppVersion,
"x-pm-drive-sdk-version": "js@0.23.0",
})
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
}
func TestFaultInjection(t *testing.T) {
t.Run("Times", func(t *testing.T) {
s := New(t)
s.Inject(Fault{Pattern: "/tests/ping", Times: 2, Status: 429, RetryAfter: 1})
for i := range 2 {
resp, _ := ping(t, s)
assert.Equal(t, http.StatusTooManyRequests, resp.StatusCode, "call %d", i)
assert.Equal(t, "1", resp.Header.Get("Retry-After"), "call %d", i)
}
resp, _ := ping(t, s)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Empty(t, resp.Header.Get("Retry-After"))
})
t.Run("Skip", func(t *testing.T) {
s := New(t)
s.Inject(Fault{Pattern: "/tests/ping", Skip: 1, Times: 1, Status: 503, Code: 2028})
resp, _ := ping(t, s)
assert.Equal(t, http.StatusOK, resp.StatusCode)
resp, body := ping(t, s)
assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode)
assert.Equal(t, api.CodeFeatureBlocked, decodeError(t, body).Code)
resp, _ = ping(t, s)
assert.Equal(t, http.StatusOK, resp.StatusCode)
})
t.Run("Forever", func(t *testing.T) {
s := New(t)
s.Inject(Fault{Pattern: "/tests/ping", Status: 500})
for range 5 {
resp, _ := ping(t, s)
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
}
})
t.Run("Method", func(t *testing.T) {
s := New(t)
s.Inject(Fault{Method: "POST", Pattern: "/tests/ping", Status: 500})
resp, _ := ping(t, s)
assert.Equal(t, http.StatusOK, resp.StatusCode)
})
t.Run("Drop", func(t *testing.T) {
s := New(t)
s.Inject(Fault{Pattern: "/tests/ping", Times: 1, Drop: true})
req, err := http.NewRequest("GET", s.URL()+"/tests/ping", nil)
require.NoError(t, err)
req.Header.Set("x-pm-appversion", testAppVersion)
resp, err := s.Client().Do(req)
if err == nil {
_ = resp.Body.Close()
}
require.Error(t, err)
resp2, _ := ping(t, s)
assert.Equal(t, http.StatusOK, resp2.StatusCode)
})
t.Run("Delay", func(t *testing.T) {
s := New(t)
s.Inject(Fault{Pattern: "/tests/ping", Times: 1, Delay: 100 * time.Millisecond})
start := time.Now()
resp, _ := ping(t, s)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.GreaterOrEqual(t, time.Since(start), 100*time.Millisecond)
})
t.Run("Gather", func(t *testing.T) {
s := New(t)
s.Inject(Fault{Pattern: "/tests/ping", Times: 2, Gather: true})
req, err := http.NewRequest("GET", s.URL()+"/tests/ping", nil)
require.NoError(t, err)
req.Header.Set("x-pm-appversion", testAppVersion)
first := make(chan int)
go func() {
resp, err := s.Client().Do(req)
if err != nil {
first <- 0
return
}
_ = resp.Body.Close()
first <- resp.StatusCode
}()
select {
case <-first:
t.Fatal("first request answered before the second arrived")
case <-time.After(100 * time.Millisecond):
}
resp, _ := ping(t, s)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, http.StatusOK, <-first)
// Requests after Times are not held.
resp, _ = ping(t, s)
assert.Equal(t, http.StatusOK, resp.StatusCode)
})
t.Run("Hold", func(t *testing.T) {
s := New(t)
n := 0
s.handle("GET", "/tests/count", func(w http.ResponseWriter, r *http.Request) {
n++
writeJSON(w, http.StatusOK, map[string]int{"Code": api.CodeOK, "N": n})
})
release := make(chan struct{})
held := make(chan struct{}, 1)
s.Inject(Fault{Pattern: "/tests/count", Times: 1, Hold: release, Held: held})
first := make(chan []byte)
go func() {
_, body := do(t, s, "GET", "/tests/count", map[string]string{"x-pm-appversion": testAppVersion})
first <- body
}()
<-held
// The held request was handled on arrival; later ones pass.
_, body := do(t, s, "GET", "/tests/count", map[string]string{"x-pm-appversion": testAppVersion})
assert.JSONEq(t, `{"Code":1000,"N":2}`, string(body))
select {
case <-first:
t.Fatal("held request answered before release")
default:
}
close(release)
assert.JSONEq(t, `{"Code":1000,"N":1}`, string(<-first))
})
t.Run("AfterHandler", func(t *testing.T) {
s := New(t)
called := 0
s.handle("POST", "/tests/commit", func(w http.ResponseWriter, r *http.Request) {
called++
writeJSON(w, http.StatusOK, map[string]int{"Code": api.CodeOK})
})
s.Inject(Fault{Pattern: "/tests/commit", Times: 1, Status: 500, AfterHandler: true})
resp, body := do(t, s, "POST", "/tests/commit", map[string]string{"x-pm-appversion": testAppVersion})
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
assert.NotContains(t, string(body), `"Code":1000`)
assert.Equal(t, 1, called)
})
}
func TestRequestCounting(t *testing.T) {
s := New(t)
s.handle("GET", "/tests/volumes/{volumeID}/folders/{linkID}/children", func(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, map[string]any{"Code": api.CodeOK, "Volume": r.PathValue("volumeID")})
})
header := map[string]string{"x-pm-appversion": testAppVersion}
_, body := do(t, s, "GET", "/tests/volumes/v1/folders/l1/children", header)
assert.JSONEq(t, `{"Code":1000,"Volume":"v1"}`, string(body))
do(t, s, "GET", "/tests/volumes/v2/folders/l2/children", header)
ping(t, s)
assert.Equal(t, map[string]int{
"GET /tests/volumes/{volumeID}/folders/{linkID}/children": 2,
"GET /tests/ping": 1,
}, s.Requests())
// The returned map is a copy.
s.Requests()["GET /tests/ping"] = 99
assert.Equal(t, 1, s.Requests()["GET /tests/ping"])
s.ResetRequests()
assert.Empty(t, s.Requests())
ping(t, s)
assert.Equal(t, map[string]int{"GET /tests/ping": 1}, s.Requests())
}
func TestClockSkew(t *testing.T) {
s := New(t)
check := func(skew time.Duration) {
resp, _ := ping(t, s)
date, err := http.ParseTime(resp.Header.Get("Date"))
require.NoError(t, err)
assert.WithinDuration(t, time.Now().Add(skew), date, 5*time.Second)
}
check(0)
s.SetClockSkew(-2 * time.Hour)
check(-2 * time.Hour)
s.SetClockSkew(30 * time.Minute)
check(30 * time.Minute)
}
func TestErrorJSON(t *testing.T) {
e := api.Error{Status: 422, Code: api.CodeAlreadyExists, Message: "exists", Details: json.RawMessage(`{"ConflictLinkID":"x"}`)}
b, err := json.Marshal(&e)
require.NoError(t, err)
assert.JSONEq(t, `{"Code":2500,"Error":"exists","Details":{"ConflictLinkID":"x"}}`, string(b))
assert.True(t, strings.Contains(e.Error(), "exists") && strings.Contains(e.Error(), "2500") && strings.Contains(e.Error(), "422"), e.Error())
}
func TestNewStandalone(t *testing.T) {
s, err := NewStandalone("127.0.0.1:0")
require.NoError(t, err)
assert.False(t, strings.HasSuffix(s.URL(), ":0"), "URL %q must carry the bound port", s.URL())
resp, _ := ping(t, s)
assert.Equal(t, http.StatusOK, resp.StatusCode)
s.Close()
_, err = s.Client().Get(s.URL() + "/tests/ping")
assert.Error(t, err, "server still answering after Close")
_, err = NewStandalone("not an address")
assert.Error(t, err)
}
+191
View File
@@ -0,0 +1,191 @@
package protontest
import (
"bytes"
"time"
"github.com/ProtonMail/gopenpgp/v3/crypto"
)
// StoredFile is a file node as the server stores it: the encrypted
// metadata and content a client sent, with nothing decrypted.
type StoredFile struct {
Link StoredLink
Revision StoredRevision // the active revision
}
// StoredLink is the encrypted metadata of a file node.
type StoredLink struct {
Name string // armored, encrypted to the parent key
NameHash string
NodeKey string // armored, locked with the node passphrase
NodePassphrase string // armored, encrypted to the parent key
NodePassphraseSignature string
SignatureEmail string
NameSignatureEmail string
MediaType string
ContentKeyPacket string // base64
ContentKeyPacketSignature string
CreateTime int64 // Unix seconds; the time of seeding if zero
ModifyTime int64 // Unix seconds; the time of seeding if zero
}
// StoredRevision is a revision of a file node.
type StoredRevision struct {
ManifestSignature string
XAttr string // armored; empty if none
SignatureEmail string
CreateTime int64 // Unix seconds; the time of seeding if zero
Blocks []StoredBlock // in index order, from 1
}
// StoredBlock is an encrypted block of a revision.
type StoredBlock struct {
Hash []byte // SHA-256 of Data
EncSignature string
SignatureEmail string
Data []byte `json:"-"`
}
// StoredFile returns file id with its active revision as stored. It
// panics if id is not a file with an active revision.
func (u *User) StoredFile(id string) StoredFile {
u.srv.mu.Lock()
defer u.srv.mu.Unlock()
l := u.mustDriveLocked().links[id]
if l == nil || l.typ != linkTypeFile {
panic("protontest: no file " + id)
}
rev := l.activeRevision()
if rev == nil {
panic("protontest: file " + id + " has no active revision")
}
f := StoredFile{
Link: StoredLink{
Name: l.name,
NameHash: l.nameHash,
NodeKey: l.nodeKey,
NodePassphrase: l.nodePass.armored,
NodePassphraseSignature: l.nodePass.signature,
SignatureEmail: l.signatureEmail,
NameSignatureEmail: l.nameSignatureEmail,
MediaType: l.mediaType,
ContentKeyPacket: l.contentKeyPacket,
ContentKeyPacketSignature: l.contentKeyPacketSignature,
CreateTime: l.createTime,
ModifyTime: l.modifyTime,
},
Revision: StoredRevision{
ManifestSignature: rev.manifestSignature,
XAttr: rev.xattr,
SignatureEmail: rev.signatureEmail,
CreateTime: rev.createTime,
},
}
for _, b := range rev.blocks {
f.Revision.Blocks = append(f.Revision.Blocks, StoredBlock{
Hash: bytes.Clone(b.hash),
EncSignature: b.encSignature,
SignatureEmail: b.signatureEmail,
Data: bytes.Clone(b.data),
})
}
return f
}
// SeedStoredFile creates a file in parentID from f, stored as given
// without any check, and returns its ID. It panics as insertLink does.
func (u *User) SeedStoredFile(parentID string, f StoredFile) (id string) {
rev := &revision{
id: randomID(),
state: revisionStateActive,
createTime: f.Revision.CreateTime,
manifestSignature: f.Revision.ManifestSignature,
xattr: f.Revision.XAttr,
signatureEmail: f.Revision.SignatureEmail,
}
for i, b := range f.Revision.Blocks {
rev.blocks = append(rev.blocks, &block{
index: i + 1,
hash: bytes.Clone(b.Hash),
data: bytes.Clone(b.Data),
encSignature: b.EncSignature,
signatureEmail: b.SignatureEmail,
})
}
if rev.createTime == 0 {
rev.createTime = time.Now().Unix()
}
return u.insertLink(parentID, &link{
typ: linkTypeFile,
name: f.Link.Name,
nameHash: f.Link.NameHash,
nodeKey: f.Link.NodeKey,
nodePass: sealedPassphrase{armored: f.Link.NodePassphrase, signature: f.Link.NodePassphraseSignature},
signatureEmail: f.Link.SignatureEmail,
nameSignatureEmail: f.Link.NameSignatureEmail,
mediaType: f.Link.MediaType,
contentKeyPacket: f.Link.ContentKeyPacket,
contentKeyPacketSignature: f.Link.ContentKeyPacketSignature,
createTime: f.Link.CreateTime,
modifyTime: f.Link.ModifyTime,
revisions: []*revision{rev},
})
}
// StoredFolder is a folder node as the server stores it. Its Link has
// no file members.
type StoredFolder struct {
Link StoredLink
HashKey string // armored, encrypted to the node key
XAttr string // armored; empty if none
}
// SeedStoredFolder creates a folder in parentID from f, stored as given
// without any check, and returns its ID. It panics as insertLink does.
func (u *User) SeedStoredFolder(parentID string, f StoredFolder) (id string) {
return u.insertLink(parentID, &link{
typ: linkTypeFolder,
name: f.Link.Name,
nameHash: f.Link.NameHash,
nodeKey: f.Link.NodeKey,
nodePass: sealedPassphrase{armored: f.Link.NodePassphrase, signature: f.Link.NodePassphraseSignature},
signatureEmail: f.Link.SignatureEmail,
nameSignatureEmail: f.Link.NameSignatureEmail,
createTime: f.Link.CreateTime,
modifyTime: f.Link.ModifyTime,
hashKey: f.HashKey,
xattr: f.XAttr,
})
}
// FixedKeys is key material for AddUserWithKeys, in place of keys the
// fake would generate.
type FixedKeys struct {
AddressKey string // armored, locked with AddressKeyPassphrase
AddressKeyPassphrase string
RootKey string // armored root folder node key, locked with RootKeyPassphrase
RootKeyPassphrase string
RootHashKey string // armored root folder hash key, encrypted to the root key
}
// AddUserWithKeys is AddUser with the primary address key and the root
// folder's node key and hash key taken from keys, so that files stored
// by a client holding those keys can be seeded with SeedStoredFile. The
// user key and share key are generated as usual. It panics if a key
// doesn't unlock.
func (s *Server) AddUserWithKeys(username, password string, keys FixedKeys) *User {
addrKey, err := crypto.NewPrivateKeyFromArmored(keys.AddressKey, []byte(keys.AddressKeyPassphrase))
mustCrypto("unlock fixed address key", err)
rootKey, err := crypto.NewPrivateKeyFromArmored(keys.RootKey, []byte(keys.RootKeyPassphrase))
mustCrypto("unlock fixed root key", err)
u := newUser(s, username, password, password)
addr := u.addresses[0]
k := addr.keys[0]
k.passphrase, k.armored, k.key = []byte(keys.AddressKeyPassphrase), keys.AddressKey, addrKey
k.sealPassphrase(u.keys[0])
root := lockedKey{key: rootKey, passphrase: []byte(keys.RootKeyPassphrase), armored: keys.RootKey}
u.drive = s.newDriveWithRoot(addr, root, keys.RootHashKey)
s.addUser(u)
return u
}
+701
View File
@@ -0,0 +1,701 @@
package protontest
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"io"
"net/http"
"slices"
"sort"
"time"
"github.com/ProtonMail/go-crypto/openpgp/packet"
"github.com/ProtonMail/gopenpgp/v3/crypto"
"github.com/rclone/rclone/backend/protondrive/api"
)
const (
// storageUploadPattern is the route of block uploads.
storageUploadPattern = "/storage/upload/{token}"
// verificationCodeSize is the size of a draft's verification code,
// and so of each block's verifier token.
verificationCodeSize = 32
// maxUploadBlockSize is the largest encrypted block accepted.
maxUploadBlockSize = 8 << 20
// intendedSizePrecision is the precision of IntendedUploadSize.
intendedSizePrecision = 100_000
)
// pendingBlock is a block of a draft revision requested for upload.
type pendingBlock struct {
index int
encSignature string
token []byte // verifier token
hash []byte // SHA-256 the client declared; nil if none
data []byte // nil until uploaded
}
// uploadURL is a block upload URL issued by /drive/blocks.
type uploadURL struct {
token string
user *User
linkID string
rev *revision
block *pendingBlock
expired bool
}
// Wire types of the upload routes, named after and shaped like the
// schemas in the SDK's driveTypes.ts.
type createFileRequest struct {
ParentLinkID string
Name string
Hash string
MIMEType string
ClientUID *string
IntendedUploadSize *int64
NodeKey string
NodePassphrase string
NodePassphraseSignature string
ContentKeyPacket string
ContentKeyPacketSignature *string
SignatureAddress *string
}
type fileResponseJSON struct {
ID string
RevisionID string
ClientUID *string
}
type createFileResponse struct {
File fileResponseJSON
Code int
}
type createRevisionRequest struct {
CurrentRevisionID *string
ClientUID *string
IntendedUploadSize *int64
}
type revisionIDJSON struct {
ID string
}
type createRevisionResponse struct {
Revision revisionIDJSON
Code int
}
type verificationResponse struct {
VerificationCode string
ContentKeyPacket string
Code int
}
type verifierJSON struct {
Token string
}
type blockUploadInput struct {
Index int
Verifier *verifierJSON
EncSignature *string
Size *int64
Hash *string
}
type blockUploadRequest struct {
LinkID string
RevisionID string
AddressID *string
VolumeID *string
BlockList []blockUploadInput
ThumbnailList []struct{ Type int }
}
type blockURLJSON struct {
BareURL string
Token string
URL string
Index int
}
type blockUploadResponse struct {
UploadLinks []blockURLJSON
ThumbnailLinks []struct{}
Code int
}
type commitRevisionRequest struct {
ManifestSignature string
SignatureAddress *string
XAttr *string
ChecksumVerified bool
Photo *struct{}
}
// registerUpload registers the routes which upload files.
func (s *Server) registerUpload() {
s.handleDrive("POST", "/drive/v2/volumes/{volumeID}/files", s.handleCreateFile)
s.handleDrive("POST", "/drive/v2/volumes/{volumeID}/files/{linkID}/revisions", s.handleCreateRevision)
s.handleDrive("GET", "/drive/v2/volumes/{volumeID}/links/{linkID}/revisions/{revisionID}/verification", s.handleVerification)
s.handleAuth("POST", "/drive/blocks", s.handleRequestUpload)
s.handle("POST", storageUploadPattern, s.handleStorageUpload)
s.handleDrive("PUT", "/drive/v2/volumes/{volumeID}/files/{linkID}/revisions/{revisionID}", s.handleCommit)
s.handleDrive("DELETE", "/drive/v2/volumes/{volumeID}/files/{linkID}/revisions/{revisionID}", s.handleDeleteRevision)
s.handleDrive("POST", "/drive/v2/volumes/{volumeID}/delete_multiple", s.handleDeleteMultiple)
}
// ExpireUploadTokens makes every block upload URL issued so far answer
// HTTP 404, as an expired one does.
func (s *Server) ExpireUploadTokens() {
s.mu.Lock()
defer s.mu.Unlock()
for _, up := range s.uploads {
up.expired = true
}
}
// StorageUploadPeak returns the most block uploads that were in
// progress at once since the server started or ResetRequests.
func (s *Server) StorageUploadPeak() int {
s.mu.Lock()
defer s.mu.Unlock()
return s.storagePeak
}
// nowLocked returns the server's clock. srv.mu must be held.
func (s *Server) nowLocked() time.Time {
return time.Now().Add(s.clockSkew)
}
// newDraftRevision returns a draft revision created by clientUID.
func newDraftRevision(clientUID *string) *revision {
r := &revision{
id: randomID(),
state: revisionStateDraft,
createTime: time.Now().Unix(),
verificationCode: randomBytes(verificationCodeSize),
pending: make(map[int]*pendingBlock),
}
if clientUID != nil {
r.clientUID = *clientUID
}
return r
}
// checkIntendedSize returns an error if size, when given, is not
// rounded as the SDK rounds it: the server must not learn the size.
func checkIntendedSize(size *int64) error {
if size == nil {
return nil
}
if *size < 0 || (*size%intendedSizePrecision != 0 && *size != 4095) {
return fmt.Errorf("IntendedUploadSize %d is not rounded", *size)
}
return nil
}
// checkContentKeyPacket returns an error unless packet64 is the base64
// of one v6 PKESK: new content is written in the RFC 9580 format.
func checkContentKeyPacket(packet64 string) error {
raw, err := base64.StdEncoding.DecodeString(packet64)
if err != nil {
return fmt.Errorf("not base64: %w", err)
}
r := packet.NewOpaqueReader(bytes.NewReader(raw))
op, err := r.Next()
if err != nil {
return fmt.Errorf("malformed packet: %w", err)
}
if op.Tag != tagPKESK || len(op.Contents) == 0 || op.Contents[0] != 6 {
return fmt.Errorf("packet with tag %d version %d, want a v6 PKESK", op.Tag, firstByte(op.Contents))
}
if _, err := r.Next(); err != io.EOF {
return errors.New("more than one packet")
}
return nil
}
// contentKeyVersion returns the version of the PKESK of a stored
// content key packet, or -1.
func contentKeyVersion(packet64 string) int {
raw, err := base64.StdEncoding.DecodeString(packet64)
if err != nil {
return -1
}
op, err := packet.NewOpaqueReader(bytes.NewReader(raw)).Next()
if err != nil || op.Tag != tagPKESK {
return -1
}
return int(firstByte(op.Contents))
}
// checkBlockFormat returns an error unless data is one SEIPD packet of
// version: SEIPDv2 for content whose key packet is a v6 PKESK, SEIPDv1
// for a v3 one.
func checkBlockFormat(data []byte, version int) error {
r := packet.NewOpaqueReader(bytes.NewReader(data))
op, err := r.Next()
if err != nil {
return fmt.Errorf("malformed packet: %w", err)
}
if op.Tag != tagSEIPD || int(firstByte(op.Contents)) != version {
return fmt.Errorf("packet with tag %d version %d, want SEIPDv%d", op.Tag, firstByte(op.Contents), version)
}
if _, err := r.Next(); err != io.EOF {
return errors.New("more than one packet")
}
return nil
}
// firstByte returns b[0], or 0 if b is empty.
func firstByte(b []byte) byte {
if len(b) == 0 {
return 0
}
return b[0]
}
// draftRevision returns the draft revision id of file l, or nil.
func draftRevision(l *link, id string) *revision {
if l == nil || l.typ != linkTypeFile {
return nil
}
for _, r := range l.revisions {
if r.id == id && r.state == revisionStateDraft {
return r
}
}
return nil
}
// handleCreateFile creates a draft file, with a draft revision, from
// key material the client generated.
func (s *Server) handleCreateFile(w http.ResponseWriter, r *http.Request, u *User, d *drive) {
var req createFileRequest
if !decodeRequest(w, r, &req) {
return
}
parent := d.activeFolder(req.ParentLinkID)
if parent == nil {
writeError(w, http.StatusUnprocessableEntity, api.CodeNotExists, "Parent folder not found")
return
}
err := errors.Join(
checkHash(req.Hash),
checkSigner(u, "SignatureAddress", req.SignatureAddress),
checkMessages(map[string]string{"Name": req.Name, "NodePassphrase": req.NodePassphrase}),
annotateField("ContentKeyPacket", checkContentKeyPacket(req.ContentKeyPacket)),
checkIntendedSize(req.IntendedUploadSize),
)
switch {
case err != nil:
case req.NodePassphraseSignature == "":
err = errors.New("NodePassphraseSignature is required")
case req.ContentKeyPacketSignature == nil || *req.ContentKeyPacketSignature == "":
err = errors.New("ContentKeyPacketSignature is required")
case req.MIMEType == "":
err = errors.New("MIMEType is required")
default:
err = annotateField("NodeKey", checkFileKey(req.NodeKey))
}
if err != nil {
writeInvalid(w, err)
return
}
if other := d.conflict(parent.id, req.Hash, ""); other != nil {
writeConflict(w, other)
return
}
now := time.Now().Unix()
rev := newDraftRevision(req.ClientUID)
l := &link{
id: randomID(),
parentID: parent.id,
typ: linkTypeFile,
state: linkStateDraft,
createTime: now,
modifyTime: now,
name: req.Name,
nameHash: req.Hash,
nodeKey: req.NodeKey,
nodePass: sealedPassphrase{armored: req.NodePassphrase, signature: req.NodePassphraseSignature},
signatureEmail: *req.SignatureAddress,
nameSignatureEmail: *req.SignatureAddress,
mediaType: req.MIMEType,
contentKeyPacket: req.ContentKeyPacket,
contentKeyPacketSignature: *req.ContentKeyPacketSignature,
revisions: []*revision{rev},
}
d.addLink(l)
writeJSON(w, http.StatusOK, &createFileResponse{
File: fileResponseJSON{ID: l.id, RevisionID: rev.id, ClientUID: nullable(rev.clientUID)},
Code: api.CodeOK,
})
}
// handleCreateRevision creates a draft revision of an active file whose
// active revision is CurrentRevisionID.
func (s *Server) handleCreateRevision(w http.ResponseWriter, r *http.Request, u *User, d *drive) {
var req createRevisionRequest
if !decodeRequest(w, r, &req) {
return
}
l := d.activeLink(r.PathValue("linkID"))
if l == nil || l.typ != linkTypeFile {
writeError(w, http.StatusUnprocessableEntity, api.CodeNotExists, "File not found")
return
}
if err := checkIntendedSize(req.IntendedUploadSize); err != nil {
writeInvalid(w, err)
return
}
active := l.activeRevision()
if active == nil || req.CurrentRevisionID == nil || *req.CurrentRevisionID != active.id {
writeError(w, http.StatusUnprocessableEntity, api.CodeInvalidRequirements, "protontest: CurrentRevisionID is not the active revision: the file changed")
return
}
rev := newDraftRevision(req.ClientUID)
l.revisions = append(l.revisions, rev)
writeJSON(w, http.StatusOK, &createRevisionResponse{Revision: revisionIDJSON{ID: rev.id}, Code: api.CodeOK})
}
// handleVerification returns the verification code and content key
// packet of a draft revision.
func (s *Server) handleVerification(w http.ResponseWriter, r *http.Request, u *User, d *drive) {
l := d.links[r.PathValue("linkID")]
rev := draftRevision(l, r.PathValue("revisionID"))
if rev == nil {
writeError(w, http.StatusUnprocessableEntity, api.CodeNotExists, "Revision not found")
return
}
writeJSON(w, http.StatusOK, &verificationResponse{
VerificationCode: base64.StdEncoding.EncodeToString(rev.verificationCode),
ContentKeyPacket: l.contentKeyPacket,
Code: api.CodeOK,
})
}
// handleRequestUpload issues upload URLs on this server for blocks of a
// draft revision. Requesting a block again replaces it, and its URL.
func (s *Server) handleRequestUpload(w http.ResponseWriter, r *http.Request, u *User) {
var req blockUploadRequest
if !decodeRequest(w, r, &req) {
return
}
s.mu.Lock()
defer s.mu.Unlock()
d := u.drive
if d == nil || req.VolumeID == nil || *req.VolumeID != d.volumeID {
writeError(w, http.StatusUnprocessableEntity, api.CodeNotExists, "Volume not found")
return
}
l := d.links[req.LinkID]
rev := draftRevision(l, req.RevisionID)
if rev == nil {
writeError(w, http.StatusUnprocessableEntity, api.CodeNotExists, "Revision not found")
return
}
if req.AddressID == nil || !slices.ContainsFunc(u.addresses, func(a *address) bool { return a.id == *req.AddressID }) {
writeInvalid(w, errors.New("AddressID must be an address of the user"))
return
}
if len(req.ThumbnailList) > 0 {
writeInvalid(w, errors.New("protontest: thumbnails are not supported"))
return
}
blocks := make([]*pendingBlock, 0, len(req.BlockList))
for _, in := range req.BlockList {
b, err := pendingBlockOf(in)
if err != nil {
writeInvalid(w, fmt.Errorf("block %d: %w", in.Index, err))
return
}
blocks = append(blocks, b)
}
resp := &blockUploadResponse{UploadLinks: []blockURLJSON{}, ThumbnailLinks: []struct{}{}, Code: api.CodeOK}
for _, b := range blocks {
rev.pending[b.index] = b
id, token := randomID(), s.newSecretLocked(StorageToken)
s.uploads[id] = &uploadURL{token: token, user: u, linkID: l.id, rev: rev, block: b}
bareURL := s.URL() + "/storage/upload/" + id
resp.UploadLinks = append(resp.UploadLinks, blockURLJSON{BareURL: bareURL, Token: token, URL: bareURL, Index: b.index})
}
writeJSON(w, http.StatusOK, resp)
}
// pendingBlockOf returns the block in an upload request, checked.
func pendingBlockOf(in blockUploadInput) (*pendingBlock, error) {
b := &pendingBlock{index: in.Index}
if in.Index < 1 {
return nil, errors.New("field Index must be at least 1")
}
if in.Verifier == nil {
return nil, errors.New("field Verifier is required")
}
token, err := base64.StdEncoding.DecodeString(in.Verifier.Token)
if err != nil || len(token) != verificationCodeSize {
return nil, fmt.Errorf("Verifier.Token is not the base64 of %d bytes", verificationCodeSize)
}
b.token = token
if in.EncSignature == nil || *in.EncSignature == "" {
return nil, errors.New("EncSignature is required")
}
b.encSignature = *in.EncSignature
if in.Hash != nil {
if b.hash, err = base64.StdEncoding.DecodeString(*in.Hash); err != nil || len(b.hash) != sha256.Size {
return nil, errors.New("field Hash is not the base64 of a SHA-256")
}
}
return b, nil
}
// handleStorageUpload stores the block of a multipart upload to a URL
// issued by handleRequestUpload, which carries its pm-storage-token and
// none of the session's credentials.
func (s *Server) handleStorageUpload(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "" || r.Header.Get("x-pm-uid") != "" {
writeError(w, http.StatusBadRequest, api.CodeInvalidValue, "protontest: session credentials must not be sent to the storage host")
return
}
s.mu.Lock()
up := s.uploads[r.PathValue("token")]
usable := up != nil && !up.expired
s.mu.Unlock()
switch {
case !usable:
writeError(w, http.StatusNotFound, api.CodeNotExists, "protontest: no such upload URL, or it expired")
return
case r.Header.Get("pm-storage-token") != up.token:
writeError(w, http.StatusForbidden, api.CodeNotEnoughPermissions, "protontest: missing or wrong pm-storage-token")
return
}
data, err := readBlockPart(r)
if err != nil {
writeError(w, http.StatusBadRequest, api.CodeInvalidValue, "protontest: bad block upload: "+err.Error())
return
}
s.mu.Lock()
defer s.mu.Unlock()
if up.rev.state != revisionStateDraft || up.rev.pending[up.block.index] != up.block {
writeError(w, http.StatusNotFound, api.CodeNotExists, "protontest: the block is no longer awaited")
return
}
up.block.data = data
writeJSON(w, http.StatusOK, &codeJSON{Code: api.CodeOK})
}
// readBlockPart returns the content of the one part, named Block, of
// the multipart body of r.
func readBlockPart(r *http.Request) ([]byte, error) {
mr, err := r.MultipartReader()
if err != nil {
return nil, err
}
var data []byte
for {
part, err := mr.NextPart()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
if part.FormName() != "Block" || data != nil {
return nil, fmt.Errorf("unexpected part %q", part.FormName())
}
if data, err = io.ReadAll(io.LimitReader(part, maxUploadBlockSize+1)); err != nil {
return nil, err
}
if len(data) > maxUploadBlockSize {
return nil, fmt.Errorf("block larger than %d bytes", maxUploadBlockSize)
}
}
if len(data) == 0 {
return nil, errors.New("no Block part, or it is empty")
}
return data, nil
}
// handleCommit makes a draft revision with every requested block
// uploaded the file's active revision, after checking each block's
// verifier token and format, the manifest signature over the uploaded
// blocks and the extended attributes' format.
func (s *Server) handleCommit(w http.ResponseWriter, r *http.Request, u *User, d *drive) {
var req commitRevisionRequest
if !decodeRequest(w, r, &req) {
return
}
l := d.links[r.PathValue("linkID")]
rev := draftRevision(l, r.PathValue("revisionID"))
if rev == nil {
writeError(w, http.StatusUnprocessableEntity, api.CodeNotExists, "Revision not found")
return
}
xattr := ""
if req.XAttr != nil {
xattr = *req.XAttr
}
err := errors.Join(
checkSigner(u, "SignatureAddress", req.SignatureAddress),
checkMessages(map[string]string{"XAttr": xattr}),
)
if err == nil && req.ManifestSignature == "" {
err = errors.New("ManifestSignature is required")
}
if err != nil {
writeInvalid(w, err)
return
}
blocks, err := uploadedBlocks(rev, *req.SignatureAddress)
if err != nil {
writeError(w, http.StatusUnprocessableEntity, api.CodeInvalidRequirements, "protontest: "+err.Error())
return
}
var manifest []byte
var formatErrs []error
version := 1
if contentKeyVersion(l.contentKeyPacket) == 6 {
version = 2
}
for _, b := range blocks {
manifest = append(manifest, b.hash...)
formatErrs = append(formatErrs, annotateField(fmt.Sprintf("block %d", b.index), checkBlockFormat(b.data, version)))
}
addr := u.addressByEmailLocked(*req.SignatureAddress)
keys := make([]*crypto.Key, 0, len(addr.keys))
for _, k := range addr.keys {
keys = append(keys, k.key)
}
sigErr := verifyDetached(manifest, []byte(req.ManifestSignature), crypto.Armor, keyRing(keys...), s.nowLocked())
if err := errors.Join(append(formatErrs, annotateField("manifest", sigErr))...); err != nil {
writeInvalid(w, err)
return
}
before := d.linkDetails(l)
event := EventUpdate
if l.state == linkStateDraft {
event = EventCreate
}
for _, other := range l.revisions {
if other.state == revisionStateActive {
other.state = revisionStateObsolete
}
}
now := time.Now().Unix()
rev.state, rev.createTime = revisionStateActive, now
rev.manifestSignature, rev.xattr, rev.signatureEmail = req.ManifestSignature, xattr, *req.SignatureAddress
rev.blocks, rev.pending, rev.verificationCode = blocks, nil, nil
l.state, l.modifyTime = linkStateActive, now
d.logLink(event, l)
s.dropUploadsLocked(rev)
s.lagLocked(l.id, &before)
writeJSON(w, http.StatusOK, &codeJSON{Code: api.CodeOK})
}
// uploadedBlocks returns the blocks of draft rev, signed by email, after
// checking that blocks 1 to n were requested and each uploaded, with the
// hash it was declared with, if any, and its verifier token.
func uploadedBlocks(rev *revision, email string) ([]*block, error) {
indexes := make([]int, 0, len(rev.pending))
for i := range rev.pending {
indexes = append(indexes, i)
}
sort.Ints(indexes)
blocks := make([]*block, 0, len(indexes))
for i, index := range indexes {
p := rev.pending[index]
switch {
case index != i+1:
return nil, fmt.Errorf("block %d was not requested", i+1)
case p.data == nil:
return nil, fmt.Errorf("block %d was not uploaded", index)
}
sum := sha256.Sum256(p.data)
if p.hash != nil && !bytes.Equal(p.hash, sum[:]) {
return nil, fmt.Errorf("block %d: declared hash doesn't match the uploaded block", index)
}
if want := xorPrefix(rev.verificationCode, p.data); !bytes.Equal(p.token, want) {
return nil, fmt.Errorf("block %d: verifier token doesn't match the uploaded block", index)
}
blocks = append(blocks, &block{index: index, hash: sum[:], data: p.data, encSignature: p.encSignature, signatureEmail: email})
}
return blocks, nil
}
// xorPrefix returns code XOR the start of data, padded with zeros: the
// verifier token of a block.
func xorPrefix(code, data []byte) []byte {
out := make([]byte, len(code))
for i := range code {
out[i] = code[i]
if i < len(data) {
out[i] ^= data[i]
}
}
return out
}
// dropUploadsLocked forgets the upload URLs of rev. srv.mu must be
// held.
func (s *Server) dropUploadsLocked(rev *revision) {
for id, up := range s.uploads {
if up.rev == rev {
delete(s.uploads, id)
}
}
}
// handleDeleteRevision deletes a draft revision.
func (s *Server) handleDeleteRevision(w http.ResponseWriter, r *http.Request, u *User, d *drive) {
l := d.links[r.PathValue("linkID")]
rev := draftRevision(l, r.PathValue("revisionID"))
if rev == nil {
writeError(w, http.StatusUnprocessableEntity, api.CodeNotExists, "Draft revision not found")
return
}
l.revisions = slices.DeleteFunc(l.revisions, func(other *revision) bool { return other == rev })
s.dropUploadsLocked(rev)
writeJSON(w, http.StatusOK, &codeJSON{Code: api.CodeOK})
}
// handleDeleteMultiple permanently deletes up to 100 draft links,
// answering for each.
func (s *Server) handleDeleteMultiple(w http.ResponseWriter, r *http.Request, u *User, d *drive) {
var req linksRequest
if !decodeRequest(w, r, &req) {
return
}
if len(req.LinkIDs) > maxLinksPerRequest {
writeError(w, http.StatusUnprocessableEntity, api.CodeInvalidValue, fmt.Sprintf("protontest: %d link IDs, at most %d allowed", len(req.LinkIDs), maxLinksPerRequest))
return
}
resp := &multiResponse{Code: api.CodeOKMany, Responses: []linkResultJSON{}}
for _, id := range req.LinkIDs {
result := linkResultJSON{LinkID: id, Response: codeJSON{Code: api.CodeOK}}
l := d.links[id]
switch {
case l == nil:
result.Response = codeJSON{Code: api.CodeNotExists, Error: "File or folder not found"}
case l.state != linkStateDraft:
result.Response = codeJSON{Code: api.CodeInvalidRequirements, Error: "protontest: only drafts can be deleted"}
default:
for _, rev := range l.revisions {
s.dropUploadsLocked(rev)
}
delete(d.links, id)
}
resp.Responses = append(resp.Responses, result)
}
writeJSON(w, http.StatusOK, resp)
}
@@ -0,0 +1,502 @@
package protontest
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"io"
"mime/multipart"
"net/http"
"strconv"
"testing"
"time"
"github.com/ProtonMail/go-crypto/openpgp/packet"
"github.com/ProtonMail/go-crypto/openpgp/s2k"
"github.com/ProtonMail/gopenpgp/v3/crypto"
"github.com/ProtonMail/gopenpgp/v3/profile"
"github.com/rclone/rclone/backend/protondrive/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const testClientUID = "fake-test-client"
// uploader uploads files to the root of a user's drive as the SDK
// does, step by step, for tests to spoil one step.
type uploader struct {
t *testing.T
s *Server
u *User
sess Session
volume string
}
func newUploader(t *testing.T) *uploader {
s := New(t)
u := s.AddUser("user", "pass")
return &uploader{t: t, s: s, u: u, sess: s.NewSession(u), volume: u.VolumeID()}
}
// fileDraft is a file being uploaded, with its keys.
type fileDraft struct {
req *createFileRequest
node lockedKey
ck contentKey
signer *crypto.Key
linkID string
revID string
code []byte
content []byte
}
// newFileRequest returns a create file request for name in the root
// made as the SDK's createDraft does.
func (x *uploader) newFileRequest(name string) *fileDraft {
v := x.u.view()
parentKey, parentHashKey := v.folderKeys(v.d.rootID)
node := newDriveKey(true)
x.s.rememberKey(node)
pass := sealPassphrase(node.passphrase, parentKey, v.addrKey)
ck := newContentKey(node.key, true)
uid := testClientUID
return &fileDraft{
req: &createFileRequest{
ParentLinkID: v.d.rootID,
Name: encryptArmored([]byte(name), parentKey, v.addrKey, false),
Hash: lookupHash(parentHashKey, name),
MIMEType: "text/plain",
ClientUID: &uid,
NodeKey: node.armored,
NodePassphrase: pass.armored,
NodePassphraseSignature: pass.signature,
ContentKeyPacket: ck.packet,
ContentKeyPacketSignature: &ck.signature,
SignatureAddress: &v.email,
},
node: node,
ck: ck,
signer: v.addrKey,
}
}
// send sends in with method to path as the user.
func (x *uploader) send(method, path string, in any) (int, []byte) {
return sendJSON(x.t, x.s, x.sess, method, path, in)
}
// create creates the draft of f, failing the test if it can't.
func (x *uploader) create(f *fileDraft) {
t := x.t
status, body := x.send("POST", "/drive/v2/volumes/"+x.volume+"/files", f.req)
require.Equal(t, http.StatusOK, status, "body: %s", body)
var resp createFileResponse
require.NoError(t, json.Unmarshal(body, &resp))
f.linkID, f.revID = resp.File.ID, resp.File.RevisionID
x.loadCode(f)
}
// loadCode fetches the verification code of f's draft revision.
func (x *uploader) loadCode(f *fileDraft) {
var ver verificationResponse
status, body := getJSON(x.t, x.s, x.sess, "/drive/v2/volumes/"+x.volume+"/links/"+f.linkID+"/revisions/"+f.revID+"/verification", &ver)
require.Equal(x.t, http.StatusOK, status, "body: %s", body)
code, err := base64.StdEncoding.DecodeString(ver.VerificationCode)
require.NoError(x.t, err)
require.Len(x.t, code, verificationCodeSize)
assert.Equal(x.t, f.ck.packet, ver.ContentKeyPacket)
f.code = code
}
// testBlock is an encrypted block with its upload request entry.
type testBlock struct {
data []byte
in blockUploadInput
}
// encrypt encrypts content as f's blocks of blockSize.
func (f *fileDraft) encrypt(content []byte) []testBlock {
f.content = content
var blocks []testBlock
for i := 0; i < len(content); i += blockSize {
data, encSig := f.ck.encryptBlock(content[i:min(i+blockSize, len(content))], f.node.key, f.signer)
blocks = append(blocks, testBlock{data: data, in: blockUploadInput{
Index: len(blocks) + 1,
Verifier: &verifierJSON{Token: base64.StdEncoding.EncodeToString(xorPrefix(f.code, data))},
EncSignature: &encSig,
}})
}
return blocks
}
// requestURLs requests upload URLs for blocks of f.
func (x *uploader) requestURLs(f *fileDraft, blocks []testBlock) (int, []byte, []blockURLJSON) {
addressID := x.u.addresses[0].id
req := &blockUploadRequest{LinkID: f.linkID, RevisionID: f.revID, AddressID: &addressID, VolumeID: &x.volume}
for _, b := range blocks {
req.BlockList = append(req.BlockList, b.in)
}
status, body := x.send("POST", "/drive/blocks", req)
var resp blockUploadResponse
if status == http.StatusOK {
require.NoError(x.t, json.Unmarshal(body, &resp))
}
return status, body, resp.UploadLinks
}
// putBlock uploads data to link as the SDK does, with extra headers,
// and returns the HTTP status.
func (x *uploader) putBlock(link blockURLJSON, data []byte, header map[string]string) int {
t := x.t
var body bytes.Buffer
mw := multipart.NewWriter(&body)
w, err := mw.CreateFormFile("Block", "blob")
require.NoError(t, err)
_, err = w.Write(data)
require.NoError(t, err)
require.NoError(t, mw.Close())
req, err := http.NewRequest("POST", link.BareURL, &body)
require.NoError(t, err)
req.Header.Set("Content-Type", mw.FormDataContentType())
req.Header.Set("x-pm-appversion", testAppVersion)
req.Header.Set("pm-storage-token", link.Token)
for k, v := range header {
req.Header.Set(k, v)
}
resp, err := x.s.Client().Do(req)
require.NoError(t, err)
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
return resp.StatusCode
}
// uploadAll requests URLs for blocks and uploads each.
func (x *uploader) uploadAll(f *fileDraft, blocks []testBlock) {
status, body, links := x.requestURLs(f, blocks)
require.Equal(x.t, http.StatusOK, status, "body: %s", body)
require.Len(x.t, links, len(blocks))
for i, link := range links {
require.Equal(x.t, blocks[i].in.Index, link.Index)
require.Equal(x.t, http.StatusOK, x.putBlock(link, blocks[i].data, nil))
}
}
// commitRequest returns the commit request of f with blocks, as the SDK
// makes it.
func (f *fileDraft) commitRequest(blocks []testBlock) *commitRevisionRequest {
var manifest []byte
var sizes []int64
for i, b := range blocks {
sum := sha256.Sum256(b.data)
manifest = append(manifest, sum[:]...)
sizes = append(sizes, int64(min(blockSize, len(f.content)-i*blockSize)))
}
xattr := encryptArmored(newFileXAttr(f.content, sizes, "2024-01-02T03:04:05.000Z"), f.node.key, f.signer, true)
return &commitRevisionRequest{
ManifestSignature: signArmored(manifest, f.signer),
SignatureAddress: f.req.SignatureAddress,
XAttr: &xattr,
}
}
// commit commits f's draft revision with req.
func (x *uploader) commit(f *fileDraft, req *commitRevisionRequest) (int, []byte) {
return x.send("PUT", "/drive/v2/volumes/"+x.volume+"/files/"+f.linkID+"/revisions/"+f.revID, req)
}
func TestCreateFileFormats(t *testing.T) {
x := newUploader(t)
post := func(f *fileDraft) (int, []byte) {
return x.send("POST", "/drive/v2/volumes/"+x.volume+"/files", f.req)
}
for variant, bad := range aeadMessages(t) {
for field, spoil := range map[string]func(req *createFileRequest){
"Name": func(req *createFileRequest) { req.Name = bad },
"NodePassphrase": func(req *createFileRequest) { req.NodePassphrase = bad },
} {
t.Run(field+" "+variant, func(t *testing.T) {
f := x.newFileRequest("f")
spoil(f.req)
status, body := post(f)
requireRejected(t, api.CodeInvalidValue, status, body, field)
})
}
}
aeadLocked := profile.Default()
aeadLocked.AeadKeyEncryption = &packet.AEADConfig{}
salted := profile.Default()
salted.S2kKeyEncryption = &s2k.Config{S2KMode: s2k.SaltedS2K, PassphraseIsHighEntropy: true}
v6Key, err := crypto.PGPWithProfile(profile.RFC9580()).KeyGeneration().AddUserId(driveKeyName, "").New().GenerateKey()
require.NoError(t, err)
v6Locked, err := crypto.PGPWithProfile(profile.RFC9580()).LockKey(v6Key, newPassphrase())
require.NoError(t, err)
v6Armored, err := v6Locked.Armor()
require.NoError(t, err)
for name, tc := range map[string]struct {
key string
want string
}{
"v6": {v6Armored, "not a v4 key"},
"AEAD locked": {lockedWith(t, aeadLocked), "locked with AEAD"},
"salted S2K": {lockedWith(t, salted), "S2K mode 1"},
} {
t.Run("NodeKey "+name, func(t *testing.T) {
f := x.newFileRequest("f")
f.req.NodeKey = tc.key
status, body := post(f)
requireRejected(t, api.CodeInvalidValue, status, body, "NodeKey", tc.want)
})
}
legacy := newContentKey(newDriveKey(false).key, false)
unrounded := int64(123_456)
for name, tc := range map[string]struct {
spoil func(req *createFileRequest)
want string
}{
"v3 content key packet": {func(req *createFileRequest) { req.ContentKeyPacket = legacy.packet }, "v6 PKESK"},
"no content key sig": {func(req *createFileRequest) { req.ContentKeyPacketSignature = nil }, "ContentKeyPacketSignature"},
"no passphrase sig": {func(req *createFileRequest) { req.NodePassphraseSignature = "" }, "NodePassphraseSignature"},
"unrounded size": {func(req *createFileRequest) { req.IntendedUploadSize = &unrounded }, "IntendedUploadSize"},
"no signer": {func(req *createFileRequest) { req.SignatureAddress = nil }, "SignatureAddress"},
"no media type": {func(req *createFileRequest) { req.MIMEType = "" }, "MIMEType"},
} {
t.Run(name, func(t *testing.T) {
f := x.newFileRequest("f")
tc.spoil(f.req)
status, body := post(f)
requireRejected(t, api.CodeInvalidValue, status, body, tc.want)
})
}
assert.Empty(t, x.u.Drafts())
// The name hash must be unique, and a conflict names the draft and
// its creator.
f := x.newFileRequest("f")
rounded := int64(200_000)
f.req.IntendedUploadSize = &rounded
x.create(f)
assert.Equal(t, []string{f.linkID}, x.u.Drafts())
status, body := post(x.newFileRequest("f"))
requireRejected(t, api.CodeAlreadyExists, status, body)
var conflict conflictResponse
require.NoError(t, json.Unmarshal(body, &conflict))
assert.Equal(t, f.linkID, conflict.Details.ConflictLinkID)
assert.Nil(t, conflict.Details.ConflictRevisionID)
require.NotNil(t, conflict.Details.ConflictDraftRevisionID)
assert.Equal(t, f.revID, *conflict.Details.ConflictDraftRevisionID)
require.NotNil(t, conflict.Details.ConflictDraftClientUID)
assert.Equal(t, testClientUID, *conflict.Details.ConflictDraftClientUID)
assert.Empty(t, x.u.Dump(), "a draft is dumped")
}
func TestCommitChecks(t *testing.T) {
x := newUploader(t)
content := patterned(blockSize + 1000)
n := 0
// prepare returns a new draft with content encrypted, its blocks
// and their commit request.
prepare := func() (*fileDraft, []testBlock, *commitRevisionRequest) {
n++
f := x.newFileRequest("f" + strconv.Itoa(n))
x.create(f)
blocks := f.encrypt(content)
return f, blocks, f.commitRequest(blocks)
}
impostor := generateKey("impostor", "impostor@proton.test")
empty := ""
aead := aeadMessages(t)["v6 PKESK and SEIPDv2"]
legacy := newContentKey(newDriveKey(false).key, false)
otherHash := base64.StdEncoding.EncodeToString(make([]byte, sha256.Size))
for name, tc := range map[string]struct {
spoil func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock
code int
want string
}{
"AEAD XAttr": {func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock {
req.XAttr = &aead
return blocks
}, api.CodeInvalidValue, "XAttr"},
"empty XAttr": {func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock {
req.XAttr = &empty
return blocks
}, api.CodeInvalidValue, "XAttr"},
"no XAttr": {func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock {
req.XAttr = nil
return blocks
}, api.CodeInvalidValue, "XAttr"},
"verifier": {func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock {
token := xorPrefix(f.code, blocks[1].data)
token[5] ^= 1
blocks[1].in.Verifier.Token = base64.StdEncoding.EncodeToString(token)
return blocks
}, api.CodeInvalidRequirements, "block 2: verifier token"},
"block not uploaded": {func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock {
_, _, links := x.requestURLs(f, blocks[1:])
require.Len(t, links, 1)
return blocks[:1]
}, api.CodeInvalidRequirements, "block 2 was not uploaded"},
"index gap": {func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock {
blocks[1].in.Index = 3
return blocks
}, api.CodeInvalidRequirements, "block 2 was not requested"},
"declared hash": {func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock {
blocks[0].in.Hash = &otherHash
return blocks
}, api.CodeInvalidRequirements, "declared hash"},
"manifest": {func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock {
req.ManifestSignature = signArmored([]byte("not the manifest"), f.signer)
return blocks
}, api.CodeInvalidValue, "manifest"},
"manifest signer": {func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock {
var manifest []byte
for _, b := range blocks {
sum := sha256.Sum256(b.data)
manifest = append(manifest, sum[:]...)
}
req.ManifestSignature = signArmored(manifest, impostor)
return blocks
}, api.CodeInvalidValue, "manifest"},
"SEIPDv1 block": {func(f *fileDraft, blocks []testBlock, req *commitRevisionRequest) []testBlock {
data, _ := legacy.encryptBlock(content[blockSize:], f.node.key, f.signer)
blocks[1].data = data
blocks[1].in.Verifier.Token = base64.StdEncoding.EncodeToString(xorPrefix(f.code, data))
*req = *f.commitRequest(blocks)
return blocks
}, api.CodeInvalidValue, "block 2: packet with tag 18 version 1, want SEIPDv2"},
} {
t.Run(name, func(t *testing.T) {
f, blocks, req := prepare()
blocks = tc.spoil(f, blocks, req)
x.uploadAll(f, blocks)
status, body := x.commit(f, req)
requireRejected(t, tc.code, status, body, tc.want)
assert.Contains(t, x.u.Drafts(), f.linkID)
})
}
assert.Empty(t, x.u.Dump(), "a draft was committed")
f, blocks, req := prepare()
x.uploadAll(f, blocks)
status, body := x.commit(f, req)
require.Equal(t, http.StatusOK, status, "body: %s", body)
dump := x.u.Dump()
require.Len(t, dump, 1)
assert.NoError(t, dump[0].Err)
assert.Equal(t, f.linkID, dump[0].ID)
assert.True(t, bytes.Equal(content, dump[0].Content))
assert.NotContains(t, x.u.Drafts(), f.linkID)
// A committed revision can't be committed again or deleted as a
// draft.
status, body = x.commit(f, req)
requireRejected(t, api.CodeNotExists, status, body)
status, body = x.send("POST", "/drive/v2/volumes/"+x.volume+"/delete_multiple", &linksRequest{LinkIDs: []string{f.linkID}})
require.Equal(t, http.StatusOK, status, "body: %s", body)
var multi multiResponse
require.NoError(t, json.Unmarshal(body, &multi))
require.Len(t, multi.Responses, 1)
assert.Equal(t, api.CodeInvalidRequirements, multi.Responses[0].Response.Code)
status, body = x.send("DELETE", "/drive/v2/volumes/"+x.volume+"/files/"+f.linkID+"/revisions/"+f.revID, nil)
requireRejected(t, api.CodeNotExists, status, body)
require.Len(t, x.u.Dump(), 1)
}
func TestStorageUploadRules(t *testing.T) {
x := newUploader(t)
f := x.newFileRequest("f")
x.create(f)
blocks := f.encrypt([]byte("content"))
status, body, links := x.requestURLs(f, blocks)
require.Equal(t, http.StatusOK, status, "body: %s", body)
require.Len(t, links, 1)
link := links[0]
assert.Equal(t, http.StatusBadRequest, x.putBlock(link, blocks[0].data, sessionHeader(x.sess)), "session credentials accepted")
wrong := link
wrong.Token = "wrong"
assert.Equal(t, http.StatusForbidden, x.putBlock(wrong, blocks[0].data, nil))
x.s.ExpireUploadTokens()
assert.Equal(t, http.StatusNotFound, x.putBlock(link, blocks[0].data, nil))
// A block requested again gets a URL which works.
_, _, links = x.requestURLs(f, blocks)
require.Len(t, links, 1)
require.Equal(t, http.StatusOK, x.putBlock(links[0], blocks[0].data, nil))
status, body = x.commit(f, f.commitRequest(blocks))
require.Equal(t, http.StatusOK, status, "body: %s", body)
dump := x.u.Dump()
require.Len(t, dump, 1)
require.NoError(t, dump[0].Err)
// Uploads to a committed revision are refused.
assert.Equal(t, http.StatusNotFound, x.putBlock(links[0], blocks[0].data, nil))
// Another user's AddressID is refused.
other := x.s.AddUser("other", "pass")
g := x.newFileRequest("g")
x.create(g)
addressID := other.addresses[0].id
status, body = x.send("POST", "/drive/blocks", &blockUploadRequest{LinkID: g.linkID, RevisionID: g.revID, AddressID: &addressID, VolumeID: &x.volume, BlockList: []blockUploadInput{g.encrypt([]byte("x"))[0].in}})
requireRejected(t, api.CodeInvalidValue, status, body, "AddressID")
}
func TestRevisionRules(t *testing.T) {
x := newUploader(t)
id := x.u.SeedFile(x.u.RootID(), "f", []byte("old"), time.Now())
current := x.u.view().d.links[id].activeRevision().id
path := "/drive/v2/volumes/" + x.volume + "/files/" + id + "/revisions"
stale := "stale"
status, body := x.send("POST", path, &createRevisionRequest{CurrentRevisionID: &stale})
requireRejected(t, api.CodeInvalidRequirements, status, body, "CurrentRevisionID")
status, body = x.send("POST", path, &createRevisionRequest{})
requireRejected(t, api.CodeInvalidRequirements, status, body, "CurrentRevisionID")
uid := testClientUID
status, body = x.send("POST", path, &createRevisionRequest{CurrentRevisionID: &current, ClientUID: &uid})
require.Equal(t, http.StatusOK, status, "body: %s", body)
var resp createRevisionResponse
require.NoError(t, json.Unmarshal(body, &resp))
assert.Equal(t, []string{resp.Revision.ID}, x.u.Drafts())
// The name conflict of a file with a draft revision names both.
status, body = x.send("POST", "/drive/v2/volumes/"+x.volume+"/files", x.newFileRequest("f").req)
requireRejected(t, api.CodeAlreadyExists, status, body)
var conflict conflictResponse
require.NoError(t, json.Unmarshal(body, &conflict))
require.NotNil(t, conflict.Details.ConflictRevisionID)
assert.Equal(t, current, *conflict.Details.ConflictRevisionID)
require.NotNil(t, conflict.Details.ConflictDraftRevisionID)
assert.Equal(t, resp.Revision.ID, *conflict.Details.ConflictDraftRevisionID)
status, body = x.send("DELETE", path+"/"+resp.Revision.ID, nil)
require.Equal(t, http.StatusOK, status, "body: %s", body)
assert.Empty(t, x.u.Drafts())
status, body = x.send("DELETE", path+"/"+current, nil)
requireRejected(t, api.CodeNotExists, status, body)
dump := x.u.Dump()
require.Len(t, dump, 1)
assert.Equal(t, []byte("old"), dump[0].Content)
}
// TestUploadWireNames checks the backend's upload request types decode
// into the fake's without unknown fields.
func TestUploadWireNames(t *testing.T) {
for _, tc := range []struct {
client any
fake any
}{
{&api.CreateFileRequest{}, &createFileRequest{}},
{&api.CreateRevisionRequest{}, &createRevisionRequest{}},
{&api.BlockUploadRequest{BlockList: []api.BlockUploadInfo{{}}, ThumbnailList: []struct{}{}}, &blockUploadRequest{}},
{&api.CommitRevisionRequest{}, &commitRevisionRequest{}},
} {
data, err := json.Marshal(tc.client)
require.NoError(t, err)
dec := json.NewDecoder(bytes.NewReader(data))
dec.DisallowUnknownFields()
assert.NoError(t, dec.Decode(tc.fake), "%T", tc.client)
}
}
+631
View File
@@ -0,0 +1,631 @@
package protondrive_test
import (
"bytes"
"context"
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"errors"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
_ "github.com/rclone/rclone/backend/local"
"github.com/rclone/rclone/backend/protondrive"
"github.com/rclone/rclone/backend/protondrive/protontest"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/accounting"
"github.com/rclone/rclone/fs/config/configmap"
"github.com/rclone/rclone/fs/config/obscure"
"github.com/rclone/rclone/fs/fserrors"
"github.com/rclone/rclone/fs/object"
"github.com/rclone/rclone/fs/operations"
fssync "github.com/rclone/rclone/fs/sync"
"github.com/rclone/rclone/lib/random"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
childrenRoute = "GET /drive/v2/volumes/{volumeID}/folders/{linkID}/children"
refreshRoute = "POST /auth/v4/refresh"
draftRoute = "POST /drive/v2/volumes/{volumeID}/files"
)
// credentialKeys are the config keys holding a saved session.
var credentialKeys = []string{"client_uid", "client_access_token", "client_refresh_token", "client_salted_key_pass"}
// newFakeFs returns an Fs for remote at root, shut down, if it can
// be, when the test ends.
func newFakeFs(t *testing.T, remote, root string) fs.Fs {
t.Helper()
f, err := fs.NewFs(context.Background(), remote+root)
require.NoError(t, err)
if s, ok := f.(fs.Shutdowner); ok {
t.Cleanup(func() { _ = s.Shutdown(context.Background()) })
}
return f
}
// sessionMap returns the config of a remote on srv with a new saved
// session of u.
func sessionMap(srv *protontest.Server, u *protontest.User) configmap.Simple {
sess := srv.NewSession(u)
return configmap.Simple{
"endpoint": srv.URL(),
"client_uid": sess.UID,
"client_access_token": sess.AccessToken,
"client_refresh_token": sess.RefreshToken,
"client_salted_key_pass": base64.StdEncoding.EncodeToString([]byte(u.KeyPassword())),
"draft_client_uid": random.String(32),
}
}
// lockedMap is a configmap.Mapper safe for concurrent use, as rclone's
// config file is. It records every value set.
type lockedMap struct {
mu sync.Mutex
m configmap.Simple
set map[string][]string // every value set per key, in order
}
// newLockedMap returns a lockedMap holding a copy of m.
func newLockedMap(m configmap.Simple) *lockedMap {
l := &lockedMap{m: configmap.Simple{}, set: map[string][]string{}}
for k, v := range m {
l.m[k] = v
}
return l
}
// Get returns the value of key.
func (l *lockedMap) Get(key string) (string, bool) {
l.mu.Lock()
defer l.mu.Unlock()
v, ok := l.m[key]
return v, ok
}
// Set sets key to value.
func (l *lockedMap) Set(key, value string) {
l.mu.Lock()
defer l.mu.Unlock()
l.m[key] = value
l.set[key] = append(l.set[key], value)
}
// values returns every value set for key.
func (l *lockedMap) values(key string) []string {
l.mu.Lock()
defer l.mu.Unlock()
return append([]string(nil), l.set[key]...)
}
// processMap is one process's view of a config file shared through
// backing: it reads its own writes at once, but they reach backing,
// and so other processes, only after delay.
type processMap struct {
backing *lockedMap
delay time.Duration
flushes *sync.WaitGroup
mu sync.Mutex
pending map[string]string // values set and not yet in backing
}
// Get returns the value of key.
func (p *processMap) Get(key string) (string, bool) {
p.mu.Lock()
v, ok := p.pending[key]
p.mu.Unlock()
if ok {
return v, true
}
return p.backing.Get(key)
}
// Set sets key to value, writing it to backing after p.delay.
func (p *processMap) Set(key, value string) {
p.mu.Lock()
p.pending[key] = value
p.mu.Unlock()
p.flushes.Add(1)
time.AfterFunc(p.delay, func() {
defer p.flushes.Done()
p.mu.Lock()
defer p.mu.Unlock()
p.backing.Set(key, value)
if p.pending[key] == value {
delete(p.pending, key)
}
})
}
// requireValidSession checks the session saved in m is accepted by srv.
func requireValidSession(t *testing.T, srv *protontest.Server, m configmap.Getter) {
t.Helper()
uid, _ := m.Get("client_uid")
token, _ := m.Get("client_access_token")
req, err := http.NewRequest("GET", srv.URL()+"/core/v4/users", nil)
require.NoError(t, err)
req.Header.Set("x-pm-appversion", "external-drive-rclone@1.0.0-stable")
req.Header.Set("x-pm-uid", uid)
req.Header.Set("Authorization", "Bearer "+token)
resp, err := srv.Client().Do(req)
require.NoError(t, err)
_ = resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode, "the saved session is rejected")
}
// listConcurrently lists the root of every Fs at once and checks each
// listing succeeds.
func listConcurrently(ctx context.Context, t *testing.T, fsys ...fs.Fs) {
t.Helper()
var wg sync.WaitGroup
for _, f := range fsys {
wg.Add(1)
go func() {
defer wg.Done()
_, err := f.List(ctx, "")
assert.NoError(t, err)
}()
}
wg.Wait()
}
// TestRegressionTwoFsRefreshRace is a regression test for rclone issues
// #9880 and #8135: two remotes on one config refreshing at once lost
// the session.
func TestRegressionTwoFsRefreshRace(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
m := newLockedMap(sessionMap(srv, u))
uid, _ := m.Get("client_uid")
f1, err := protondrive.NewFs(ctx, "remote", "", m)
require.NoError(t, err)
f2, err := protondrive.NewFs(ctx, "remote", "", m)
require.NoError(t, err)
// Both refreshes reach the server with the same refresh token.
srv.Inject(protontest.Fault{Method: "POST", Pattern: "/auth/v4/refresh", Times: 2, Gather: true})
srv.ExpireAccessToken(uid)
srv.ResetRequests()
listConcurrently(ctx, t, f1, f2)
assert.Equal(t, 2, srv.Requests()[refreshRoute])
requireValidSession(t, srv, m)
for _, k := range credentialKeys {
for _, v := range m.values(k) {
assert.NotEmpty(t, v, "%s was blanked", k)
}
}
// Both use the surviving session without refreshing again.
f1.(fs.DirCacheFlusher).DirCacheFlush()
f2.(fs.DirCacheFlusher).DirCacheFlush()
listConcurrently(ctx, t, f1, f2)
assert.Equal(t, 2, srv.Requests()[refreshRoute])
}
// TestRegressionTwoProcessRefreshRace checks two processes sharing a
// config file, which sees each one's saves only after a delay, both
// keep working when they refresh at once.
func TestRegressionTwoProcessRefreshRace(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
backing := newLockedMap(sessionMap(srv, u))
uid, _ := backing.Get("client_uid")
var flushes sync.WaitGroup
newProcess := func() fs.Fs {
p := &processMap{backing: backing, delay: 500 * time.Millisecond, flushes: &flushes, pending: map[string]string{}}
f, err := protondrive.NewFs(ctx, "remote", "", p)
require.NoError(t, err)
return f
}
f1, f2 := newProcess(), newProcess()
srv.Inject(protontest.Fault{Method: "POST", Pattern: "/auth/v4/refresh", Times: 2, Gather: true})
srv.ExpireAccessToken(uid)
srv.ResetRequests()
listConcurrently(ctx, t, f1, f2)
flushes.Wait()
assert.Equal(t, 2, srv.Requests()[refreshRoute], "one refresh wins, the other is rejected")
requireValidSession(t, srv, backing)
for _, k := range credentialKeys {
for _, v := range backing.values(k) {
assert.NotEmpty(t, v, "%s was blanked", k)
}
}
// The loser adopted the winner's tokens.
f1.(fs.DirCacheFlusher).DirCacheFlush()
f2.(fs.DirCacheFlusher).DirCacheFlush()
listConcurrently(ctx, t, f1, f2)
assert.Equal(t, 2, srv.Requests()[refreshRoute])
}
// TestRegressionManyNewFs is a regression test for rclone issue #9816:
// many remotes created at once were OOM-killed unlocking keys.
//
// All the NewFs calls are held until each has fetched its keys, so
// their unlocks contend. That unlocks then run one at a time can't be
// seen from the server; TestLoadAccountSerialised checks it.
func TestRegressionManyNewFs(t *testing.T) {
const n = 16
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
maps := make([]configmap.Simple, n)
for i := range maps {
maps[i] = sessionMap(srv, u)
}
srv.Inject(protontest.Fault{Method: "GET", Pattern: "/core/v4/addresses", Times: n, Gather: true})
fsys := make([]fs.Fs, n)
var wg sync.WaitGroup
for i := range fsys {
wg.Add(1)
go func() {
defer wg.Done()
f, err := protondrive.NewFs(ctx, "remote", "", maps[i])
if assert.NoError(t, err) {
fsys[i] = f
}
}()
}
wg.Wait()
if t.Failed() {
return
}
listConcurrently(ctx, t, fsys...)
// Keys are fetched, and so unlocked, once per Fs.
assert.Equal(t, n, srv.Requests()["GET /core/v4/users"])
assert.Equal(t, n, srv.Requests()["GET /core/v4/addresses"])
}
// TestRegressionFileOverDirectory is a regression test for rclone issue
// #9438: copying a file over a directory destroyed data.
func TestRegressionFileOverDirectory(t *testing.T) {
ctx := context.Background()
remote, _, _ := setupFakeRemote(t)
f := newFakeFs(t, remote, "")
inside := object.NewMemoryObject("x/inside.txt", time.Now(), []byte("inside"))
_, err := operations.Copy(ctx, f, nil, inside.Remote(), inside)
require.NoError(t, err)
src := object.NewMemoryObject("x", time.Now(), []byte("a file"))
_, err = operations.Copy(ctx, f, nil, "x", src)
require.ErrorIs(t, err, fs.ErrorIsDir)
f = newFakeFs(t, remote, "")
entries, err := f.List(ctx, "x")
require.NoError(t, err, "the directory survives")
require.Len(t, entries, 1)
assert.Equal(t, "x/inside.txt", entries[0].Remote())
}
// TestRegressionDirectoryOverFile is a regression test for rclone issue
// #9438: copying into a directory where a file of its name is
// destroyed data.
func TestRegressionDirectoryOverFile(t *testing.T) {
ctx := context.Background()
remote, _, _ := setupFakeRemote(t)
f := newFakeFs(t, remote, "")
file := object.NewMemoryObject("x", time.Now(), []byte("a file"))
_, err := operations.Copy(ctx, f, nil, "x", file)
require.NoError(t, err)
src := object.NewMemoryObject("x/y", time.Now(), []byte("inside"))
_, err = operations.Copy(ctx, f, nil, "x/y", src)
require.Error(t, err)
assert.Contains(t, err.Error(), `create folder "x": already exists`)
f = newFakeFs(t, remote, "")
o, err := f.NewObject(ctx, "x")
require.NoError(t, err, "the file survives")
assert.Equal(t, "a file", readObject(t, o))
}
// readObject returns the content of o.
func readObject(t *testing.T, o fs.Object) string {
t.Helper()
rc, err := o.Open(context.Background())
require.NoError(t, err)
data, err := io.ReadAll(rc)
require.NoError(t, rc.Close())
require.NoError(t, err)
return string(data)
}
// TestRegressionDocsNodeSkipped is a regression test for rclone issue
// #7959: a Proton Docs document made every sync fail.
func TestRegressionDocsNodeSkipped(t *testing.T) {
ctx := accounting.WithStatsGroup(context.Background(), "docs")
remote, _, u := setupFakeRemote(t)
dir := u.SeedFolder(u.RootID(), "dir")
u.SeedDoc(dir, "notes")
u.SeedFile(dir, "file.txt", []byte("content"), time.Now())
src := newFakeFs(t, remote, "dir")
dst := newFakeFs(t, "", t.TempDir())
require.NoError(t, fssync.Sync(ctx, dst, src, false))
assert.Zero(t, accounting.Stats(ctx).GetErrors())
entries, err := os.ReadDir(dst.Root())
require.NoError(t, err)
require.Len(t, entries, 1)
assert.Equal(t, "file.txt", entries[0].Name())
}
// TestRegressionBadSignatureFile is a regression test for rclone issue
// #8003: files whose signatures didn't verify couldn't be read at all.
func TestRegressionBadSignatureFile(t *testing.T) {
ctx := context.Background()
remote, _, u := setupFakeRemote(t)
const second = "second@proton.test"
u.AddAddress(second)
content := []byte("signed by another address")
u.SeedFileContentKeySignedBy(second, time.Now(), u.RootID(), "bad.txt", content)
f := newFakeFs(t, remote, "")
entries, err := f.List(ctx, "")
require.NoError(t, err)
require.Len(t, entries, 1)
o, err := f.NewObject(ctx, "bad.txt")
require.NoError(t, err)
_, err = o.Open(ctx)
require.Error(t, err)
assert.Contains(t, err.Error(), "bad.txt")
assert.Contains(t, err.Error(), "signature")
assert.Contains(t, err.Error(), "ignore_signature_errors")
f = newFakeFs(t, strings.TrimSuffix(remote, ":")+",ignore_signature_errors=true:", "")
o, err = f.NewObject(ctx, "bad.txt")
require.NoError(t, err)
assert.Equal(t, string(content), readObject(t, o))
}
// TestRegressionSDKCreatedFolderResolvable is a regression test for
// rclone issue #9622: files in folders made by Proton's own clients
// couldn't be found by path.
func TestRegressionSDKCreatedFolderResolvable(t *testing.T) {
ctx := context.Background()
remote, _, u := setupFakeRemote(t)
for _, names := range [][3]string{
{"dir", "sub", "file"},
{"Café", "é combining", "Ünïcødé file.txt"},
} {
dir := u.SeedFolder(u.RootID(), names[0])
sub := u.SeedFolder(dir, names[1])
u.SeedFile(sub, names[2], []byte(names[2]), time.Now())
f := newFakeFs(t, remote, "")
o, err := f.NewObject(ctx, strings.Join(names[:], "/"))
require.NoError(t, err, "%q", names)
assert.Equal(t, names[2], readObject(t, o))
}
}
// blockTransfers returns the number of block uploads and downloads in
// requests.
func blockTransfers(requests map[string]int) int {
n := 0
for route, count := range requests {
if strings.Contains(route, "/storage/") || route == "POST /drive/blocks" {
n += count
}
}
return n
}
// TestRegressionMoveIsServerSide checks a move transfers no content.
func TestRegressionMoveIsServerSide(t *testing.T) {
ctx := context.Background()
remote, srv, _ := setupFakeRemote(t)
f := newFakeFs(t, remote, "")
src, err := operations.Copy(ctx, f, nil, "a.txt", object.NewMemoryObject("a.txt", time.Now(), []byte("move me")))
require.NoError(t, err)
srv.ResetRequests()
moved, err := operations.Move(ctx, f, nil, "dir/b.txt", src)
require.NoError(t, err)
assert.Zero(t, blockTransfers(srv.Requests()), "%v", srv.Requests())
assert.Equal(t, "move me", readObject(t, moved))
_, err = f.NewObject(ctx, "a.txt")
assert.ErrorIs(t, err, fs.ErrorObjectNotFound)
}
// TestRegressionCopyIsServerSide checks a copy transfers no content.
func TestRegressionCopyIsServerSide(t *testing.T) {
ctx := context.Background()
remote, srv, _ := setupFakeRemote(t)
f := newFakeFs(t, remote, "")
src, err := operations.Copy(ctx, f, nil, "a.txt", object.NewMemoryObject("a.txt", time.Now(), []byte("copy me")))
require.NoError(t, err)
srv.ResetRequests()
copied, err := operations.Copy(ctx, f, nil, "dir/b.txt", src)
require.NoError(t, err)
assert.Zero(t, blockTransfers(srv.Requests()), "%v", srv.Requests())
assert.Equal(t, "copy me", readObject(t, copied))
assert.Equal(t, "copy me", readObject(t, src))
}
// TestRegressionMultiThreadDownload checks a multi-thread download
// assembles the file correctly.
func TestRegressionMultiThreadDownload(t *testing.T) {
ctx, ci := fs.AddConfig(context.Background())
ci.MultiThreadStreams = 4
ci.MultiThreadSet = true
ci.MultiThreadCutoff = 1 << 20
// Chunks which don't line up with the 4 MiB blocks.
ci.MultiThreadChunkSize = 5 << 20
remote, srv, u := setupFakeRemote(t)
content := []byte(random.String(20 << 20))
u.SeedFile(u.RootID(), "big.bin", content, time.Now())
src := newFakeFs(t, remote, "")
o, err := src.NewObject(ctx, "big.bin")
require.NoError(t, err)
dst := newFakeFs(t, "", t.TempDir())
srv.ResetRequests()
_, err = operations.Copy(ctx, dst, nil, "big.bin", o)
require.NoError(t, err)
// A single stream fetches each of the 5 blocks once.
assert.Greater(t, srv.Requests()["GET /storage/blocks/{id}"], 5, "not downloaded in parallel ranges")
got, err := os.ReadFile(filepath.Join(dst.Root(), "big.bin"))
require.NoError(t, err)
want := sha1.Sum(content)
gotSum := sha1.Sum(got)
assert.Equal(t, hex.EncodeToString(want[:]), hex.EncodeToString(gotSum[:]))
}
// TestRegressionPutStreamUnknownSize checks an upload of unknown size.
func TestRegressionPutStreamUnknownSize(t *testing.T) {
ctx := context.Background()
remote, _, _ := setupFakeRemote(t)
f := newFakeFs(t, remote, "")
require.NotNil(t, f.Features().PutStream)
content := []byte(random.String(9 << 20))
modTime := time.Now().Truncate(time.Second)
info := object.NewStaticObjectInfo("stream.bin", modTime, -1, true, nil, nil)
_, err := f.Features().PutStream(ctx, bytes.NewReader(content), info)
require.NoError(t, err)
o, err := newFakeFs(t, remote, "").NewObject(ctx, "stream.bin")
require.NoError(t, err)
assert.Equal(t, int64(len(content)), o.Size())
assert.True(t, modTime.Equal(o.ModTime(ctx)))
assert.True(t, readObject(t, o) == string(content), "content differs")
}
// TestRegressionCaptcha checks a CAPTCHA on password sign-in tells the
// user how to sign in with a browser.
func TestRegressionCaptcha(t *testing.T) {
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
srv.RequireCaptcha(u)
m := configmap.Simple{
"endpoint": srv.URL(),
"username": "user",
"password": obscure.MustObscure("pass"),
}
_, err := protondrive.NewFs(context.Background(), "myremote", "", m)
require.Error(t, err)
assert.True(t, fserrors.IsFatalError(err), "not fatal: %v", err)
assert.Contains(t, err.Error(), `rclone config reconnect myremote:`)
assert.Contains(t, err.Error(), "browser sign-in")
}
// TestRegressionAppVersionRejected checks Proton refusing rclone's
// version is a fatal error which says to upgrade.
func TestRegressionAppVersionRejected(t *testing.T) {
remote, srv, _ := setupFakeRemote(t)
f := newFakeFs(t, remote, "")
srv.Inject(protontest.Fault{Method: "GET", Pattern: "/drive/v2/volumes/{volumeID}/folders/{linkID}/children", Status: http.StatusBadRequest, Code: 5003})
_, err := f.List(context.Background(), "")
require.Error(t, err)
assert.True(t, fserrors.IsFatalError(err), "not fatal: %v", err)
assert.Contains(t, err.Error(), "upgrade rclone")
}
// TestRegressionFeatureBlockedRetried checks Proton's code for a
// feature disabled, rate-limited or blocked is retried, not fatal.
func TestRegressionFeatureBlockedRetried(t *testing.T) {
remote, srv, u := setupFakeRemote(t)
u.SeedFile(u.RootID(), "a.txt", []byte("a"), time.Now())
f := newFakeFs(t, remote, "")
srv.Inject(protontest.Fault{Method: "GET", Pattern: "/drive/v2/volumes/{volumeID}/folders/{linkID}/children", Times: 2, Status: http.StatusUnprocessableEntity, Code: 2028})
entries, err := f.List(context.Background(), "")
require.NoError(t, err)
assert.Len(t, entries, 1)
}
// TestRegressionRenameResendNotFatal checks a rename whose response was
// lost, so that its resend fails Proton's stale-state check, doesn't
// abort the whole run as an "upgrade rclone" error.
func TestRegressionRenameResendNotFatal(t *testing.T) {
ctx := context.Background()
remote, srv, u := setupFakeRemote(t)
u.SeedFile(u.RootID(), "old.txt", []byte("x"), time.Now())
f := newFakeFs(t, remote, "")
o, err := f.NewObject(ctx, "old.txt")
require.NoError(t, err)
srv.Inject(protontest.Fault{Method: "PUT", Pattern: "/drive/v2/volumes/{volumeID}/links/{linkID}/rename", Status: http.StatusBadGateway, AfterHandler: true, Times: 1})
_, err = f.Features().Move(ctx, o, "new.txt")
require.Error(t, err, "the resend saw the rename already made")
assert.False(t, fserrors.IsFatalError(err), "fatal: %v", err)
assert.NotContains(t, err.Error(), "upgrade rclone")
}
// TestRegressionQuotaExceeded checks running out of space is reported
// with Proton's message and not retried.
func TestRegressionQuotaExceeded(t *testing.T) {
remote, srv, _ := setupFakeRemote(t)
f := newFakeFs(t, remote, "")
srv.Inject(protontest.Fault{Method: "POST", Pattern: "/drive/v2/volumes/{volumeID}/files", Status: http.StatusUnprocessableEntity, Code: 200001})
srv.ResetRequests()
src := object.NewMemoryObject("a.txt", time.Now(), []byte("no room"))
_, err := operations.Copy(context.Background(), f, nil, "a.txt", src)
require.Error(t, err)
assert.True(t, fserrors.IsNoRetryError(err), "retryable: %v", err)
assert.Contains(t, err.Error(), "protontest: injected fault")
assert.Equal(t, 1, srv.Requests()[draftRoute])
}
// TestRegressionRateLimited checks listing waits out every Retry-After
// and then succeeds.
func TestRegressionRateLimited(t *testing.T) {
const times, retryAfter = 5, 1
remote, srv, u := setupFakeRemote(t)
u.SeedFile(u.RootID(), "a.txt", []byte("a"), time.Now())
f := newFakeFs(t, remote, "")
srv.Inject(protontest.Fault{Method: "GET", Pattern: "/drive/v2/volumes/{volumeID}/folders/{linkID}/children", Times: times, Status: http.StatusTooManyRequests, RetryAfter: retryAfter})
srv.ResetRequests()
start := time.Now()
entries, err := f.List(context.Background(), "")
require.NoError(t, err)
assert.GreaterOrEqual(t, time.Since(start), times*retryAfter*time.Second)
assert.Len(t, entries, 1)
assert.Equal(t, times+1, srv.Requests()[childrenRoute])
}
// TestRenamedFolderByOtherClient checks a folder renamed by another
// client is seen under its new name after a poll, and that nothing is
// left at its old path, as a mount needs.
func TestRenamedFolderByOtherClient(t *testing.T) {
ctx := context.Background()
remote, srv, u := setupFakeRemote(t)
f := newFakeFs(t, remote, "")
other, err := protondrive.NewFs(ctx, "other", "", sessionMap(srv, u))
require.NoError(t, err)
_, err = operations.Copy(ctx, f, nil, "a/b/f", object.NewMemoryObject("a/b/f", time.Now(), []byte("file")))
require.NoError(t, err)
entries, err := f.List(ctx, "a/b")
require.NoError(t, err)
require.Len(t, entries, 1)
require.NoError(t, other.Features().DirMove(ctx, other, "a", "c"))
// One interval of 1s, with up to 1s of jitter, and some slack.
assert.Eventually(t, func() bool {
_, oldErr := f.NewObject(ctx, "a/b/f")
_, newErr := f.NewObject(ctx, "c/b/f")
return errors.Is(oldErr, fs.ErrorObjectNotFound) && newErr == nil
}, 4*time.Second, 50*time.Millisecond, "the old path lingers or the new one isn't found")
_, err = f.List(ctx, "a/b")
assert.ErrorIs(t, err, fs.ErrorDirNotFound)
entries, err = f.List(ctx, "c/b")
require.NoError(t, err)
assert.Len(t, entries, 1)
}
File diff suppressed because it is too large. Load diff
+679
View File
@@ -0,0 +1,679 @@
package protondrive
import (
"context"
"crypto/rand"
"crypto/subtle"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"maps"
"net/http"
"net/url"
"regexp"
"strings"
"sync"
"time"
"github.com/ProtonMail/go-srp"
"github.com/rclone/rclone/backend/protondrive/api"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/fserrors"
"github.com/rclone/rclone/lib/rest"
)
// errSessionExpired is returned when the session can no longer be
// refreshed and the user must sign in again.
var errSessionExpired = errors.New("session expired")
// errNotSignedIn is returned by an authenticated call of a session
// which has no credentials, as after signing out.
var errNotSignedIn = errors.New("not signed in")
// errCaptcha is returned by password sign-in when Proton asks for human
// verification, which only its sign-in page can do.
var errCaptcha = errors.New("password sign-in needs a CAPTCHA solved")
const (
// srpBitLength is the bit length of Proton's SRP modulus.
srpBitLength = 2048
// forkClientID identifies rclone to Proton's sign-in page.
forkClientID = "external-drive"
// forkSignInURL is the sign-in page for session forks, followed by
// the escaped payload.
forkSignInURL = "https://account.proton.me/desktop/login?app=drive&pv=3#payload="
)
var (
// rcloneVersionRe splits an rclone version into its number and suffix.
rcloneVersionRe = regexp.MustCompile(`^v?([0-9]+\.[0-9]+\.[0-9]+)(?:-(.*))?$`)
// invalidBuildCharsRe matches characters not allowed in a semver
// build identifier.
invalidBuildCharsRe = regexp.MustCompile(`[^0-9A-Za-z-]+`)
)
// appVersionFromRcloneVersion returns the x-pm-appversion for the
// rclone version v, as external-drive-rclone@<semver>-<channel>.
//
// Releases map to stable, betas to beta with their build number as
// build metadata, and anything else to alpha. An unparsable version
// maps to 1.0.0-stable.
func appVersionFromRcloneVersion(v string) string {
const prefix = "external-drive-rclone@"
m := rcloneVersionRe.FindStringSubmatch(strings.TrimSpace(v))
if m == nil {
return prefix + "1.0.0-stable"
}
number, suffix := m[1], m[2]
switch {
case suffix == "":
return prefix + number + "-stable"
case suffix == "beta":
return prefix + number + "-beta"
case strings.HasPrefix(suffix, "beta."):
var build []string
for _, id := range strings.Split(strings.TrimPrefix(suffix, "beta."), ".") {
id = strings.Trim(invalidBuildCharsRe.ReplaceAllString(id, "-"), "-")
if id != "" {
build = append(build, id)
}
}
if len(build) == 0 {
return prefix + number + "-beta"
}
return prefix + number + "-beta+" + strings.Join(build, ".")
default:
return prefix + number + "-alpha"
}
}
// credentials are the saved tokens of a session.
type credentials struct {
UID string
AccessToken string
RefreshToken string
KeyPassword string // salted password which unlocks the user keys
}
// credentialStore persists credentials, possibly shared with other
// sessions and processes.
type credentialStore interface {
Load() credentials
Save(credentials)
}
// memoryStore is a credentialStore held in memory.
type memoryStore struct {
mu sync.Mutex
creds credentials
}
// Load returns the stored credentials.
func (m *memoryStore) Load() credentials {
m.mu.Lock()
defer m.mu.Unlock()
return m.creds
}
// Save replaces the stored credentials.
func (m *memoryStore) Save(c credentials) {
m.mu.Lock()
defer m.mu.Unlock()
m.creds = c
}
// session makes API calls with a Proton session, refreshing its tokens
// when they expire.
type session struct {
srv *rest.Client
store credentialStore
adoptWait time.Duration // how long a rejected refresh waits for the store to change
unlockHook func() // if set, called by loadAccount while unlockMu is held; for tests
reconnect string // if set, ends errors which need the user to sign in again, saying how
refreshMu sync.Mutex // held while refreshing, so one refresh runs at a time
mu sync.Mutex
creds credentials
deadRefresh string // refresh token the server rejected
serverDate time.Time // Date of the last response
serverDateLocal time.Time // local time serverDate was received
requirements string // x-pm-drive-requirements last logged
}
// newSession returns a session for the API at baseURL using the
// credentials in store.
func newSession(client *http.Client, baseURL, appVersion string, store credentialStore) *session {
own := *client
own.CheckRedirect = checkRedirect
s := &session{
srv: rest.NewClient(&own).SetRoot(baseURL),
store: store,
adoptWait: 3 * time.Second,
creds: store.Load(),
}
s.srv.SetHeader("x-pm-appversion", appVersion)
s.srv.SetHeader("Accept", "application/vnd.protonmail.v1+json")
s.srv.SetErrorHandler(errorHandler)
return s
}
// checkRedirect follows a redirect as Go's HTTP client does by
// default, except that a block's storage token and the session's UID
// aren't sent on to another host: Go drops only standard credential
// headers such as Authorization.
func checkRedirect(req *http.Request, via []*http.Request) error {
if len(via) >= 10 {
return errors.New("stopped after 10 redirects")
}
if req.URL.Host != via[0].URL.Host {
req.Header.Del("pm-storage-token")
req.Header.Del("x-pm-uid")
}
return nil
}
// errorHandler decodes an unsuccessful response into an *api.Error.
func errorHandler(resp *http.Response) error {
body, err := rest.ReadBody(resp)
apiErr := &api.Error{Status: resp.StatusCode}
if err == nil {
_ = json.Unmarshal(body, apiErr)
}
if apiErr.Message == "" {
apiErr.Message = resp.Status
}
return apiErr
}
// current returns the credentials in use.
func (s *session) current() credentials {
s.mu.Lock()
defer s.mu.Unlock()
return s.creds
}
// serverTime returns the server's clock: the Date of the last response
// advanced by the local time elapsed since. It is zero before the
// first response.
func (s *session) serverTime() time.Time {
s.mu.Lock()
defer s.mu.Unlock()
if s.serverDate.IsZero() {
return time.Time{}
}
return s.serverDate.Add(time.Since(s.serverDateLocal))
}
// call makes an authenticated API call with rest.Client.CallJSON
// semantics.
//
// An unsuccessful HTTP status or a response Code outside 1000-1999 is
// returned as an *api.Error. Without credentials it returns
// errNotSignedIn and sends nothing. On HTTP 401 the session is refreshed and
// the call retried once, so opts.Body, if set, must be an io.Seeker to
// be retried. If the session can't be refreshed the error wraps
// errSessionExpired.
func (s *session) call(ctx context.Context, opts *rest.Opts, request, response any) (*http.Response, error) {
creds := s.current()
if creds.UID == "" {
return nil, s.signInNeeded(errNotSignedIn)
}
resp, err := s.do(ctx, opts, request, response, &creds)
var apiErr *api.Error
if !errors.As(err, &apiErr) || apiErr.Status != http.StatusUnauthorized {
return resp, err
}
if refreshErr := s.refresh(ctx, creds.AccessToken); refreshErr != nil {
// Not the 401's response, so that a retry is decided by the
// refresh's error.
return nil, refreshErr
}
if opts.Body != nil {
seeker, ok := opts.Body.(io.Seeker)
if !ok {
return resp, err
}
if _, seekErr := seeker.Seek(0, io.SeekStart); seekErr != nil {
return resp, fmt.Errorf("rewind request body: %w", seekErr)
}
}
creds = s.current()
if creds.UID == "" {
// Signed out while the call was made.
return resp, s.signInNeeded(errNotSignedIn)
}
return s.do(ctx, opts, request, response, &creds)
}
// callUnauthenticated makes an API call like call but without the
// session's credentials and without refreshing.
func (s *session) callUnauthenticated(ctx context.Context, opts *rest.Opts, request, response any) (*http.Response, error) {
return s.do(ctx, opts, request, response, nil)
}
// do makes one API call, authenticated with creds if not nil.
func (s *session) do(ctx context.Context, opts *rest.Opts, request, response any, creds *credentials) (*http.Response, error) {
if creds != nil {
opts = opts.Copy()
opts.ExtraHeaders = maps.Clone(opts.ExtraHeaders)
if opts.ExtraHeaders == nil {
opts.ExtraHeaders = make(map[string]string, 2)
}
opts.ExtraHeaders["x-pm-uid"] = creds.UID
opts.ExtraHeaders["Authorization"] = "Bearer " + creds.AccessToken
}
var body json.RawMessage
resp, err := s.srv.CallJSON(ctx, opts, request, &body)
if resp != nil {
s.observeDate(resp.Header.Get("Date"))
s.observeRequirements(resp.Header.Values("x-pm-drive-requirements"))
}
if err != nil || opts.NoResponse {
return resp, err
}
var result struct {
Code int `json:"Code"`
}
if err := json.Unmarshal(body, &result); err != nil {
return resp, fmt.Errorf("decode response: %w", err)
}
// A body without a Code is not a Proton envelope, so is passed on.
if result.Code != 0 && result.Code/1000 != 1 {
apiErr := &api.Error{Status: resp.StatusCode}
_ = json.Unmarshal(body, apiErr)
return resp, apiErr
}
if response != nil {
if err := json.Unmarshal(body, response); err != nil {
return resp, fmt.Errorf("decode response: %w", err)
}
}
return resp, nil
}
// observeDate records the server's clock from a Date header.
func (s *session) observeDate(date string) {
t, err := http.ParseTime(date)
if err != nil {
return
}
s.mu.Lock()
defer s.mu.Unlock()
s.serverDate = t
s.serverDateLocal = time.Now()
}
// maxRequirementsLog bounds the x-pm-drive-requirements text logged.
const maxRequirementsLog = 1024
// observeRequirements logs at debug level the x-pm-drive-requirements
// header values of a response, by which Proton announces requirements
// it will enforce, when they differ from those last logged.
func (s *session) observeRequirements(values []string) {
if len(values) == 0 {
return
}
v := strings.Join(values, ", ")
if len(v) > maxRequirementsLog {
v = v[:maxRequirementsLog]
}
s.mu.Lock()
changed := v != s.requirements
s.requirements = v
s.mu.Unlock()
if changed {
fs.Debugf(nil, "protondrive: Proton announces drive requirements: %q", v)
}
}
// refresh replaces the access token rejected with a working one.
//
// It uses tokens another goroutine or another user of the store has
// already refreshed if there are any, since refresh tokens are single
// use. Otherwise it asks the server, saving the new tokens to the
// store. If the server rejects the refresh token, it waits up to
// s.adoptWait for another user of the store to save new tokens before
// returning errSessionExpired. Credentials are never removed from the
// store, because another process may have just saved new ones.
func (s *session) refresh(ctx context.Context, rejected string) error {
s.refreshMu.Lock()
defer s.refreshMu.Unlock()
cur := s.current()
if cur.AccessToken != rejected {
return nil
}
if stored := s.store.Load(); stored.RefreshToken != "" && stored.RefreshToken != cur.RefreshToken {
fs.Debugf(nil, "protondrive: using session tokens refreshed elsewhere")
s.setCredentials(stored)
return nil
}
s.mu.Lock()
dead := cur.RefreshToken == "" || cur.RefreshToken == s.deadRefresh
s.mu.Unlock()
if dead {
return s.signInNeeded(errSessionExpired)
}
fs.Debugf(nil, "protondrive: refreshing session")
var out api.AuthRefreshResponse
_, err := s.do(ctx, &rest.Opts{Method: "POST", Path: "/auth/v4/refresh"}, &api.AuthRefreshRequest{
ResponseType: "token",
GrantType: "refresh_token",
RefreshToken: cur.RefreshToken,
}, &out, &cur)
if err != nil {
// As the SDK does, treat a client error other than rate
// limiting, or a timeout, as the refresh token being rejected.
var apiErr *api.Error
if errors.As(err, &apiErr) && apiErr.Status >= 400 && apiErr.Status < 500 &&
apiErr.Status != http.StatusTooManyRequests && apiErr.Status != http.StatusRequestTimeout {
// Another user of the store may have spent the same refresh
// token and not saved its replacement yet.
if stored, ok := s.waitForStoredRefresh(ctx, cur.RefreshToken); ok {
fs.Debugf(nil, "protondrive: using session tokens refreshed elsewhere")
s.setCredentials(stored)
return nil
}
s.mu.Lock()
s.deadRefresh = cur.RefreshToken
s.mu.Unlock()
return s.signInNeeded(fmt.Errorf("%w: %w", errSessionExpired, err))
}
return fmt.Errorf("refresh session: %w", err)
}
if out.AccessToken == "" {
return errors.New("refresh session: no access token in response")
}
next := cur
next.AccessToken = out.AccessToken
if out.UID != "" {
next.UID = out.UID
}
if out.RefreshToken != "" {
next.RefreshToken = out.RefreshToken
}
s.setCredentials(next)
s.store.Save(next)
return nil
}
// signInNeeded returns err, which the user can only fix by signing in
// again, as a fatal error ending with s.reconnect.
func (s *session) signInNeeded(err error) error {
if s.reconnect != "" {
err = fmt.Errorf("%w: %s", err, s.reconnect)
}
return fserrors.FatalError(err)
}
// waitForStoredRefresh polls the store for up to s.adoptWait for a
// refresh token other than spent and returns those credentials. It
// returns false if none appears or ctx is done.
func (s *session) waitForStoredRefresh(ctx context.Context, spent string) (credentials, bool) {
const step = 50 * time.Millisecond
deadline := time.Now().Add(s.adoptWait)
for {
if stored := s.store.Load(); stored.RefreshToken != "" && stored.RefreshToken != spent {
return stored, true
}
if !time.Now().Before(deadline) {
return credentials{}, false
}
select {
case <-ctx.Done():
return credentials{}, false
case <-time.After(min(step, time.Until(deadline))):
}
}
}
// setCredentials replaces the credentials in use.
func (s *session) setCredentials(c credentials) {
s.mu.Lock()
defer s.mu.Unlock()
s.creds = c
}
// signInError marks a human verification error as errCaptcha.
func signInError(err error) error {
var apiErr *api.Error
if errors.As(err, &apiErr) && apiErr.Code == api.CodeHumanVerification {
return fmt.Errorf("sign in: %w: %w", errCaptcha, err)
}
return fmt.Errorf("sign in: %w", err)
}
// saveCredentials makes c the credentials in use and saves them to the store.
func (s *session) saveCredentials(c credentials) {
s.setCredentials(c)
s.store.Save(c)
}
// loginSRP signs in with a username and password using SRP, saving the
// new credentials to the store only if sign-in succeeds.
//
// The key password derives from mailboxPassword if set, else from
// password. twoFA is called for a TOTP code only if the account needs
// one. A request for human verification returns an error wrapping
// errCaptcha.
func (s *session) loginSRP(ctx context.Context, username, password, mailboxPassword string, twoFA func() (string, error)) (err error) {
var info api.AuthInfoResponse
_, err = s.callUnauthenticated(ctx, &rest.Opts{Method: "POST", Path: "/core/v4/auth/info"}, &api.AuthInfoRequest{
Intent: "Proton",
Username: username,
}, &info)
if err != nil {
return signInError(err)
}
if info.Modulus == "" || info.ServerEphemeral == "" || info.SRPSession == "" {
return errors.New("sign in: incomplete SRP challenge")
}
srpAuth, err := srp.NewAuth(info.Version, username, []byte(password), info.Salt, info.Modulus, info.ServerEphemeral)
if err != nil {
return fmt.Errorf("sign in: %w", err)
}
proofs, err := srpAuth.GenerateProofs(srpBitLength)
if err != nil {
return fmt.Errorf("sign in: %w", err)
}
var auth api.AuthResponse
_, err = s.callUnauthenticated(ctx, &rest.Opts{Method: "POST", Path: "/core/v4/auth"}, &api.AuthRequest{
Username: username,
ClientEphemeral: base64.StdEncoding.EncodeToString(proofs.ClientEphemeral),
ClientProof: base64.StdEncoding.EncodeToString(proofs.ClientProof),
SRPSession: info.SRPSession,
PersistentCookies: 1,
Payload: map[string]string{},
}, &auth)
if err != nil {
return signInError(err)
}
serverProof, err := base64.StdEncoding.DecodeString(auth.ServerProof)
if err != nil || subtle.ConstantTimeCompare(serverProof, proofs.ExpectedServerProof) != 1 {
return errors.New("sign in: server proof verification failed")
}
creds := credentials{UID: auth.UID, AccessToken: auth.AccessToken, RefreshToken: auth.RefreshToken}
if creds.UID == "" || creds.AccessToken == "" || creds.RefreshToken == "" {
return errors.New("sign in: no session tokens in response")
}
defer func() {
if err != nil {
// Don't leave a half signed in session behind.
_, _ = s.do(ctx, &rest.Opts{Method: "DELETE", Path: "/core/v4/auth"}, nil, nil, &creds)
}
}()
if auth.TwoFA.Enabled != 0 {
if auth.TwoFA.Enabled&api.TwoFATOTP == 0 {
return errors.New("sign in: password sign-in supports only TOTP two-factor authentication: choose browser sign-in")
}
if twoFA == nil {
return errors.New("sign in: a two-factor authentication code is required")
}
var code string
code, err = twoFA()
if err != nil {
return fmt.Errorf("sign in: %w", err)
}
_, err = s.do(ctx, &rest.Opts{Method: "POST", Path: "/core/v4/auth/2fa"}, &api.Auth2FARequest{TwoFactorCode: code}, nil, &creds)
if err != nil {
return fmt.Errorf("sign in: two-factor authentication: %w", err)
}
}
keyPassword := password
if mailboxPassword != "" {
keyPassword = mailboxPassword
} else if auth.PasswordMode == api.PasswordModeTwo {
return errors.New("sign in: the account has a separate mailbox password: set mailbox_password")
}
var user api.UserResponse
if _, err = s.do(ctx, &rest.Opts{Method: "GET", Path: "/core/v4/users"}, nil, &user, &creds); err != nil {
return fmt.Errorf("sign in: get user: %w", err)
}
var salts api.KeySaltsResponse
if _, err = s.do(ctx, &rest.Opts{Method: "GET", Path: "/core/v4/keys/salts"}, nil, &salts, &creds); err != nil {
return fmt.Errorf("sign in: get key salts: %w", err)
}
salt, ok := primaryKeySalt(user.User.Keys, salts.KeySalts)
if !ok {
return errors.New("sign in: no key salt for the account")
}
salted, err := computeKeyPassword([]byte(keyPassword), salt)
if err != nil {
return fmt.Errorf("sign in: %w", err)
}
creds.KeyPassword = string(salted)
s.saveCredentials(creds)
return nil
}
// primaryKeySalt returns the salt of the primary user key in keys.
//
// The SDK uses the first salt, which is wrong for an account whose
// user keys have different salts, so the first salt is only the
// fallback when none matches the primary key.
func primaryKeySalt(keys []api.UserKey, salts []api.KeySalt) (string, bool) {
for _, key := range keys {
if key.Primary != 1 {
continue
}
for _, salt := range salts {
if salt.ID == key.ID {
return salt.KeySalt, true
}
}
}
if len(salts) == 0 {
return "", false
}
return salts[0].KeySalt, true
}
// forkRequest is a pending browser sign-in.
type forkRequest struct {
UserCode string // code shown on the sign-in page
Selector string // polls the fork for the new session
URL string // sign-in page for the user to open
key []byte // AES-256 key the page encrypts the key password with
}
// forkStart starts a browser sign-in by requesting a session fork.
func (s *session) forkStart(ctx context.Context) (*forkRequest, error) {
var out api.ForkInitResponse
if _, err := s.callUnauthenticated(ctx, &rest.Opts{Method: "GET", Path: "/auth/v4/sessions/forks"}, nil, &out); err != nil {
return nil, fmt.Errorf("browser sign-in: %w", err)
}
if out.UserCode == "" || out.Selector == "" {
return nil, errors.New("browser sign-in: no fork in response")
}
key := make([]byte, 32)
if _, err := rand.Read(key); err != nil {
return nil, fmt.Errorf("browser sign-in: %w", err)
}
payload := "0:" + out.UserCode + ":" + base64.StdEncoding.EncodeToString(key) + ":" + forkClientID
return &forkRequest{
UserCode: out.UserCode,
Selector: out.Selector,
// The SDK escapes the payload with encodeURIComponent, which
// QueryEscape matches for the characters a payload contains.
URL: forkSignInURL + url.QueryEscape(payload),
key: key,
}, nil
}
// forkPoll waits for the user to complete the browser sign-in f,
// polling every interval for up to timeout, then saves the new
// credentials to the store.
func (s *session) forkPoll(ctx context.Context, f *forkRequest, interval, timeout time.Duration) error {
pollCtx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
stopped := func() error {
if err := ctx.Err(); err != nil {
return err
}
return fmt.Errorf("browser sign-in: not completed within %v", timeout)
}
opts := &rest.Opts{Method: "GET", Path: "/auth/v4/sessions/forks/" + url.PathEscape(f.Selector)}
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-pollCtx.Done():
return stopped()
case <-ticker.C:
}
var out api.ForkStatusResponse
_, err := s.callUnauthenticated(pollCtx, opts, nil, &out)
var apiErr *api.Error
if errors.As(err, &apiErr) && apiErr.Status == http.StatusUnprocessableEntity {
// The fork isn't approved yet.
continue
}
if err != nil {
if pollCtx.Err() != nil {
return stopped()
}
return fmt.Errorf("browser sign-in: %w", err)
}
keyPassword, err := decryptForkPayload(out.Payload, f.key)
if err != nil {
return fmt.Errorf("browser sign-in: %w", err)
}
creds := credentials{UID: out.UID, AccessToken: out.AccessToken, RefreshToken: out.RefreshToken, KeyPassword: keyPassword}
if creds.UID == "" || creds.AccessToken == "" || creds.RefreshToken == "" {
return errors.New("browser sign-in: no session tokens in response")
}
s.saveCredentials(creds)
return nil
}
}
// logout ends the session on the server and removes the credentials
// from the session and the store, so neither can be used again.
func (s *session) logout(ctx context.Context) error {
var err error
if s.current().UID != "" {
_, err = s.call(ctx, &rest.Opts{Method: "DELETE", Path: "/core/v4/auth"}, nil, nil)
if errors.Is(err, errSessionExpired) {
err = nil
}
}
// A refresh in flight would otherwise save its new tokens after
// these are removed.
s.refreshMu.Lock()
s.saveCredentials(credentials{})
s.refreshMu.Unlock()
if err != nil {
return fmt.Errorf("sign out: %w", err)
}
return nil
}
@@ -0,0 +1,596 @@
package protondrive
import (
"bytes"
"context"
"encoding/base64"
"errors"
"io"
"net/http"
"net/url"
"regexp"
"strings"
"sync"
"testing"
"time"
"github.com/pquerna/otp/totp"
"github.com/rclone/rclone/backend/protondrive/api"
"github.com/rclone/rclone/backend/protondrive/protontest"
"github.com/rclone/rclone/fs/fserrors"
"github.com/rclone/rclone/lib/rest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
testAppVersion = "external-drive-rclone@1.77.0-stable"
refreshRoute = "POST /auth/v4/refresh"
)
// appVersionGrammar is Proton's x-pm-appversion grammar with the name
// fixed to rclone and the channel required.
var appVersionGrammar = regexp.MustCompile(`^external-drive-rclone@[0-9]+\.[0-9]+\.[0-9]+-(stable|beta|alpha)(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$`)
func TestAppVersionFromRcloneVersion(t *testing.T) {
for _, test := range []struct {
in string
want string
}{
{"v1.77.0", "external-drive-rclone@1.77.0-stable"},
{"1.77.0", "external-drive-rclone@1.77.0-stable"},
{"v1.77.0-DEV", "external-drive-rclone@1.77.0-alpha"},
{"v1.77.0-beta.10754.448d7361c", "external-drive-rclone@1.77.0-beta+10754.448d7361c"},
{"v1.77.0-beta.10754.448d7361c.fix/proton_drive", "external-drive-rclone@1.77.0-beta+10754.448d7361c.fix-proton-drive"},
{"v1.77.0-beta", "external-drive-rclone@1.77.0-beta"},
{"v1.77.0-rc1", "external-drive-rclone@1.77.0-alpha"},
{"garbage", "external-drive-rclone@1.0.0-stable"},
{"", "external-drive-rclone@1.0.0-stable"},
{"v1.77", "external-drive-rclone@1.0.0-stable"},
} {
got := appVersionFromRcloneVersion(test.in)
assert.Equal(t, test.want, got, "input %q", test.in)
assert.Regexp(t, appVersionGrammar, got, "input %q", test.in)
}
}
// headerRecorder records the headers of every request it sends.
type headerRecorder struct {
mu sync.Mutex
headers []http.Header
next http.RoundTripper
}
func (h *headerRecorder) RoundTrip(req *http.Request) (*http.Response, error) {
h.mu.Lock()
h.headers = append(h.headers, req.Header.Clone())
h.mu.Unlock()
return h.next.RoundTrip(req)
}
// newTestSession signs a new user in to srv and returns a session using
// a store holding its credentials.
func newTestSession(t *testing.T, srv *protontest.Server) (*session, *memoryStore, protontest.Session) {
t.Helper()
tokens := srv.NewSession(srv.AddUser("user", "pass"))
store := &memoryStore{creds: credentials{
UID: tokens.UID,
AccessToken: tokens.AccessToken,
RefreshToken: tokens.RefreshToken,
KeyPassword: "key-pass",
}}
return newSession(srv.Client(), srv.URL(), testAppVersion, store), store, tokens
}
// authCall makes an authenticated test call.
func authCall(ctx context.Context, s *session) error {
var out struct {
UserID string `json:"UserID"`
}
_, err := s.call(ctx, &rest.Opts{Method: "GET", Path: "/tests/auth"}, nil, &out)
if err == nil && out.UserID == "" {
return errors.New("no UserID in response")
}
return err
}
func TestSessionHeaders(t *testing.T) {
srv := protontest.New(t)
tokens := srv.NewSession(srv.AddUser("user", "pass"))
rec := &headerRecorder{next: srv.Client().Transport}
client := &http.Client{Transport: rec}
store := &memoryStore{creds: credentials{UID: tokens.UID, AccessToken: tokens.AccessToken, RefreshToken: tokens.RefreshToken}}
s := newSession(client, srv.URL(), testAppVersion, store)
require.NoError(t, authCall(context.Background(), s))
_, err := s.callUnauthenticated(context.Background(), &rest.Opts{Method: "GET", Path: "/tests/ping"}, nil, nil)
require.NoError(t, err)
require.Len(t, rec.headers, 2)
auth, unauth := rec.headers[0], rec.headers[1]
assert.Equal(t, tokens.UID, auth.Get("x-pm-uid"))
assert.Equal(t, "Bearer "+tokens.AccessToken, auth.Get("Authorization"))
for _, h := range []http.Header{auth, unauth} {
assert.Equal(t, testAppVersion, h.Get("x-pm-appversion"))
assert.Equal(t, "application/vnd.protonmail.v1+json", h.Get("Accept"))
assert.Empty(t, h.Get("x-pm-drive-sdk-version"))
}
assert.Empty(t, unauth.Get("x-pm-uid"))
assert.Empty(t, unauth.Get("Authorization"))
}
func TestSessionUnauthenticatedNoRefresh(t *testing.T) {
srv := protontest.New(t)
s, _, _ := newTestSession(t, srv)
_, err := s.callUnauthenticated(context.Background(), &rest.Opts{Method: "GET", Path: "/tests/auth"}, nil, nil)
var apiErr *api.Error
require.ErrorAs(t, err, &apiErr)
assert.Equal(t, http.StatusUnauthorized, apiErr.Status)
assert.Zero(t, srv.Requests()[refreshRoute])
}
func TestSessionRefreshOn401(t *testing.T) {
srv := protontest.New(t)
s, store, tokens := newTestSession(t, srv)
srv.ExpireAccessToken(tokens.UID)
require.NoError(t, authCall(context.Background(), s))
assert.Equal(t, 1, srv.Requests()[refreshRoute])
assert.Equal(t, 2, srv.Requests()["GET /tests/auth"])
saved := store.Load()
assert.Equal(t, tokens.UID, saved.UID)
assert.NotEqual(t, tokens.AccessToken, saved.AccessToken)
assert.NotEqual(t, tokens.RefreshToken, saved.RefreshToken)
assert.Equal(t, "key-pass", saved.KeyPassword)
// The new tokens are used without another refresh.
require.NoError(t, authCall(context.Background(), s))
assert.Equal(t, 1, srv.Requests()[refreshRoute])
}
func TestSessionRefreshResendsBody(t *testing.T) {
srv := protontest.New(t)
s, _, tokens := newTestSession(t, srv)
type echo struct {
Echo string `json:"Echo"`
}
opts := &rest.Opts{Method: "POST", Path: "/tests/echo"}
srv.ExpireAccessToken(tokens.UID)
var out echo
_, err := s.call(context.Background(), opts, &echo{Echo: "request"}, &out)
require.NoError(t, err)
assert.Equal(t, "request", out.Echo)
srv.ExpireAccessToken(tokens.UID)
out = echo{}
bodyOpts := *opts
bodyOpts.Body = bytes.NewReader([]byte(`{"Echo":"body"}`))
_, err = s.call(context.Background(), &bodyOpts, nil, &out)
require.NoError(t, err)
assert.Equal(t, "body", out.Echo)
assert.Equal(t, 2, srv.Requests()[refreshRoute])
}
func TestSessionRefreshSingleFlight(t *testing.T) {
srv := protontest.New(t)
s, _, tokens := newTestSession(t, srv)
srv.ExpireAccessToken(tokens.UID)
var wg sync.WaitGroup
errs := make([]error, 20)
for i := range errs {
wg.Go(func() {
errs[i] = authCall(context.Background(), s)
})
}
wg.Wait()
for i, err := range errs {
assert.NoError(t, err, "call %d", i)
}
assert.Equal(t, 1, srv.Requests()[refreshRoute])
}
func TestSessionRefreshRereadsStore(t *testing.T) {
srv := protontest.New(t)
a, store, tokens := newTestSession(t, srv)
b := newSession(srv.Client(), srv.URL(), testAppVersion, store)
srv.ExpireAccessToken(tokens.UID)
require.NoError(t, authCall(context.Background(), a))
assert.Equal(t, 1, srv.Requests()[refreshRoute])
// b still holds the rotated tokens, so its first call gets 401 and
// adopts a's tokens from the store instead of reusing its refresh
// token.
require.NoError(t, authCall(context.Background(), b))
assert.Equal(t, 1, srv.Requests()[refreshRoute])
assert.Equal(t, 4, srv.Requests()["GET /tests/auth"])
}
func TestSessionRefreshTwoSessionsConcurrent(t *testing.T) {
srv := protontest.New(t)
a, store, tokens := newTestSession(t, srv)
b := newSession(srv.Client(), srv.URL(), testAppVersion, store)
// Holding each refresh makes both sessions post the same refresh
// token before either saves the new one.
srv.Inject(protontest.Fault{Method: "POST", Pattern: "/auth/v4/refresh", Delay: 200 * time.Millisecond})
srv.ExpireAccessToken(tokens.UID)
var wg sync.WaitGroup
errs := make([]error, 2)
for i, s := range []*session{a, b} {
wg.Go(func() {
errs[i] = authCall(context.Background(), s)
})
}
wg.Wait()
for i, err := range errs {
assert.NoError(t, err, "session %d", i)
}
assert.Equal(t, 2, srv.Requests()[refreshRoute], "both sessions posted the refresh token")
// The stored credentials work in a fresh session.
c := newSession(srv.Client(), srv.URL(), testAppVersion, store)
require.NoError(t, authCall(context.Background(), c))
assert.Equal(t, 2, srv.Requests()[refreshRoute])
}
func TestSessionRevoked(t *testing.T) {
srv := protontest.New(t)
s, store, tokens := newTestSession(t, srv)
s.adoptWait = 50 * time.Millisecond
s.reconnect = `run "rclone config reconnect myremote:"`
before := store.Load()
srv.RevokeSession(tokens.UID)
err := authCall(context.Background(), s)
require.Error(t, err)
assert.ErrorIs(t, err, errSessionExpired)
assert.True(t, fserrors.IsFatalError(err), "error is fatal: %v", err)
assert.Contains(t, err.Error(), `rclone config reconnect myremote:`)
assert.Equal(t, before, store.Load(), "credentials kept")
assert.Equal(t, 1, srv.Requests()[refreshRoute])
// Further calls fail the same way without retrying the refresh.
err = authCall(context.Background(), s)
assert.ErrorIs(t, err, errSessionExpired)
assert.Equal(t, 1, srv.Requests()[refreshRoute])
assert.Equal(t, before, store.Load(), "credentials kept")
}
func TestSessionServerTime(t *testing.T) {
srv := protontest.New(t)
srv.SetClockSkew(3 * time.Hour)
s, _, _ := newTestSession(t, srv)
assert.True(t, s.serverTime().IsZero())
require.NoError(t, authCall(context.Background(), s))
assert.WithinDuration(t, time.Now().Add(3*time.Hour), s.serverTime(), 5*time.Second)
}
func TestSessionAPIError(t *testing.T) {
for _, status := range []int{http.StatusUnprocessableEntity, http.StatusOK} {
srv := protontest.New(t)
s, _, _ := newTestSession(t, srv)
srv.Inject(protontest.Fault{Pattern: "/tests/auth", Times: 1, Status: status, Code: api.CodeAlreadyExists})
err := authCall(context.Background(), s)
var apiErr *api.Error
require.ErrorAs(t, err, &apiErr, "status %d", status)
assert.Equal(t, status, apiErr.Status)
assert.Equal(t, api.CodeAlreadyExists, apiErr.Code)
assert.Zero(t, srv.Requests()[refreshRoute])
}
}
// newLoginSession returns a session for srv with an empty store.
func newLoginSession(srv *protontest.Server) (*session, *memoryStore) {
store := &memoryStore{}
return newSession(srv.Client(), srv.URL(), testAppVersion, store), store
}
// noTwoFA is a twoFA callback for accounts without 2FA.
func noTwoFA() (string, error) {
return "", errors.New("unexpected 2FA prompt")
}
// requireSignedIn checks store holds complete credentials for a working
// session whose key password derives from keyPassword and u's key salt.
func requireSignedIn(t *testing.T, s *session, store *memoryStore, u *protontest.User, keyPassword string) {
t.Helper()
saved := store.Load()
require.NotEmpty(t, saved.UID)
require.NotEmpty(t, saved.AccessToken)
require.NotEmpty(t, saved.RefreshToken)
want, err := computeKeyPassword([]byte(keyPassword), u.KeySalt)
require.NoError(t, err)
assert.Equal(t, string(want), saved.KeyPassword)
assert.Equal(t, saved, s.current())
require.NoError(t, authCall(context.Background(), s))
}
func TestLoginSRP(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "correct password")
s, store := newLoginSession(srv)
err := s.loginSRP(context.Background(), "user", "wrong password", "", noTwoFA)
require.Error(t, err)
assert.Equal(t, credentials{}, store.Load(), "nothing stored")
assert.Equal(t, credentials{}, s.current())
require.NoError(t, s.loginSRP(context.Background(), "user", "correct password", "", noTwoFA))
requireSignedIn(t, s, store, u, "correct password")
}
func TestLoginSRPSaltOfPrimaryKey(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
oldSalt := u.KeySalt
u.AddUserKey()
require.NotEqual(t, oldSalt, u.KeySalt)
// The salts list the old key first; the primary key's salt is second.
s, store := newLoginSession(srv)
require.NoError(t, s.loginSRP(context.Background(), "user", "pass", "", noTwoFA))
requireSignedIn(t, s, store, u, "pass")
assert.Equal(t, u.KeyPassword(), store.Load().KeyPassword)
a, err := loadAccount(context.Background(), s, []byte(store.Load().KeyPassword))
require.NoError(t, err)
a.close()
}
func TestPrimaryKeySalt(t *testing.T) {
keys := []api.UserKey{{ID: "old", Primary: 0}, {ID: "new", Primary: 1}}
salts := []api.KeySalt{{ID: "old", KeySalt: "old-salt"}, {ID: "new", KeySalt: "new-salt"}}
salt, ok := primaryKeySalt(keys, salts)
assert.True(t, ok)
assert.Equal(t, "new-salt", salt)
salt, ok = primaryKeySalt(keys, []api.KeySalt{{ID: "other", KeySalt: "first"}, {ID: "x", KeySalt: "second"}})
assert.True(t, ok)
assert.Equal(t, "first", salt, "falls back to the first salt")
salt, ok = primaryKeySalt(nil, salts)
assert.True(t, ok)
assert.Equal(t, "old-salt", salt)
_, ok = primaryKeySalt(keys, nil)
assert.False(t, ok)
}
func TestLoginSRPBadServerProof(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
srv.CorruptServerProof(u)
s, store := newLoginSession(srv)
err := s.loginSRP(context.Background(), "user", "pass", "", noTwoFA)
require.Error(t, err)
assert.Contains(t, err.Error(), "server proof verification")
assert.Equal(t, credentials{}, store.Load(), "nothing stored")
assert.Equal(t, credentials{}, s.current())
}
func TestLoginSRPPasswords(t *testing.T) {
t.Parallel()
for _, password := range []string{
"with space ",
`quo"te'`,
"пароль-密码",
strings.Repeat("abcdefghij", 10),
} {
t.Run(password, func(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", password)
s, store := newLoginSession(srv)
require.NoError(t, s.loginSRP(context.Background(), "user", password, "", noTwoFA))
requireSignedIn(t, s, store, u, password)
})
}
}
func TestLoginSRPMailboxPassword(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddTwoPasswordUser("user", "login password", "mailbox password")
s, store := newLoginSession(srv)
require.NoError(t, s.loginSRP(context.Background(), "user", "login password", "mailbox password", noTwoFA))
requireSignedIn(t, s, store, u, "mailbox password")
// Without the mailbox password the key password can't be derived.
s, store = newLoginSession(srv)
err := s.loginSRP(context.Background(), "user", "login password", "", noTwoFA)
require.Error(t, err)
assert.Contains(t, err.Error(), "mailbox_password")
assert.Equal(t, credentials{}, store.Load())
}
func TestLoginSRPTOTP(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
secret := u.EnableTOTP()
calls := 0
s, store := newLoginSession(srv)
require.NoError(t, s.loginSRP(context.Background(), "user", "pass", "", func() (string, error) {
calls++
return totp.GenerateCode(secret, time.Now())
}))
assert.Equal(t, 1, calls)
requireSignedIn(t, s, store, u, "pass")
calls = 0
s, store = newLoginSession(srv)
err := s.loginSRP(context.Background(), "user", "pass", "", func() (string, error) {
calls++
return "000000x", nil
})
require.Error(t, err)
assert.Equal(t, 1, calls)
assert.Equal(t, credentials{}, store.Load())
}
func TestLoginSRPCaptcha(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
srv.RequireCaptcha(u)
s, store := newLoginSession(srv)
err := s.loginSRP(context.Background(), "user", "pass", "", noTwoFA)
require.ErrorIs(t, err, errCaptcha)
assert.Equal(t, credentials{}, store.Load())
}
func TestForkLogin(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
s, store := newLoginSession(srv)
f, err := s.forkStart(context.Background())
require.NoError(t, err)
assert.NotEmpty(t, f.Selector)
assert.Len(t, f.key, 32)
prefix := "https://account.proton.me/desktop/login?app=drive&pv=3#payload="
require.True(t, strings.HasPrefix(f.URL, prefix), f.URL)
payload, err := url.QueryUnescape(strings.TrimPrefix(f.URL, prefix))
require.NoError(t, err)
assert.Equal(t, "0:"+f.UserCode+":"+base64.StdEncoding.EncodeToString(f.key)+":external-drive", payload)
go func() {
time.Sleep(50 * time.Millisecond)
assert.NoError(t, srv.ApproveFork(f.URL, u))
}()
require.NoError(t, s.forkPoll(context.Background(), f, 10*time.Millisecond, 10*time.Second))
requireSignedIn(t, s, store, u, "pass")
}
func TestForkLoginTimeout(t *testing.T) {
srv := protontest.New(t)
s, store := newLoginSession(srv)
f, err := s.forkStart(context.Background())
require.NoError(t, err)
start := time.Now()
err = s.forkPoll(context.Background(), f, 10*time.Millisecond, 100*time.Millisecond)
require.Error(t, err)
assert.GreaterOrEqual(t, time.Since(start), 100*time.Millisecond)
assert.Less(t, time.Since(start), 5*time.Second)
assert.Greater(t, srv.Requests()["GET /auth/v4/sessions/forks/{selector}"], 1)
assert.Equal(t, credentials{}, store.Load())
}
func TestForkLoginCancel(t *testing.T) {
srv := protontest.New(t)
s, store := newLoginSession(srv)
f, err := s.forkStart(context.Background())
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
time.AfterFunc(50*time.Millisecond, cancel)
start := time.Now()
err = s.forkPoll(ctx, f, 10*time.Millisecond, 10*time.Minute)
assert.ErrorIs(t, err, context.Canceled)
assert.Less(t, time.Since(start), 5*time.Second)
assert.Equal(t, credentials{}, store.Load())
}
func TestLogout(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
srv.AddUser("user", "pass")
s, store := newLoginSession(srv)
require.NoError(t, s.loginSRP(context.Background(), "user", "pass", "", noTwoFA))
before := store.Load()
require.NoError(t, s.logout(context.Background()))
assert.Equal(t, credentials{}, store.Load())
srv.ResetRequests()
err := authCall(context.Background(), s)
assert.ErrorIs(t, err, errNotSignedIn)
assert.True(t, fserrors.IsFatalError(err))
assert.Empty(t, srv.Requests(), "no request is sent without a session")
// The server ended the session too.
old := newSession(srv.Client(), srv.URL(), testAppVersion, &memoryStore{creds: before})
old.adoptWait = 0
assert.ErrorIs(t, authCall(context.Background(), old), errSessionExpired)
}
func TestCallDuringLogout(t *testing.T) {
t.Parallel()
ctx := context.Background()
srv := protontest.New(t)
s, _, _ := newTestSession(t, srv)
// The call is rejected only after the session has signed out.
srv.Inject(protontest.Fault{Method: "GET", Pattern: "/tests/auth", Times: 1, Status: http.StatusUnauthorized, Delay: 500 * time.Millisecond})
errc := make(chan error, 1)
go func() { errc <- authCall(ctx, s) }()
require.Eventually(t, func() bool { return srv.Requests()["GET /tests/auth"] == 1 }, 5*time.Second, 5*time.Millisecond)
require.NoError(t, s.logout(ctx))
err := <-errc
assert.ErrorIs(t, err, errNotSignedIn)
assert.Equal(t, 1, srv.Requests()["GET /tests/auth"], "retried without credentials")
assert.Zero(t, srv.Requests()[refreshRoute])
}
func TestRefreshDuringLogout(t *testing.T) {
t.Parallel()
ctx := context.Background()
srv := protontest.New(t)
tokens := srv.NewSession(srv.AddUser("user", "pass"))
refreshing, release := make(chan struct{}), make(chan struct{})
// The refresh succeeds, but its answer arrives only after the
// session has signed out.
client := &http.Client{Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
if r.URL.Path != "/auth/v4/refresh" {
return srv.Client().Transport.RoundTrip(r)
}
close(refreshing)
<-release
body := `{"Code":1000,"UID":"` + tokens.UID + `","AccessToken":"new-access","RefreshToken":"new-refresh"}`
return &http.Response{StatusCode: http.StatusOK, Header: http.Header{"Content-Type": {"application/json"}}, Body: io.NopCloser(strings.NewReader(body)), Request: r}, nil
})}
store := &memoryStore{creds: credentials{UID: tokens.UID, AccessToken: tokens.AccessToken, RefreshToken: tokens.RefreshToken, KeyPassword: "key-pass"}}
s := newSession(client, srv.URL(), testAppVersion, store)
srv.Inject(protontest.Fault{Method: "GET", Pattern: "/tests/auth", Times: 1, Status: http.StatusUnauthorized})
errc := make(chan error, 1)
go func() { errc <- authCall(ctx, s) }()
<-refreshing
logoutErr := make(chan error, 1)
go func() { logoutErr <- s.logout(ctx) }()
require.Eventually(t, func() bool { return srv.Requests()["DELETE /core/v4/auth"] == 1 }, 5*time.Second, 5*time.Millisecond)
close(release)
require.NoError(t, <-logoutErr)
assert.Error(t, <-errc, "the call succeeded with tokens refreshed after signing out")
assert.Equal(t, credentials{}, store.Load(), "tokens saved after signing out")
assert.Equal(t, credentials{}, s.current())
}
func TestRefreshTransientErrorRetried(t *testing.T) {
for _, status := range []int{http.StatusServiceUnavailable, http.StatusRequestTimeout} {
t.Run(http.StatusText(status), func(t *testing.T) {
c, srv, _ := newTestClient(t)
ctx := context.Background()
srv.ExpireAccessToken(c.s.current().UID)
srv.Inject(protontest.Fault{Method: "POST", Pattern: "/auth/v4/refresh", Status: status, Times: 1})
_, _, err := c.about(ctx)
require.NoError(t, err, "a %d on the refresh wasn't retried", status)
assert.Equal(t, 2, srv.Requests()["POST /auth/v4/refresh"])
})
}
}
+11
View File
@@ -0,0 +1,11 @@
{
"Comment": "Keys made by TestGoldenCapture for the tests alone.",
"Email": "golden@example.invalid",
"Keys": {
"AddressKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavQ+PBYJKwYBBAHaRw8BAQdAtg+4mrPbyi6Tjo0WFynmLyYdmwCE243WA+Ak\nQ7yhPRL+BwMIOyH0WSccjL3gS/40gwjnjBy7+HjOp7WJxPc2Eh8FDjOHUkrWi0Qi\nj6R4hVeNOStSLVvewHyPphKoSdQxVM4Q5/mvrkQowER6fdEGmpYAjc0fZ29sZGVu\nIDxnb2xkZW5AZXhhbXBsZS5pbnZhbGlkPsK/BBMWCABxBYJq9D48AwsJBwkQdw3k\nqPWekNs1FAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ2VOFH8X\nh5yxg5eroXQ18qcCFQgDFgACAhkBApsDAh4BFiEEK2cnqpmZRXt67JN/dw3kqPWe\nkNsAAG6fAQDV8ehsHsFOKQzpeYLsixmXs/h0/DAH2sHWoj49Z9CqeQD+Oa+nLFB7\nqUQUr66n59p4R6RQ8yv5H3HSiP222s9fmAXHiwRq9D48EgorBgEEAZdVAQUBAQdA\n6FutOZ0NLbRiCAqLSd2txpkq/hfv0HgedU5u29B4mkYDAQoJ/gcDCDsh9FknHIy9\n4NWExuQW8SLjmpFYDBp0NvnfM8CdqvOnOmpuE3Fefei8GtlgkYmQferCHqw38bBv\nrKXVMg3lxzsl4PhnykpmmcYP7niNnCzCrgQYFggAYAWCavQ+PAkQdw3kqPWekNs1\nFAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ34i6EDHnkQF/krz\nlrpZWpICmwwWIQQrZyeqmZlFe3rsk393DeSo9Z6Q2wAA4ocA/Rr/qWSSPj+q+uSN\n5imo6ET2IhnLJvcfhXQaMjN9aZvRAP9RWbdgdexbkbyoPI1I6zT1Yfb3Lg/9Vk/h\n0WiqPzmhAQ==\n=13w4\n-----END PGP PRIVATE KEY BLOCK-----",
"AddressKeyPassphrase": "qoww/oDsj2wClAHEcf1w6KRmiiIegYZN8gnIEYiR+yo=",
"RootKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavRU8BYJKwYBBAHaRw8BAQdAyYx5cRV31ovNSetf7tN542QIjXofDRwb5REq\nbgAUfTL+BwMIMMJR+/aYJWvg8RWLH459XM6tdGMymPBJf6uYIfNkCZPJuH/xgQlP\nUgnfauP/HE3mB4raN0Ewfdd5t3YHFH3LkKi9PTeyBIOxoSq+03v9ec0fZ29sZGVu\nIDxnb2xkZW5AZXhhbXBsZS5pbnZhbGlkPsK/BBMWCABxBYJq9FTwAwsJBwkQcokY\nuWqSnJY1FAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZwCnRKL/\nEVKgVnzorTxO7x0CFQgDFgACAhkBApsDAh4BFiEElLi748AC52UTTWtJcokYuWqS\nnJYAACRRAPwN9DnHokHSakHFOxhk4uQ7Vr782ISOzHngJ/NgOYtmygD/R/1ctxo5\nY32q3O+N1memq7Dij6DfU4GN6WbTA6ypaArHiwRq9FTwEgorBgEEAZdVAQUBAQdA\ncVbkQob/gu0TMrl+8sxRZKfmMKrnRC8QtN63i88MFE8DAQoJ/gcDCDDCUfv2mCVr\n4OixHp4Iy/zOVSPhV3eQSvPgFIb7lBp0m3nSGBgs6BGHRWLOys+Od7+AlLA0HjGa\nQf4cLnRKfvmiHV69WS0flFzsqxwfkdrCrgQYFggAYAWCavRU8AkQcokYuWqSnJY1\nFAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ4m//YFqqVLRVFiw\njVvAD08CmwwWIQSUuLvjwALnZRNNa0lyiRi5apKclgAAAbYA/jDBwlC+AccJp5hS\n1nHztq3bhQqs88y8eS7fCucXwDA7AQDkXzeePakq4JUtoPCiTe/Zf5wzME0BJ6Hk\nEu0vBJkWCg==\n=oZX1\n-----END PGP PRIVATE KEY BLOCK-----",
"RootKeyPassphrase": "a2W6imzT596kxrkFLloSQicvaE0k4CcUFRAmBkdDp3s=",
"RootHashKey": "-----BEGIN PGP MESSAGE-----\n\nwV4DC8CXHujYN4ASAQdAAGQNjBfh2oFIyAgUxqF6Nk9KA70Yg/S4IuThF/nFnAMw\nTjLXIcTbQmZsjmIwJi2xSDFAKvaGADIE7f2J9s65RPHkzcea2btnJOeifP0cJJSy\n0sBZAT+gElIVu/UWLXbOl4r84L9SyJCrb5dAOxrAOmTjdpEif28K3oOo3AanKCc8\nkBVYnFz5tUWq4wjYLYKOG/UZJFY+X6ue6gBBxR7tbXWQ4BGcrF7EeeaSE2XyfX+A\naSsZ7EuLKD6Ow3c0lxFx77/9hZFDlYGRyfZIDFERZw/ibOfMITxIj9DsXI31Xl8u\nf40ZrdRragq2Wzk8t8sd0h5pvFzw0y99PjWlzeo4pTKHT5lfz+xPpj4Nn09KMqhR\nMW1VWtfTES3rwNEYw3NmLRSrEP480kr1tp10uLt4hNfDs9ymCCW/N5ZdNc0ycKJ6\nt7fH9QPa4fbPAUdgNRBjniYeco6hyUzEH7s4kSayX+IqB2zicdpEQhqdM/Q=\n=DyXT\n-----END PGP MESSAGE-----"
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+240
View File
@@ -0,0 +1,240 @@
{
"Comment": "A folder tree cli wrote, captured by TestGoldenCapture: the encrypted data is the client's, its key packets re-wrapped and its signatures re-made with the keys in keys.json.",
"Label": "cli",
"Nodes": [
{
"ID": "link-1",
"ParentID": "",
"Folder": {
"Link": {
"Name": "-----BEGIN PGP MESSAGE-----\n\nwV4DC8CXHujYN4ASAQdAVCWynZ4Ws578li2WEIKaTFx0a28laac8Q4y480PhQwYw\nrrwUKsATjjjZHNjU60zBAZsyibShEyrfP3G34OU/qWHSilsLAL3UeFmIb290neu6\n0sBXAXvvlSSMnGjcRsD+d+eYvEgcoczww5Zr2RoOiGHMA0jTAuxh3/HgGGjzP9KM\nWdzIEYaLFEc+HsO7QcpChbO6AfFGrzzC6fOXj4XOqmZio7zR3EPv3jHXqk92y6x1\nRdT8MBw7LLqzGRUPk3hKQ3e26vtpPIlYwjQsgmajf+Vwb9T1BtJKYMmR+0kaAGHF\nh+rcq5G9qtyz/Rx1I/pU02AIC/bd57Vp6rSAMPjw+rF64OhJPDmOoG6rjdawOrvz\n/5Wmu04GxOVVhbSlqK2tfOml0RPW39TD0txZWfL2iIobpVHOFXmYjqWjnTVNV9ai\nYE4MmvcYHmb6LK/lMuxVtJSQHRzqbEFg440w1aW+dwqr61G77NQE3UPm\n=CrKu\n-----END PGP MESSAGE-----",
"NameHash": "2b4a9b35edf57ac4c20fadffcc0699aa47e35de65a62462dc2f7921efa8c8986",
"NodeKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavWdyxYJKwYBBAHaRw8BAQdAulKMYMn3ftjv8EbhL4LPlCghXtsB4upOrikr\ntdSvhm/+BwMImdBBjTbsZdPgCmpRj6uXfVFGrpxeTwmlJIqp/vWcAo4Fkhu3ciMD\nTsvhXlMF4PSN3JB0bs9ave1fv0edpVYfuuxobEyplWsO7/u/+hLgkM0fZ29sZGVu\nIDxnb2xkZW5AZXhhbXBsZS5pbnZhbGlkPsK/BBMWCABxBYJq9Z3LAwsJBwkQhgWK\nhuOqBQk1FAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ/N4Ok+M\nMo/7aNeC9+TNU1gCFQgDFgACAhkBApsDAh4BFiEEl5I219jVL4IkLk69hgWKhuOq\nBQkAAP+yAP4ndOJRRMPm8bFr2q30hazPl2ZG/6h51st993O0aiy63gD9ECzejnqI\ni5J90M8MnunYrCno1/pVjhZjQAVYycNmzgfHiwRq9Z3LEgorBgEEAZdVAQUBAQdA\nKc5oTJGdyYBUPgEVw/ZEKJ5AzAR55F6L76XYQ831aAcDAQoJ/gcDCJnQQY027GXT\n4Bh5iomv/s/yVQ9w/Pb6MgGRaQB8udlhAHLEzwTRNboLYm68rXTxraGrvJoOTcAB\nqbidcoSD5/3fxAOWkAEYpAUSfvv6PrLCrgQYFggAYAWCavWdywkQhgWKhuOqBQk1\nFAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZzDwqhidrzqSrO0J\nU/jki3wCmwwWIQSXkjbX2NUvgiQuTr2GBYqG46oFCQAA4nkBAJ+rZmGuzEYO4zHC\n5ZJPxb/GnX7/xHyiizAECkt0tWLQAP4qfpSm+CxmhONe18fKRx3XVjwo6EGx996O\nU1n/AgwCAg==\n=lSeB\n-----END PGP PRIVATE KEY BLOCK-----",
"NodePassphrase": "-----BEGIN PGP MESSAGE-----\n\nwV4DC8CXHujYN4ASAQdAfkWEN2fIMJmG6UzcTe81wFDtNwUAsCZOGDukHWPokRsw\nlgrkSJCZ3HkKw++Z09FztwVcyRtd18m50FJnm494eN5SztcWvoFs4/zRuPREAoO1\n0l0B4cHmcy0t9AG4tdCkoVltnePRrNoCWUDJ0tmm7RFZB1vM4gCZnf7lyo8bFkVy\nNJaTRy2UETGGodSpLFM6cbdjjcJuW5KVNEwzr3FlL2toa/TX3KMslcrovcru5i4=\n=FsDy\n-----END PGP MESSAGE-----",
"NodePassphraseSignature": "-----BEGIN PGP SIGNATURE-----\n\nwrsEABYKAG0Fgmr1ngcJEHcN5Kj1npDbRRQAAAAAABwAIHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmek9UDO2r6B60LL1Koime1yh9+qzLuuBFIWEUpKidznmxYh\nBCtnJ6qZmUV7euyTf3cN5Kj1npDbAABFFAD+MgdEGeu7giFRdeCRfs7aIyeyPAdU\nIacUPcMNn6rKSQkA/3Pz/qArb6gqFfsHhcMOHrbZFyhoKxw41EQ1GbabqyIP\n=sOfZ\n-----END PGP SIGNATURE-----",
"SignatureEmail": "golden@example.invalid",
"NameSignatureEmail": "golden@example.invalid",
"MediaType": "",
"ContentKeyPacket": "",
"ContentKeyPacketSignature": "",
"CreateTime": 1794481672,
"ModifyTime": 1794481672
},
"HashKey": "-----BEGIN PGP MESSAGE-----\n\nwV4DMsmw6Vp9F9oSAQdAFdmEPiq1DPeS1/IEcv28qTPvAuIKtK20PP7GR88HImMw\nPZdNCzmu8NtbyW7tOHUlDEY/KpTDwLuD1iuYMsJVYV/taEjWbILw9PdZhjdp5PQG\n0sBpAQUrEiwIl9jnCGfad4fQ8Ctls1IRckFwdxChKaxCaBr2OgyPOa8n+P8oY8Gv\n0uWYR1vURqPqd56eiyCQ3NO22HGeOMQNqyKDYNkzqfwq33dp+twjRjM42bANybqd\nEzHUQVsM6GDA+6lzhqbc/dUyoh6/YxX4Rc7R6pjjCh8b4JUHpefnAEeLIagyhIRa\n0gkvwtkjm+LC1W+RnOvvMhI+/b2ADGzeoTauTT1FCCIP0uxXEFPP3PBxPximgrXo\nhCRdom7UVoVm1uZqI9mN50MvXCn90p2KFXjF3VOi/GyBriBkqtsF8eRTDSC0IgTr\nkD2BfLjN0a8GH4s35ZP/DeSqVvru1nDsHgX7/i7FNuo5EMqru7x5h8mi30FUuc/N\nx5G8xjRrwOzWdVW3\n=+Nga\n-----END PGP MESSAGE-----",
"XAttr": ""
},
"Want": {
"Name": "rclone-golden-cli-20261112",
"ModTime": "2026-11-12T12:07:52+01:00"
},
"Rekeyed": [
"NodeKey: new key of the same algorithm, version and creation time, user ID golden \u003cgolden@example.invalid\u003e",
"NameHash: made again with the new parent's hash key",
"NodePassphrase: new content, encrypted with a new session key of the same kind",
"NodePassphrase: session key re-wrapped in a v3 PKESK",
"NodePassphrase: SEIPDv1 data packet encrypted again with the same parameters",
"NodePassphraseSignature: v4 signature (type 0, SHA-512) by the address key made again",
"Name: v4 signature (type 0, SHA-512) by the address key made again",
"Name: session key re-wrapped in a v3 PKESK",
"Name: SEIPDv1 data packet encrypted again with the same parameters",
"SignatureEmail: replaced by golden@example.invalid",
"NameSignatureEmail: replaced by golden@example.invalid",
"NodeHashKey: v4 signature (type 0, SHA-512) by the node key of link-1 made again",
"NodeHashKey: session key re-wrapped in a v3 PKESK",
"NodeHashKey: SEIPDv1 data packet encrypted again with the same parameters"
]
},
{
"ID": "link-2",
"ParentID": "link-1",
"File": {
"Link": {
"Name": "-----BEGIN PGP MESSAGE-----\n\nwV4DMsmw6Vp9F9oSAQdAC8NUD6JZTctrbyKmHE2Wq04F7sv9rfmdzYXRIVlb/Vsw\neP0G3IKAgTqjD1Y3NtBVaxgspQg8yRfdC9PWfdzPjOy+74RURel6yqBkqJeHrjr5\n0sBGARj9PCsxwdb17Sp4SZ9Wrb6dvmI7+j1a378XVh30TyoGfR3CEgMUorVNnHfS\naq2n3YNo9IiDY4RA7tXlPIcLp/3D7VP7c69UG8wJ4XdNnl/3AiBuZV01JGRPR54A\nfTyTo/SSwZ0pgerXc5a9nQg05qSS7vtXwnBqOsX00+RAuW82ORZGBpxwdt9rLWrN\nXTUsf9fW4coX8IaVzySPPnPrBaXVgBn4PJpOZWjn4Eswwj0O/LqyC/NuFKWxQsiO\n7XFm7P++QE/jUjPIR7mQiXXGvZ1xQRVlS+fkAEghPsbRp0JS3lbp3yd5d+CpsbHL\nirjGCxaqxUxb6KQfxm/11tMuCQJWSbGUYA==\n=4r6S\n-----END PGP MESSAGE-----",
"NameHash": "72f3641700c6aecfd7ad80e9b96ab4cfb0060616b3e2fc6862ecce513fac0e56",
"NodeKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavWdyxYJKwYBBAHaRw8BAQdAdCOugFgFFjUwF/ytzyfBuMFDIs/29rtHOMCZ\nKsvdqi3+BwMI8JBYANoiBO/gEGnJVRMzsuQWIorM8bgLjR7Nm9kzEkeKB3BcoiSp\nx7QpPBOxRoTxB4XQRLHL8/Up2zscGyLvfPovfD6E1DEPcIpmEmxF3s0fZ29sZGVu\nIDxnb2xkZW5AZXhhbXBsZS5pbnZhbGlkPsLACQQTFggAewWCavWdywMLCQcJEC7Q\npZiVfnssNRQAAAAAABwAEHNhbHRAbm90YXRpb25zLm9wZW5wZ3Bqcy5vcmex3hCs\nFunCVj9h8MhMF5/tAhUIAxYAAgIZAQKbAwIeCRYhBPiFzK9h2iWtxo6irC7QpZiV\nfnssCScJAwkCBwMHAgAA8iwBAOV2n+cJrVo2VqZxL53qs3wNfr6yG5A9I3ka9YC/\nlAgPAQCI6iujwnZJn1OCrA8mTpbGEQArrN6WCQPmXD4L9YfBC8eLBGr1ncsSCisG\nAQQBl1UBBQEBB0AJ5ah613aN4KShInv2+nfAqisfju6/IwFGjC2ujH9oYgMBCgn+\nBwMI8JBYANoiBO/gNcRTgHPCNOwQEC4pPqYAu/uikiAG+4xORaEzTMzqYjqSdby2\nKjKWyn5TJd1hu+SyRwK+ARwGl0+JpZ3HI8gqGJAuALS2E8KuBBgWCABgBYJq9Z3L\nCRAu0KWYlX57LDUUAAAAAAAcABBzYWx0QG5vdGF0aW9ucy5vcGVucGdwanMub3Jn\ncATLrFwrSqvvGRd64T/eLwKbDBYhBPiFzK9h2iWtxo6irC7QpZiVfnssAACGWAD9\nE2kjFlVk3Vs4yg+kuYKIUE8kHg7IG4d5UO9p6w8HmMAA/0Xm4LKfArpMYLZZ4quy\n7FQO0wsRYR+PTfZlq0k5hh8M\n=d3I3\n-----END PGP PRIVATE KEY BLOCK-----",
"NodePassphrase": "-----BEGIN PGP MESSAGE-----\n\nwV4DMsmw6Vp9F9oSAQdApoI55bosfVIC4/9wqXnk0rTugDAkNcoyQvZl5p3Imz8w\n7aRBsUUVHf4dFhVikzFS6vNSJo/MOEdrQkh55Hrfjg4SKwOk9g75WSS5bqfmZ1tq\n0l0BOkMCFdTfSjmr441C+tpxsngxizmbS3nUuemz6fULU7hyCM3UiwOE/ptdHNs1\nYge8H8zcyIbMANu2d5fZjKtt+NFjg6EzpN1BiXRK90GDIfXBfNRcDBD/ihCDDJY=\n=yXbX\n-----END PGP MESSAGE-----",
"NodePassphraseSignature": "-----BEGIN PGP SIGNATURE-----\n\nwrsEABYKAG0Fgmr1ngcJEHcN5Kj1npDbRRQAAAAAABwAIHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmeJGtOOsHzCHLkeMyuJuZ9MgPR5j92NYzblAfD+vItUXhYh\nBCtnJ6qZmUV7euyTf3cN5Kj1npDbAAAXqQEAyPSvJwmcWmPh3ALvAtG4nmj+HXi0\n0o+Sa2JNXFTARl0BAP++uwOTI4IwDrAiRNnDqLSuT+W/o/i6oX8j2nzObSgC\n=MP/y\n-----END PGP SIGNATURE-----",
"SignatureEmail": "golden@example.invalid",
"NameSignatureEmail": "golden@example.invalid",
"MediaType": "application/octet-stream",
"ContentKeyPacket": "wWwGFQSiLbvpxpjyD4IUPRB30fGq9Ser+BIBB0AmevMprhlgl6kqYGJUfrRaNKPAkUpm0+rm7L+BRgO9EDAzLDfgFID6Ypo+rezGc847gzfBmRDEyiZYdvokI8IAoUppqtmiwoFS/Xopd7Hsges=",
"ContentKeyPacketSignature": "-----BEGIN PGP SIGNATURE-----\n\nwrsEABYKAG0Fgmr1ngcJEC7QpZiVfnssRRQAAAAAABwAIHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmcNZBbuzvtGk35zqYkNcXBMlSdpeLIckRtI2q+kdxDBFRYh\nBPiFzK9h2iWtxo6irC7QpZiVfnssAAB4RQEAiZW9gbsfpAV77+1JuWP927dUJ11a\nyOEQnkjaybu/jnoA+wfvAz2s6hqbVyAd+ecvjuyllj8JP83SE3E7JqZgBqMC\n=C/L/\n-----END PGP SIGNATURE-----",
"CreateTime": 1794481672,
"ModifyTime": 1794481672
},
"Revision": {
"ManifestSignature": "-----BEGIN PGP SIGNATURE-----\n\nwrsEABYKAG0Fgmr1ngcJEHcN5Kj1npDbRRQAAAAAABwAIHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmdvM1S08e1SN+ZV//Y5dNQcht1xOdpjBErb2ZgYH/KANhYh\nBCtnJ6qZmUV7euyTf3cN5Kj1npDbAADMigD/VH9yfR4nqu8FvGe6lSOYOypEkfZe\n74CISi7ZHUA1A1QA/REocDnh1/rTqnSmDsMhbwUuj7ZZYQ+mHxVA0ONdhFcE\n=qQIO\n-----END PGP SIGNATURE-----",
"XAttr": "-----BEGIN PGP MESSAGE-----\n\nwV4Dd9HxqvUnq/gSAQdAyYIhzyUG5ibTJo901Pulc0VUNTfm8gk+DeWXyOcixyQw\nxvk6zyVnNcXfwwQ+Uydx8N5X6UMaz20im27EMcQAbVOAKor5CpwZ/x2yYwIFCfsF\n0sDqAUFV7q/wEp/VF37RbXhK7cPO4pnT2uqKKm4fxeboTaCvFO9aIVkpqtXqHcKo\nKU040hDdjXq2yQsJWPm3Qn3kHr728SqPf7CZ/zsJuaX+7U+8CFQrQxmPAemNBOPb\nEDNsOlHKYAsGb/Nk1NARiKTT8CLptYy/OJOUm8AkwnLKOcHF+OQ/Nuo7BQDI3FcS\nMHmFtqSV95Pur4g3VI7WRlYcWpUBRUCzIdp6nEPInenTJQy6MhpM519Iv0cA4ahn\nn1x8pGeKeaW2nQjGeGJwtAhSEgUirxaLr+2Hw7fgZgnnqLyeCfUaLKgzYv9t2HeF\nJq45eFYqVs8n0dClDEGTmfNDZVRqVaGI5YNaLaKvD1NtJwm3pRmhy4JuaBoEhfzi\nnWsGeKCjj6cIpUc0D2jtzH/Iwx/WfVRaEl6inl/DGwUwajTjRWce8lLEBx4hvBWV\n5dtTZRUfgZZko7yIO9qvEDfCVLOLKOevqcbPWkQozvQpg4PvTs0/q8Gks/lTHS1V\nv/zYwxOHAiAS1Pms5YtejmEN8sHzYrFmXw+sOsD89+ipro0E5vm37kqZaCAM\n=uMeP\n-----END PGP MESSAGE-----",
"SignatureEmail": "golden@example.invalid",
"CreateTime": 1794481672,
"Blocks": null
}
},
"Want": {
"Name": "empty.bin",
"ModTime": "2026-11-12T11:07:50.956Z",
"MIMEType": "application/octet-stream",
"SHA1": "da39a3ee5e6b4b0d3255bfef95601890afd80709"
},
"Rekeyed": [
"NodeKey: new key of the same algorithm, version and creation time, user ID golden \u003cgolden@example.invalid\u003e",
"NodePassphrase: new content, encrypted with a new session key of the same kind",
"NodePassphrase: session key re-wrapped in a v3 PKESK",
"NodePassphrase: SEIPDv1 data packet encrypted again with the same parameters",
"NodePassphraseSignature: v4 signature (type 0, SHA-512) by the address key made again",
"Name: v4 signature (type 0, SHA-512) by the address key made again",
"Name: session key re-wrapped in a v3 PKESK",
"Name: SEIPDv1 data packet encrypted again with the same parameters",
"SignatureEmail: replaced by golden@example.invalid",
"NameSignatureEmail: replaced by golden@example.invalid",
"ContentKeyPacket: session key re-wrapped in a v6 PKESK",
"ContentKeyPacketSignature: v4 signature (type 0, SHA-512) by the node key of link-2 made again",
"XAttr: v4 signature (type 0, SHA-512) by the address key made again",
"XAttr: session key re-wrapped in a v3 PKESK",
"XAttr: SEIPDv1 data packet encrypted again with the same parameters",
"ManifestSignature: v4 signature (type 0, SHA-512) by the address key made again",
"Revision SignatureEmail: replaced by golden@example.invalid"
]
},
{
"ID": "link-3",
"ParentID": "link-1",
"File": {
"Link": {
"Name": "-----BEGIN PGP MESSAGE-----\n\nwV4DMsmw6Vp9F9oSAQdA6bpTzVBpo8JalizlpwIsBtOtVZVfcLgnXu/4dWnYhSIw\nCU4GUOWxJ1i+ozntudEXmzeSdJD7shBW7eDFoBnVHo8pYfp8KM1erRcoNq0brVOE\n0sBJAZt3K7D0TpZxLLUiqBzstNWDST5MF9sr02UV2h8JcvGGu99nMC5ZrlqZBJC5\nuy/1BQEOtrGtr6ATuQsLIkW2PWI7jnvbnHu8rBAtSetAbu+Zyb4uUjyqbZaq5FQ3\nRlJFb6IQC8TmeFosaDaZQAVgguP0MrQjwXHH4/78tDGgHR/6/WkX72rdFsotsnQr\nHxvQtyIrTzx6Lk7g0YtUn+ZHA9pAkPiwKVAFrPtZHdp5jBY5Iy4C7BNSbUHrYV4j\nObs7eddNYcguC54rvtVOSOhaLD+xp4IJrcZKpK4IpdbLrNSI/hi29ZX6ONjZFCfJ\npl9E9cSnJD60sHx/ngnQGYZ/JYYVtMBG4JHHmg==\n=To2y\n-----END PGP MESSAGE-----",
"NameHash": "e7a31618a78d64677ac080f54168dda356c7cb1f2d231edc8f8cce9b4a4bc253",
"NodeKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavWdyxYJKwYBBAHaRw8BAQdAFB1g1dCu6PD/Mn8LoB7njYIleBah27bvBADu\nL5FaMVP+BwMI2Dol/MEZ7c/gljP8eUxe82HBP/XPfBzauKh88+SyOqMu8gbhmObp\nL6pR2xf9r5wMVguLq7QMJaW67lPanYTmCLcEVgoxAOv6RQaoRTHG380fZ29sZGVu\nIDxnb2xkZW5AZXhhbXBsZS5pbnZhbGlkPsLACQQTFggAewWCavWdywMLCQcJEKyb\nb8LzyuTSNRQAAAAAABwAEHNhbHRAbm90YXRpb25zLm9wZW5wZ3Bqcy5vcmeE9LcN\n6ntUZTHo9aGwb9+oAhUIAxYAAgIZAQKbAwIeCRYhBM8/XBQ0SNElXzIXlaybb8Lz\nyuTSCScJAwkCBwMHAgAAUjMA/i7fMPE9nkPvDsRyVKDZi4YBZmPK4L1m8GK5d8yH\n2w4gAP9vOIAeU1L3nusk/A6VDNiwhn/MfnX0m21/k1psk7auBseLBGr1ncsSCisG\nAQQBl1UBBQEBB0BevZ4EN2dnAPCK3H12SBQyc4Vt52RQ8nK6b+jCZMEJHQMBCgn+\nBwMI2Dol/MEZ7c/gLS3pXSeAFeDU98itjLM41L+ZmXIdNcOwu50/lP2B1yl5MLE2\nDSVUhlfj69D0FvRIqO3SmfjwxjUO5scH+2qhRE9q6ReDJcKuBBgWCABgBYJq9Z3L\nCRCsm2/C88rk0jUUAAAAAAAcABBzYWx0QG5vdGF0aW9ucy5vcGVucGdwanMub3Jn\n2+p/IrS9Q9C2af7/1sFBawKbDBYhBM8/XBQ0SNElXzIXlaybb8LzyuTSAACB0AD9\nE1nGtVpLRccSMMVT0iefsAuIS3wauoEjzta3GuOI8MsA/1ADWpHZVWAXoGNVoSal\naD81YwSLx7wMhj73AJhCiIEJ\n=6dAU\n-----END PGP PRIVATE KEY BLOCK-----",
"NodePassphrase": "-----BEGIN PGP MESSAGE-----\n\nwV4DMsmw6Vp9F9oSAQdASGjo4QmMRBPihm7jkfvxYSjXAGnnjLYWSY2Y5PTVL1gw\nnAgbUA6QjorukRNF7V3fxr8v20tKAtpN+PNesG6P2ESzgqz4r7jZiNY2z95jr0O/\n0l0BPnV+gIojUlLF9BZrc3p0gJVU0zkvCEHfw7f+WE8DXjryYc2hnAljDVqrMU1p\nEkFdEAr3sR9qo50SZfMz5E9K5wU0V5XkYPpIBudgFfiXF5pJnU47ookjnBCeGzE=\n=tZhO\n-----END PGP MESSAGE-----",
"NodePassphraseSignature": "-----BEGIN PGP SIGNATURE-----\n\nwrsEABYKAG0Fgmr1ngcJEHcN5Kj1npDbRRQAAAAAABwAIHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmcSTfCei5wEKB6h88+ZbCFkKGhOSvFy5o37LdbQ29BkRRYh\nBCtnJ6qZmUV7euyTf3cN5Kj1npDbAABogQEAvWtAIE+LL3QDh64Z8k/M1Ooj9QfU\n4HQEfsW67FQTOocBAM0aHd4I1hCtEC9CDJWsGZsfcr+vXgDAKfC5xqVK7yAA\n=50U7\n-----END PGP SIGNATURE-----",
"SignatureEmail": "golden@example.invalid",
"NameSignatureEmail": "golden@example.invalid",
"MediaType": "application/octet-stream",
"ContentKeyPacket": "wWwGFQSFOo6NRa3E19B5MlVXqQGmn/ovHBIBB0A3WV6SsqClzY+tlbSWaHxbaL+X3vbC7NVqbg9PR73GRTDiTzh8r856XrWYU19ifpNzLg6TbrrKpmchEkWmNCsPrscWokAuJbe65UONo+H4CPg=",
"ContentKeyPacketSignature": "-----BEGIN PGP SIGNATURE-----\n\nwrsEABYKAG0Fgmr1ngcJEKybb8LzyuTSRRQAAAAAABwAIHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmeiIgRSWLrcezV7ZLN/LBkrzQAWwfpDHLSBsEOS/BlgdhYh\nBM8/XBQ0SNElXzIXlaybb8LzyuTSAADGAwEAngc1us8FCirbV/9riXTyD+hszCpH\nQNP7ZcGJ5N0+UWIA/jFfDLhFYU4sDbhmdhRTkwITwHEmQUR5TycSJ/hnMQcL\n=Tbsv\n-----END PGP SIGNATURE-----",
"CreateTime": 1794481672,
"ModifyTime": 1794481674
},
"Revision": {
"ManifestSignature": "-----BEGIN PGP SIGNATURE-----\n\nwrsEABYKAG0Fgmr1ngkJEHcN5Kj1npDbRRQAAAAAABwAIHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmeFgdD1dX/pQplrZLHAk2LC/LsMB2n8FGB+zMkWMBfAyRYh\nBCtnJ6qZmUV7euyTf3cN5Kj1npDbAAA2LAEA1V9x6x/nomWNygyD6b2GfbNJJPN8\nxrrBKzADDA/ZD70BALjrIVQfISsDBIlHEqzbyBzkA5rpdNJiFxEHKfgmYnwF\n=r/Me\n-----END PGP SIGNATURE-----",
"XAttr": "-----BEGIN PGP MESSAGE-----\n\nwV4DV6kBpp/6LxwSAQdACsb9JyqVMyl3yQ+JSyZ4X0AN1gaxX3WUuEM4OMsCIWcw\nPZAq5x2aqL9DLInFUG4WFeT1BS8fpOz6ZSKkytmZDN7IFHFguGtfD+/jPB2oxyVK\n0sEHASB/55dbWy8bozmUxXvrQDlsLheJesNwIzTLFqTm+2MbW2lG3DGfPMa4gPJi\nAk/2JzwyngOVEjlR2C5VJfeDx6Di0HVjcA8kGu8hctFlESgXIiYBvVPUYi0Y8DSV\nApOIcO52uQupzz63LJrhjw/VQV+ijSvrWBzCvBO06xiRPZkItztuu9IpjTuJo1Zx\nAn3rAH1M7SWPkg91jMlWy8dfCvbXnecEedVGu9XVoS8Uq3wgXh9Yaj6Zp4ZkZFo2\ng/9dKJ4ZeWOShi4dC1wncp4wqa/XaM3hGbdw5kfSEucVSnWWNsqlH5HSYWZzmVpq\nY/IjfqHF3W9TtPx3ZEnTglRffCrkf0uEbBE7qCa6GZOD8cqLZG1LCmc78k76rLbk\nCFymOrvLY8qzfC4ASfbFvTUYqXjlmdMfw4j7eqaeFQeQs/VmpciWD77Us1BjMoV3\nzgRIEME6w0dwxOiqYUIkdwduTuwwhyv6P8icg3vCuW6CIdYshwjmdmW085uIATID\nfeErYQurs6+tLBBu+4d8nGoK/c0eGJKSQ8VJIJERSG/23pW/aOWCOSttKrOtkdtk\nIkhsArFrEbwMfHlfIcqzfmDUPSjn9NOOtFI=\n=hJwq\n-----END PGP MESSAGE-----",
"SignatureEmail": "golden@example.invalid",
"CreateTime": 1794481672,
"Blocks": [
{
"Hash": "ctjxzA1hPKzU7YVJf8njbHChlhSnTb53l6BNXj47cko=",
"EncSignature": "",
"SignatureEmail": ""
},
{
"Hash": "kW91o7X09+8AyzU9Yoiy4239epE1/1na8HOQt+OWL0c=",
"EncSignature": "",
"SignatureEmail": ""
},
{
"Hash": "7xepALxxWiEfdwpYhj0MqoLTx/x2ciRgBbE1hNIDkUU=",
"EncSignature": "",
"SignatureEmail": ""
}
]
}
},
"Want": {
"Name": "nine-mib.bin",
"ModTime": "2026-11-12T11:07:50.956Z",
"MIMEType": "application/octet-stream",
"Size": 9437191,
"SHA1": "39c27cf5bba5da8ccb5ab470efb380c9341c8cdc",
"BlockSizes": [
4194304,
4194304,
1048583
],
"BlockSizesRecorded": true
},
"Rekeyed": [
"NodeKey: new key of the same algorithm, version and creation time, user ID golden \u003cgolden@example.invalid\u003e",
"NodePassphrase: new content, encrypted with a new session key of the same kind",
"NodePassphrase: session key re-wrapped in a v3 PKESK",
"NodePassphrase: SEIPDv1 data packet encrypted again with the same parameters",
"NodePassphraseSignature: v4 signature (type 0, SHA-512) by the address key made again",
"Name: v4 signature (type 0, SHA-512) by the address key made again",
"Name: session key re-wrapped in a v3 PKESK",
"Name: SEIPDv1 data packet encrypted again with the same parameters",
"SignatureEmail: replaced by golden@example.invalid",
"NameSignatureEmail: replaced by golden@example.invalid",
"blocks: 3 kept byte for byte; their signatures, which the backend doesn't read, left out",
"ContentKeyPacket: session key re-wrapped in a v6 PKESK",
"ContentKeyPacketSignature: v4 signature (type 0, SHA-512) by the node key of link-3 made again",
"XAttr: v4 signature (type 0, SHA-512) by the address key made again",
"XAttr: session key re-wrapped in a v3 PKESK",
"XAttr: SEIPDv1 data packet encrypted again with the same parameters",
"ManifestSignature: v4 signature (type 0, SHA-512) by the address key made again",
"Revision SignatureEmail: replaced by golden@example.invalid"
]
},
{
"ID": "link-4",
"ParentID": "link-1",
"File": {
"Link": {
"Name": "-----BEGIN PGP MESSAGE-----\n\nwV4DMsmw6Vp9F9oSAQdAMatEYS7L8qGawYLZwFMD8H+KfbmuaDr02rCfJTyx5V0w\nVmugXUagwAqbjry7GWzUEAvosBwVZo+YBGtECBRA6lmsGVZJ9U3EPbnZ1zffOBx7\n0sBGAQIe2DEfIoKGkx7UUIXk2Fs0m0yvFWfofIFfw0juXOOAdUCOCt2kX0tfxKjD\nfdN140hLRwJIiJjqqEDogjYyBvM6Kk4cweFOCCaf0Msu62NS5y9lex3bN3sEDkSS\nErIIpKV4pZzFmfS67io8eJs8SlkBZhQvzxYhq1tF8P930mzV6uTw5tRJUD3vDTbt\niWfUoufLWULn71u7qHSY5FYnrUjsPiogyth4L8NGbz4UpzoQtxHMACTKm2gne0fX\nRWUbosbH9ksi8o9nc92LzyfbbqRJ4OMl4/51D+wqTdoURUL5zMbRVmJtrdWH2cOT\ncsVHsw7VgLnxXQftY8x/WE36GKzr/5LMCw==\n=+FHb\n-----END PGP MESSAGE-----",
"NameHash": "772cc26af9940f978b18ec02b07a9c7da7f73ac84439891b163a9fcbcfb7d2e3",
"NodeKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavWdzBYJKwYBBAHaRw8BAQdAU8+czT99s/PiAmGDbdOh4RFs3KJOIbgQl2tW\nQRLk34D+BwMIkIekrECs5ejgDdTzu42p1A1BXxnKWpLZ7hfynmTIyqRZoU89tX+F\nJMY0Vp5rWJ84KZz5n9HG6BGu2mFP1GEXXpAzwS6ktCzQzB01z9d+ns0fZ29sZGVu\nIDxnb2xkZW5AZXhhbXBsZS5pbnZhbGlkPsLACQQTFggAewWCavWdzAMLCQcJEPyh\n46k0I/1ZNRQAAAAAABwAEHNhbHRAbm90YXRpb25zLm9wZW5wZ3Bqcy5vcmd+aRLq\n6r5IG60Q9cNERKUIAhUIAxYAAgIZAQKbAwIeCRYhBA3/cBz48qmSx92IIvyh46k0\nI/1ZCScJAwkCBwMHAgAAhTsBANPEJukaVTA1AZ5O5jQsYctfASgtxBbEbH1UidKh\n4UNqAP4jmCZVfDIfYo9uWgrycf7KplnR/FYEYq6RNjaU8Pn7C8eLBGr1ncwSCisG\nAQQBl1UBBQEBB0B4QMQnvDn/A1SFeHHYj3Gw1edvBiNvTEvEdZCCOLT7fgMBCgn+\nBwMIkIekrECs5ejglJAviEzcqBj5oi/aqYe+r9or0FKs0ujXHpGHRXe8gKcHR1bc\ncTWx2+4vUrcnkkBKLtvy/1Ez04LNAew9szh2zT6SGYR/pMKuBBgWCABgBYJq9Z3M\nCRD8oeOpNCP9WTUUAAAAAAAcABBzYWx0QG5vdGF0aW9ucy5vcGVucGdwanMub3Jn\ngC6tjIoMHKaTSbAsI8uZ6QKbDBYhBA3/cBz48qmSx92IIvyh46k0I/1ZAADZGAEA\n6d4TRAYkTg1t7hhrhXzZjsIPmwAfuI9Z5qdzv+7Nlj0BAJYvIK160ezWzkztIWcD\nwbaBgnUJFv4WECLUbHzec1YL\n=zexJ\n-----END PGP PRIVATE KEY BLOCK-----",
"NodePassphrase": "-----BEGIN PGP MESSAGE-----\n\nwV4DMsmw6Vp9F9oSAQdAsqsxYzkqdFBtM4W3SWv35+OZhYLWUQ+Vk9Ep7rc0MWow\nDHToM4ACfia0j+uUtDrCyLrEXAvGb2xsnacLTVAOsraRvhL6YqitioRScep9e8BX\n0l0Bs0ORRXGA7TMFM0Iq/D7oyrXyq4pyQz9qHzs7kcvn5BIbYZCjH4caEY5w/ZI7\nj4UK5He8yXzb7hRRxXc25eXWdj2W0rrC+Qoa3HCc02/+kzkrAcionOeZ/+n8yFU=\n=0yXl\n-----END PGP MESSAGE-----",
"NodePassphraseSignature": "-----BEGIN PGP SIGNATURE-----\n\nwrsEABYKAG0Fgmr1nggJEHcN5Kj1npDbRRQAAAAAABwAIHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmfmbJhr/w7RyB4hQQd+HoJstxGtWBh8mqSrIM1GxjYBzxYh\nBCtnJ6qZmUV7euyTf3cN5Kj1npDbAAApKgEA/zZjImgQoaRzrux89tYdB7OfHbWt\n3BWrNYiNqUSdQhgBALNYI4DeNsuTj34UzMQvIycsQjWSlu9v1kdBQlPvgC0A\n=bSjE\n-----END PGP SIGNATURE-----",
"SignatureEmail": "golden@example.invalid",
"NameSignatureEmail": "golden@example.invalid",
"MediaType": "text/plain;charset=utf-8",
"ContentKeyPacket": "wWwGFQQYYwxWuRINPXIAcr+F5z029VeY8hIBB0B+pOsmx0uPlIxhKXGTqhhPLT0bZ4//C5/V27Ce7p0UYTCL48dlmkLSV6woofxIJChzaop1FSWkewF1Bw/K7H1C1zsMSdzD4dWqAjdssFG8nDU=",
"ContentKeyPacketSignature": "-----BEGIN PGP SIGNATURE-----\n\nwrsEABYKAG0Fgmr1nggJEPyh46k0I/1ZRRQAAAAAABwAIHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmfE3hfEg2GB0fFrL3aLDLUGREWbmth7hdWwqmoF1D1jiRYh\nBA3/cBz48qmSx92IIvyh46k0I/1ZAAB/pgEA8dS8Vp8SBLjxDL1/XB9jPmeVaLNh\n6kcK8uU2UecAeUQBALYgscqL8cgwc8BHsVtAlb2+NFqVifHjc7zfDJeXGjgB\n=CeY5\n-----END PGP SIGNATURE-----",
"CreateTime": 1794481672,
"ModifyTime": 1794481672
},
"Revision": {
"ManifestSignature": "-----BEGIN PGP SIGNATURE-----\n\nwrsEABYKAG0Fgmr1nggJEHcN5Kj1npDbRRQAAAAAABwAIHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmecmke39tT3ONOthgxo96qDGxiHb3vkBRPlxtHN5iOy2RYh\nBCtnJ6qZmUV7euyTf3cN5Kj1npDbAAAG/wD/W8IIW27DjBGUoLthKiJO03pd1i07\nKWUZnYtOIhGnLrMA/jCmr9E672YvBAt+t9244IeMl0ZyerrFLJT9eyj0Q0sM\n=bSpp\n-----END PGP SIGNATURE-----",
"XAttr": "-----BEGIN PGP MESSAGE-----\n\nwV4Dhec9NvVXmPISAQdAnJzy+Z4EYv/zgHK2S94AMFhN1clER60mR1TzSdISpwYw\n7UAb9yqA98CN6PRHZFMGQ8yZrhNJn0jXCCwQSKGktFGT3mZkt9nM9RCPs1bBjbDF\n0sDtAauvyEydUBOmX28hYzec9wxVTyhLvanmYhybdEkrmlYBxxWWHa1Ti8jxpvFt\nneGccgk0jonYZA6qrUMTSCFq+TngnMBWGEW3SHJl2xCDFsMzNrVkJED5mtljvxh5\nHy2ic9UW8ww6HAgQ2Hcmw49u5jqyjQQZJzqxFeWRp4+Yk/NExy3Y/zQYPj9irSb0\nkaNfun9jvJcMawOV+OrjFzj+aMwLZXHk9MDhZ8XRrHRnr9yOMGJE0S6K2akfvWXv\nGZM9NZqo/pjrzQzBDm5k413Hnau0QxK/jsO1SnooWIX9sMc3DieWfHeIxSPtfK9M\nsOkkYWX2WcLzs/+UdwmTnX9KOqUfTormRpC+lv75UptkFFrTx7HnniG5/7c4TN8k\n5NQfc1V97V8/ZAn1ByzNuLL9I8edIXitu/MDHFoPu/lbXBJhnf+/lMV2E/Tfzacp\nzSqetfzbJGvNxFlTCcPqdhPy8kALHkSNdsXuo/8YacfiAiiGk6VTczjLDeg/pi4A\nvHTDnPATn1w9NDE7jd2Jnt8NSVwlGc0fMUECqyJ5r7/X8GJl8OPIVts3TND567U6\n=xB9z\n-----END PGP MESSAGE-----",
"SignatureEmail": "golden@example.invalid",
"CreateTime": 1794481672,
"Blocks": [
{
"Hash": "bk2FU1Wwg2aV4sRH3Ziam+HJZ/lTXVE4Z2FGh4MJIGA=",
"EncSignature": "",
"SignatureEmail": ""
}
]
}
},
"Want": {
"Name": "small.txt",
"ModTime": "2026-11-12T11:07:50.956Z",
"MIMEType": "text/plain;charset=utf-8",
"Size": 71,
"SHA1": "4f488768897aa1fb85991dfa584e8818bdcd812f",
"BlockSizes": [
71
],
"BlockSizesRecorded": true
},
"Rekeyed": [
"NodeKey: new key of the same algorithm, version and creation time, user ID golden \u003cgolden@example.invalid\u003e",
"NodePassphrase: new content, encrypted with a new session key of the same kind",
"NodePassphrase: session key re-wrapped in a v3 PKESK",
"NodePassphrase: SEIPDv1 data packet encrypted again with the same parameters",
"NodePassphraseSignature: v4 signature (type 0, SHA-512) by the address key made again",
"Name: v4 signature (type 0, SHA-512) by the address key made again",
"Name: session key re-wrapped in a v3 PKESK",
"Name: SEIPDv1 data packet encrypted again with the same parameters",
"SignatureEmail: replaced by golden@example.invalid",
"NameSignatureEmail: replaced by golden@example.invalid",
"blocks: 1 kept byte for byte; their signatures, which the backend doesn't read, left out",
"ContentKeyPacket: session key re-wrapped in a v6 PKESK",
"ContentKeyPacketSignature: v4 signature (type 0, SHA-512) by the node key of link-4 made again",
"XAttr: v4 signature (type 0, SHA-512) by the address key made again",
"XAttr: session key re-wrapped in a v3 PKESK",
"XAttr: SEIPDv1 data packet encrypted again with the same parameters",
"ManifestSignature: v4 signature (type 0, SHA-512) by the address key made again",
"Revision SignatureEmail: replaced by golden@example.invalid"
]
}
]
}
+11
View File
@@ -0,0 +1,11 @@
{
"Comment": "Keys made by TestGoldenCapture for the tests alone.",
"Email": "golden@example.invalid",
"Keys": {
"AddressKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavQ+PBYJKwYBBAHaRw8BAQdAsOznLcXFVFSRVUaH3qo5rys3OiRQwbtMZdLS\nafRdETH+BwMIFGmRbNl/t5Dgc7IAKj9uR/Qp+0d6UvLObm2j7uFmY/JiT0TmzGcy\n0z28V5VeJXxPKS2mXIgHGcHFthM/6oir0rD2eOaAjEqvCEH1NL7nbc0fZ29sZGVu\nIDxnb2xkZW5AZXhhbXBsZS5pbnZhbGlkPsK/BBMWCABxBYJq9D48AwsJBwkQX5SX\niFgESiA1FAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ8LZnPyP\n6B8T/JtLAxTd1MgCFQgDFgACAhkBApsDAh4BFiEEj06tdhNZBtSdxQPxX5SXiFgE\nSiAAADRPAQCSqkVAJQCYqqDwUcve+Ic2JDY6FqI5NhTYa9oNwuvJfAEA8/U4+QSf\n3L7m17fNVodJuqXyLtGAXj4R2s8NUrZL0Q7HiwRq9D48EgorBgEEAZdVAQUBAQdA\nqwqDKwlcjpTOMzdB8H2D4D7VVfLYXvr9CAZEW024DlUDAQoJ/gcDCBRpkWzZf7eQ\n4Dm5B4j9mEhDfCeUTa7FcfESDm9gQmL/bcqO2a6tYS/nFfnSYMnAziUMrotnPeFV\n58WTjD91PYdWnfuXirjFmtjXNnTEW2XCrgQYFggAYAWCavQ+PAkQX5SXiFgESiA1\nFAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ5GqVSI3uM+AyGGY\nFO72yw0CmwwWIQSPTq12E1kG1J3FA/FflJeIWARKIAAAvjcBAIwoz2rGW5meAhUa\n+ysq/MqVPYtygk6z9tlIGvhrJxCNAP4vrKesIguDbL5zHf5/siiv2UQh41tt5Nqk\nWrqKioV5Cw==\n=xp0s\n-----END PGP PRIVATE KEY BLOCK-----",
"AddressKeyPassphrase": "nvWWe/o5p5ieeEByEqGfuRaJNXXfteZrJr22WkqvHko=",
"RootKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavRo/BYJKwYBBAHaRw8BAQdARj1hfxg3SL9faiG9GeT82C/3WDbFk3yY2Jv9\n/zo57v7+BwMIcIdQScQjSPLgwNxM8qtWy98Iz8EXDAbvYf8+3jI1ZR4E8jwAYUJd\nJwTC4EUWdvlJ/k3X0liLYJHC5GaGpJgYzyZYs0P/6e9OhZ4RT21UbM0fZ29sZGVu\nIDxnb2xkZW5AZXhhbXBsZS5pbnZhbGlkPsK/BBMWCABxBYJq9Gj8AwsJBwkQ5PnT\nllRvlt01FAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ7G9xJ/j\nKCxEgBDA5b6TE50CFQgDFgACAhkBApsDAh4BFiEEHenU9PEZupJFwO4z5PnTllRv\nlt0AAEYWAPwM8n4hqlRVGzr6ho8dDBwmMe3gsbiHoXKHY+Tp6iDPvwD+P7YG7dCi\n3ImNbPWSFdXCFqCaf9Rfuko5koMaKDuFzATHiwRq9Gj8EgorBgEEAZdVAQUBAQdA\nzNzrB5jKURbG8jFlkAST0Ywn+ihk4Qb5nAGc6WIYPE4DAQoJ/gcDCHCHUEnEI0jy\n4Pe93bTMUt0jLmP66nC1c/ac4da9lNq6WZq1wdgrLtnqeLvbdGUyD/Ysojm21nj2\naiiGU3WY1pMs2EoZ68CYtEibR4uP7N/CrgQYFggAYAWCavRo/AkQ5PnTllRvlt01\nFAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ2ORKI/eh/s517M+\nvIf9MBQCmwwWIQQd6dT08Rm6kkXA7jPk+dOWVG+W3QAAe2sBAMQUH2VxtEvuQe/5\n/YIbsUn0GzsEQFQ7rAZg1ksrn5B3AP429RYv8Q4eUuta61QO67hWU8kERgP2PcPF\nGOvTBjMGCA==\n=vu31\n-----END PGP PRIVATE KEY BLOCK-----",
"RootKeyPassphrase": "kmPagbeact/lbELQyb2f/FUKXWTEdulQkKuOhBCit4Y=",
"RootHashKey": "-----BEGIN PGP MESSAGE-----\n\nwV4DGPXs2ttF5mkSAQdAGe77n4ZD663wlp6uqrVAJFT3FSJdA+zwVlbhr3XFsyww\noDJkCuU7wtIi/0a4KM+TVAnr3/JY62FzZIkKmx/rQ5TADRyqBFPt0eDir3IVny5B\n0sBZAbY2xnC/zccUXQYVLDWYdlP7lKkRTMyNmlXtecKiFIwYfCS6ygYTkKFhMkSr\nL4relY6ZhEa1pKMRliLOi88G8NctEi9vtLdNAHdhuElnrenBHaOz6lHE59I+0t3n\nuM9StK1FpAZVG4k+s9cxq16ywqQt3lVGrbv1Gjsp646IgbN+q/ftYGeRgbbbv8CJ\nz7IxWGJRYZvL8AOo4YfSerFVM7qCDy2XXLgPj1Vr71xVFrOi6p6mjJll/h+g/gOH\nmhOEAE+LFr9zRG19lJHFK9j8POS0W64FyPX/B1GjewlFYskw8bwI78BBJfaxkLHk\nlEUV1BYumWS/KI39bhEUYAYy7FgLVKEdSbwXIpCjv+xVSFzQRkaPBbrykrM=\n=Wj5Y\n-----END PGP MESSAGE-----"
}
}
@@ -0,0 +1,2 @@
��  c�ζζF
k ¤σρλRp�Θ¨Άπ*Ϊαϋπ`η»:y*ρ¬HξΗ;η\­ΝΏώ‡¬ z•Rx9ΑΊ‘5fx~Ρnϊn$—� PΔΫΦ›ayΩ›q1tyΉ*R΄Κρ#ΑzΪ½h`NfΣ)<m―t ΅eu"Θ…H�TΈ–β΅bVΙSΊQ› Ό™'#
+112
View File
@@ -0,0 +1,112 @@
{
"Comment": "A folder tree web wrote, captured by TestGoldenCapture: the encrypted data is the client's, its key packets re-wrapped and its signatures re-made with the keys in keys.json.",
"Label": "web",
"Nodes": [
{
"ID": "link-1",
"ParentID": "",
"Folder": {
"Link": {
"Name": "-----BEGIN PGP MESSAGE-----\n\nwV4DGPXs2ttF5mkSAQdAjv1Py6YKv0xa5ntdhDuvyA56877CUfUHPdoeMwVqCgww\n9UB4yaN2RAjz1vD1ESa1prPpSn9LSjLz7BUrlBb12Mzzoc8paxu5umDyZSWNc77W\n0sBNAcwBFLQ/IFxgy6PFCKmFKPYawniODwUxsqCQRtlUF7lqqE09AE8bmYFIzXYH\nkAvm9Vnnd6CixTGgnpXVCWPxC/fO5CCBdIIiF0iqp6ZYbKSBNBy/jmxLwLTchvT3\ncyXB6FJrmUXDwnjgwB8lLWIc1aObtHGr/NyYb5bpOeb+b9LNYC9pCkOsXv4vJCO4\nQ/FdjRM0IM0sv1e93VtLKghsZHrceFEgQIBV5ayXP4tC9BRlxGCc4tEJN2M/PIpj\nXGyZKTafT8FF+O3MpFpN1/kfSxubc5B5zYHBxxXmeVzaBXxPWw9DA130jwE6wADO\naUUdFNXqn5Xv6ThDk5KoNsvGTC6o/Tf/om6vsKfvOpA=\n=1iDU\n-----END PGP MESSAGE-----",
"NameHash": "1aec699a25b29cd854349f4db51025cdf07194ccda6c9414f7427f13cd6e1de0",
"NodeKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavRqERYJKwYBBAHaRw8BAQdApOz9j+Vp6dGa1yghnTOtN1ms3D6GLwLd8Zog\noTuYsKL+BwMINkkhYmrV4DPgjtWLpEWq1gff2SZPpRew+I14+0Zh7xjKJ0TmViWj\nXoV0HiYrL6LvuFSO9i95AkbXFMWHzTskfm7LBBUyDldbz73ODyZUOM0fZ29sZGVu\nIDxnb2xkZW5AZXhhbXBsZS5pbnZhbGlkPsK/BBMWCABxBYJq9GoRAwsJBwkQMc+V\nVZR3yyU1FAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ6z28b+s\n05/5BfqzTmK+hFcCFQgDFgACAhkBApsDAh4BFiEEnCbcE//QVcCZRXlkMc+VVZR3\nyyUAABJ9AQDeUi4H3RnjAc2V7tKzmCmP5bnIVqHMporcfapOMdppHAEAk83Y/000\njeArOoHiw/yLWuOnIchbQBap8Du2BU8OpQPHiwRq9GoREgorBgEEAZdVAQUBAQdA\nqk0yrljf9PLOdC4AsbRf6wwsE5N6uyOaIa11kVsruUYDAQoJ/gcDCDZJIWJq1eAz\n4B0P5nOHDqrAzZlNlCnlW5Hmz8DO1iQ5wvIODoOHPEWSMJ70ncJbsoVBJmajpF2z\nGBDMOfCRT3JICwcXNL6OhwD/P4z1pkbCrgQYFggAYAWCavRqEQkQMc+VVZR3yyU1\nFAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZy30fllhx7yxyeRo\ndJqI77ECmwwWIQScJtwT/9BVwJlFeWQxz5VVlHfLJQAAXjgA/0eHPltWW547814o\nTx9CTdbJsFeDNZbStEfm0oHTnoeCAQCkE1Pr1iPaxP9zw1S/56/8/ydPcyUPp9xi\noUz7K7yqBw==\n=MB90\n-----END PGP PRIVATE KEY BLOCK-----",
"NodePassphrase": "-----BEGIN PGP MESSAGE-----\n\nwV4DGPXs2ttF5mkSAQdA0kxuDcRRLu8Bg3XLntuMzUncod5R9xhJjeJQlspp5jww\nE/6creXAEmBzKRvZ0seescMLBrXpmFm1S13Yi1iWYmTDzDg3S75/akYFSfMha7Ga\n0l0Bbwyqclv2qR7M0VrNp5iKoVMmEmKeSVSyq7ALpzfxlAZHlmJNjkZrlARfN3J7\nsEOSNVWMHdCGKv9yqxFZ3KFRkLS3pCpXcXa4lWxNK/fosPWBqQritq4lzDVSM98=\n=F187\n-----END PGP MESSAGE-----",
"NodePassphraseSignature": "-----BEGIN PGP SIGNATURE-----\n\nwrsEABYKAG0Fgmr0ak0JEF+Ul4hYBEogRRQAAAAAABwAIHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmeznVHeSr/8EIU9NVqF2GqTqO86HqAISug+35Ab7EghLRYh\nBI9OrXYTWQbUncUD8V+Ul4hYBEogAACmnAEAzq9+/mf78Tu5V7MjlLIe9QpKqPGV\nrm0yD6L1ekPPuXgA/jDlNFWAFtwjpunPlf0lfrJthuufPWw0B32CvjWiRo8E\n=TObr\n-----END PGP SIGNATURE-----",
"SignatureEmail": "golden@example.invalid",
"NameSignatureEmail": "golden@example.invalid",
"MediaType": "",
"ContentKeyPacket": "",
"ContentKeyPacketSignature": "",
"CreateTime": 1794402893,
"ModifyTime": 1794402893
},
"HashKey": "-----BEGIN PGP MESSAGE-----\n\nwV4DjeI2jIiMS9YSAQdACQiO66XunmdiAbV7WNlJEKdP6M4YgtNyc0By/PCp7x0w\ntGhMU+1XIOClM/HKrf8l81iXi8QieBQSygwlzgdjnclMROKMWmjXSrti0+qWWsbu\n0sBpAUK5+31iO0JfNo6p69pEc3qaRxB+yws1fxq63MaCDxHFyExVjJdfWdaPeyov\n8xmcUXX8Mi0aRLB2aBkqdVfuqQjaKzrG7RRnbf4J9r+gSehSW7rho2YmykRfmP7z\nk4F/gEXdIZKhAvHznWIgMgkRZeYQSnF2/HEUxW7c2RK9XOhYhPEGK4RIKMf9O2qo\nJl614p+WXlTjAN+hH7aB02/ObPPosXc80vH9GGkaXWnEtN1qkryHXOtODsoQwl47\nv8+xF823U3kWx4kM0Ohhgi7DpMRDW/ERe0phbgpDhX46/jbzkf9Ikb80Xtbsk3MW\nRoHsQIL3KJKrzfAdzSNAptmcIFPDaWY91i33YqN8BctqwJbVLaCywXjC+Rmrirb+\n+S6hbNthHRSNyeBU\n=kXPH\n-----END PGP MESSAGE-----",
"XAttr": ""
},
"Want": {
"Name": "my-little-folder",
"ModTime": "2026-11-11T14:14:53+01:00"
},
"Rekeyed": [
"NodeKey: new key of the same algorithm, version and creation time, user ID golden \u003cgolden@example.invalid\u003e",
"NameHash: made again with the new parent's hash key",
"NodePassphrase: new content, encrypted with a new session key of the same kind",
"NodePassphrase: session key re-wrapped in a v3 PKESK",
"NodePassphrase: SEIPDv1 data packet encrypted again with the same parameters",
"NodePassphraseSignature: v4 signature (type 0, SHA-512) by the address key made again",
"Name: v4 signature (type 0, SHA-512) by the address key made again",
"Name: session key re-wrapped in a v3 PKESK",
"Name: SEIPDv1 data packet encrypted again with the same parameters",
"SignatureEmail: replaced by golden@example.invalid",
"NameSignatureEmail: replaced by golden@example.invalid",
"NodeHashKey: v4 signature (type 0, SHA-512) by the node key of link-1 made again",
"NodeHashKey: session key re-wrapped in a v3 PKESK",
"NodeHashKey: SEIPDv1 data packet encrypted again with the same parameters"
]
},
{
"ID": "link-2",
"ParentID": "link-1",
"File": {
"Link": {
"Name": "-----BEGIN PGP MESSAGE-----\n\nwV4DjeI2jIiMS9YSAQdAy1RT5JmyoKbu4zexRT/rd2BYJGGx20ua4lrLmwWKEiQw\nAuaD9uF446+/EeHj312E63yjCnpu+K2Vg7scA3tb0PKMbhxIe3YVBtCXsdG8S17G\n0sBHAelTk0SGOENvXeE0XIFwi0j0ys1ao6GZvtmFZnUddAiEMhPNiiqYySzTqanO\n8kOBGOjTz9ZtcZFFKepn/smOlF6+w55SM/j/wZzJCD0FMfhESOx7ld2eJpDPBU3q\nTHwLAPdtBWQvSygmR4IITQvgbGzTAFkazgWvjfb+b6AIIjrSFOZZ6MVPMetuemBY\n6zd1JSDwWgunoTeLaNV1aCq2fiU0L8u5g5gmXIfV8UnIhbra7WlmBuIe5jirAqYK\nZ2/FXKPGcLcHYFAu/A5/Auz0uVB8DEtXnhOWK+DScHQkMrDjFDoeXkhWVUjZ84xG\nNHXvEXtA+Q0D4axWSNsrhVDQYjKLlg0/UuE=\n=cK1p\n-----END PGP MESSAGE-----",
"NameHash": "ee2f7816bd5d57c533c1aa90f3615aaa3726c5b1cd110b690cddfacaacc4f225",
"NodeKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavRqQRYJKwYBBAHaRw8BAQdAaI8f/bJhfDpJk3J0qbrCcD8wYykylO+m7a5H\nbK3RDwv+BwMIye2S3scpSijgQSXYF6wfIVPIgPPSGYdPnZ8p3lUUsBNerdSHJ3P9\nUgzjlRHjiczay4LSk2PIR4gKRgVWfhobzqGBfFHFUjohY1PNxTrd680fZ29sZGVu\nIDxnb2xkZW5AZXhhbXBsZS5pbnZhbGlkPsLACQQTFggAewWCavRqQQMLCQcJEOB8\n/myMSsIPNRQAAAAAABwAEHNhbHRAbm90YXRpb25zLm9wZW5wZ3Bqcy5vcmf0Pg5o\necNTvKUK8/ZbQvB8AhUIAxYAAgIZAQKbAwIeCRYhBM3CCbHOgC83pg0vYeB8/myM\nSsIPCScJAwkCBwMHAgAAC1cA/3TzPE0bdWk4US0Ha7EsWykVDunT3uUHYjXUtpB+\nWqHFAQC3ZGml+y2s/+RD2Qc8mWt4DHgvf2Gw4XNwJnv4SCK3CseLBGr0akESCisG\nAQQBl1UBBQEBB0D9eG5JZddZAPxajMLZOTOlNI0rm8b+zsVVLwkQ0YrtdAMBCgn+\nBwMIye2S3scpSijg7hO/JcviyPE3zBM/Pu2HGw/KrfLgBNBAmjHG0aHtw/MhfgTD\nUv6Co/lIP9adULX5cY8Z17mWncFMWq4yQ/5JQvy7CFSDb8KuBBgWCABgBYJq9GpB\nCRDgfP5sjErCDzUUAAAAAAAcABBzYWx0QG5vdGF0aW9ucy5vcGVucGdwanMub3Jn\nmBRN5YKg7s4iFcbEuJF/gAKbDBYhBM3CCbHOgC83pg0vYeB8/myMSsIPAACGXQEA\noI1IZQhRj9Wh2SzkZjJEtJGC5xqj3uInHlVVFWYxJ2YA/j7ro/WmoEWYnlyE9qLF\nJyQTBATXKJ3aX646ZvgqpAEE\n=JQ9D\n-----END PGP PRIVATE KEY BLOCK-----",
"NodePassphrase": "-----BEGIN PGP MESSAGE-----\n\nwV4DjeI2jIiMS9YSAQdAmZFBcoGy1Uzu5EmfYuCxatQcokQJqynRhNLFBN0/Tgkw\nwcOiKRAjqagoG3E29eMEY3vZLXdNUk1eelCeJiMkimqH6lLZY6o+I0G7Y2Le/dLI\n0l0BkWJMed4QEQYv22v13gcq3vthwHeIaJsTQZspbGBiFaVGLO5C5BD65PgHQ4z1\npVXMDrPIu3gaZPo9VVLUDtXVfxMX2Vtyp+D72dzC1g0IvVdkaLN8voC9AwRTHJA=\n=cYbS\n-----END PGP MESSAGE-----",
"NodePassphraseSignature": "-----BEGIN PGP SIGNATURE-----\n\nwrsEABYKAG0Fgmr0an0JEF+Ul4hYBEogRRQAAAAAABwAIHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmcicLjwV6D2L3nubZLhNwumrSX361A7wRc+UsACfqsHExYh\nBI9OrXYTWQbUncUD8V+Ul4hYBEogAAB5SAD/a4BS8ZvfVyAOz73LsImMuXh6PU5w\nqpdypL3wv9wYcXsBAMPhamPNlQ+CJXj7ZKdBKkoyYxLHNZYxc0ZNb15YLXAE\n=UrGq\n-----END PGP SIGNATURE-----",
"SignatureEmail": "golden@example.invalid",
"NameSignatureEmail": "golden@example.invalid",
"MediaType": "text/plain",
"ContentKeyPacket": "wWwGFQSnLIQTXgUA95Q6F2xR3klEr6+aMRIBB0DLVgdDCcF6Ddum3oqXgZwJ4p8s5jspwe6NRAjmV242ejDFByn2QUAxrBm+Y8vXVUOdVHjpFxxgopgmw1lblDYI7vc7EyZtn7t7KiqIovyGwrc=",
"ContentKeyPacketSignature": "-----BEGIN PGP SIGNATURE-----\n\nwrsEABYKAG0Fgmr0an0JEOB8/myMSsIPRRQAAAAAABwAIHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmct5vykzPe19AeIij8SC7dccQgTXdnD7TqVwj+xE0i3bRYh\nBM3CCbHOgC83pg0vYeB8/myMSsIPAACUbgEAyagfoI1Q1aAki2aLeT0rGJ7oNi0G\nUk5aDoT8ZFcwiEoA/RPVX+Gm9I6EPb2s2hKDgDLcwyXlU0hr0ai5SlwA4hQA\n=Tyi4\n-----END PGP SIGNATURE-----",
"CreateTime": 1794402952,
"ModifyTime": 1794402952
},
"Revision": {
"ManifestSignature": "-----BEGIN PGP SIGNATURE-----\n\nwrsEABYKAG0Fgmr0an0JEF+Ul4hYBEogRRQAAAAAABwAIHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmcsYbIW3DX4VBgYltfHcjBIgN3fhkqhKU1qr7YtGTqiMBYh\nBI9OrXYTWQbUncUD8V+Ul4hYBEogAAAyCwEA+EBCXpCtPqEHJp0Jlbx8PCa2Uh28\ntkXfiyCU0wu+45oBANSQUFHucAq0UqspD6wHFcCBvE0jeZXbOQPcopr1iC0H\n=IYeK\n-----END PGP SIGNATURE-----",
"XAttr": "-----BEGIN PGP MESSAGE-----\n\nwV4DUd5JRK+vmjESAQdArd0mXTdoTVeffGXSbD/J/VmeZalNaabquXle71t/yhIw\nLZg6EOZu5AZ8PbzzrhgikBj76R+c0K5nycnOWhpm+1GTf9T3TkiyXxBrVPAExgGQ\n0sDtAbB2WXLbUL9+cuXcUc5E06J8ALH0R0x9U1pfALC155vnv23NpXJ4+ClD+YyF\n+pN9clIEnfqbgjrD5mqQ8dTmQDfhOMUI7KfaZVAbOOUAaAytQxOXmo7iHB5mmMuI\nf95ckYXL5EIBDVJzLkbcDJvPj3paw41++DYTd/ol9a2gvWdLd8MGpCH8saps2lAr\noX5ljZYoZk3z3wmItgGRW/Rid3/Z9yaKJm5ITbpEbnUGrAAaBVTUxIHTqH+5MqUx\nsdb/OJ6EYYpLtKuFwIRp0JFWolu/8Pvsz3gSt8IHd/pDL2S123eAe1MCtr65kC6H\nHwJ7Hc+ejgUuqhFvyIhuyQuyEC40bFcGHmzrb+/zI2lTGUu9pjtcqp373QTZ/dJ+\nlvnV8CyCu7h124Bwnn63FdTrV3mXyFebGmKMEBuwEXF+0uJb7GnN018Z8OYKCDOA\nBGXpMVYzu8eYjgtBijVP4bO2uI4Os/rDNITzexi0GV3TcBELZ87zq/qDRYx2D04p\nM5lnOIMXEDN/zWyucJsnbfwTuYiPvNUmbd114TJk0dAXylo7jvqysWX9sORBhW2s\n=9Hrt\n-----END PGP MESSAGE-----",
"SignatureEmail": "golden@example.invalid",
"CreateTime": 1794402952,
"Blocks": [
{
"Hash": "i2qnVyJAs9VK8ghYx//9yU1cnrfpJ+g8/Wo8MkxmZNg=",
"EncSignature": "",
"SignatureEmail": ""
}
]
}
},
"Want": {
"Name": "Readme.txt",
"ModTime": "2026-11-11T13:15:53.571Z",
"MIMEType": "text/plain",
"Size": 66,
"SHA1": "a719cb0b84f635dfbd6e4ebb9ab0430a337e1fe4",
"BlockSizes": [
66
],
"BlockSizesRecorded": true
},
"Rekeyed": [
"NodeKey: new key of the same algorithm, version and creation time, user ID golden \u003cgolden@example.invalid\u003e",
"NodePassphrase: new content, encrypted with a new session key of the same kind",
"NodePassphrase: session key re-wrapped in a v3 PKESK",
"NodePassphrase: SEIPDv1 data packet encrypted again with the same parameters",
"NodePassphraseSignature: v4 signature (type 0, SHA-512) by the address key made again",
"Name: v4 signature (type 0, SHA-512) by the address key made again",
"Name: session key re-wrapped in a v3 PKESK",
"Name: SEIPDv1 data packet encrypted again with the same parameters",
"SignatureEmail: replaced by golden@example.invalid",
"NameSignatureEmail: replaced by golden@example.invalid",
"blocks: 1 kept byte for byte; their signatures, which the backend doesn't read, left out",
"ContentKeyPacket: session key re-wrapped in a v6 PKESK",
"ContentKeyPacketSignature: v4 signature (type 0, SHA-512) by the node key of link-2 made again",
"XAttr: v4 signature (type 0, SHA-512) by the address key made again",
"XAttr: session key re-wrapped in a v3 PKESK",
"XAttr: SEIPDv1 data packet encrypted again with the same parameters",
"ManifestSignature: v4 signature (type 0, SHA-512) by the address key made again",
"Revision SignatureEmail: replaced by golden@example.invalid"
]
}
]
}
+29
View File
@@ -0,0 +1,29 @@
{
"Comment": "File written by go-proton-api v1.0.4 and Proton-API-Bridge v1.0.5 with test-only keys.",
"Type": "file",
"ParentKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavRGjxYJKwYBBAHaRw8BAQdAcxECZNQ4lVFMtNsKOynFlDJucZF6T2fiG1Xg\nAcPbsBj+BwMIW7QxRJI04C/gqBs53KUbuGjmEhYxMTOnCoFiPLLNk8dFZMWeIuHY\nj/c6kqk8USn/PTta1KGFW2wVoT1t0rGehRoZxCmh/EOKi26tzkMEls0iRHJpdmUg\na2V5IDxub3JlcGx5QHByb3Rvbm1haWwuY29tPsK/BBMWCABxBYJq9EaPAwsJBwkQ\nOW3ol9vsq381FAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ7sH\nDVufUE3ZEidEYWxokZACFQgDFgACAhkBApsDAh4BFiEEFieXN9ibAooUurRPOW3o\nl9vsq38AABPPAP9AUPozVu96istERmpB0nxNl6jZNSjQqYhFrFY7RuijpwD+I43/\nUyAYxn4yjdERbSpbJ1UIFmQ0eHxZARZRXBNJ4QHHiwRq9EaPEgorBgEEAZdVAQUB\nAQdAYmEyw9gdVGFAcbRmIbIZDZk/3L4MdouVywQj6ZWIxVUDAQoJ/gcDCFu0MUSS\nNOAv4A0vXnvSdZXU1vnnSMmDY7ckUn4M3xC3OacK59ho4XwGEZGYBHXE6DkhHxlD\ntH7Y1KPHqa8p4L/fZFLEIqgY0kKWRZs12z/CrgQYFggAYAWCavRGjwkQOW3ol9vs\nq381FAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ/f359je70Fd\nlw21Vr7EIuMCmwwWIQQWJ5c32JsCihS6tE85beiX2+yrfwAA+ewA/j2udkH8FHHm\n7FobVhNzMs4cuvJq8iLcLyb4He5kABMNAP99hX2S4NwPqTKcKALsifcI6U6BtpKO\nsPJKhapNINbNBw==\n=HkFm\n-----END PGP PRIVATE KEY BLOCK-----",
"ParentKeyPassphrase": "Ec2hLKXDjN5pVFnbZliDIgC5W0z3oCUFDrkPY/+sZNE=",
"AddressKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavRGjxYJKwYBBAHaRw8BAQdARWcSouC7sGNRuyVZoKs3SWWFYihHeq9P1Byv\nGkcqbtv+BwMIFdF5s0sA7VXg0qHXJ73pLNAbvFf1GrrtQYmrM8fZAbsfGaNgMczI\nK8onNODe4Ti5QaScmTEccWWcNWWmT/pHimMzeLqbK0+WwAKL3xGmWc0XdGVzdCA8\ndGVzdEBwcm90b24udGVzdD7CvwQTFggAcQWCavRGjwMLCQcJEAyZoc4ZKeB6NRQA\nAAAAABwAEHNhbHRAbm90YXRpb25zLm9wZW5wZ3Bqcy5vcmfqIiUAbBuc+hLJF/eV\n8LwvAhUIAxYAAgIZAQKbAwIeARYhBI0BQxJ3GY4DEE67cwyZoc4ZKeB6AABP8AEA\n/4zyw5U3FwgB/osSM8vswD0ooIxsXpAjxsWm4Wmzwp0BANkBBZEpQqXwinPJNFgF\njKdR3nQ6TaUdgbuLs7juGx4Ox4sEavRGjxIKKwYBBAGXVQEFAQEHQBa3CAAgulYq\nnJziefREc2nW0gRQPB8JZZ1TLAFD0rxeAwEKCf4HAwgV0XmzSwDtVeAXJYeuTIWA\nQYtMQn4+Kl5vu26nR8lUqYEQDu+h28XbyfnFTORCteFPEzs+fwmaPW3MjghvpY7H\nFMdUb5l9W2VhVXvApqpKwq4EGBYIAGAFgmr0Ro8JEAyZoc4ZKeB6NRQAAAAAABwA\nEHNhbHRAbm90YXRpb25zLm9wZW5wZ3Bqcy5vcmfl4YcfuQTTdndcPRHWUQKtApsM\nFiEEjQFDEncZjgMQTrtzDJmhzhkp4HoAANjzAPsEUwd1MSBwMYQlTQk57jHtDDJu\nHXam/ulN7MKrmqrteQEA/7u8NOc+UNuo9/DHp7so6thEdwSDIKihHeHsORQlxwc=\n=Tu28\n-----END PGP PRIVATE KEY BLOCK-----",
"AddressKeyPassphrase": "9sHbnSDCspkMZyKHdGAQ9B6zrYgbLHkWQqsECWvzmJY=",
"NodeKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavRGjxYJKwYBBAHaRw8BAQdAMdQ8eoAyMsrtLZG+Ja6KEJ4MMh2AUHQQr1TK\nLjR83JD+BwMIWJ4sfdaGNG/gtXp7upM3K9GZJi/YCLG0R0crgfRD3dB1SAK0srAR\nuGV1rjVmX+RdhLD0388bJGwe1b8cT78HrVgoyEl3rYbGh3KVofWC8M0iRHJpdmUg\na2V5IDxub3JlcGx5QHByb3Rvbm1haWwuY29tPsK/BBMWCABxBYJq9EaPAwsJBwkQ\n3RlbDelLl201FAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ50x\nFfdZqPwGy/8zDhp14l4CFQgDFgACAhkBApsDAh4BFiEE6Ll4lgslPayNe/e33Rlb\nDelLl20AACoFAQCkOD1psuolcg/OHsHgXOeFp8k+DEWhgyF0DsT+4W2WZQD/cGaR\nI94gxrk0d+/GVMEJ8K8cGgVUKk8bVokguOlZTAnHiwRq9EaPEgorBgEEAZdVAQUB\nAQdAgSEFuMxdPzSo81cQj65bZ0Byfzv8MZcCGkVlUmBhM1UDAQoJ/gcDCFieLH3W\nhjRv4C7ZRdYc1yUz0wptMrnqB+46s4MnlhxevozWeNnfm3wfrvF6/9UVpTA1WTXl\nhuySGG6+wQbOKiLVeUfG4ylNvm/CmrhwZvTCrgQYFggAYAWCavRGjwkQ3RlbDelL\nl201FAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ2XJ6f3YWAv/\nAQ+PhALqTCECmwwWIQTouXiWCyU9rI1797fdGVsN6UuXbQAAPCsBAKSfTYaoZyjh\nffeSWz9nLbjk8WGkcyjlrwp6kBcBwCWbAQC8nMN99x12EEplbWSCIZW7HpwqeZPw\nBXrKSDGi8oQ5Cg==\n=CTFh\n-----END PGP PRIVATE KEY BLOCK-----",
"NodePassphrase": "-----BEGIN PGP MESSAGE-----\n\nwV4DG/kl44TpWE8SAQdATdhH9ktXSlE5lASZIDs4WQhYeh5PYNIZeElref9f9Fgw\nakiTnteDGk5WsFbmLqqRJVKzlCLJmwzlK2LZYl0K/enDs+amgSiLmAmHUWQ0tJ06\n0l0Bjuc9mLeUqLC7HLpYxVaq4Sc3yti09iJ5nbKeRfwMkrGgF6kx7RXaS6yPWIuJ\nO2HKKtrfZPE31q+557sJroNzd0qxmrtkLGW9TU7l9mQAAL+vp9ZEcXplDUTkT5Q=\n=uYiD\n-----END PGP MESSAGE-----",
"NodePassphraseSignature": "-----BEGIN PGP SIGNATURE-----\n\nwqsEABYIAF0Fgmr0Ro8JEAyZoc4ZKeB6NRQAAAAAABwAEHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmfrNxwp4eaCYF7zWtW5log+FiEEjQFDEncZjgMQTrtzDJmh\nzhkp4HoAADOtAP9TOTKJycueysm+xuWWGKFTl6JITkgjixzCb0DdAlKhYQEAmXb3\nRU/ppZKbBq1CzdVSlrZD8cYYE8X/2VhveyYV+As=\n=3ybK\n-----END PGP SIGNATURE-----",
"NodePassphrasePlain": "2b2rA5XFrudVun9TZmVWlzBivMS2asUFUuPiZEB6AxY=",
"Name": "-----BEGIN PGP MESSAGE-----\n\nwV4DG/kl44TpWE8SAQdAJ2WiRvBRE3rxC3caxYNHgV9nQMbWVxp3HT4NAca7+yAw\nh/70OPPlroAvnXkL4QBheuNZ/W0iYNzi5hhRH3WUoDCj/R2PTOCWUtihv3zCPrSS\n0sBDAZ9HgQU1SpdB4IrUcl3jSFfd1mGtEfFwI96lqqqXaquW5yDrwoTegAzzNJY4\nBM2rYt4vwsvaVbYenz24c0LBGQMB1CW3mCfhcBFLGoDxwFSJmlq89KUhO2tOaebO\n78mNPVa3B+KQVDPC9MqO3ZD/OKGu9KC/IgCOgB+hzM/IoAmq0a+qGFWeX6wU+eEQ\n1bzczNTcZjCJPGphLfb5UNhOwlLifxlHOm2zGA8hyNOB4sKVTHNe179fnlLiA9w9\nJnN9kbzjla/aGH4wVkY5yFhyGGJm/99qxT+84JpKy1i0DWiuvLl37civmghgCp3u\n5MEljn/6L61m7eDSXclYXhwB7Y5YUQ==\n=Zlzt\n-----END PGP MESSAGE-----",
"NamePlain": "Résumé – final.pdf",
"ContentKeyPacket": "wV4DE2cZOOUo3HsSAQdA+HpQeTNWdV927TQjQEQ33kdJjv3M+P8JAorH4hoehwEwSgTIAKthsuZESyJAekahsn6pMirpgSg17ZCWjrTqJDxoEx1yqpQXTvLaX5G0vEAh",
"ContentKeyPacketSignature": "-----BEGIN PGP SIGNATURE-----\n\nwqsEABYIAF0Fgmr0Ro8JEN0ZWw3pS5dtNRQAAAAAABwAEHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmfqeuaugxYe/Ga6r44Dz7UVFiEE6Ll4lgslPayNe/e33Rlb\nDelLl20AAKq0AQDj9Y0j1qVMofDJOQjXCluf3AWimxB6nHifhzSVGK3rugD/W5hj\nP3BZ0GwTUSpI70RSF3qNtlI+4DJTvatFXOQ8Www=\n=pNq3\n-----END PGP SIGNATURE-----",
"ContentSessionKey": "ff21ed4de15ad45e6333155fe471a6230e104f69b89fdba4148f4282bc1f1c3a",
"Block": "0uoB0Lr9th+xnh0RYvc9L+7htwoLpGNs6lz9HAPTnBcj63IDpa4REH4ShlaCbi8eE8f3dQGBroWTyVgqoA1CnzSU+s3pGZAdA66AsgTGzQyBEKWIwMzBSaXN3Za68dFLV2N+Vf/wV6qgcJQDj/1/6AM6SRTGD+vvC8iGMX3sY0H6YVr6mnwOIUwdMAqpT50baWnZ2uQoAMjooFzF/BgPRoQuG7Sgq1D2qMZKYBsPE1NbEbcG4PmNQsK78P9iFIwLAdHlJSOA6NsgQ2k++w31Zrm+Jl4BqLo02zsw7Oz7yuYUgmYK5d6Mg0CTclp1EWm4osSQjdY8zyG/BKfK+V8ISy9xP99zdGHKc1iF++rrpI+HxcS2G0LCA1AnYIAFthTReCrea4o1VQJkcM9HXoKf1E4vYtD2yFPE8dPCkAUhwRMBVhQvX6Kr8aylzLmngIs56G8f1vSHYtY7mhUTVnLvQu58wvEV5mb++g3tkwMX9HspnZHWGRy8dOgO1OHA+HLnkE/YHGKbHYW2mfYgc9BGMI5GcJo1CVR+rDzhTfAc2UddvsIuq3rulnoeBm56egYBm9SCsyDva9FnsBDrUf6R6M+M6DzYs578SBqRCP/3ojN0J+1jKGa8xXffsTJlEetf6ydfFm6I+X31R6/hgdETWWQQb7OLOxajhzV9HPdYYrjY4Qb0F//CDaBjG4qoDbSqWZopq2qwp8vkpfdxA3bn/rqph6ACaZsiWcKwyzwUxKnRRE5NtN7c33HyZB5auq3gk+kW1g1o47g29g2UQtbVfQndMiR9C1kG5AXe+78pDkXNsx41lAMr/+ZHpgcAyl8RTn5CyaQAcZ06czLESgiDXZ2uziuS0RWeGhkVlTWIZCoLuKPF/oGalQwfKupjCaNU7VACdTJevovafWduBqyIUtHoCNpUAiatfv3xwlrrj40Gt6Zd6MPzEAt6hCOTODIvLmEQqHADH+6BwKUlE9JGmod6ArfQCEBCaH7XqfpVompGstA90iUp5QpSDN0Jc/dm3JiQPUp2Jru9brOWsnL3LZ31p+kMgDQPhOyvSdkKUDGnZC/1Wz9qUQug6cgctH5HhZTDUH6lyxDGt8IniMa9Bvg+Bfq1KlW7BMNUFfTCSOSAvMVhK144rZe2hXgFKNyU5Hbutme0ciEUDsXt11ykWQrosf/64O9/u4A5MBvnw2p/CGwI0J+eGm1KD7vOR1BuWkCIKZvQEpimYW603upJhyPLQRiGE8l2IcZcp4syJvUAYr+lEUtOJKf6aDXbR96wBX6iBlfuI5GVgr+2KNsRM9TNcntDTT0rxxWXUIqxEfFPmNHHlFVm21t2sa9aVohqB9GajaAQNJ2pMb4HG68n/A5XMtK+Qccbnue8wUcxKPOXbbcnveH51qX3ufTgFuGtdATZGXqKAqablyRxhW6Umix3ZVSO",
"BlockPlain": "jhkssnVxoBNx+hSsf98+/1wDc1Em3a816t6SaYH0L732514CBIsyY7lgn9uxeiQ2y+crfl2NxSh4faSSqGqh+A8RSOtYwkVj3IwubJP4rNpapz06/ojNLPUnChA+Zph9HMnpyo0SXoM5E0D7WwrQVYWXWZkj+G6rCjeP5j0JkBVILLdAtCGMGgAaB9p6zsniAGJT9iuFSo4HY5mzhOvKY3oICQijcOyEhJxJ5SVXV4XCsGE28UMLjWME1NjMlO0c7JmKb26NER2hyY77Qrecuqqb3yylWlcdq0qGSqa/39cm5e+THkSFMAJYpeiUy2gnBRsy51m7nVxbnmqvccHw1eNGBOeQJ2duMyLzxegHeXepbwRfmw+j5x22CtbLw4RLFuq2ICEv5EUvXlhfS8hQridBUOb6cmWe6PiehoNXaEH2pSuh/YeFa83IxXGXAhUrN0eA3ldU2+MxQt+gv6ECoFvvV2kyZ3m0jHsr3vDrqHGwmzcDBCZNVZzDFnHeE6e2u+fuzQe0BXUL7DTXG8RGJokt9hGW4G8SpyAkUhQInDCHwHJxOGkrxe/OWRURJ8Z4MRBVLc7RwxzF3wTAElSobjFkbMMTtsBkA5qIrDpDptGwiGN4e3VDHMMH40oylVV/9j4hJx4VG6s+YujLVtMw5oTDQarj+2wx0LD/N0dZitAqa/jhM9E3/j1r/t3pdOWQygJGdQOnibZ1Zslz2Nzw7zUcfR6WP/scBFC5jgLKsQ5w5jsde2j7TRVZ1SgVmSMd+aVrq3vil6NIiPpa7URRA1iJ4TUz5Oev9/lhbi772vrpv7N0/5hqpU2dyY4+09NgN38sMLFqtCr830ybEoi2T7o1drK24jxNXyQ5rUmpsHM1JUvsHvYfTT09Rh3FszVjhXCeqGtKH2sIeWRJKX+GUfKZPVI/CZezKx2Wq9F7THJ3rs/MuOu5HV7aEGZEoqnQ/r7rx/ZQalqkoEPgZX5DQD7RJDG/Q/SpkkqO76GaG3S7N7SchIdsvSgv/e9TTlW9+wetAjrIrBg4dfC7JDmTf+N9fiXiN7rDS0Xs9+5z6s1SHfnYfgPFyfRDHJvSvqQqBGd5qJQngEYEYfplKryWT2iJGkPt47IfQpm4fUQ+t4fDe29/TZjwu7uhEIb33QGx35KIOU+NMs9/xqU9CuiFbGuHCHtx3z8nvzNDafis2ZyBo6ZQkOn60uU1e+X3Fykim+CjzRAk1zg8C9L2CCctR9tWkEOqmyIlLNc59KWIMSmUZNrSul6OcoMaA5W9iPzq4Gk/sowF98YfSXJ8YFK84Ff61K7hpZnZTZ2BDzFtCRYOoL5xPYJlBxzf5gNqgHDyP4GXf81n10xWRPlyCm4kMA==",
"BlockEncSignature": "-----BEGIN PGP MESSAGE-----\n\nwV4DE2cZOOUo3HsSAQdA3D0mxEsX6sY46Ax/7YxBktkQmWVFp1rhE0kOA8Im3Fcw\n/vQzS+ARcOz1lWRVfNBWU7sN0sTEAwthazkdBpJmfP+fUgMmN+YaU6sgxtiodSsh\n0sAeAQM0RFbe5+HJGyGwlV4tjSPaBsl4W4AZxhcecEXvMEMVcn7fCxSdTDcqVI5d\nAn3aLGsmotd0Wq9xeY355AwP0DgRhlW4p0ygdV++mKxH8RNxV1iYJVYYzBTeXuC3\nNESgJSAtxgPGAENnsgYBMp6+f2gMkZkLCGyd93S/rhfq05/7aCJMXAZM+nz4DtFQ\nTPdBwEkaftGVU/bArJYVysxd99zjOuSLG8Ns90cyMkwCq88W7x0oiEhkP2wjMrKD\njULd3e6C6sEXGq48IU5Q2WQC5QRVjg+rcP5fNTCbnSOY\n=nNd5\n-----END PGP MESSAGE-----",
"BlockHash": "m5+liJFgVPWGYIPe2f5BbMTMC+X11GW94W38s69/7ss=",
"ManifestSignature": "-----BEGIN PGP SIGNATURE-----\n\nwqsEABYIAF0Fgmr0Ro8JEAyZoc4ZKeB6NRQAAAAAABwAEHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmdyrNWgia1zbMJCpk6+Bv9dFiEEjQFDEncZjgMQTrtzDJmh\nzhkp4HoAAEM3AP9/r2fJp/fmtW8pd46LN7N3rd8VdlnOofL/9F2puiwD3wEA7ySH\n9JEdgvpbRTJuvxV+rVa+v6Qg/BMQhH/IdvZzyAs=\n=tJ4f\n-----END PGP SIGNATURE-----",
"XAttr": "-----BEGIN PGP MESSAGE-----\n\nwV4DE2cZOOUo3HsSAQdAY+2NSlDFnAh0+btNiRFb5iCkadXpY7b19X2eSl2L7wQw\nBNSwrZCLPVYLjcvol22EiHnFGDZErj2HDL79ycBGd2wj6xa6ima2VSU+MBPk1PaV\n0sDFAVwSz9YT6Uew4fBSWgZZ6Uq+Sx8r6vP7OLxqO99RGMjnSNhSEHK4DZKU/qzm\nTewFbJQYMe6YwtDyIO7zkm/H+msl2tfUbwXxF/pWSagG06Zt5HKsNc/Nr8HC5Teu\n0SN4qKYr1yNbjtnKvhyCU736hi+H2lXKbeZtgQ0eNyynsF2taAb58Zgpu5rMOwLc\nO6jWtaAtOHHdWnSiWVFFZx9MASGwp7vFbyJ7kiTqVaHDa61bIvDluikIFvwdvADH\nxOng2vnY/jFnxhaPHj39R0V6O3yVhUgV/lmMPt+N5NNM0Oi55zSI3cfsN/9hbheF\nxCqb0Cy9tpNCsEI6bbWz0C0Z1vOSLcLm9CO1FHkICSzcqkhPjpqB2vQJoOeSh9/g\nwiQS6dTri89tVud0HrA8LSqOY7txqUMQyZkJ9HqCW0Df0fsEJkFDv7clbZlNZzWC\nlz+UnaJcrFnsmvL04TJWimHc95wWxI/I/1jTgVR95t6c3uGJLClXwfLM3fS9KF65\nZc/3XYbEcEU=\n=afPz\n-----END PGP MESSAGE-----",
"XAttrModificationTime": "2023-11-20T09:30:15+0530",
"XAttrSize": 1024,
"XAttrBlockSizes": [
1024
],
"XAttrSHA1": "1ce9dffe44e6ef3d42ae7866bab5cb5c53584eb2"
}
Binary file not shown.
@@ -0,0 +1,14 @@
{
"Comment": "File written by go-proton-api v1.0.4 and Proton-API-Bridge v1.0.5 with test-only keys, as stored by the server.",
"Email": "test@proton.test",
"Keys": {
"AddressKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavRvDRYJKwYBBAHaRw8BAQdAn9wJYMU3m+C4tvfsvhRU93tzuWRg310WMrdV\nsx2Kl4T+BwMICgcyih3EgXTgjvT1fS6rTeedqa5rUjieViZWwF1DULHxaKeQ/9NC\nip2/VVyd6BpmLjFxRS5lBVbtuNqclWyffNJB6PLi48PVdcRghoWirc0XdGVzdCA8\ndGVzdEBwcm90b24udGVzdD7CvwQTFggAcQWCavRvDQMLCQcJEDYqIcFTgR6HNRQA\nAAAAABwAEHNhbHRAbm90YXRpb25zLm9wZW5wZ3Bqcy5vcmdMNQHu/YNFFXJDfjYH\nttlIAhUIAxYAAgIZAQKbAwIeARYhBGxv8QxbcgWIUDOOgzYqIcFTgR6HAABfEgEA\n097HW3FEUM4p8QBfRygziST5G8Pdnta9qt/8K4FWp7YBAICE+ObxNiCrHaKFKw9N\n8ZtkygUpwLPa/9U/5jHp168Lx4sEavRvDRIKKwYBBAGXVQEFAQEHQCgylrutR9Kf\n40IBXiewlxsthm2s5Bb/Mf0WKds0xIgZAwEKCf4HAwgKBzKKHcSBdODoYR71RzME\nDPMye9wjrTDZvVN9Ce1+tnH35KvtqgTYpzi/65Uy0Sqg7hJO5r//90X6wMJTNnSp\n2bLBFY7MJRkPZpHYg1Hhwq4EGBYIAGAFgmr0bw0JEDYqIcFTgR6HNRQAAAAAABwA\nEHNhbHRAbm90YXRpb25zLm9wZW5wZ3Bqcy5vcmfMW1GevtJUOgpjFl+siG8BApsM\nFiEEbG/xDFtyBYhQM46DNiohwVOBHocAAOA2AP4l88/MhdP1y+MBfEy1H27j0d+x\nhgx159BCo5uDgrp0MwD6AycyNO8rXnRXpH8UXIbx1joU7lDPq2nqrW9m9V1ooAk=\n=vDgd\n-----END PGP PRIVATE KEY BLOCK-----",
"AddressKeyPassphrase": "LzLbQlEPpaeI1rAGLEuCW2Gi35bR7rV9kmOhGO1i1o8=",
"RootKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavRvDRYJKwYBBAHaRw8BAQdAkJLDdaqwDy4m5/Y2dacYwIkjkXAQxluj4igu\n68kVdbz+BwMIsmWbW1fOheTguxoV6ylBcmCr/3qb+TuFSTMkoqMwc84cRLv4xXzf\nBEzd5gAWhlixubeV6YBu3P63/3TTGzKxM3+nXLF6Mr/9GX/SsZ60Bs0iRHJpdmUg\na2V5IDxub3JlcGx5QHByb3Rvbm1haWwuY29tPsK/BBMWCABxBYJq9G8NAwsJBwkQ\nO8tFiQrA4nU1FAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ/t4\nmbz8DuaNQ1vT/RFIIwYCFQgDFgACAhkBApsDAh4BFiEEYjEiVAYTl9y+qSUaO8tF\niQrA4nUAACGMAQCIefwEX02ipNhmV9eBYi2vq4uzW9qV6C+eFr66Y4HdtAD/ebQy\n0PmEFOk4X0lIq9qweqVOhhD4Hu0UaFmQ9A9qvAzHiwRq9G8NEgorBgEEAZdVAQUB\nAQdANv99N7oVHI9Wpz3c6Zbyem/iLYdVGvMZQyOEFYvu6gADAQoJ/gcDCLJlm1tX\nzoXk4C2C+l3NxlfgX5WLrgOgO+YQGy1kk5Kl2IvQDinoaC0sZT8wfBn/gXufXQcK\n0lqzn1DgCKnr/YSZjSdGMech2WGbTAvxpZzCrgQYFggAYAWCavRvDQkQO8tFiQrA\n4nU1FAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ2pgeZSX9qPC\nKIxpuRRxFsICmwwWIQRiMSJUBhOX3L6pJRo7y0WJCsDidQAAkNQA/3p9r/crMHES\nGvfl4RIl9qHh6IcUbAduodC6SEwGCKUrAP0cfD3AKkgmtYBPFIYiW1g6i9cYT8f4\nYOw7KHzrzr/2Cw==\n=iBcU\n-----END PGP PRIVATE KEY BLOCK-----",
"RootKeyPassphrase": "AjtWb9qdq42Nys8cIQy48farxWagZEFrZV1f9H0Cx1w=",
"RootHashKey": "-----BEGIN PGP MESSAGE-----\n\nwV4D75G3Biss+HISAQdAWjqo2i4N2XgibdrHT+PAgad2HV0owQ/sKmmcnzEILxcw\nseO8euBxqvlvt1cEx/AtX5S/1Q+nJfq0RxG2nsLVWYt0jyTjyRZ2tNCoLwow9XNh\n0sBlAaTAs3K/ZUm/MUDS1GvLMtfgQbR1eLRS0VER++m+wG2FapA/mL9SM+3E8bIC\nT21SoACKmZxJc9VrCLPC2XvNbiPXVNcvdx2e8DJfs1VVxYe2P2F9a5N8gCSPkG/y\nS26vVlz42DEtt3EE/yqYMuY1AQGGxPohT9NE1uCOvcO/h/4MLqwR2SKtcXE1Tgcf\nQLvKPlLebzb5ZBF78bi+EXn+0dTM2reLCs5a3f5Vw3abL+b/wkSIHa97jjZpD8Jf\nSFJ8GS/bOviY6d6pJQafJby7/iP07zpkYq0kJObknao9PMy3j8POLXh5dmZIhWmo\ndJn/T+6sOuqNatF63ch8K4Q03E0NbyfnPcEgj5pGodKtTumulGPXRGBfvKVYhb06\nM5GtgDVKem4=\n=pGrY\n-----END PGP MESSAGE-----"
},
"Name": "Résumé – final.txt",
"ModTime": "2023-11-20T09:30:15+0530",
"Content": "DteMSjInE8AZ4F9UF/gjhwqq9DweL4x3dF4cCGrW8kx7WAa2Clsc2YbM0o+vbk2s6GcUYEMi62HpKPQKtGy3efgelagpzZAMK78/fSSdWXQp91NGrISUl/fjEeyAzQI1y3u4Wk03ntm43yf25CCD/2Hh8uEmHMttM4e7h6A09MrW5ro7XMwOQI362C+NuIlypUqbESZkrBnnu7ONrt2bdkYtQWDVqOKhfKs0vUrCJBaARJlgAlaXdy6vOKK7HL3QvTFYNDSeeuMERJvoXoShoZMpP5aNtDAOc4/Jvjadat1honfBerTZO0V6WsK1wQt4t9pV/US9cBHJeyVuJn0ChCd14GLRmXAUVz0PMB2N7QNiXbXZ6Cf8DFoSlZ3APIitSdTqoec51w+DQ+WphxYM9pX3xEnruGuxKgp3/rWisQju8l6dQe7Z7gzmSPnL6adIo044uuHQGpBwt7p2f6DPak6sdgtnjulwQpRLF5WemSVYpvWemxeBM2OVfyXMIvCymCHVTJB1QNYD8fB8d0Iw6pycqL/tTKUKvrMzXYDmafACsFsdUcsoLqFYn9Qv5JMtcxwTxWzit9fAo3tT9bdggM4jNgKM1Bu9C7M9fOJ5ZfanCOP7VdPcBon5XnqKz7ToVbl+r3C4EiwLpeFyz5sJFr7zmp1u0E35d85JyJoL3mJ6ko9TlkO1F76OoEBr8XHpXwfWXVozUfUlVcQehY5Q1oT4fPTrwcoDVOLg3Otxrcz37B5Dw8YSyoHKr9tE+xZ7SaFx7Oskna0p+Q+ttoy2GCIq7zjxJe2QZCy+pNZtg/RlhgJ/Qh6rw1TW+Yu5Q9UU7HonVt4NXuOVQF6T/ZDZ8m1GqXTn1thBBvqO5Fg3qUSlj7lHKhGCsFpjxZQtLLNzJIFMs/ujqWwq6pLONJEw+sUl4uFTy+PCAvWjDRlUhZ/3nmlQFl/8PX0nXp4Eg70oOWBi4l+f5lx7NJBpOIpimNH6qdsVI8+7MtnkGC36zyNlc3Zskz1HabOsLHwwUGAy0GmQH/jXX9MF35hUdOH13angj37evED+nYkUH5HWHh0vkDlmGDmye6ZkNHClSkuRIYKlhr5IANuPHgISD4H8Qwi1wfUpScM+rA6ecoq7AfILBPtVXN9K9hjR1HSfguEm77rK4hFOkbOb/QTnjjbj6hw7IeOyaMqRBjjLMTtsG8tXdqJPBZJ63CKmmdOJF7MS9zMiIMchZ1XufI4K9S51LyX5g+OM090QVvSzSo2quq4eWs9bj8Nkkz3wA61m9yYi3WZ9/obyJ7P0oOw++87egK5UryUh3y5+maPGpSwuYBXvlo8pqbDJ4stCtyfRtDrk/8aY6GgugygPphb6U/E8HP5MLHL1xeHLRxDw+7lebH/j2S3CWQWykltLBftQB8/FyoWHap97iC1yvz9h+lAxwlzK/KrIS2Qs11wIZFs2vciv4ocOqPO8FZVT+OB/rNh/XvMLVMb2GSaVXGjWaBGioLNd+FdsS3cR24/OjzVjecDwCkhH1EO9ey8+jQlz2Yk+3+xDViscwjqZ60FbU+0J4dgAy7pOTy9+6Gi52NQYu/Flvul5LT5fBDDoa62uIbhuxYpHsKD9vVpgtvenCRDmrYAA7QHBhAb0/hcYCN5RiLajxfChYLobFgGcf/z0t2fX8Zn90bkZ0H6FljeLEMBx0ttJtqBRDaQrK+o/3YpzRi4GPHxKvkFmanUPzZfWfLzwM0Mdou48eTFLjKzruOtzAMXStJ09R+QqaX0CJmBHQefg0as7Iyn96h0vtIbXpanb8pjZ/uxnHZVkV4HP+ztnNx2DFCbfMUkc1Fv6bamhOHyDSEL0c5R9T0LobllYzPNvRYC649LFtrGNwnFeeXMomx92DKPu+Nhlv1kp7NkjhiA0nz98C1+ELsEEB7Wh6ppq3eBLn17cFWbAELuER8JpjOBZ2rH9apC7Q+9/58BIo9rHsYcM6Yf/bpCRFoCbovEtOO296Cf/sMyS8ImKvIZBsTguSCu4nPktE1nKL04Myr0HQjXOK1sf+j3xokRTj8w/6SQ0OZRYS7TnMBuXsAvKVou1x8+jQwv9uAmqT0lZwJNBK8CK8ZHkZfMtFxki5f1YyzhUBAr8J4P8hJ0sv9mRDWCwp/T7K80W6XXAB3IdUOHvkCpWj1BuxbJ+wjWgRL7NCFAUHQkIKGGkthESSOOZSI358M6ZZwYpPr2y+x78BfQ+9tDJDtR+b7j7EdXIqf/ULuThBxvWX4wb9S7dsFWw+ur9tA6Fq0KSdNM4ed9l3Z3gHdGIwJ6o48ji1f9vvsWJXzjhEYJPJvfFb4c1gV9gr6CqNYuiYaoUWe6tNbGe+CPp/6saETXA4f8GzjGc5KsAyh2BFhS5p31xzjUAMyfZjLSidsLF3rtVwiOsBes+8//VsYG9hCBqvHLzN5FORCnax1/q50mHdwAzw4GHmF6ja9Cia5KzIxaBBTFcIlRCJdbExfj4PRzb7+Zdp1FaEVkPmkJPEtUjPU4c4GASwLnmcIkNz79ifE7Pz0ruYEGUyrwYVSjBKaaXJrDELbmxFYa9DbUyf18NZASP6ztRt4OeK2lOAd3PTIeXHYycc98bpZJbBabA/dj/je6EtTqN/1wXaH0OCVBIwqsDSVSgJK9Rjy5YAscUKLC+Z38i05cnsRbsBsRTyKalwgAB+hQ1DaRfw2hAVYD7SKrJQuSvXwL8PQ4wsbKQJn52oD6XuNhjcdGRIoGlRnKTsAjUv67srL5WEUzkPYNRgEYIq+EDjT9B/OfMBKH+gu2p8K04qaeS0BCrVYh/Lzf9kHuExcA/MDncTupkt0eJHibl2jQ+XJVavPAa6aQ9cLFkulm0NmNE7/NHb9C3ipbhTI9p83o+Q74L/ZvWPLW6yC689pf/tRBNJJcSN/YVIq2WbcMjCNxBiJsA2eCty6R4O0ICv18XsrrQU5lZP/PDiB038uAen8DPCH8xIQn/QKi9JD9mHhf8XUVmKL3u3656YuPvJ2faOjSGbZ9WfmY73GoN/1h4er2wbBmjKyVWYHlx81GLeYWx5rPbaLVaGxke0JaSIt2sS/pH7G5gX0g3gG0Il0VkWuFiJXiK8ifqWiLtutGUGH10ijKPijNNTC5VWvsKzZy6+s+G9EeCIEluuAiMklC0cTfdNLrWZ702hvh22P2PXaWp+tlknGI6Vw+ocaatdkaz5SKz9EdHzf0N/QsIDzqlFsmDHmQ7aElAFkK7KeIq43xjVfqU3b/fgPl7bJGuig6hv3B7csvPcgX1ZuuaFaknTErR2j+mVzBspVfkcVdLhCBLf+aCyenh22EGdCWYJf/gXd2fG+wfBQ=="
}
@@ -0,0 +1,12 @@
{
"Name": "-----BEGIN PGP MESSAGE-----\n\nwV4D75G3Biss+HISAQdAeTQ04HdHu3aYN20zc8f3lCO/wa8C9lOB73AP9okbESEw\nXEGACuJqMH9Ww87+ZrXvnepYfTgfUgTt1RmgIYOvMn9oLskG/QppvpCa4WIR2z3J\n0sBDAR95/Z8E+5hfeo/AMnGWrGXAXWGhOkfkCAAthgPA/DCQ6JpLsod2Jn5WF1kf\n0oHheYBjMtowHd8XcmafBdQ5+v/mzoEOdorl6i+q+U5gzTkiHVwYA+iNa7zwoEdV\ng+vHtmG4ZLeJeYRzrjLHQ8pfZvRARjikPVbFPN0k8Fa3ISW8ccBeY7hVsNLY98wL\nEX3z46FdxGb1Hn3MY7K0JITWYPK897AoE+RHypqTKfQtze/UJw0rEuFXSTWNFGcC\nh3TiifPxzIot4PSpoNf8UneQR8I5660Iijo3yt1l9E/4yaAazFeXdEXJ0ZCJ4ZvU\nC7yA+RaHQfFp6dF/FgyNYN1UAFUv5g==\n=Uv5h\n-----END PGP MESSAGE-----",
"NameHash": "d80859185c47f576afd1f7aa0e1a2dab9127c843b0d741a7602b4564fc37b656",
"NodeKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavRvDRYJKwYBBAHaRw8BAQdADN8Yzwsl9W83z2uIQZ4Ux44dKhvc/Qojx8c7\nTEbZ+F7+BwMIikWiUxjYUjbgAd02ez9abEkUMQrYhuycQJAoE0QTKrZEKDzp+okY\ngksWkWVYmjQhMnwB/qbdwV5oYYkRe1nPcsbhAdFiVtvOCypcl9tyDs0iRHJpdmUg\na2V5IDxub3JlcGx5QHByb3Rvbm1haWwuY29tPsK/BBMWCABxBYJq9G8NAwsJBwkQ\nkUBZTYL0eA81FAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ+Db\nYMKdZE69sZOTmHSwEi4CFQgDFgACAhkBApsDAh4BFiEERPv80oTZWPDRaMcIkUBZ\nTYL0eA8AAF1XAQCFiD0Z2XTx5XaCvGdq17JGAxCQ+grj5nQmtnDfJjQqdwEAytp3\nbrYVsGfHk0Tu2piZvChMokMHhcjNghFtFmKhywvHiwRq9G8NEgorBgEEAZdVAQUB\nAQdAW4iCSarmRG2nA3sr8gscojT6gfJkhZfQE4r5WUZ+nU8DAQoJ/gcDCIpFolMY\n2FI24H5TLfkSXpV3QP1or5Acny9UVCynWdnG1o6VGJLXrQwcJvSjS6m3sGx54jyz\noWDQylzqxe3EbQKcu1v/7PxFSP8xumCcVWPCrgQYFggAYAWCavRvDQkQkUBZTYL0\neA81FAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZy3CYMcltPWm\n/6tvXL3kLcUCmwwWIQRE+/zShNlY8NFoxwiRQFlNgvR4DwAAqwQA/3M0VdF+lqvq\n2IoRd7WSVwe/AlAvuBsEUsngfhWRL1pnAQDk9Puh0cuZnqkomh+y9SlZgDAwWwV1\nfwpjPOLA7xmbDA==\n=4jUT\n-----END PGP PRIVATE KEY BLOCK-----",
"NodePassphrase": "-----BEGIN PGP MESSAGE-----\n\nwV4D75G3Biss+HISAQdAlymniWYnrqy+gLH0izGJYV3OLaVWKMr3aKCcCSU5J3Qw\nFMx6XX1hETVuI1H5Smfql5FXPGyhhQQdJhj/zt0KWvXzxmoqAzQYXmuxqBUYyeB6\n0l0B7gkXd44uLjzPpV6K3BuT0a50rEA+jtM0Yx7DHkGQdV9nIDUCsiny/4hAYUep\neUFWHS7u/APBqkKQ8MG3NnkGkhQE2mnLSIlcKl0rzELsejfAeP5xD04r6RY0rzw=\n=AVcs\n-----END PGP MESSAGE-----",
"NodePassphraseSignature": "-----BEGIN PGP SIGNATURE-----\n\nwqsEABYIAF0Fgmr0bw0JEDYqIcFTgR6HNRQAAAAAABwAEHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmfzOVpw3MH8ZeApbtY+kBxrFiEEbG/xDFtyBYhQM46DNioh\nwVOBHocAADUEAQD53J/jV+suvOSZ27uuv99Dr9BT+X9JLGl5EdpIzWieKgEA4vD5\nGzxbMgz0Siz3yuVlnwXFt1rNI6oIEV5x+bxhBgc=\n=NRz6\n-----END PGP SIGNATURE-----",
"SignatureEmail": "test@proton.test",
"NameSignatureEmail": "test@proton.test",
"MediaType": "text/plain",
"ContentKeyPacket": "wV4DPENlinHdht0SAQdAq5Hb36qZH+/AGcTBA4dVAIMFaGu92geylFqmbS5LqUYw0yB4qPE7lUnVYZCysz+3bPCnxt/pDxx1Ktzw35ocUceim2ki+cr5b3mzc0ZVXhBt",
"ContentKeyPacketSignature": "-----BEGIN PGP SIGNATURE-----\n\nwqsEABYIAF0Fgmr0bw4JEJFAWU2C9HgPNRQAAAAAABwAEHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmdEBzg7W/WHkXZnjiSnKULSFiEERPv80oTZWPDRaMcIkUBZ\nTYL0eA8AAIwUAQD5O16YIXf936L9zUviRububNQZheSxkX12fDGSLCxiDAEA0sqA\nbZ4D7d5X/kg9uVzEzG6nHUYYgXlzXYkPlVuK5wM=\n=WNUY\n-----END PGP SIGNATURE-----"
}
@@ -0,0 +1,12 @@
{
"ManifestSignature": "-----BEGIN PGP SIGNATURE-----\n\nwqsEABYIAF0Fgmr0bw4JEDYqIcFTgR6HNRQAAAAAABwAEHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmcWDcsQsoZ9oxLWcCDf/2keFiEEbG/xDFtyBYhQM46DNioh\nwVOBHocAAHOQAQCug7+Y6J4HfxtEhgjzXvJPaU5RSKdJkZTJhJTXodyAugD8DbFv\nY1IDnwP7VOrfCD28G7zbx+7WCXWnixkS9XTZDwI=\n=YQiZ\n-----END PGP SIGNATURE-----",
"XAttr": "-----BEGIN PGP MESSAGE-----\n\nwV4DPENlinHdht0SAQdAtLl5oIJ9RSeGAO3n4w1r6iQVzrNj7Y4WNgL3hyWhaTIw\njAOU7a/gztg7Q9yyKLKsrRED5j9UACo9q87qlgJKGaOyW58nfYsLdLF/oM2Bl97P\n0sDFAermVanvd3hEwvHUaZBDp7s7pIzglXER9AEALmmz3sK7yc6axP3ilXYz/+QS\nHDigy3SOLNHVwSwhGNAyAVtR4KOVXWdrfSkoSVkMtVDmL2wcmEoz43clvfjmX8Db\nXquYw/pAvDRzz1CYin8H3kok0IYPvueP7vIoQwPqbwYuJcJtcOP5OUdHcL+GXiBr\nvAEv9cN9bfzD32le+UowmODCFBI6Mbwl0CsrSxSonAcOeun0WP3dWDQ/Rrub3Jgb\nVEEt2qiSKVy9e6dkm8E91g9QB5VcnS+PmOq6ZHWTcOTREWa3sbHjUPMktQIyN6x2\nw5a36TtsIjW5+CrCvQqVTSOOxvIYAZSd0d+t/KCcKZYKPIo8Lyo74RYnZ4WqwHhv\nBrGULEbrxkofFZugRYKN9A9ZGA0FYdU83FfyACW4ACEdkLC5Na5zvd0gEKUGhZDg\ntuM7yYQyYxSuBr4JXQ87qjXwdhRYLKdjSiHwE0qaxc2vuHknHfuP7XHDflh7zu/9\nB5Cmzq0JGRY=\n=IxS/\n-----END PGP MESSAGE-----",
"SignatureEmail": "test@proton.test",
"Blocks": [
{
"Hash": "NFq37D8SE2nGxYSHBEYd1VVxjxpjpWcFLh4XYx55x8I=",
"EncSignature": "-----BEGIN PGP MESSAGE-----\n\nwV4DPENlinHdht0SAQdAcw5K1cHCktkNtqI3nz+9A6hfL8nXF6wgHX1oPo7H+nUw\nRWZ8e9hYK3k2VqdYXtCVH8vKSHYUw2hqQ7NBWErFu0IuR3iM82ojiCWHSy9iwP9Z\n0sAeAUmlJSuI1pxhxi5KFeSSeyt34m6H+ZSqo1Bm0ammv/j9I2htUAlwD1MxQjWh\ns5qMzuIjmqAXddSl5rOxJ63uOi1oQ7PUXByX3Aa774uzRri/AxktU3L240K69KBY\nt1ZDPtcbFkWOHdpULGKyZx0M4t3U6uSKKoXCiqPg0URdYyLfQPd99QdO+iaT2IJQ\nxFqZFJeREqoMq/JdNXDFuB3YUTiY2U4h5Q3zZlkha1cucI0Vw3CtWuXCjGxOV2+s\nv4aTzgx3xaBVFYye6RvBvQ9H5V23MbLEXjFWSTw9+2QG\n=bKI/\n-----END PGP MESSAGE-----",
"SignatureEmail": "test@proton.test"
}
]
}
Binary file not shown.
+14
View File
@@ -0,0 +1,14 @@
{
"Comment": "Revision written by go-proton-api v1.0.4 and Proton-API-Bridge v1.0.5 with test-only keys, as stored by the server, of a crypto-refresh file: the node key, content key packet and its signature are SDK-style stand-ins made with plain gopenpgp; the old library wrote the node passphrase, name, hash, blocks, block signatures, manifest and extended attributes.",
"Email": "test@proton.test",
"Keys": {
"AddressKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavR2jBYJKwYBBAHaRw8BAQdAYxcvYBaGtyA+Kyk4og3+LZivUVrk7cSS1M9/\n4/RztEv+BwMIAR3oa1tvWYzgG1gdifjVo2uzul/v9tOLq+OHNTuRzFOF2a76fAYr\nQoCYBmBhisymd19Z3GeC9X+sOJR5YtxAxIpXbynpZ6yi4HZHuz2US80XdGVzdCA8\ndGVzdEBwcm90b24udGVzdD7CvwQTFggAcQWCavR2jAMLCQcJEOjFeJjESJCeNRQA\nAAAAABwAEHNhbHRAbm90YXRpb25zLm9wZW5wZ3Bqcy5vcmercZpFiE6lJB7Dnzfo\n7CGFAhUIAxYAAgIZAQKbAwIeARYhBKLw1m3WxlgHlQYU5ujFeJjESJCeAABHvwEA\n+Z4fN4i4gGrvSTLIvWbTN5Ei1otzunUyaEFg4Bi0L/4BAN7Y4RxMfxXIAvXDDRcO\nHUgMaPny3l8BfSsSszRfcZcGx4sEavR2jBIKKwYBBAGXVQEFAQEHQBk/eEB3QpmN\nWzJB41zvXiWkUzsP0HzQaNAAOPjiHy9jAwEKCf4HAwgBHehrW29ZjODxy5OKTsy1\nSBIbMWlzN6H1fjtJUhWho7qXVu7AAixJen+j+FsEnR3ADZ52cpVsKmbHMfSEuesj\nzPqpQwYvFzeKKqS3TwwCwq4EGBYIAGAFgmr0dowJEOjFeJjESJCeNRQAAAAAABwA\nEHNhbHRAbm90YXRpb25zLm9wZW5wZ3Bqcy5vcmfUCpzU0sf+A8/15f86ylhxApsM\nFiEEovDWbdbGWAeVBhTm6MV4mMRIkJ4AAI+7APsHM6jxLF+DZDziuO55F67ZfqQ0\n4K46bhWMa6Zs+Eve3AEAiWgBlXRNJdAzaebECEdpCktkmZ0Lnxcql+PmtStZaA0=\n=kaLF\n-----END PGP PRIVATE KEY BLOCK-----",
"AddressKeyPassphrase": "iwbEuKOt4yaNBKaN7qmznAm8lfzXZhOASJ+5+FQ93Lw=",
"RootKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavR2jBYJKwYBBAHaRw8BAQdALR6055bPx21abVlNEHwqHKGxX2uufVwoO3l8\n8bPH+0b+BwMI0LcbSOntz43guIwcXWuS5WRg0WZ2M/AUefYKiA6d1LWMEQhieATu\n/nXRGdhx2ifSFLIEg9ec7O2I1fPOjT+3NqFKueIQaRvkSGWjmdhCmM0iRHJpdmUg\na2V5IDxub3JlcGx5QHByb3Rvbm1haWwuY29tPsK/BBMWCABxBYJq9HaMAwsJBwkQ\nOg1jrc2qPG41FAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ3S2\nqQS0vDPnpg2fJU8XmlcCFQgDFgACAhkBApsDAh4BFiEExH2zAwmj+0hpXXQDOg1j\nrc2qPG4AAEWmAP9wE87+vZEeZDGQWfg6zws6x+KMWTTQZkzY6JdcBYgYUAD/WPDY\n7nznixq+QgpKLwgpg7eHjq1JULAuMvSCNexwRA/HiwRq9HaMEgorBgEEAZdVAQUB\nAQdAEBN85n+6p3iumQ1FzRnhD5Ylf9k/U/rOlW00LXAxDzcDAQoJ/gcDCNC3G0jp\n7c+N4Ls4entT1mAbA3Fk5kTIxYnVQ8XIz5jn4K+bn2VqKksSnrbLgnQfhW5tUB0B\nrKkSGiXpWpVYdl+VaTApMtB4SVFhlRXcF7TCrgQYFggAYAWCavR2jAkQOg1jrc2q\nPG41FAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ+ioSh0RloIP\nBrzFsLHRiuYCmwwWIQTEfbMDCaP7SGlddAM6DWOtzao8bgAAeeIA/ibQx85gu0Cd\nMkWq5kCZ6+F7oxJbi1oJZx4RwCliOeufAQDq5EiXJbgM+sl7FT08TZJt0lk+J0Hb\nYWfUC8tMhAFeDw==\n=9zK0\n-----END PGP PRIVATE KEY BLOCK-----",
"RootKeyPassphrase": "K2ssV7FaPJYYC1hkv4EpYMXAR5mdpliJdXe3un8fESo=",
"RootHashKey": "-----BEGIN PGP MESSAGE-----\n\nwV4DfdbiRzc5UeoSAQdAozxxuTOM/JbY+q/TA4Y+MkF9un0reHW2d2PTlEPOxxIw\nE5hk5xx+Ac0CPFazJIEBqJS4yIMptA3xbGz+b294hbVeLvt/d+Gn1mWzB4/XPkac\n0sBlAaISANVJvSKh7vXBpGWa7hKRJv+3AdhkeEY6I9rAz8kYg4uw/+NkGNeGu8nm\nr81go8CY1ay98Bt09bUYrILvrGTrIIAOMSClGjJXmmgGPEDMe/ESQoZWhi1LVaCf\ntlHHDu8kCtjOESViElaHe8xzsEwWInz8C+sNYzgK7jx86Bos2lsM8drI9SSeXUUa\nZBH33dOnS31vKx8ZpGS+4f48CDqp+R8BJJ5S0ZKJ11fNbLfO3/c/wwvg/fE6wvSz\nFcbadk+VT0bc/c3wcxU4+S1Ifbf6TxJ1Bs7weDctak+BQxpxS/oMN8lGNxkStl5H\n2hkCF3dmB/TynoWiVyiqv+z1v7MP2hDWwpvKtOViSfrPhZZI8dwWMSD1ELEqLOFL\nOMswK1FUmIU=\n=NZ0X\n-----END PGP MESSAGE-----"
},
"Name": "Résumé – final.txt",
"ModTime": "2023-11-20T09:30:15+0530",
"Content": "DteMSjInE8AZ4F9UF/gjhwqq9DweL4x3dF4cCGrW8kx7WAa2Clsc2YbM0o+vbk2s6GcUYEMi62HpKPQKtGy3efgelagpzZAMK78/fSSdWXQp91NGrISUl/fjEeyAzQI1y3u4Wk03ntm43yf25CCD/2Hh8uEmHMttM4e7h6A09MrW5ro7XMwOQI362C+NuIlypUqbESZkrBnnu7ONrt2bdkYtQWDVqOKhfKs0vUrCJBaARJlgAlaXdy6vOKK7HL3QvTFYNDSeeuMERJvoXoShoZMpP5aNtDAOc4/Jvjadat1honfBerTZO0V6WsK1wQt4t9pV/US9cBHJeyVuJn0ChCd14GLRmXAUVz0PMB2N7QNiXbXZ6Cf8DFoSlZ3APIitSdTqoec51w+DQ+WphxYM9pX3xEnruGuxKgp3/rWisQju8l6dQe7Z7gzmSPnL6adIo044uuHQGpBwt7p2f6DPak6sdgtnjulwQpRLF5WemSVYpvWemxeBM2OVfyXMIvCymCHVTJB1QNYD8fB8d0Iw6pycqL/tTKUKvrMzXYDmafACsFsdUcsoLqFYn9Qv5JMtcxwTxWzit9fAo3tT9bdggM4jNgKM1Bu9C7M9fOJ5ZfanCOP7VdPcBon5XnqKz7ToVbl+r3C4EiwLpeFyz5sJFr7zmp1u0E35d85JyJoL3mJ6ko9TlkO1F76OoEBr8XHpXwfWXVozUfUlVcQehY5Q1oT4fPTrwcoDVOLg3Otxrcz37B5Dw8YSyoHKr9tE+xZ7SaFx7Oskna0p+Q+ttoy2GCIq7zjxJe2QZCy+pNZtg/RlhgJ/Qh6rw1TW+Yu5Q9UU7HonVt4NXuOVQF6T/ZDZ8m1GqXTn1thBBvqO5Fg3qUSlj7lHKhGCsFpjxZQtLLNzJIFMs/ujqWwq6pLONJEw+sUl4uFTy+PCAvWjDRlUhZ/3nmlQFl/8PX0nXp4Eg70oOWBi4l+f5lx7NJBpOIpimNH6qdsVI8+7MtnkGC36zyNlc3Zskz1HabOsLHwwUGAy0GmQH/jXX9MF35hUdOH13angj37evED+nYkUH5HWHh0vkDlmGDmye6ZkNHClSkuRIYKlhr5IANuPHgISD4H8Qwi1wfUpScM+rA6ecoq7AfILBPtVXN9K9hjR1HSfguEm77rK4hFOkbOb/QTnjjbj6hw7IeOyaMqRBjjLMTtsG8tXdqJPBZJ63CKmmdOJF7MS9zMiIMchZ1XufI4K9S51LyX5g+OM090QVvSzSo2quq4eWs9bj8Nkkz3wA61m9yYi3WZ9/obyJ7P0oOw++87egK5UryUh3y5+maPGpSwuYBXvlo8pqbDJ4stCtyfRtDrk/8aY6GgugygPphb6U/E8HP5MLHL1xeHLRxDw+7lebH/j2S3CWQWykltLBftQB8/FyoWHap97iC1yvz9h+lAxwlzK/KrIS2Qs11wIZFs2vciv4ocOqPO8FZVT+OB/rNh/XvMLVMb2GSaVXGjWaBGioLNd+FdsS3cR24/OjzVjecDwCkhH1EO9ey8+jQlz2Yk+3+xDViscwjqZ60FbU+0J4dgAy7pOTy9+6Gi52NQYu/Flvul5LT5fBDDoa62uIbhuxYpHsKD9vVpgtvenCRDmrYAA7QHBhAb0/hcYCN5RiLajxfChYLobFgGcf/z0t2fX8Zn90bkZ0H6FljeLEMBx0ttJtqBRDaQrK+o/3YpzRi4GPHxKvkFmanUPzZfWfLzwM0Mdou48eTFLjKzruOtzAMXStJ09R+QqaX0CJmBHQefg0as7Iyn96h0vtIbXpanb8pjZ/uxnHZVkV4HP+ztnNx2DFCbfMUkc1Fv6bamhOHyDSEL0c5R9T0LobllYzPNvRYC649LFtrGNwnFeeXMomx92DKPu+Nhlv1kp7NkjhiA0nz98C1+ELsEEB7Wh6ppq3eBLn17cFWbAELuER8JpjOBZ2rH9apC7Q+9/58BIo9rHsYcM6Yf/bpCRFoCbovEtOO296Cf/sMyS8ImKvIZBsTguSCu4nPktE1nKL04Myr0HQjXOK1sf+j3xokRTj8w/6SQ0OZRYS7TnMBuXsAvKVou1x8+jQwv9uAmqT0lZwJNBK8CK8ZHkZfMtFxki5f1YyzhUBAr8J4P8hJ0sv9mRDWCwp/T7K80W6XXAB3IdUOHvkCpWj1BuxbJ+wjWgRL7NCFAUHQkIKGGkthESSOOZSI358M6ZZwYpPr2y+x78BfQ+9tDJDtR+b7j7EdXIqf/ULuThBxvWX4wb9S7dsFWw+ur9tA6Fq0KSdNM4ed9l3Z3gHdGIwJ6o48ji1f9vvsWJXzjhEYJPJvfFb4c1gV9gr6CqNYuiYaoUWe6tNbGe+CPp/6saETXA4f8GzjGc5KsAyh2BFhS5p31xzjUAMyfZjLSidsLF3rtVwiOsBes+8//VsYG9hCBqvHLzN5FORCnax1/q50mHdwAzw4GHmF6ja9Cia5KzIxaBBTFcIlRCJdbExfj4PRzb7+Zdp1FaEVkPmkJPEtUjPU4c4GASwLnmcIkNz79ifE7Pz0ruYEGUyrwYVSjBKaaXJrDELbmxFYa9DbUyf18NZASP6ztRt4OeK2lOAd3PTIeXHYycc98bpZJbBabA/dj/je6EtTqN/1wXaH0OCVBIwqsDSVSgJK9Rjy5YAscUKLC+Z38i05cnsRbsBsRTyKalwgAB+hQ1DaRfw2hAVYD7SKrJQuSvXwL8PQ4wsbKQJn52oD6XuNhjcdGRIoGlRnKTsAjUv67srL5WEUzkPYNRgEYIq+EDjT9B/OfMBKH+gu2p8K04qaeS0BCrVYh/Lzf9kHuExcA/MDncTupkt0eJHibl2jQ+XJVavPAa6aQ9cLFkulm0NmNE7/NHb9C3ipbhTI9p83o+Q74L/ZvWPLW6yC689pf/tRBNJJcSN/YVIq2WbcMjCNxBiJsA2eCty6R4O0ICv18XsrrQU5lZP/PDiB038uAen8DPCH8xIQn/QKi9JD9mHhf8XUVmKL3u3656YuPvJ2faOjSGbZ9WfmY73GoN/1h4er2wbBmjKyVWYHlx81GLeYWx5rPbaLVaGxke0JaSIt2sS/pH7G5gX0g3gG0Il0VkWuFiJXiK8ifqWiLtutGUGH10ijKPijNNTC5VWvsKzZy6+s+G9EeCIEluuAiMklC0cTfdNLrWZ702hvh22P2PXaWp+tlknGI6Vw+ocaatdkaz5SKz9EdHzf0N/QsIDzqlFsmDHmQ7aElAFkK7KeIq43xjVfqU3b/fgPl7bJGuig6hv3B7csvPcgX1ZuuaFaknTErR2j+mVzBspVfkcVdLhCBLf+aCyenh22EGdCWYJf/gXd2fG+wfBQ=="
}
+12
View File
@@ -0,0 +1,12 @@
{
"Name": "-----BEGIN PGP MESSAGE-----\n\nwV4DfdbiRzc5UeoSAQdA7XKU4rc8MiHjOzDkMESI9r/dg5vTTfGQrYL+mmnAaDAw\nf45S5UMFYSTVp8robWoGQqMKUS5/CF3Q2SsgunObM8T5IeWcMDSiNWvvYtPexHk9\n0sBDAU4lEwh4RB9jMhExBETq3F++pWb9GFgu6utjdG52I1Gq7mGJ3rdX56KAwFiC\nyWjFXBflSJRvPSng93FyQ+DxV8OqWfugnXGxtpWbFwMolOG345R4gJ8FTQKZrrak\n1ODV1pIJjcOnTwCAw+uW9ycNTI3v8uOnu+I6J89yK1mICAphQLvxacqyeJsBqnWH\nc8d6KOAfWJdQ+PWHpyVPPjCS16IgxPISahIASnOcR/BYsE5kiHP0dIgWXG+CD6t5\n3Yq+KexiMCsxj+zlm1zHP4OGkR7NGArKDILIkf0Yvn5rcNe6aum0Qyzclfxs3yYs\nPWPBSsnUhO/yzotkTLcLxEkHMJLSuQ==\n=k/P/\n-----END PGP MESSAGE-----",
"NameHash": "33d5642922757df8750d3cd867dc34d742d198c12a1a424b3617fe07c9767718",
"NodeKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavR2jBYJKwYBBAHaRw8BAQdAhqnFsr4HWUkMEijuIQckOJ1G2K3VuFZe1J/A\nQ83Zmnj+BwMIgOIBis/Izj/gSJkJlrxE15UBHsshXlnDwxWFFG0XY6iQUkum5nsz\nVCsgm34tbr2+kYucI5OH4ZcUwhg1+4m26Y3xCBkeY1kJ1wCzcnZs6s0JRHJpdmUg\na2V5wsAJBBMWCAB7BYJq9HaMAwsJBwkQ2q15SJvUrn01FAAAAAAAHAAQc2FsdEBu\nb3RhdGlvbnMub3BlbnBncGpzLm9yZzWmwVVWqUJfqSK5PtNaUPgCFQgDFgACAhkB\nApsDAh4JFiEEvKrbb+RxgL0NxvP92q15SJvUrn0JJwkDCQIHAwcCAAD2lgEA+H8e\nDXTky36nJadZkkEaX4Ru2O6WLfLFw6DKBmYHiZABAPm6gwAWC24RzSF9mmYLsqu2\nZLSmgew6sz5vR2Arg1cAx4sEavR2jBIKKwYBBAGXVQEFAQEHQJJQPKAO2/cIbeG6\nhzoV4BMQG6HkGU5/IWT8d0Wn9UVRAwEKCf4HAwiA4gGKz8jOP+BjJSL2oj50LhQP\n/iCaAWlZId25bFkNeLnIql2EcBpB8pDitXK4CWzi3GUi414RL1SB6OYDntJ2EdQX\npGqNGQe0g84/Tp1Nwq4EGBYIAGAFgmr0dowJENqteUib1K59NRQAAAAAABwAEHNh\nbHRAbm90YXRpb25zLm9wZW5wZ3Bqcy5vcmc4cBkscMRvctJw6VuhTb4oApsMFiEE\nvKrbb+RxgL0NxvP92q15SJvUrn0AAD2RAQDsyXaWf7qw20fYSnKamaHZghCaKDgE\nAaJ/COvWeR6QtgEAgVeS5bpt69qcjl4M1VeiRg/OKuyz98LKePV/fcfK2gs=\n=mNhc\n-----END PGP PRIVATE KEY BLOCK-----",
"NodePassphrase": "-----BEGIN PGP MESSAGE-----\n\nwV4DfdbiRzc5UeoSAQdAjZU0sND1Y/3dN+xemfdmSTeudx/SPS1cgG9rjtCxmDUw\nWBn2aamgTJK98X+Kbb+ENk9SiZzIIm3q9BgcnrSAGHg5Ea45VIU3c0w2gnxTR/MG\n0l0Bpi944nmqbIlU+VvQLzyQl1TWlBrzJjPHg8VEwYOeKsWgz/VNBlYSdjFsIo0v\nbwNu3xzNRrxs/VL1Na1VG0U8kSSRBoIRrjRz/DdLnj40BCN9i5ah/2t2L0mvLjE=\n=I9+8\n-----END PGP MESSAGE-----",
"NodePassphraseSignature": "-----BEGIN PGP SIGNATURE-----\n\nwqsEABYIAF0Fgmr0dowJEOjFeJjESJCeNRQAAAAAABwAEHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmdAM0Z1Zs4qEGhmMqQerzOxFiEEovDWbdbGWAeVBhTm6MV4\nmMRIkJ4AAIacAP4qrIoEuIBveSpsDm4s5atvKIX6axrzdbpk2MmQ/ksCcAEAqj8c\natnZXVguH3HIXykGuYbKLLtgAyNIMFZtdpgDaAM=\n=75bw\n-----END PGP SIGNATURE-----",
"SignatureEmail": "test@proton.test",
"NameSignatureEmail": "test@proton.test",
"MediaType": "text/plain",
"ContentKeyPacket": "wWwGFQTuhgfuSeaElfhTcHuhVH9DcR42VBIBB0DG6NeRc4gJ42NpGhhANZZtVCsYSE1BBzntBkqT9yMTWjBP/6Xh1s7lDjQpnIsIBAiqEcEo5peTxmYHuk4Q6Ey6KpaKZcX/NWkINd3tnIXoMYw=",
"ContentKeyPacketSignature": "-----BEGIN PGP SIGNATURE-----\n\nwqsEABYIAF0Fgmr0dowJENqteUib1K59NRQAAAAAABwAEHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmc/lk7jZ7uW7qKAVVgwiZCcFiEEvKrbb+RxgL0NxvP92q15\nSJvUrn0AAG4DAP0ZZC3VbaYn+JAB7KY6LKwVLYW1o8S1xvMX0E4RSaUT0QEAjjhy\neaHYH6+HyFxlviiCQiuTZngkkFxIX+6JcHcmRgQ=\n=ZUAK\n-----END PGP SIGNATURE-----"
}
@@ -0,0 +1,12 @@
{
"ManifestSignature": "-----BEGIN PGP SIGNATURE-----\n\nwqsEABYIAF0Fgmr0dowJEOjFeJjESJCeNRQAAAAAABwAEHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmf3mNkUGB8MfMbXOgo6z4/WFiEEovDWbdbGWAeVBhTm6MV4\nmMRIkJ4AAKM8AP4t95tQdUeQrgGRbSaCO3vu8bD0n4h7tcU3myfG4jaHGwEAqGbo\nVu7KaVj4etYyw97OG4O3F9fyFytms8K+K4PEIgg=\n=4PZL\n-----END PGP SIGNATURE-----",
"XAttr": "-----BEGIN PGP MESSAGE-----\n\nwV4DoVR/Q3EeNlQSAQdAQxqlBfMGZVnnZYjaKMAuuNX9i4IBx6hJrODap23PQjgw\noUJTKYzt7ptpa+hBOMO4WPHZzYoknwub4wXqkK7yUzVekl64pVw0kBIi58/BdddK\n0sDFAXW7t6Rc+GBNYiIG6+2n/fsb6zS7DdiaywNNnI11+/pFuZbFmKYO7mB8euKX\nqjkWfrtYscJvK/ARR5y4+R83LV2ncfipCYogbKiu/TJ1KdGnIlU4O5AzBuMUnALU\nKdY9sUgwKP6UZk22z1+/aTl20sqoK75Lx8recNmWi3YAy2W+rTUfFZ1lkOwqzO5U\n6SG4YjhjjLxaVQbIU/k+01NRyS1MeBHI1iQOXTx1wMUTDiYcGcaKJaI1zLT5LLpa\n1iB5fFHBwmmhYPX+9eoehnq/odTmrioXNFObualcm3lNWkqVZr68cyXol/OKbP4M\nQAkrCu1AXP63Kx58CPy2SZPp/e44/2MpxBpCjhMjPaFwlJp25SyfkbzLVpgl0cPE\nnN1DCwhKV00uxo/OXT3TyX3AZQzkHH87enjsUIErJtzYeQwCOayZ2OsDWEqun4Iz\nm7N7WRrKkBQzXi3fsiGmS7dn8nPZQLKVl1XTRlO2gLUbcgwncvha4KewcCQrm8jn\nsjHFFj3xIys=\n=cF+A\n-----END PGP MESSAGE-----",
"SignatureEmail": "test@proton.test",
"Blocks": [
{
"Hash": "l9BiOEe3LP5ocY7N9nje8b5prF1C7cvKC8i8WVNZVSU=",
"EncSignature": "-----BEGIN PGP MESSAGE-----\n\nwV4DoVR/Q3EeNlQSAQdA+jC/DCMHLhuD7V07bcumt9RX8kd24DA0ZS+Hey7/rQww\nAXEuq4eGI/x9aNGq0/cKuVGfh3J9WiGIwf2fAe+xyMfwYdBUF0sJboRtZ7mTwEcP\n0sAeAdGSt1FmddNYRpeDyso3xCuERWnZPzO+hcS3OwcCrGW22t4bzvlc68BNyNZb\nwsp7aV/EKweMwSvB9ELkwjvGItXHCo6ch0Ugr67lGLSWFZcfO56Ji6KmYCKuRTDG\nm4uWZJIq0sCOwZHjiwlp7Uxv6ZBU5Q3bm694cyixAzeXPeQ6ZxK1vp9dIfO/FoTA\npiMk+iLN2ByGgBZaV637DpUfmu9+Aoop6JEhymioCSnG1DC4qp7vR/7009nNmftZ\nkicR4+h9JTXx5rDWWemIzCU5agGdSn7QoJm+d/kIM8F/\n=zUHV\n-----END PGP MESSAGE-----",
"SignatureEmail": "test@proton.test"
}
]
}
+18
View File
@@ -0,0 +1,18 @@
{
"Comment": "Folder written by go-proton-api v1.0.4 and Proton-API-Bridge v1.0.5 with test-only keys.",
"Type": "folder",
"ParentKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavRGjxYJKwYBBAHaRw8BAQdAcByzpVvdWeIqgDPVZmcsLXfHFl+Bc9dtNc/t\niBjn+IP+BwMIIaMaC0R//Jvgb8tK2b6Oodb3lCJtvh69ga4uP2ap0mz/xUrVex5v\nMXho5KNxYhW8EepuPH0NiYzjpUvWSEiheC7+EXeY6fGTDcjY83Y4gc0iRHJpdmUg\na2V5IDxub3JlcGx5QHByb3Rvbm1haWwuY29tPsK/BBMWCABxBYJq9EaPAwsJBwkQ\n/w8HJtPlPM81FAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ8De\n3CViT8Q6GeqHBeto3S0CFQgDFgACAhkBApsDAh4BFiEE2zfp6URSivY361ih/w8H\nJtPlPM8AANBzAP9p254j7U0XtUMTXtUMAZsFNMfEUUIONbMJ8O+ZWaIKOwEAh3Cw\nz++d8p+bJw+7ovbZrBQybAJVDgSxMrD55HdvkA7HiwRq9EaPEgorBgEEAZdVAQUB\nAQdAS6pel/FW2xZBlcnqiOP1Qb9Z+wIYWp5rHbWhrjCjPxcDAQoJ/gcDCCGjGgtE\nf/yb4O3SqVDOQcANB/1usPldBbIAqfyrHrBYhl7I22qJ8C2dA/0Jd8QZJQ2piogy\nLVCxhigyc7I/khvo0DQtm6yEiG4Ba96d4gDCrgQYFggAYAWCavRGjwkQ/w8HJtPl\nPM81FAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ1BBLxxRm18q\nrPmFuxepoKcCmwwWIQTbN+npRFKK9jfrWKH/Dwcm0+U8zwAAriMA/j6dCxHqJGV4\nH7mPiw3MRSgXs3fBfoIbED3GNUnDd1qSAPsFnc+gFS4rVj6ISFeZi9qKSBUkshY8\nODgZOvvp282WCA==\n=d0Pu\n-----END PGP PRIVATE KEY BLOCK-----",
"ParentKeyPassphrase": "epXvUdjEr7MP281MqKlqtLTP8pTp3W8Pd06oK5O5mUI=",
"AddressKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavRGjxYJKwYBBAHaRw8BAQdAjfCNZ1vHeSII9FgMPuIpksy8Ru1IYogM3kt5\nbLkbvSr+BwMImAfCaC1ZpIDgOjjorDRLeZeXJmiY4g7sHfWioT/n+KyhQHIejdCX\niwVsun/SAbywJDI410K9rSZc5EgoOjq1AY+p7PkKlVKX6N9drVAF8M0XdGVzdCA8\ndGVzdEBwcm90b24udGVzdD7CvwQTFggAcQWCavRGjwMLCQcJEMrE2wl491S0NRQA\nAAAAABwAEHNhbHRAbm90YXRpb25zLm9wZW5wZ3Bqcy5vcmf5qKaaNXG7tbQ22Gyg\n+0spAhUIAxYAAgIZAQKbAwIeARYhBBt9DXGs6YlhOFA8fcrE2wl491S0AAB4GgD9\nHPYJYDYYlrRIiXSIxwSMoA8N2qFF3dZu+JclcNJkgvQA/A19w5X8hDkSS8JIpxw3\nRZrRTYn84h2sLpc/UaGpfGMFx4sEavRGjxIKKwYBBAGXVQEFAQEHQLaoMtcytvD+\n3VWXtk86FmWAj3lGf6geHDROSdDoVXknAwEKCf4HAwiYB8JoLVmkgOCl5KYyiazU\n36OQT3lv1PN7vNGqhXKinkl3/HCyzZ9XypwUTX4iM3gMxnEcBhM2JzJgQH430u4s\nrEhT+/Bi2+B7LmsblwOxwq4EGBYIAGAFgmr0Ro8JEMrE2wl491S0NRQAAAAAABwA\nEHNhbHRAbm90YXRpb25zLm9wZW5wZ3Bqcy5vcmdPaYn0wfSZLPYbNHOiCY07ApsM\nFiEEG30NcazpiWE4UDx9ysTbCXj3VLQAAKMgAP93VZvAugvAKI3VfBnuUaLyob1j\n+hilZNaVvotWIG12nQEA9zvN8uLVIefMJRwJucbdJLN5NhDnbUerpi8BuCA+vw4=\n=aZOL\n-----END PGP PRIVATE KEY BLOCK-----",
"AddressKeyPassphrase": "LxTkQtWVXufn2S4L0TCVgkK1rIU5jbppjuUZYx8FWBc=",
"NodeKey": "-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nxYYEavRGjxYJKwYBBAHaRw8BAQdAgoNl19ESfrQk3oxuSFdeLFiEzaR3oyIoTVkb\nrRAy2av+BwMIbSMlPfZXu5PgW8A+jsuCxlebNv54CSUKk09Je3SIJvnzS7jS1V0k\naZvresVslQFzx0MHpr/yeB/+7TPR5dzuLmXd+aGgdqkC6SckRBoFZM0iRHJpdmUg\na2V5IDxub3JlcGx5QHByb3Rvbm1haWwuY29tPsK/BBMWCABxBYJq9EaPAwsJBwkQ\nfCiqEgYU2gU1FAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ+cY\nD6U5omic+nTFDziv0jACFQgDFgACAhkBApsDAh4BFiEEdiCJ15XfeWq8CFT2fCiq\nEgYU2gUAAOR7AP0Ww8YYcvQgrTviO3apsVEZ9q3S6sUDsI29+Bqj1n4C+AEA+e5N\nSPzQnzp5SxU0x3TUxEbCbtwJxi8NCHm/Bc/4AQbHiwRq9EaPEgorBgEEAZdVAQUB\nAQdAvvwZKOA/WYuQVsafq2YaPExJg4gKOJTpkwdATIUkcW0DAQoJ/gcDCG0jJT32\nV7uT4BD4YWn+qvsjXQb3yXpa2rSDpObO3IKs51BiSBzkwhvuZeQ0j/O0W0w6d1ne\n2z5wNfFobuQvPStR/FrE8HcxM2j/vvpPXNXCrgQYFggAYAWCavRGjwkQfCiqEgYU\n2gU1FAAAAAAAHAAQc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ6t1L01T3l1V\nMHJ8evZ4088CmwwWIQR2IInXld95arwIVPZ8KKoSBhTaBQAAwxIBANPX36Lsy88R\ngm7CeRx0oUFVMrvVTPoik525Kc4o9GhCAP4pKY9o9jYMZu3SKgK2uczL8COEE5NN\nDngG7LerEf6NAQ==\n=fklD\n-----END PGP PRIVATE KEY BLOCK-----",
"NodePassphrase": "-----BEGIN PGP MESSAGE-----\n\nwV4DWp6jv64eIygSAQdAFKUkba5JwRWgNoAP15SVnd0+LIj+gqv04dy89Wrcqjcw\nBoEKO4cg+EoKp8VZ/NkGnpvDGU3NYfd/wWhjL/pk5O08Sq+hy5iGxfOjjRw87Y4U\n0l0BlVAuSvMd4jOnDXZjsHu9ADtRoAD7IyVtr98eKmkY3/qz/Ox1L069MkTUf19j\nElDiOZmcfklfMlmMJvHQVtmwfgiXRdtto/kNFejNfBtSxF6AJw0s0LedtHyftSU=\n=tho6\n-----END PGP MESSAGE-----",
"NodePassphraseSignature": "-----BEGIN PGP SIGNATURE-----\n\nwqsEABYIAF0Fgmr0Ro8JEMrE2wl491S0NRQAAAAAABwAEHNhbHRAbm90YXRpb25z\nLm9wZW5wZ3Bqcy5vcmcz9ZAF6bobxc93L+dEnb6ZFiEEG30NcazpiWE4UDx9ysTb\nCXj3VLQAAO67AP4o6cOdU9EdMFbuavjw+i+GOE2WrFqwuf7gl+/jk7hFgAD+Ll8q\n5ZtTs4DQaf10P6P94tL5eqlO4j9jJxBxrX5VnAc=\n=52X6\n-----END PGP SIGNATURE-----",
"NodePassphrasePlain": "porsaPWJDYpZlXNOUaiyE9uz+Z4IVpaqppO4rz1Iy/A=",
"Name": "-----BEGIN PGP MESSAGE-----\n\nwV4DWp6jv64eIygSAQdAD9q58a7P6MpyqXT3QEI62k4j1hwx68MO+smShKOyZjgw\nDqSyKqnQzixY386+aKQq7l7rlrnSLqALfsnS4bjdSGf1+yu7+ut0eHyKkDc4UQFx\n0sBCAdBVEh8oc/f86Z+BCEOyWL2ffhWYdNQNyCue3jXTrZt0UrbADWijwDMmhL05\n/29TSLf5XcvR5lWfxWWZI+3YCa9upo3Gb4mIpDQuUmHaagzn6TC4HlL9GyG5CaEx\nfFu9Ra93myXTtDwhm/ysUjkguNfMr5VwjARfJmuKac6TpyjSzOh+a7KW/kN094iK\nHvMmO9PTCMSIKALFBRKlEBllqMhWzpiu533ykHW7u7JfzDRm+aSxT48YrfbibF3D\nQVRCiQOLMU3NuwtMwQxcentzCKwNvPTyxMqG9m4IrwFO3CU84hHpnPo0sTeemAp/\nBrum0uIpd3fACmTzT1ZgsKB7S4Mi\n=uiCU\n-----END PGP MESSAGE-----",
"NamePlain": "Fichiers d'été 📁",
"NodeHashKey": "-----BEGIN PGP MESSAGE-----\n\nwV4DIMIraMSXSp4SAQdAahEjiRUe66lBrrIDwZCkrZ3SbdkVD775SsblwvMFWgEw\nVQ3SJsiAxfYlYivvtDSuT0fR8jfcsJjUuxtE2qUKvogUBo0CxBwuJ5JvCr12oRUf\n0sBlAbdKXFsKDr423bt4NbOOKKgY3SJcfjEbylMA/uEP/t3OLbTn6feKplhgWtoA\nKuI66xgdkPkW+KTg65Ju8GrQISFepYv6mF0VVzuKmLsgyBJwbCvOvTH6X/EnzOAG\nWvcjAEXyBtmQkEkOsNU2CdzfFDOvD7N5bquFUIdM+Mq6gEDUNxBZg3JkPrELe2Ll\nZfeWsdXRvRnliuLn9OeCoUCBIVtIRfUdMu+/qCnE2G9UHc70fiZtOJdNNnJBnKlo\n8kQKG28vZ+NlUtdRGQ9Aoh4qQbRyqKIg209BhuetlTYiWX2odrSXey5HuCkyxXWc\nTdJd+bVOZ0KQtreKAxW5GqWvWYGpkHMpCJ5oHxMABfa4HAJivTtOkRRSkQSue2cj\nD714MR8nb1A=\n=ocOA\n-----END PGP MESSAGE-----",
"HashKeyPlain": "i49BgOd0lq0m/q3GWVY3UxmraZAKUmqsZcZCQXznf00=",
"ChildName": "Document.txt",
"ChildNameHash": "f3d4d9f7ee96a050b95cfd441e91258685cf57fc09fa55fea31e3a68b179d9b4"
}
@@ -0,0 +1,51 @@
package protondrive
import (
"bytes"
"context"
"fmt"
"io"
"testing"
"github.com/ProtonMail/gopenpgp/v3/crypto"
"github.com/rclone/rclone/backend/protondrive/protontest"
"github.com/stretchr/testify/require"
)
// newTestClient returns a client with caching enabled for a new user
// of a new fake server, signed in without a login round trip.
func newTestClient(t testing.TB) (*client, *protontest.Server, *protontest.User) {
t.Helper()
return newTestClientOpt(t, clientOptions{EnableCaching: true})
}
// newTestClientOpt is newTestClient with the options opt.
func newTestClientOpt(t testing.TB, opt clientOptions) (*client, *protontest.Server, *protontest.User) {
t.Helper()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
return newTestClientFor(t, srv, u, opt), srv, u
}
// newTestClientFor returns a client for a new session of u.
func newTestClientFor(t testing.TB, srv *protontest.Server, u *protontest.User, opt clientOptions) *client {
t.Helper()
c, err := newClient(context.Background(), newAccountSession(srv, u), opt)
require.NoError(t, err)
t.Cleanup(c.close)
return c
}
// decryptBlock returns the encrypted block enc (SEIPDv1 or v2)
// decrypted with the content session key sk.
func decryptBlock(enc []byte, sk *crypto.SessionKey) ([]byte, error) {
r, err := blockDecrypter(bytes.NewReader(enc), sk)
if err != nil {
return nil, err
}
plain, err := io.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("decrypt block: %w", err)
}
return plain, nil
}
+975
View File
@@ -0,0 +1,975 @@
package protondrive
import (
"bytes"
"context"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"strings"
"sync"
"time"
"github.com/ProtonMail/gopenpgp/v3/crypto"
"github.com/rclone/rclone/backend/protondrive/api"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/lib/pacer"
"github.com/rclone/rclone/lib/pool"
"github.com/rclone/rclone/lib/rest"
"golang.org/x/sync/errgroup"
)
// errUploadMismatch is returned when the bytes read from the source of
// an upload don't match its declared size or SHA1.
var errUploadMismatch = errors.New("source doesn't match its declared size or hash")
// errUploadOutcomeUnknown is returned when the commit of an upload
// failed and whether it took effect can't be found out.
var errUploadOutcomeUnknown = errors.New("upload outcome unknown: it may have succeeded")
// errUploadSuperseded is returned when an upload was committed but the
// file read back has another revision, which is not this upload's.
var errUploadSuperseded = errors.New("upload committed, but the file now has another revision")
// errUploadUnconfirmed is returned when an upload was committed but the
// server still didn't show it when the file was last read back.
var errUploadUnconfirmed = errors.New("upload committed, but the server doesn't show it yet")
const (
// defaultUploadConcurrency and maxUploadConcurrency bound the blocks
// of one file being uploaded at once, as in the SDK.
defaultUploadConcurrency = 4
maxUploadConcurrency = 5
// blockUploadAttempts is how many times uploading a block is tried.
blockUploadAttempts = 3
// blockSealAttempts is how many times a block whose encryption
// fails its check is encrypted, as in the SDK.
blockSealAttempts = 2
// intendedSizePrecision is the precision of the size an upload
// declares in advance, which must not tell the server the size.
intendedSizePrecision = 100_000
// draftCleanupTimeout bounds deleting the draft of a failed upload.
draftCleanupTimeout = 30 * time.Second
)
// uploadRequest is a file to upload.
type uploadRequest struct {
ParentID string // folder to create the file in; unused if Existing is set
Name string // name of the new file; unused if Existing is set
Existing *node // file to give a new revision; nil for a new file
In io.Reader
Size int64 // bytes In holds; < 0 if unknown
ModTime time.Time // zero if unknown
MIMEType string
SHA1 string // hex SHA-1 of the bytes In holds; "" if unknown
}
// draft is an uncommitted revision being uploaded.
type draft struct {
nodeID string
revisionID string
parentID string
newFile bool // the node is a draft too, not just the revision
baseRevisionID string // active revision the draft revision was created from; new revisions only
nodeKR *crypto.KeyRing
ownKeys bool // nodeKR was generated for the draft and is cleared with it
sk *crypto.SessionKey // content key
}
// upload uploads r as a new file, or a new revision of r.Existing, as
// the SDK's upload module does, and returns the file.
//
// A new file whose name is taken by a file is uploaded as a new
// revision of it. A draft of the name left by this client (by
// DraftClientUID) is deleted and the upload tried again; one left by
// another client is errExists unless ReplaceExistingDraft is set. If
// the bytes read differ from r.Size or r.SHA1 the upload fails with
// errUploadMismatch.
//
// A failed upload deletes its draft only when nothing may have been
// committed: before the commit, or when the server refused the commit
// with a 4xx API error on its only attempt. Otherwise nothing is deleted
// and a failed commit whose revision the file read back doesn't show is
// errUploadOutcomeUnknown: the upload may have succeeded. A commit which
// succeeded but whose file has another revision by the time it is read
// back is errUploadSuperseded, and one the server still doesn't show
// after a few reads is errUploadUnconfirmed. The returned node is always
// the upload's own revision.
func (c *client) upload(ctx context.Context, r uploadRequest) (*node, error) {
what := fmt.Sprintf("upload %q", r.Name)
if r.Existing != nil {
what = fmt.Sprintf("upload %q", r.Existing.Name)
}
signKR, err := c.addr.signingKeyRing()
if err != nil {
return nil, fmt.Errorf("%s: %w", what, err)
}
d, err := c.createDraft(ctx, r, signKR)
if err != nil {
return nil, fmt.Errorf("%s: %w", what, err)
}
if d.ownKeys {
defer d.nodeKR.ClearPrivateParams()
}
n, committed, err := c.uploadDraft(ctx, d, r, signKR)
if err != nil {
if !committed {
c.deleteDraft(ctx, d)
}
return nil, fmt.Errorf("%s: %w", what, err)
}
return n, nil
}
// draftClientUID returns the ClientUID marking the client's drafts, or
// nil if it has none.
func (c *client) draftClientUID() *string {
if c.opt.DraftClientUID == "" {
return nil
}
uid := c.opt.DraftClientUID
return &uid
}
// derivedDraftClientUID returns a draft client ID for the remote name
// derived from the name and the account's volume, so it is the same on
// every run.
func (c *client) derivedDraftClientUID(name string) string {
sum := sha256.Sum256([]byte("rclone draft client\x00" + c.volumeID + "\x00" + name))
return hex.EncodeToString(sum[:16])
}
// uploadConcurrency returns the most blocks of one file to upload at
// once.
func (c *client) uploadConcurrency() int {
if c.opt.UploadConcurrency <= 0 {
return defaultUploadConcurrency
}
return min(c.opt.UploadConcurrency, maxUploadConcurrency)
}
// draftIntendedSize returns the IntendedUploadSize of a new file of
// size bytes as the SDK's createDraft sends it: rounded down to
// intendedSizePrecision, or nil if that is at most the precision or the
// size is unknown.
func draftIntendedSize(size int64) *int64 {
rounded := size / intendedSizePrecision * intendedSizePrecision
if size < 0 || rounded <= intendedSizePrecision {
return nil
}
return &rounded
}
// revisionIntendedSize returns the IntendedUploadSize of a new revision
// of size bytes as the SDK's createDraftRevision sends it
// (reduceSizePrecision), or nil if the size is zero or unknown.
func revisionIntendedSize(size int64) *int64 {
var v int64
switch {
case size <= 0:
return nil
case size < 4096:
v = 4095
case size < intendedSizePrecision:
v = intendedSizePrecision
default:
v = size / intendedSizePrecision * intendedSizePrecision
}
return &v
}
// createDraft creates the draft to upload r to: a draft file in
// r.ParentID, or a draft revision of r.Existing or of the file which
// has the name.
func (c *client) createDraft(ctx context.Context, r uploadRequest, signKR *crypto.KeyRing) (*draft, error) {
if r.Existing != nil {
return c.createRevision(ctx, r.Existing, r.Size)
}
if err := validateName(r.Name); err != nil {
return nil, err
}
parent, err := c.targetFolder(ctx, r.ParentID)
if err != nil {
return nil, err
}
now := c.s.serverTime()
nodeKey, passphrase, passphraseSig, keys, err := generateNodeKeys(parent.keys.keyRing, signKR, true, now)
if err != nil {
return nil, err
}
created := false
defer func() {
if !created {
keys.keyRing.ClearPrivateParams()
}
}()
contentKeyPacket, contentKeyPacketSig, sk, err := generateContentKey(keys.keyRing, now)
if err != nil {
return nil, err
}
encName, err := encryptName(r.Name, nil, parent.keys.keyRing, signKR, now)
if err != nil {
return nil, err
}
mimeType := r.MIMEType
if mimeType == "" {
mimeType = "application/octet-stream"
}
req := &api.CreateFileRequest{
ParentLinkID: parent.ID,
Name: encName,
Hash: nameHash(r.Name, parent.hashKey),
MIMEType: mimeType,
ClientUID: c.draftClientUID(),
IntendedUploadSize: draftIntendedSize(r.Size),
NodeKey: nodeKey,
NodePassphrase: passphrase,
NodePassphraseSignature: passphraseSig,
ContentKeyPacket: contentKeyPacket,
ContentKeyPacketSignature: contentKeyPacketSig,
SignatureAddress: c.addr.Email,
}
opts := &rest.Opts{Method: "POST", Path: c.volumePath("/drive/v2/volumes") + "/files"}
for retried := false; ; retried = true {
var resp api.CreateFileResponse
err = c.apiCall(ctx, opts, req, &resp)
if err == nil {
created = true
return &draft{
nodeID: resp.File.ID,
revisionID: resp.File.RevisionID,
parentID: parent.ID,
newFile: true,
nodeKR: keys.keyRing,
ownKeys: true,
sk: sk,
}, nil
}
existing, err := c.resolveConflict(ctx, err, retried)
if err != nil {
return nil, err
}
if existing != nil {
return c.createRevision(ctx, existing, r.Size)
}
}
}
// resolveConflict decides what to do after creating a draft failed
// with err, as the SDK's handleConflictError does. It returns the file
// to upload a new revision of if the name is taken by a file, or nil
// and no error to try again once a draft in the way is deleted.
//
// A draft is deleted if it was left by this client or
// ReplaceExistingDraft is set, unless retried is set: a draft which
// comes back is another client's upload in progress.
func (c *client) resolveConflict(ctx context.Context, err error, retried bool) (*node, error) {
var apiErr *api.Error
if !errors.As(err, &apiErr) || apiErr.Code != api.CodeAlreadyExists {
return nil, fmt.Errorf("create draft: %w", err)
}
var conflict api.ConflictDetails
if json.Unmarshal(apiErr.Details, &conflict) != nil || conflict.ConflictLinkID == "" {
return nil, mutationError("create draft", err)
}
if conflict.ConflictRevisionID != "" || conflict.ConflictDraftRevisionID == "" {
// Only a file, which has an active revision, can take a new
// revision. A draft revision of it doesn't stop that.
c.cache.invalidate(conflict.ConflictLinkID)
n, getErr := c.getNode(ctx, conflict.ConflictLinkID)
if getErr != nil {
return nil, fmt.Errorf("create draft: %w", getErr)
}
if n.IsFolder || n.State != api.LinkStateActive || n.RevisionID == "" {
return nil, mutationError("create draft", err)
}
return n, nil
}
own := c.opt.DraftClientUID != "" && conflict.ConflictDraftClientUID == c.opt.DraftClientUID
switch {
case retried:
return nil, fmt.Errorf("create draft: a draft of the file came back after it was deleted: %w", mutationError("another client is uploading it", err))
case !own && !c.opt.ReplaceExistingDraft:
return nil, fmt.Errorf("create draft: another client is uploading the file (set replace_existing_draft to replace its draft): %w", mutationError("draft exists", err))
}
fs.Debugf(nil, "protondrive: deleting draft %s left by client %q", conflict.ConflictLinkID, conflict.ConflictDraftClientUID)
if delErr := c.deleteDraftLink(ctx, conflict.ConflictLinkID); delErr != nil {
return nil, fmt.Errorf("create draft: %w (deleting the draft in the way failed: %v)", mutationError("draft exists", err), delErr)
}
return nil, nil
}
// createRevision creates a draft revision of file n, which must be
// unchanged since it was read.
func (c *client) createRevision(ctx context.Context, n *node, size int64) (*draft, error) {
if n.IsFolder || n.link == nil || n.link.File == nil || n.RevisionID == "" {
return nil, fmt.Errorf("%q is not a file with content", n.Name)
}
sk, err := c.contentKey(n, "updating")
if err != nil {
return nil, err
}
req := &api.CreateRevisionRequest{
CurrentRevisionID: n.RevisionID,
ClientUID: c.draftClientUID(),
IntendedUploadSize: revisionIntendedSize(size),
}
var resp api.CreateRevisionResponse
err = c.apiCall(ctx, &rest.Opts{Method: "POST", Path: c.volumePath("/drive/v2/volumes", "files", n.ID, "revisions")}, req, &resp)
c.cache.invalidate(n.ID)
if err != nil {
return nil, mutationError("create revision", err)
}
return &draft{
nodeID: n.ID,
revisionID: resp.Revision.ID,
baseRevisionID: n.RevisionID,
parentID: n.ParentID,
nodeKR: n.keys.keyRing,
sk: sk,
}, nil
}
// uploadResult is what was read from the source of an upload and
// uploaded.
type uploadResult struct {
size int64
sha1 string // hex
hashes [][]byte // SHA-256 of each encrypted block, in order
blockSizes []int64 // plaintext size of each block, in order
}
// check checks res against the size and SHA1 r declares, and its block
// count against its size.
func (res *uploadResult) check(r uploadRequest) error {
switch {
case r.Size >= 0 && res.size != r.Size:
return fmt.Errorf("%w: read %d bytes, expected %d", errUploadMismatch, res.size, r.Size)
case r.SHA1 != "" && !strings.EqualFold(r.SHA1, res.sha1):
return fmt.Errorf("%w: read bytes with SHA1 %s, expected %s", errUploadMismatch, res.sha1, r.SHA1)
case int64(len(res.hashes)) != (res.size+defaultBlockSize-1)/defaultBlockSize:
return fmt.Errorf("%d blocks uploaded for %d bytes", len(res.hashes), res.size)
}
return nil
}
// uploadDraft uploads the content of r to draft d and commits it,
// returning the committed file. committed reports whether the revision
// may be committed, even if err is set, when the draft must not be
// deleted: committed is false only when the commit wasn't sent or the
// server refused it on its only attempt. What a read shows never proves
// a commit didn't take effect: reads may lag.
func (c *client) uploadDraft(ctx context.Context, d *draft, r uploadRequest, signKR *crypto.KeyRing) (n *node, committed bool, err error) {
code, verifySK, err := c.verificationData(ctx, d)
if err != nil {
return nil, false, err
}
res, err := c.uploadBlocks(ctx, d, r, code, verifySK, signKR)
if err != nil {
return nil, false, err
}
if err := res.check(r); err != nil {
return nil, false, err
}
now := c.s.serverTime()
manifest, err := signManifest(res.hashes, signKR, now)
if err != nil {
return nil, false, err
}
x := &xattrs{Size: res.size, BlockSizes: res.blockSizes, SHA1: res.sha1}
if !r.ModTime.IsZero() {
x.ModTime = r.ModTime.UTC()
}
encXAttrs, err := encryptXAttrs(x, d.nodeKR, signKR, now)
if err != nil {
return nil, false, err
}
req := &api.CommitRevisionRequest{
ManifestSignature: manifest,
SignatureAddress: c.addr.Email,
XAttr: encXAttrs,
ChecksumVerified: r.SHA1 != "",
}
opts := &rest.Opts{Method: "PUT", Path: c.volumePath("/drive/v2/volumes", "files", d.nodeID, "revisions", d.revisionID)}
attempts := 0
commitErr := c.pacedCall(ctx, c.apiPacer, func() (*http.Response, error) {
attempts++
return c.s.call(ctx, opts, req, nil)
})
if commitErr != nil && attempts == 1 && refusedCommit(commitErr) {
c.forget(d.nodeID, d.parentID)
return nil, false, fmt.Errorf("commit: %w", commitErr)
}
// Whether a failed commit took effect is read even if ctx is done,
// though only once then, as a commit which did is a success.
readCtx, cancel := context.WithTimeout(ctx, draftCleanupTimeout)
defer cancel()
if commitErr != nil && ctx.Err() != nil {
detached, cancelDetached := context.WithTimeout(context.WithoutCancel(ctx), draftCleanupTimeout)
defer cancelDetached()
n, err = c.fetchNode(detached, d.nodeID)
} else {
n, err = c.readBack(readCtx, d.nodeID, func(n *node, err error) bool { return !d.lagging(n, err) })
}
ours := err == nil && n.State == api.LinkStateActive && n.RevisionID == d.revisionID
if ours {
c.cache.add(n)
} else {
c.forget(d.nodeID, d.parentID)
}
switch {
case ours && commitErr != nil:
// The response of a commit which succeeded may have been lost,
// and its retry then failed.
fs.Debugf(nil, "protondrive: %q (ID %s): commit failed but the revision is active: %v", n.Name, n.ID, commitErr)
return n, true, nil
case ours:
return n, true, nil
case commitErr == nil && (d.lagging(n, err) || ctx.Err() != nil):
fs.Debugf(nil, "protondrive: node %s: revision %s committed, but the server doesn't show it yet", d.nodeID, d.revisionID)
return nil, true, errUploadUnconfirmed
case commitErr == nil && err != nil:
return nil, true, fmt.Errorf("read the uploaded file: %w", err)
case commitErr == nil:
return nil, true, fmt.Errorf("%w (the file is in state %d with revision %q active, not %s)", errUploadSuperseded, n.State, n.RevisionID, d.revisionID)
case err != nil:
// Any draft left is replaced by the next upload of the name.
return nil, true, fmt.Errorf("commit: %w: %w (reading the file failed: %v)", errUploadOutcomeUnknown, commitErr, err)
}
// A read may lag, or the file have changed since, so what it shows
// doesn't prove the commit didn't take effect.
return nil, true, fmt.Errorf("commit: %w: %w (the file doesn't show the revision)", errUploadOutcomeUnknown, commitErr)
}
// refusedCommit reports whether err, from a commit sent only once, is
// the server refusing it, so that it didn't take effect: an API error
// with a 4xx status other than 408 and a Proton code.
func refusedCommit(err error) bool {
var apiErr *api.Error
return errors.As(err, &apiErr) && apiErr.Code != 0 &&
apiErr.Status >= 400 && apiErr.Status < 500 && apiErr.Status != http.StatusRequestTimeout
}
// lagging reports whether n and err, read after the commit of d, may
// be from before the server shows the commit: the node isn't found, is
// active without an active revision, or looks as it did before (a new
// file still a draft, or the revision d started from still active).
func (d *draft) lagging(n *node, err error) bool {
switch {
case errors.Is(err, errNotFound):
return true
case err != nil:
return false
case n.State == api.LinkStateActive && n.RevisionID == "":
return true
case d.newFile:
return n.State == api.LinkStateDraft
}
return n.State == api.LinkStateActive && n.RevisionID == d.baseRevisionID
}
// verificationData returns the verification code of draft d and the
// content key the server holds for it, which blocks are checked
// against as in the SDK.
func (c *client) verificationData(ctx context.Context, d *draft) (code []byte, sk *crypto.SessionKey, err error) {
var resp api.VerificationResponse
opts := &rest.Opts{Method: "GET", Path: c.volumePath("/drive/v2/volumes", "links", d.nodeID, "revisions", d.revisionID, "verification")}
if err := c.apiCall(ctx, opts, nil, &resp); err != nil {
return nil, nil, fmt.Errorf("get verification data: %w", err)
}
code, err = base64.StdEncoding.DecodeString(resp.VerificationCode)
if err != nil {
return nil, nil, fmt.Errorf("get verification data: %w", err)
}
sk, _, err = decryptContentKey(resp.ContentKeyPacket, "", d.nodeKR, nil, c.s.serverTime())
if err != nil {
return nil, nil, fmt.Errorf("get verification data: %w", err)
}
return code, sk, nil
}
// encryptedBlock is a block of a draft, encrypted and checked, which
// holds its share of the upload concurrency until freed.
type encryptedBlock struct {
index int
enc *pool.RW
encSignature string
token []byte // verifier token
release func() // gives back the share of the upload concurrency
}
// free frees the block's buffer and its share of the upload
// concurrency.
func (b *encryptedBlock) free() {
_ = b.enc.Close()
b.release()
}
// freeBlocks frees every block of blocks.
func freeBlocks(blocks []*encryptedBlock) {
for _, b := range blocks {
b.free()
}
}
// blockUpload is a block with the URL to upload it to.
type blockUpload struct {
block *encryptedBlock
link api.UploadLink
}
// uploadBlocks reads, encrypts and uploads the content of r to draft
// d: one goroutine reads and encrypts blocks, one requests their upload
// URLs in batches, and uploadConcurrency workers upload them. Every
// wait is bounded by ctx.
func (c *client) uploadBlocks(ctx context.Context, d *draft, r uploadRequest, code []byte, verifySK *crypto.SessionKey, signKR *crypto.KeyRing) (*uploadResult, error) {
concurrency := c.uploadConcurrency()
g, gCtx := errgroup.WithContext(ctx)
ready := make(chan *encryptedBlock)
uploads := make(chan blockUpload)
res := &uploadResult{}
g.Go(func() error {
defer close(ready)
return c.readBlocks(gCtx, d, r, code, verifySK, signKR, concurrency, res, ready)
})
g.Go(func() error {
defer close(uploads)
return c.requestURLs(gCtx, d, ready, uploads)
})
for range concurrency {
g.Go(func() error {
for {
var u blockUpload
var ok bool
select {
case u, ok = <-uploads:
case <-gCtx.Done():
return gCtx.Err()
}
if !ok {
return nil
}
err := c.uploadBlock(gCtx, d, u)
u.block.free()
if err != nil {
return fmt.Errorf("block %d: %w", u.block.index, err)
}
}
})
}
if err := g.Wait(); err != nil {
return nil, err
}
return res, nil
}
// readBlocks reads r.In block by block into pool buffers, encrypts and
// checks each block and sends it to ready, recording what it read in
// res. At most concurrency blocks of the upload, and blocksInFlight of
// the client, are held at once.
//
// A source longer than r.Size fails with errUploadMismatch as soon as
// that is seen.
func (c *client) readBlocks(ctx context.Context, d *draft, r uploadRequest, code []byte, verifySK *crypto.SessionKey, signKR *crypto.KeyRing, concurrency int, res *uploadResult, ready chan<- *encryptedBlock) error {
slots := make(chan struct{}, concurrency)
digest := sha1.New()
in := io.TeeReader(r.In, digest)
for index := 1; ; index++ {
release, err := c.reserveBlock(ctx, slots)
if err != nil {
return err
}
plain := pool.NewRW(pool.Global())
n, err := plain.ReadFrom(io.LimitReader(in, defaultBlockSize))
res.size += n
switch {
case err != nil:
err = fmt.Errorf("read source: %w", err)
case r.Size >= 0 && res.size > r.Size:
err = fmt.Errorf("%w: source is longer than %d bytes", errUploadMismatch, r.Size)
}
if err != nil || n == 0 {
_ = plain.Close()
release()
if err != nil {
return err
}
break
}
b, hash, err := c.sealBlock(d, plain, index, code, verifySK, signKR)
_ = plain.Close()
if err != nil {
release()
return fmt.Errorf("block %d: %w", index, err)
}
b.release = release
res.hashes = append(res.hashes, hash)
res.blockSizes = append(res.blockSizes, n)
select {
case ready <- b:
case <-ctx.Done():
b.free()
return ctx.Err()
}
if n < defaultBlockSize {
break
}
}
res.sha1 = hex.EncodeToString(digest.Sum(nil))
return nil
}
// reserveBlock takes a share of the upload's concurrency from slots and
// of the client's from uploadTokens, returning the function which
// gives both back.
func (c *client) reserveBlock(ctx context.Context, slots chan struct{}) (release func(), err error) {
select {
case slots <- struct{}{}:
case <-ctx.Done():
return nil, ctx.Err()
}
if err := acquireToken(ctx, c.uploadTokens); err != nil {
<-slots
return nil, err
}
return func() {
c.uploadTokens.Put()
<-slots
}, nil
}
// acquireToken gets a token from td, or returns the error of ctx if it
// is done first. TokenDispenser.Get can't be cancelled, so an abandoned
// wait gives the token back when it gets it.
func acquireToken(ctx context.Context, td *pacer.TokenDispenser) error {
if err := ctx.Err(); err != nil {
return err
}
got := make(chan struct{})
go func() {
td.Get()
select {
case got <- struct{}{}:
case <-ctx.Done():
td.Put()
}
}()
select {
case <-got:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
// sealBlock encrypts and signs the block in plain as block index of d,
// checks it decrypts with the server's content key verifySK and makes
// its verifier token from code. As in the SDK, a block whose check
// fails, which may be a bit flip, is encrypted once more.
func (c *client) sealBlock(d *draft, plain *pool.RW, index int, code []byte, verifySK *crypto.SessionKey, signKR *crypto.KeyRing) (b *encryptedBlock, hash []byte, err error) {
for attempt := 1; ; attempt++ {
b, hash, err = c.sealBlockOnce(d, plain, index, code, verifySK, signKR)
if err == nil || attempt == blockSealAttempts {
return b, hash, err
}
fs.Debugf(nil, "protondrive: block %d: %v: encrypting it again", index, err)
}
}
// sealBlockOnce makes one attempt of sealBlock.
func (c *client) sealBlockOnce(d *draft, plain *pool.RW, index int, code []byte, verifySK *crypto.SessionKey, signKR *crypto.KeyRing) (*encryptedBlock, []byte, error) {
if _, err := plain.Seek(0, io.SeekStart); err != nil {
return nil, nil, err
}
enc := pool.NewRW(pool.Global())
h := sha256.New()
encSignature, err := encryptBlockTo(io.MultiWriter(enc, h), plain, d.sk, d.nodeKR, signKR, c.s.serverTime())
if err == nil {
err = checkBlock(enc, verifySK)
}
var token []byte
if err == nil {
token, err = blockToken(enc, code)
}
if err != nil {
_ = enc.Close()
return nil, nil, err
}
if c.verifierHook != nil {
c.verifierHook(token)
}
return &encryptedBlock{index: index, enc: enc, encSignature: encSignature, token: token}, h.Sum(nil), nil
}
// checkBlock checks the encrypted block enc decrypts with sk.
func checkBlock(enc *pool.RW, sk *crypto.SessionKey) error {
if _, err := enc.Seek(0, io.SeekStart); err != nil {
return err
}
dec, err := blockDecrypter(enc, sk)
if err == nil {
_, err = io.Copy(io.Discard, dec)
}
if err != nil {
return fmt.Errorf("encrypted block doesn't decrypt with the file's content key: %w", err)
}
return nil
}
// blockToken returns the verifier token of the encrypted block enc.
func blockToken(enc *pool.RW, code []byte) ([]byte, error) {
if _, err := enc.Seek(0, io.SeekStart); err != nil {
return nil, err
}
prefix := make([]byte, len(code))
n, err := io.ReadFull(enc, prefix)
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
return nil, err
}
return verifierToken(code, prefix[:n]), nil
}
// requestURLs takes blocks from ready as they come, requests their
// upload URLs in batches of those waiting and passes them to uploads.
func (c *client) requestURLs(ctx context.Context, d *draft, ready <-chan *encryptedBlock, uploads chan<- blockUpload) error {
for {
var batch []*encryptedBlock
select {
case b, ok := <-ready:
if !ok {
return nil
}
batch = append(batch, b)
case <-ctx.Done():
return ctx.Err()
}
done := false
waiting:
for !done {
select {
case b, ok := <-ready:
if ok {
batch = append(batch, b)
}
done = !ok
default:
break waiting
}
}
links, err := c.blockURLs(ctx, d, batch)
if err != nil {
freeBlocks(batch)
return err
}
for i, b := range batch {
select {
case uploads <- blockUpload{block: b, link: links[i]}:
case <-ctx.Done():
freeBlocks(batch[i:])
return ctx.Err()
}
}
if done {
return nil
}
}
}
// blockURLs requests the upload URLs of blocks of draft d, in the
// order of blocks.
func (c *client) blockURLs(ctx context.Context, d *draft, blocks []*encryptedBlock) ([]api.UploadLink, error) {
req := &api.BlockUploadRequest{
AddressID: c.addr.ID,
VolumeID: c.volumeID,
LinkID: d.nodeID,
RevisionID: d.revisionID,
BlockList: make([]api.BlockUploadInfo, 0, len(blocks)),
ThumbnailList: []struct{}{},
}
for _, b := range blocks {
req.BlockList = append(req.BlockList, api.BlockUploadInfo{
Index: b.index,
EncSignature: b.encSignature,
Verifier: api.BlockVerifier{Token: base64.StdEncoding.EncodeToString(b.token)},
})
}
var resp api.BlockUploadResponse
if err := c.apiCall(ctx, &rest.Opts{Method: "POST", Path: "/drive/blocks"}, req, &resp); err != nil {
return nil, fmt.Errorf("request upload URLs: %w", err)
}
byIndex := make(map[int]api.UploadLink, len(resp.UploadLinks))
for _, l := range resp.UploadLinks {
byIndex[l.Index] = l
}
links := make([]api.UploadLink, len(blocks))
for i, b := range blocks {
l, ok := byIndex[b.index]
if !ok || l.BareURL == "" {
return nil, fmt.Errorf("request upload URLs: no URL for block %d", b.index)
}
links[i] = l
}
return links, nil
}
// uploadBlock uploads block u to the storage host, trying up to
// blockUploadAttempts times. A 404 means the URL expired, so a new one
// is requested.
func (c *client) uploadBlock(ctx context.Context, d *draft, u blockUpload) error {
link := u.link
for attempt := 1; ; attempt++ {
retry, err := c.postBlock(ctx, link, u.block.enc)
var apiErr *api.Error
switch {
case err == nil:
return nil
case ctx.Err() != nil:
return ctx.Err()
case attempt >= blockUploadAttempts:
return err
case errors.As(err, &apiErr) && apiErr.Status == http.StatusNotFound:
fs.Debugf(nil, "protondrive: block %d: upload URL expired: requesting a new one", u.block.index)
links, err := c.blockURLs(ctx, d, []*encryptedBlock{u.block})
if err != nil {
return err
}
link = links[0]
case !retry:
return err
default:
fs.Debugf(nil, "protondrive: block %d: upload failed: %v: retrying", u.block.index, err)
}
}
}
// postBlock makes one attempt to upload the encrypted block enc to
// link, paced by the storage pacer, and reports whether a failure may
// be retried.
func (c *client) postBlock(ctx context.Context, link api.UploadLink, enc *pool.RW) (retry bool, err error) {
paced := c.paced(ctx, func() (*http.Response, error) {
if _, err := enc.Seek(0, io.SeekStart); err != nil {
return nil, err
}
body := newMultipartBlock(enc)
defer body.finish()
opts := &rest.Opts{
Method: "POST",
RootURL: link.BareURL,
Body: body,
ContentType: body.contentType,
ContentLength: &body.size,
NoResponse: true,
// The storage host gets the block's token, never the
// session's credentials, which only session.call adds.
ExtraHeaders: map[string]string{"pm-storage-token": link.Token},
}
return c.s.srv.Call(ctx, opts)
})
err = c.storagePacer.CallNoRetry(func() (bool, error) {
var err error
retry, err = paced()
return retry, err
})
return retry, err
}
// errAttemptOver is returned by reads of a multipartBlock whose upload
// attempt is over.
var errAttemptOver = errors.New("upload attempt is over")
// multipartBlock is the multipart/form-data body of one attempt to
// upload a block, as the SDK's uploadBlock sends it: the block in the
// field Block.
//
// The HTTP transport may still read a request body after the request
// returns, so the body stops reading the block once finished: the
// block may then be read for a retry, or freed.
type multipartBlock struct {
contentType string
size int64
mu sync.Mutex
r io.Reader
done bool
}
// newMultipartBlock returns the body uploading the block in enc from
// its read position.
func newMultipartBlock(enc *pool.RW) *multipartBlock {
var buf bytes.Buffer
mw := multipart.NewWriter(&buf)
// Writes to a bytes.Buffer don't fail.
_, _ = mw.CreateFormFile("Block", "blob")
head := buf.String()
buf.Reset()
_ = mw.Close()
tail := buf.String()
pos, _ := enc.Seek(0, io.SeekCurrent)
return &multipartBlock{
contentType: mw.FormDataContentType(),
size: int64(len(head)+len(tail)) + enc.Size() - pos,
r: io.MultiReader(strings.NewReader(head), enc, strings.NewReader(tail)),
}
}
// Read reads the body, or fails once the body is finished.
func (b *multipartBlock) Read(p []byte) (int, error) {
b.mu.Lock()
defer b.mu.Unlock()
if b.done {
return 0, errAttemptOver
}
return b.r.Read(p)
}
// finish stops the body reading the block, waiting for a read in
// progress.
func (b *multipartBlock) finish() {
b.mu.Lock()
b.done = true
b.mu.Unlock()
}
// deleteDraft deletes draft d of a failed upload, even if ctx is done,
// logging a failure: the draft stands in the way of the next upload of
// the name until it is replaced.
func (c *client) deleteDraft(ctx context.Context, d *draft) {
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), draftCleanupTimeout)
defer cancel()
var err error
if d.newFile {
err = c.deleteDraftLink(ctx, d.nodeID)
} else {
err = c.apiCall(ctx, &rest.Opts{Method: "DELETE", Path: c.volumePath("/drive/v2/volumes", "files", d.nodeID, "revisions", d.revisionID)}, nil, nil)
}
c.forget(d.nodeID, d.parentID)
if err != nil {
fs.Errorf(nil, "protondrive: failed to delete the draft of a failed upload (node %s, revision %s): %v", d.nodeID, d.revisionID, err)
}
}
// deleteDraftLink permanently deletes the draft node id.
func (c *client) deleteDraftLink(ctx context.Context, id string) error {
var resp api.MultiLinkResponse
opts := &rest.Opts{Method: "POST", Path: c.volumePath("/drive/v2/volumes") + "/delete_multiple"}
if err := c.apiCall(ctx, opts, &api.LinksRequest{LinkIDs: []string{id}}, &resp); err != nil {
return fmt.Errorf("delete draft: %w", err)
}
for _, r := range resp.Responses {
if r.Response.Code/1000 != 1 || r.Response.Error != "" {
return mutationError("delete draft "+r.LinkID, &api.Error{Code: r.Response.Code, Message: r.Response.Error})
}
}
return nil
}
+975
View File
@@ -0,0 +1,975 @@
package protondrive
import (
"bytes"
"context"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/rclone/rclone/backend/protondrive/api"
"github.com/rclone/rclone/backend/protondrive/protontest"
"github.com/rclone/rclone/lib/pacer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
filesPattern = "/drive/v2/volumes/{volumeID}/files"
commitPattern = "/drive/v2/volumes/{volumeID}/files/{linkID}/revisions/{revisionID}"
deleteDraftsPattern = "/drive/v2/volumes/{volumeID}/delete_multiple"
storageUploadPattern = "/storage/upload/{token}"
testDraftClientUID = "rclone-test-draft-client"
)
// newUploadClient returns a client of a new user of a new fake server
// with a draft client UID and the options opt.
func newUploadClient(t *testing.T, opt clientOptions) (*client, *protontest.Server, *protontest.User) {
t.Helper()
if opt.DraftClientUID == "" {
opt.DraftClientUID = testDraftClientUID
}
return newTestClientOpt(t, opt)
}
// testModTime is the modification time of uploaded files.
var testModTime = time.Date(2024, 5, 6, 7, 8, 9, 0, time.UTC)
// sha1Of returns the hex SHA-1 of b.
func sha1Of(b []byte) string {
sum := sha1.Sum(b)
return hex.EncodeToString(sum[:])
}
// uploadContent uploads content as name in the root with size declared
// as size.
func uploadContent(ctx context.Context, c *client, name string, in io.Reader, size int64) (*node, error) {
return c.upload(ctx, uploadRequest{
ParentID: c.rootID(),
Name: name,
In: in,
Size: size,
ModTime: testModTime,
MIMEType: "application/octet-stream",
})
}
// uploadBytes uploads content as name in the root, declaring its size.
func uploadBytes(t *testing.T, c *client, name string, content []byte) *node {
t.Helper()
n, err := uploadContent(context.Background(), c, name, bytes.NewReader(content), int64(len(content)))
require.NoError(t, err)
return n
}
// requireUploaded fails the test unless the fake decrypts and verifies
// every node of u without error, and finds the files of want, by path,
// with that content, the test modification time and its SHA1.
func requireUploaded(t *testing.T, u *protontest.User, want map[string][]byte) {
t.Helper()
dump := u.Dump()
got := make(map[string]bool)
for _, d := range dump {
require.NoError(t, d.Err, "node %q", d.Path)
content, ok := want[d.Path]
if !ok {
continue
}
got[d.Path] = true
assert.True(t, bytes.Equal(content, d.Content), "%q: content differs: %d bytes, want %d", d.Path, len(d.Content), len(content))
assert.Equal(t, sha1Of(content), d.SHA1, "%q: SHA1", d.Path)
assert.True(t, testModTime.Equal(d.ModTime), "%q: modification time %v", d.Path, d.ModTime)
}
for path := range want {
assert.True(t, got[path], "%q not in the drive", path)
}
assert.Empty(t, u.Drafts(), "drafts left")
}
// files returns the paths of the files in u's drive.
func files(u *protontest.User) []string {
var paths []string
for _, d := range u.Dump() {
if !d.IsFolder {
paths = append(paths, d.Path)
}
}
return paths
}
func TestUploadSizes(t *testing.T) {
t.Parallel()
c, srv, u := newUploadClient(t, clientOptions{})
want := make(map[string][]byte)
for _, size := range []int{0, 1, 4<<20 - 1, 4 << 20, 4<<20 + 1, bigSize} {
t.Run(strconv.Itoa(size), func(t *testing.T) {
name := "f" + strconv.Itoa(size)
content := testContent(size)
want[name] = content
srv.ResetRequests()
n := uploadBytes(t, c, name, content)
blocks := (size + 4<<20 - 1) / (4 << 20)
assert.Equal(t, blocks, srv.Requests()["POST "+storageUploadPattern], "blocks uploaded")
assert.Equal(t, name, n.Name)
assert.Equal(t, int64(size), n.size(true))
assert.Equal(t, sha1Of(content), n.sha1())
assert.True(t, testModTime.Equal(n.modTime()))
require.NotNil(t, n.X)
assert.Len(t, n.X.BlockSizes, blocks)
})
}
requireUploaded(t, u, want)
}
func TestUploadUnknownSize(t *testing.T) {
t.Parallel()
c, _, u := newUploadClient(t, clientOptions{})
want := make(map[string][]byte)
for _, size := range []int{0, 5 << 20} {
name := "f" + strconv.Itoa(size)
want[name] = testContent(size)
n, err := uploadContent(context.Background(), c, name, bytes.NewReader(want[name]), -1)
require.NoError(t, err)
assert.Equal(t, int64(size), n.size(true))
}
requireUploaded(t, u, want)
}
func TestUploadNewRevision(t *testing.T) {
t.Parallel()
c, _, u := newUploadClient(t, clientOptions{})
first := uploadBytes(t, c, "f", testContent(5<<20))
content := []byte("new content")
n, err := c.upload(context.Background(), uploadRequest{
Existing: first,
In: bytes.NewReader(content),
Size: int64(len(content)),
ModTime: testModTime,
SHA1: sha1Of(content),
})
require.NoError(t, err)
assert.Equal(t, first.ID, n.ID)
assert.NotEqual(t, first.RevisionID, n.RevisionID)
requireUploaded(t, u, map[string][]byte{"f": content})
assert.Equal(t, []string{"f"}, files(u))
old, err := c.revisionBlocks(context.Background(), first, 1, 1)
require.NoError(t, err)
assert.Equal(t, 2, old.State, "old revision not superseded")
}
func TestUploadOverExistingNameCreatesRevision(t *testing.T) {
t.Parallel()
c, _, u := newUploadClient(t, clientOptions{})
first := uploadBytes(t, c, "f", []byte("first"))
n := uploadBytes(t, c, "f", []byte("second"))
assert.Equal(t, first.ID, n.ID)
assert.NotEqual(t, first.RevisionID, n.RevisionID)
requireUploaded(t, u, map[string][]byte{"f": []byte("second")})
assert.Equal(t, []string{"f"}, files(u))
// A folder in the way is not replaced.
_, err := c.createFolder(context.Background(), c.rootID(), "dir", time.Time{})
require.NoError(t, err)
_, err = uploadContent(context.Background(), c, "dir", strings.NewReader("x"), 1)
assert.ErrorIs(t, err, errExists)
assert.Empty(t, u.Drafts())
}
// testSourceMismatch uploads actual bytes declared as 5 MiB, which must
// fail cleanly.
func testSourceMismatch(t *testing.T, actual int) {
c, srv, u := newUploadClient(t, clientOptions{})
goroutines, buffers := leakBaseline(srv)
_, err := uploadContent(context.Background(), c, "f", bytes.NewReader(testContent(actual)), 5<<20)
assert.ErrorIs(t, err, errUploadMismatch)
assert.Empty(t, u.Drafts())
assert.Empty(t, files(u))
requireNoLeaks(t, srv, goroutines, buffers)
}
func TestUploadShortSource(t *testing.T) {
testSourceMismatch(t, 3<<20)
testSourceMismatch(t, 0)
}
func TestUploadLongSource(t *testing.T) {
testSourceMismatch(t, 6<<20)
testSourceMismatch(t, 9<<20)
}
func TestUploadSHA1Mismatch(t *testing.T) {
t.Parallel()
c, _, u := newUploadClient(t, clientOptions{})
content := testContent(1000)
_, err := c.upload(context.Background(), uploadRequest{
ParentID: c.rootID(), Name: "f", In: bytes.NewReader(content), Size: 1000,
SHA1: sha1Of([]byte("something else")),
})
assert.ErrorIs(t, err, errUploadMismatch)
assert.Empty(t, u.Drafts())
assert.Empty(t, files(u))
_, err = c.upload(context.Background(), uploadRequest{
ParentID: c.rootID(), Name: "f", In: bytes.NewReader(content), Size: 1000,
ModTime: testModTime, SHA1: strings.ToUpper(sha1Of(content)),
})
require.NoError(t, err)
requireUploaded(t, u, map[string][]byte{"f": content})
}
func TestUploadBlock502Once(t *testing.T) {
t.Parallel()
c, srv, u := newUploadClient(t, clientOptions{})
srv.Inject(protontest.Fault{Method: "POST", Pattern: storageUploadPattern, Status: http.StatusBadGateway, Times: 1})
srv.ResetRequests()
content := testContent(5 << 20)
uploadBytes(t, c, "f", content)
requireUploaded(t, u, map[string][]byte{"f": content})
assert.Equal(t, 3, srv.Requests()["POST "+storageUploadPattern], "two blocks and the resent one")
}
func TestRefusedCommit(t *testing.T) {
for _, test := range []struct {
name string
err error
want bool
}{
{"422 with a code", &api.Error{Status: 422, Code: api.CodeInvalidRequirements}, true},
{"408 with a code", &api.Error{Status: 408, Code: api.CodeInvalidRequirements}, false},
{"422 without a code", &api.Error{Status: 422}, false},
{"500 with a code", &api.Error{Status: 500, Code: api.CodeInvalidRequirements}, false},
{"200 with a code", &api.Error{Status: 200, Code: api.CodeInvalidRequirements}, false},
{"network error", io.ErrUnexpectedEOF, false},
} {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.want, refusedCommit(fmt.Errorf("commit: %w", test.err)))
})
}
}
// TestUploadBlock502Forever is issue 10224: a block which can't be
// uploaded must fail the upload, not hang it.
func TestUploadBlock502Forever(t *testing.T) {
c, srv, u := newUploadClient(t, clientOptions{})
goroutines, buffers := leakBaseline(srv)
srv.Inject(protontest.Fault{Method: "POST", Pattern: storageUploadPattern, Status: http.StatusBadGateway})
start := time.Now()
_, err := uploadContent(context.Background(), c, "f", bytes.NewReader(testContent(9<<20)), 9<<20)
require.Error(t, err)
assert.Less(t, time.Since(start), 30*time.Second)
var apiErr *api.Error
require.ErrorAs(t, err, &apiErr)
assert.Equal(t, http.StatusBadGateway, apiErr.Status)
assert.Empty(t, u.Drafts())
requireNoLeaks(t, srv, goroutines, buffers)
}
func TestUploadStorageTokenExpired(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
var expired atomic.Bool
c := newTestClientTransport(t, srv, u, clientOptions{DraftClientUID: testDraftClientUID}, func(next http.RoundTripper) http.RoundTripper {
return roundTripFunc(func(r *http.Request) (*http.Response, error) {
if strings.HasPrefix(r.URL.Path, "/storage/upload/") && !expired.Swap(true) {
srv.ExpireUploadTokens()
}
return next.RoundTrip(r)
})
})
content := testContent(9 << 20)
uploadBytes(t, c, "f", content)
assert.Greater(t, srv.Requests()["POST /drive/blocks"], 1, "no new URL requested")
assert.Greater(t, srv.Requests()["POST "+storageUploadPattern], 3, "no upload refused")
requireUploaded(t, u, map[string][]byte{"f": content})
}
func TestUploadCommitErrorButCommitted(t *testing.T) {
t.Parallel()
c, srv, u := newUploadClient(t, clientOptions{})
srv.Inject(protontest.Fault{Method: "PUT", Pattern: commitPattern, Status: http.StatusBadGateway, AfterHandler: true, Times: 1})
content := testContent(1 << 20)
n := uploadBytes(t, c, "f", content)
assert.Equal(t, 2, srv.Requests()["PUT "+commitPattern], "commit not retried")
assert.Equal(t, "f", n.Name)
requireUploaded(t, u, map[string][]byte{"f": content})
assert.Equal(t, []string{"f"}, files(u))
}
func TestUploadDraftResponseLost(t *testing.T) {
t.Parallel()
c, srv, u := newUploadClient(t, clientOptions{})
srv.Inject(protontest.Fault{Method: "POST", Pattern: filesPattern, Status: http.StatusBadGateway, AfterHandler: true, Times: 1})
content := testContent(1000)
uploadBytes(t, c, "f", content)
assert.Equal(t, 3, srv.Requests()["POST "+filesPattern], "draft creation: first, retry, after deleting the draft")
requireUploaded(t, u, map[string][]byte{"f": content})
assert.Equal(t, []string{"f"}, files(u))
}
// leaveDraft makes an upload of name by c fail at commit and its
// cleanup fail too, leaving its draft behind.
func leaveDraft(t *testing.T, c *client, srv *protontest.Server, u *protontest.User, name string) {
t.Helper()
srv.Inject(protontest.Fault{Method: "PUT", Pattern: commitPattern, Status: http.StatusUnprocessableEntity, Code: api.CodeInvalidValue, Times: 1})
srv.Inject(protontest.Fault{Method: "POST", Pattern: deleteDraftsPattern, Status: http.StatusUnprocessableEntity, Code: api.CodeInvalidValue, Times: 1})
_, err := uploadContent(context.Background(), c, name, strings.NewReader("draft"), 5)
require.Error(t, err)
require.Len(t, u.Drafts(), 1)
}
// TestUploadOwnDraftReplaced is issue 9622: a draft left by a failed
// upload must not block the next upload of the name.
func TestUploadOwnDraftReplaced(t *testing.T) {
t.Parallel()
c, srv, u := newUploadClient(t, clientOptions{})
leaveDraft(t, c, srv, u, "f")
content := testContent(1000)
uploadBytes(t, c, "f", content)
requireUploaded(t, u, map[string][]byte{"f": content})
assert.Equal(t, []string{"f"}, files(u))
}
func TestUploadForeignDraft(t *testing.T) {
t.Parallel()
other, srv, u := newUploadClient(t, clientOptions{DraftClientUID: "another client"})
leaveDraft(t, other, srv, u, "f")
c := newTestClientFor(t, srv, u, clientOptions{DraftClientUID: testDraftClientUID})
_, err := uploadContent(context.Background(), c, "f", strings.NewReader("x"), 1)
assert.ErrorIs(t, err, errExists)
assert.Len(t, u.Drafts(), 1, "another client's draft deleted")
// Without a draft client UID no draft is the client's own.
anon := newTestClientFor(t, srv, u, clientOptions{})
_, err = uploadContent(context.Background(), anon, "f", strings.NewReader("x"), 1)
assert.ErrorIs(t, err, errExists)
replacer := newTestClientFor(t, srv, u, clientOptions{DraftClientUID: testDraftClientUID, ReplaceExistingDraft: true})
content := testContent(1000)
uploadBytes(t, replacer, "f", content)
requireUploaded(t, u, map[string][]byte{"f": content})
assert.Equal(t, []string{"f"}, files(u))
}
func TestUploadCancel(t *testing.T) {
for _, tc := range []struct {
name string
fault protontest.Fault
drafts int // drafts left: a cancelled commit may have taken effect
}{
{"draft", protontest.Fault{Method: "POST", Pattern: filesPattern}, 0},
{"blocks", protontest.Fault{Method: "POST", Pattern: storageUploadPattern, Skip: 1}, 0},
{"commit", protontest.Fault{Method: "PUT", Pattern: commitPattern}, 1},
} {
t.Run(tc.name, func(t *testing.T) {
c, srv, u := newUploadClient(t, clientOptions{})
goroutines, buffers := leakBaseline(srv)
tc.fault.Delay = time.Minute
srv.Inject(tc.fault)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
start := time.Now()
_, err := uploadContent(ctx, c, "f", bytes.NewReader(testContent(9<<20)), 9<<20)
assert.ErrorIs(t, err, context.DeadlineExceeded)
assert.Less(t, time.Since(start), 10*time.Second)
assert.Len(t, u.Drafts(), tc.drafts)
assert.Empty(t, files(u))
requireNoLeaks(t, srv, goroutines, buffers)
})
}
}
func TestUploadConcurrencyBound(t *testing.T) {
t.Run("PerFile", func(t *testing.T) {
t.Parallel()
c, srv, u := newUploadClient(t, clientOptions{UploadConcurrency: 2})
srv.Inject(protontest.Fault{Method: "POST", Pattern: storageUploadPattern, Delay: 200 * time.Millisecond})
content := testContent(12<<20 + 1)
uploadBytes(t, c, "f", content)
assert.LessOrEqual(t, srv.StorageUploadPeak(), 2)
requireUploaded(t, u, map[string][]byte{"f": content})
})
t.Run("PerClient", func(t *testing.T) {
t.Parallel()
c, srv, u := newUploadClient(t, clientOptions{UploadConcurrency: maxUploadConcurrency})
srv.Inject(protontest.Fault{Method: "POST", Pattern: storageUploadPattern, Delay: 300 * time.Millisecond})
want := make(map[string][]byte)
var wg sync.WaitGroup
errs := make([]error, 16)
for i := range errs {
name := "f" + strconv.Itoa(i)
content := testContent(1000 + i)
want[name] = content
wg.Go(func() {
_, errs[i] = uploadContent(context.Background(), c, name, bytes.NewReader(content), int64(len(content)))
})
}
wg.Wait()
for _, err := range errs {
require.NoError(t, err)
}
peak := srv.StorageUploadPeak()
assert.LessOrEqual(t, peak, blocksInFlight)
assert.Greater(t, peak, 1, "uploads not concurrent")
requireUploaded(t, u, want)
})
}
func TestUploadVerifierChecked(t *testing.T) {
t.Parallel()
c, _, u := newUploadClient(t, clientOptions{})
c.verifierHook = func(token []byte) { token[0] ^= 1 }
_, err := uploadContent(context.Background(), c, "f", bytes.NewReader(testContent(1000)), 1000)
require.Error(t, err)
assert.Equal(t, api.CodeInvalidRequirements, apiErrorCode(err))
assert.Contains(t, err.Error(), "verifier token")
assert.Empty(t, u.Drafts())
assert.Empty(t, files(u))
}
func TestUploadClockSkew(t *testing.T) {
t.Parallel()
for _, skew := range []time.Duration{3 * time.Hour, -3 * time.Hour} {
t.Run(skew.String(), func(t *testing.T) {
srv := protontest.New(t)
srv.SetClockSkew(skew)
u := srv.AddUser("user", "pass")
c := newTestClientFor(t, srv, u, clientOptions{DraftClientUID: testDraftClientUID})
first := uploadBytes(t, c, "f", []byte("first"))
content := testContent(5 << 20)
_, err := c.upload(context.Background(), uploadRequest{Existing: first, In: bytes.NewReader(content), Size: -1, ModTime: testModTime})
require.NoError(t, err)
requireUploaded(t, u, map[string][]byte{"f": content})
})
}
}
func TestUploadThenDownload(t *testing.T) {
t.Parallel()
c, _, u := newUploadClient(t, clientOptions{})
content := testContent(bigSize)
n := uploadBytes(t, c, "f", content)
data, err := readRange(t, c, n, 0, -1)
require.NoError(t, err)
assert.True(t, bytes.Equal(content, data), "content differs")
data, err = readRange(t, c, n, bigSize-1, -1)
require.NoError(t, err)
assert.Equal(t, content[bigSize-1:], data)
requireUploaded(t, u, map[string][]byte{"f": content})
}
func TestIntendedUploadSize(t *testing.T) {
ptr := func(v int64) *int64 { return &v }
for _, tc := range []struct {
size int64
draft, revise *int64
}{
{-1, nil, nil},
{0, nil, nil},
{1, nil, ptr(4095)},
{4096, nil, ptr(100_000)},
{150_000, nil, ptr(100_000)},
{199_999, nil, ptr(100_000)},
{200_000, ptr(200_000), ptr(200_000)},
{9_876_543, ptr(9_800_000), ptr(9_800_000)},
} {
assert.Equal(t, tc.draft, draftIntendedSize(tc.size), "draft %d", tc.size)
assert.Equal(t, tc.revise, revisionIntendedSize(tc.size), "revision %d", tc.size)
}
}
// TestAcquireTokenCancel checks a cancelled wait for a token returns at
// once and gives the token back when it gets it.
func TestAcquireTokenCancel(t *testing.T) {
td := pacer.NewTokenDispenser(1)
td.Get()
ctx, cancel := context.WithCancel(context.Background())
done := make(chan error)
go func() { done <- acquireToken(ctx, td) }()
time.Sleep(20 * time.Millisecond)
cancel()
select {
case err := <-done:
assert.ErrorIs(t, err, context.Canceled)
case <-time.After(5 * time.Second):
t.Fatal("cancelled wait for a token didn't return")
}
td.Put()
got := make(chan error)
go func() { got <- acquireToken(context.Background(), td) }()
select {
case err := <-got:
require.NoError(t, err)
case <-time.After(5 * time.Second):
t.Fatal("token kept by the cancelled wait")
}
assert.True(t, errors.Is(acquireToken(ctx, td), context.Canceled))
}
const (
linksPattern = "/drive/v2/volumes/{volumeID}/links"
deleteRevisionRequest = "DELETE " + commitPattern
deleteDraftsRequest = "POST " + deleteDraftsPattern
)
// TestUploadCancelDuringCommit cancels an upload while a commit which
// the server makes is in flight: the committed file must not be deleted.
func TestUploadCancelDuringCommit(t *testing.T) {
t.Parallel()
for _, revision := range []bool{false, true} {
t.Run("revision="+strconv.FormatBool(revision), func(t *testing.T) {
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var armed atomic.Bool
c := newTestClientTransport(t, srv, u, clientOptions{DraftClientUID: testDraftClientUID}, func(next http.RoundTripper) http.RoundTripper {
return roundTripFunc(func(r *http.Request) (*http.Response, error) {
if !armed.Load() || r.Method != "PUT" || !strings.Contains(r.URL.Path, "/revisions/") {
return next.RoundTrip(r)
}
// The server commits, then the caller gives up
// before the response arrives.
resp, err := next.RoundTrip(r)
if err == nil {
_ = resp.Body.Close()
}
cancel()
return nil, context.Canceled
})
})
var existing *node
if revision {
existing = uploadBytes(t, c, "f", []byte("old"))
}
srv.ResetRequests()
armed.Store(true)
content := testContent(5 << 20)
_, err := c.upload(ctx, uploadRequest{ParentID: c.rootID(), Name: "f", Existing: existing, In: bytes.NewReader(content), Size: int64(len(content)), ModTime: testModTime})
require.NoError(t, err, "a commit which is active is a success")
reqs := srv.Requests()
assert.Zero(t, reqs[deleteDraftsRequest], "draft deletion sent")
assert.Zero(t, reqs[deleteRevisionRequest], "revision deletion sent")
requireUploaded(t, u, map[string][]byte{"f": content})
})
}
}
// TestUploadCommitOutcomeUnknown makes the commit fail without a
// refusal and the read which would tell whether it took effect fail
// too: nothing may be deleted, and the error must say the outcome is
// unknown.
func TestUploadCommitOutcomeUnknown(t *testing.T) {
t.Parallel()
c, srv, u := newUploadClient(t, clientOptions{})
srv.Inject(protontest.Fault{Method: "PUT", Pattern: commitPattern, Status: http.StatusUnprocessableEntity})
srv.Inject(protontest.Fault{Method: "POST", Pattern: linksPattern, Status: http.StatusUnprocessableEntity, Code: api.CodeInvalidValue})
_, err := uploadContent(context.Background(), c, "f", strings.NewReader("content"), 7)
require.Error(t, err)
assert.ErrorIs(t, err, errUploadOutcomeUnknown)
assert.Contains(t, err.Error(), "may have succeeded")
reqs := srv.Requests()
assert.Zero(t, reqs[deleteDraftsRequest], "draft deletion sent")
assert.Zero(t, reqs[deleteRevisionRequest], "revision deletion sent")
assert.Len(t, u.Drafts(), 1, "the draft is left to the own-draft rule")
}
// TestUploadCommitRefused makes the commit fail without effect: the
// draft is deleted.
func TestUploadCommitRefused(t *testing.T) {
t.Parallel()
c, srv, u := newUploadClient(t, clientOptions{})
srv.Inject(protontest.Fault{Method: "PUT", Pattern: commitPattern, Status: http.StatusUnprocessableEntity, Code: api.CodeInvalidValue, Times: 1})
_, err := uploadContent(context.Background(), c, "f", strings.NewReader("content"), 7)
require.Error(t, err)
assert.NotErrorIs(t, err, errUploadOutcomeUnknown)
assert.Equal(t, 1, srv.Requests()[deleteDraftsRequest])
assert.Empty(t, u.Drafts())
assert.Empty(t, files(u))
}
// failingReader reads r, then fails with err where r ends.
type failingReader struct {
r io.Reader
err error
}
// Read reads from r, then fails with err.
func (f *failingReader) Read(p []byte) (int, error) {
n, err := f.r.Read(p)
if err == io.EOF {
return n, f.err
}
return n, err
}
func TestUploadSourceError(t *testing.T) {
c, srv, u := newUploadClient(t, clientOptions{})
goroutines, buffers := leakBaseline(srv)
errSource := errors.New("source broke")
in := &failingReader{r: bytes.NewReader(testContent(5<<20 + 10)), err: errSource}
_, err := uploadContent(context.Background(), c, "f", in, 9<<20)
assert.ErrorIs(t, err, errSource)
assert.Empty(t, u.Drafts())
assert.Empty(t, files(u))
requireNoLeaks(t, srv, goroutines, buffers)
}
// TestUploadBlockFailsProducerBlocked fails every block upload with one
// worker, so the producer is waiting to hand on a block when the
// worker gives up.
func TestUploadBlockFailsProducerBlocked(t *testing.T) {
c, srv, u := newUploadClient(t, clientOptions{UploadConcurrency: 1})
goroutines, buffers := leakBaseline(srv)
srv.Inject(protontest.Fault{Method: "POST", Pattern: storageUploadPattern, Status: http.StatusBadGateway})
_, err := uploadContent(context.Background(), c, "f", bytes.NewReader(testContent(12<<20+1)), 12<<20+1)
require.Error(t, err)
assert.Equal(t, 3, srv.Requests()["POST "+storageUploadPattern], "one block, tried 3 times")
assert.Empty(t, u.Drafts())
assert.Empty(t, files(u))
requireNoLeaks(t, srv, goroutines, buffers)
}
// revisionIn returns the revision ID in the path of a request to a
// revision route, and the node ID.
func revisionIn(path string) (nodeID, revisionID string) {
parts := strings.Split(path, "/")
for i, p := range parts {
if p == "files" && i+3 < len(parts) && parts[i+2] == "revisions" {
return parts[i+1], parts[i+3]
}
}
return "", ""
}
// interveneAfterCommit returns a client of u whose commit requests reach
// the server, which commits, after which act runs with the node and
// revision IDs; the client then sees a network error unless keep is set.
func interveneAfterCommit(t *testing.T, srv *protontest.Server, u *protontest.User, keep bool, act func(nodeID, revisionID string)) (c *client, armed *atomic.Bool) {
armed = new(atomic.Bool)
c = newTestClientTransport(t, srv, u, clientOptions{DraftClientUID: testDraftClientUID}, func(next http.RoundTripper) http.RoundTripper {
return roundTripFunc(func(r *http.Request) (*http.Response, error) {
if r.Method != "PUT" || !armed.Load() {
return next.RoundTrip(r)
}
resp, err := next.RoundTrip(r)
armed.Store(false)
act(revisionIn(r.URL.Path))
if keep {
return resp, err
}
if err == nil {
_ = resp.Body.Close()
}
return nil, errors.New("connection reset")
})
})
return c, armed
}
// requireNoDeletes fails the test if a draft or revision deletion was
// sent.
func requireNoDeletes(t *testing.T, srv *protontest.Server) {
t.Helper()
reqs := srv.Requests()
assert.Zero(t, reqs[deleteDraftsRequest], "draft deletion sent")
assert.Zero(t, reqs[deleteRevisionRequest], "revision deletion sent")
}
// TestUploadCommitThenChanged commits an upload whose response is lost,
// after which another client changes the file: the follow-up read
// can't prove what happened, so nothing may be deleted.
func TestUploadCommitThenChanged(t *testing.T) {
t.Parallel()
other := []byte("another client's content")
t.Run("NewFileNewRevision", func(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
c, armed := interveneAfterCommit(t, srv, u, false, func(nodeID, _ string) { u.SeedRevision(nodeID, other, testModTime) })
armed.Store(true)
_, err := uploadContent(context.Background(), c, "f", strings.NewReader("mine"), 4)
assert.ErrorIs(t, err, errUploadOutcomeUnknown)
requireNoDeletes(t, srv)
requireUploaded(t, u, map[string][]byte{"f": other})
})
t.Run("NewFileTrashed", func(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
trasher := newTestClientFor(t, srv, u, clientOptions{})
c, armed := interveneAfterCommit(t, srv, u, false, func(nodeID, _ string) {
require.NoError(t, trasher.trash(context.Background(), nodeID))
})
armed.Store(true)
_, err := uploadContent(context.Background(), c, "f", strings.NewReader("mine"), 4)
assert.ErrorIs(t, err, errUploadOutcomeUnknown)
requireNoDeletes(t, srv)
dump := requireDump(t, u)
require.Contains(t, dump, trashedPrefix+"f")
assert.Equal(t, []byte("mine"), dump[trashedPrefix+"f"].Content)
})
t.Run("NewRevision", func(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
var ours string
c, armed := interveneAfterCommit(t, srv, u, false, func(nodeID, revisionID string) {
ours = revisionID
u.SeedRevision(nodeID, other, testModTime)
})
first := uploadBytes(t, c, "f", []byte("first"))
srv.ResetRequests()
armed.Store(true)
_, err := c.upload(context.Background(), uploadRequest{Existing: first, In: strings.NewReader("mine"), Size: 4, ModTime: testModTime})
assert.ErrorIs(t, err, errUploadOutcomeUnknown)
requireNoDeletes(t, srv)
requireUploaded(t, u, map[string][]byte{"f": other})
// Our committed revision is kept, superseded.
mine := *first
mine.RevisionID = ours
rev, err := c.revisionBlocks(context.Background(), &mine, 1, 1)
require.NoError(t, err)
assert.Equal(t, 2, rev.State)
})
}
// TestUploadCommittedThenSuperseded commits an upload which succeeds,
// after which another client gives the file a new revision before it
// is read back: the other revision is not returned as the upload's.
func TestUploadCommittedThenSuperseded(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
other := []byte("another client's content")
c, armed := interveneAfterCommit(t, srv, u, true, func(nodeID, _ string) { u.SeedRevision(nodeID, other, testModTime) })
armed.Store(true)
n, err := uploadContent(context.Background(), c, "f", strings.NewReader("mine"), 4)
assert.ErrorIs(t, err, errUploadSuperseded)
assert.Nil(t, n)
requireNoDeletes(t, srv)
requireUploaded(t, u, map[string][]byte{"f": other})
}
// TestUploadRevisionCommitRefused makes the commit of a new revision
// fail without effect: the draft revision is deleted and the file keeps
// its content.
func TestUploadRevisionCommitRefused(t *testing.T) {
t.Parallel()
c, srv, u := newUploadClient(t, clientOptions{})
first := uploadBytes(t, c, "f", []byte("first"))
srv.ResetRequests()
srv.Inject(protontest.Fault{Method: "PUT", Pattern: commitPattern, Status: http.StatusUnprocessableEntity, Code: api.CodeInvalidValue, Times: 1})
_, err := c.upload(context.Background(), uploadRequest{Existing: first, In: strings.NewReader("mine"), Size: 4, ModTime: testModTime})
require.Error(t, err)
assert.NotErrorIs(t, err, errUploadOutcomeUnknown)
assert.Equal(t, 1, srv.Requests()[deleteRevisionRequest])
requireUploaded(t, u, map[string][]byte{"f": []byte("first")})
}
// TestUploadReadBackLag uploads while the server takes a moment to show
// what was committed, as Proton's does: the upload still succeeds with
// its own revision and nothing is deleted.
func TestUploadReadBackLag(t *testing.T) {
t.Parallel()
content := []byte("content")
for _, tc := range []struct {
name string
view protontest.LagView
existing bool
}{
{name: "NewFileStale", view: protontest.LagBefore},
{name: "NewFileMissing", view: protontest.LagMissing},
{name: "NewFileNoRevision", view: protontest.LagNoRevision},
{name: "NewRevisionStale", view: protontest.LagBefore, existing: true},
{name: "NewRevisionMissing", view: protontest.LagMissing, existing: true},
{name: "NewRevisionNoRevision", view: protontest.LagNoRevision, existing: true},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
c, srv, u := newUploadClient(t, clientOptions{})
r := uploadRequest{ParentID: c.rootID(), Name: "f", In: bytes.NewReader(content), Size: int64(len(content)), ModTime: testModTime}
var first *node
if tc.existing {
first = uploadBytes(t, c, "f", []byte("first"))
r.Existing = first
}
srv.ResetRequests()
srv.SetReadLag(2, tc.view)
n, err := c.upload(context.Background(), r)
require.NoError(t, err)
assert.NotEmpty(t, n.RevisionID)
if first != nil {
assert.NotEqual(t, first.RevisionID, n.RevisionID)
}
assert.Equal(t, sha1Of(content), n.sha1())
requireNoDeletes(t, srv)
requireUploaded(t, u, map[string][]byte{"f": content})
})
}
}
// TestUploadIntoNewFolderReadBackLag creates nested folders and uploads
// into the deepest without a cache while the server takes a moment to
// show new nodes.
func TestUploadIntoNewFolderReadBackLag(t *testing.T) {
t.Parallel()
c, srv, u := newTestClientOpt(t, clientOptions{DraftClientUID: testDraftClientUID})
srv.SetReadLag(2, protontest.LagMissing)
ctx := context.Background()
parentID := c.rootID()
for _, name := range []string{"A1", "B1", "C1"} {
n, err := c.createFolder(ctx, parentID, name, time.Time{})
require.NoError(t, err)
parentID = n.ID
}
content := []byte("aaa")
_, err := c.upload(ctx, uploadRequest{ParentID: parentID, Name: "one", In: bytes.NewReader(content), Size: int64(len(content)), ModTime: testModTime})
require.NoError(t, err)
requireNoDeletes(t, srv)
requireUploaded(t, u, map[string][]byte{"A1/B1/C1/one": content})
}
// TestUploadReadBackLagOutlasted commits an upload which the server
// doesn't show for longer than the client reads it back: the upload
// fails as unconfirmed and nothing is deleted.
func TestUploadReadBackLagOutlasted(t *testing.T) {
t.Parallel()
for _, view := range []protontest.LagView{protontest.LagBefore, protontest.LagMissing, protontest.LagNoRevision} {
t.Run(fmt.Sprintf("view=%d", view), func(t *testing.T) {
t.Parallel()
c, srv, u := newUploadClient(t, clientOptions{})
c.readBackDelay = time.Millisecond
srv.SetReadLag(readBackAttempts, view)
n, err := uploadContent(context.Background(), c, "f", strings.NewReader("mine"), 4)
assert.ErrorIs(t, err, errUploadUnconfirmed)
assert.NotErrorIs(t, err, errUploadSuperseded)
assert.Nil(t, n)
assert.Equal(t, readBackAttempts, srv.Requests()["POST /drive/v2/volumes/{volumeID}/links"])
requireNoDeletes(t, srv)
requireUploaded(t, u, map[string][]byte{"f": []byte("mine")})
})
}
}
// commitFailure returns a client of u whose next commit lands on srv
// and then fails.
type commitFailure func(t *testing.T, srv *protontest.Server, u *protontest.User) *client
// TestUploadAmbiguousCommitFailureUnderLag makes a commit land on the
// server and then fail in a way which doesn't prove it didn't, while
// the read back shows the node as before the commit: nothing may be
// deleted, as the deletion could hit the committed file or revision.
func TestUploadAmbiguousCommitFailureUnderLag(t *testing.T) {
t.Parallel()
failures := map[string]commitFailure{
// The commit lands and its response is lost; the resent commit
// is refused, as the draft revision is gone.
"ResentThenRefused": func(t *testing.T, srv *protontest.Server, u *protontest.User) *client {
srv.Inject(protontest.Fault{Method: "PUT", Pattern: commitPattern, Status: http.StatusBadGateway, AfterHandler: true, Times: 1})
srv.Inject(protontest.Fault{Method: "PUT", Pattern: commitPattern, Status: http.StatusUnprocessableEntity, Code: api.CodeInvalidValue, AfterHandler: true, Skip: 1, Times: 1})
return newTestClientFor(t, srv, u, clientOptions{DraftClientUID: testDraftClientUID})
},
// The commit lands and the connection drops before its response.
"ConnectionLost": func(t *testing.T, srv *protontest.Server, u *protontest.User) *client {
c, armed := interveneAfterCommit(t, srv, u, false, func(string, string) {})
armed.Store(true)
return c
},
}
for name, fail := range failures {
for _, existing := range []bool{false, true} {
t.Run(fmt.Sprintf("%s/existing=%v", name, existing), func(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
var first *node
if existing {
c := newTestClientFor(t, srv, u, clientOptions{DraftClientUID: testDraftClientUID})
first = uploadBytes(t, c, "f", []byte("first"))
}
c := fail(t, srv, u)
c.readBackDelay = time.Millisecond
srv.ResetRequests()
srv.SetReadLag(readBackAttempts, protontest.LagBefore)
r := uploadRequest{ParentID: c.rootID(), Name: "f", Existing: first, In: strings.NewReader("mine"), Size: 4, ModTime: testModTime}
_, err := c.upload(context.Background(), r)
assert.ErrorIs(t, err, errUploadOutcomeUnknown)
requireNoDeletes(t, srv)
requireUploaded(t, u, map[string][]byte{"f": []byte("mine")})
})
}
}
}
// TestUploadCommitRefusedNoReadBack makes the server refuse a commit on
// its only attempt: the refusal alone proves it didn't land, so the
// draft is deleted without reading the file back, as a read may lag.
func TestUploadCommitRefusedNoReadBack(t *testing.T) {
t.Parallel()
for _, existing := range []bool{false, true} {
t.Run(fmt.Sprintf("existing=%v", existing), func(t *testing.T) {
t.Parallel()
c, srv, u := newUploadClient(t, clientOptions{})
var first *node
want := map[string][]byte{}
if existing {
first = uploadBytes(t, c, "f", []byte("first"))
want["f"] = []byte("first")
}
srv.ResetRequests()
srv.Inject(protontest.Fault{Method: "PUT", Pattern: commitPattern, Status: http.StatusUnprocessableEntity, Code: api.CodeInvalidValue, Times: 1})
_, err := c.upload(context.Background(), uploadRequest{ParentID: c.rootID(), Name: "f", Existing: first, In: strings.NewReader("mine"), Size: 4, ModTime: testModTime})
require.Error(t, err)
assert.NotErrorIs(t, err, errUploadOutcomeUnknown)
reqs := srv.Requests()
assert.Equal(t, 1, reqs["PUT "+commitPattern])
assert.Zero(t, reqs["POST "+linksPattern], "file read back")
assert.Equal(t, 1, reqs[deleteDraftsRequest]+reqs[deleteRevisionRequest], "draft not deleted")
assert.Empty(t, u.Drafts())
requireUploaded(t, u, want)
})
}
}
// TestUploadCommittedThenCancelledUnderLag cancels an upload once its
// commit has succeeded and the first read back lagged: it returns at
// once as unconfirmed and deletes nothing.
func TestUploadCommittedThenCancelledUnderLag(t *testing.T) {
t.Parallel()
srv := protontest.New(t)
u := srv.AddUser("user", "pass")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := newTestClientTransport(t, srv, u, clientOptions{DraftClientUID: testDraftClientUID}, func(next http.RoundTripper) http.RoundTripper {
var committed atomic.Bool
return roundTripFunc(func(r *http.Request) (*http.Response, error) {
resp, err := next.RoundTrip(r)
switch {
case r.Method == "PUT" && strings.Contains(r.URL.Path, "/revisions/"):
committed.Store(true)
case committed.Load() && strings.HasSuffix(r.URL.Path, "/links"):
cancel()
}
return resp, err
})
})
srv.SetReadLag(readBackAttempts, protontest.LagBefore)
start := time.Now()
_, err := uploadContent(ctx, c, "f", strings.NewReader("mine"), 4)
assert.ErrorIs(t, err, errUploadUnconfirmed)
assert.Less(t, time.Since(start), 2*time.Second, "kept reading after cancel")
requireNoDeletes(t, srv)
requireUploaded(t, u, map[string][]byte{"f": []byte("mine")})
}
+93
View File
@@ -0,0 +1,93 @@
# Architecture
Fork-only orientation for agents. The summary in `AGENTS.md` ("Architecture")
is upstream's and is not repeated here; this page adds what that summary
leaves out. Update in the same commit as any structural change.
## Map
| Path | Responsibility |
|---|---|
| `rclone.go` | Entry point; blank-imports `backend/all` and `cmd/all` |
| `backend/<name>/` | One storage backend per package, registered in `init()` |
| `cmd/<name>/` | One cobra command per package |
| `fs/` | Core interfaces, config, operations, sync, accounting |
| `vfs/` | Virtual filesystem used by mount and serve |
| `lib/` | Shared helpers: `rest`, `pacer`, `dircache`, `oauthutil`, `encoder` |
| `fstest/` | Test framework: `fstests` (backend contract), `test_all` (runner) |
| `docs/content/` | Hugo website source; partly autogenerated |
| `docs/specs/`, `docs/plans/` | Fork-only agent workflow artefacts |
| `bin/` | Build and docs scripts |
## Test layers
- Unit tests: `RCLONE_CONFIG=/notfound go test ./...` - no credentials.
- Backend contract: each `backend/<name>/<name>_test.go` calls
`fstests.Run()` against a remote named `Test<Name>:` in the user's rclone
config. Without that remote the test skips, so a green `go test` on a
backend package says nothing about the backend against the real service.
- Integration: `fstest/test_all` runs the backend contract plus the
`fs/operations`, `fs/sync`, `vfs` etc. suites against the backends listed in
`fstest/test_all/config.yaml`.
## Invariants
- Backwards compatibility of commands, flags, config keys and the rc API.
- `fs` and `vfs` contain no backend-specific code.
- cgo use differs per release target (the `cgo` and `build_flags` columns of
the matrix in `.github/workflows/build.yml`), so a backend cannot assume cgo.
## `backend/protondrive`
A native client of the Proton Drive API, following Proton's Drive SDK
(spec: `docs/specs/001-protondrive-tier1.md`).
| File | Responsibility |
|---|---|
| `protondrive.go` | `Fs`, `Object`, options, `Config` sign-in, dircache glue, `translateError` |
| `session.go` | SRP and browser (session fork) sign-in, token refresh, app version |
| `account.go` | User and address keys, public-key lookups |
| `crypto.go` | Every OpenPGP operation |
| `nodes.go` | The `client`: volume bootstrap, pacing, listing, folder/rename/move/copy/trash |
| `cache.go` | In-memory node and listing cache |
| `events.go` | Volume events: the poller (a `time.AfterFunc` timer, running only while the cache holds anything or `ChangeNotify` subscribes), cache invalidation from events |
| `upload.go`, `download.go` | Block pipelines; `download.go` also keeps the per-client cache of block sizes learnt for files that don't record them |
| `api/` | Wire types |
| `protontest/` | Fake Proton server, imported by tests only; never imports the backend |
- **The seam:** `protondrive.go` holds no cryptography and no API routes;
the other files never take or return `fs.Fs`/`fs.Object`.
- No package-level mutable state except `unlockMu` (key unlocking is
serialised across `Fs` instances). Each `Fs` has its own client, session
and cache; credentials go through a per-`Fs` `configStore` over the
remote's config.
- `fake_test.go` runs the full `fstests` contract suite against
`protontest` through the hidden `endpoint` option, on every
`go test`; `protondrive_test.go` is the live suite (`TestProtonDrive:`).
- `fstest/testserver/init.d/TestProtonDriveFake` builds and starts
`protontest/cmd/protonfake` for `test_all`, which runs the wider suites
against the `TestProtonDriveFake:` remote. Fork CI is
`.gitea/workflows/ci.yml`.
- User docs: `docs/content/protondrive.md` (hand-written parts; the
options section is generated from the `Help:` fields).
`docs/specs/001-conformance-checklist.md` maps each SDK module to its Go
file and the tag it was last checked against.
- Events: each `Fs` polls its volume's events on its own (no shared
state). `events.go` invalidates the node cache; `protondrive.go`
observes every applied batch to flush changed folders from the
dircache (`forgetChanged`) and turns it into `ChangeNotify` paths
(`changedPaths`). Subscribers and the observer run only on the
poller's goroutine, never inside a user operation.
With no subscriber, a cache unused for 10 minutes is cleared, which
stops polling. `Shutdown` stops the poller (waiting for a poll in
flight) and keeps the cursor; later use restarts it. `Disconnect`
stops it for good.
- Not implemented yet: the persistent metadata cache, rclone Metadata
and the small-file upload path (M6).
Ranged reads check block hashes but not the signed manifest. Open items
and unrun live checks are in `docs/TODO.md`.
## Known rough edges
- See `docs/TODO.md` for host tooling gaps.
+622
View File
@@ -0,0 +1,622 @@
# Decisions
Fork-only. Append-only, newest first. Never edit or delete an entry - supersede
it with a new one that links back. One entry per decision a future maintainer
would otherwise have to reverse-engineer.
No index: `grep -n '^## ' docs/DECISIONS.md` lists every entry.
## 2026-11-23 - Proton Drive stays on its own code, not clayliddell's Go SDK
**Status:** accepted
**Context:** github.com/clayliddell/proton-drive-go-sdk (MIT, about 2.3k
lines, written 2026-03-23..27, inactive since) is a pure-Go Proton Drive
client offered on rclone issue #9252. It builds on go-proton-api and
gopenpgp v2, uses the share-scoped routes Proton will remove, has no events,
no new revisions, no AEAD and no retries; its `go.mod` path doesn't match
its repository, and large uploads drop the extended attributes.
**Decision:** Don't depend on it or copy from it; mention it as prior art.
Two details are noted for later: its small-file upload (`POST
/drive/v2/volumes/{vol}/files/small`, verification token = last 32 bytes of
the content key packet XOR the encrypted block, matching the official JS
SDK) is a reference for M6, with the JS SDK's 128 KiB limit rather than its
4 MiB; and block upload requests omit `Hash` and `Size` as the official JS
SDK does (the C# SDK still sends them) - revisit if Proton starts requiring
them.
**Alternatives:** Using it as the API layer (narrower, deprecated routes,
the old stack rclone is leaving).
**Consequences:** None for the code now.
## 2026-11-12 - Proton Drive's test server reports events as the real one does
**Status:** accepted (supersedes ruling 8 of "Proton Drive follows volume
events for change notification", below; plan 002 Task 6)
**Context:** Ruling 8 described the fake server's events, to be checked
live. The check (free account, `rclone test changenotify` at the 30 s
floor, changes made with rclone and the official CLI 0.9.0) found:
| Action | Events |
|---|---|
| upload (at commit), mkdir, server-side copy | Create (1); a copied folder: one per link |
| new revision | Update (2) |
| rename, move, trash (file or folder) | UpdateMetadata (3) |
| permanent delete, empty trash | Delete (0), one per link removed |
Link fields (`ParentLinkID`, `IsTrashed`) are the link's state when the
events are read: a folder made and removed between polls comes back as
a Create with `IsTrashed` set; a link deleted for good has no parent and
isn't trashed. Pages hold 299 events; a 2783-event poll read 10 pages.
**Decision:** The fake emits these types and reports Link fields at read
time; events added with `InjectEvent` keep their own fields. The backend
needed no change: only Delete is treated specially, so UpdateMetadata is
handled like Update. `pollEvents` logs one DEBUG line per page (IDs and
counts only). Live `FsChangeNotify` is ignored for `TestProtonDrive:`:
fstests waits 27 s, below the 30 s floor.
**Alternatives:** Keeping the fake's per-change snapshots (tests would
pass on states the real server never reports).
**Consequences:** A file trashed and emptied between polls is named at
its cached path only; its Delete names no parent.
## 2026-11-12 - Proton Drive follows volume events for change notification
**Status:** accepted (plan 002 rulings 1-9, as built in M5 Tasks 2-4; ruling 2
amended in the Task 4 review; ruling 9 and the idle cache added in Task 7)
**Context:** The node cache never saw changes made by other clients, so a
long `rclone mount` or `rclone serve` showed stale listings. Proton's
volume events (`GET /drive/volumes/{volumeID}/events/latest` for the
start cursor, `GET /drive/v2/volumes/{volumeID}/events/{eventID}` for
pages) report changed nodes by ID only: no names, no paths, no client.
**Decision:**
1. Own writes come back as events and are applied like any other
(invalidate, re-fetch on next use); there is no attempt to recognise
them.
2. `ChangeNotify` names a cached node at its old path (from the
dircache), and at its new folder if it moved to a known one. A node
that wasn't cached is reported at once as its parent folder, if that
is in the dircache, else not at all, and at its old path if it is a
folder the dircache holds. Its ID is then kept (at most 1000, oldest
dropped) for the next two polls, which look it up in the node cache
even when they bring no events; if it has been cached by then (an own
upload or `Mkdir` caches its node a round trip after the server
commits it, so its event can be applied first) it is reported at its
exact path. No extra requests are made to name nodes; pending IDs are
dropped on refresh, volume gone, `Shutdown` and when the last
subscriber leaves. A folder renamed in place is reported as its old
path plus its parent, since events carry no names. Nothing outside
the `Fs` root is reported.
3. `Refresh` (cursor unknown or expired) clears the node cache, takes the
returned `EventID` as the cursor and notifies `""` as a directory. The
dircache is flushed but keeps the root folder's ID, not `ResetRoot`:
after `ResetRoot` an `Fs` below the volume root would name later
events relative to the volume root until something found its root
again. `DirCacheFlush` keeps the root's ID for the same reason.
4. A Delete, or an Update that moves or trashes a folder, drops the node
and everything cached below it (`invalidateTree`). Every applied batch
flushes each changed folder's old path, and everything below it, from
the dircache, with or without `ChangeNotify`.
5. Proton code 2501 on an events route (volume gone) stops polling for
the life of the `Fs`, clears the cache, logs one ERROR and notifies
subscribers as for a refresh.
6. The hidden option `events_min_interval` (default 30s) sets the floor;
below 30s it is raised to 30s, with a NOTICE, when the endpoint host is
`proton.me` or ends in `.proton.me` (trailing dot ignored) or can't be
parsed. The interval rclone passes to `ChangeNotify` is used when
longer. Each wait adds up to 1s of jitter; failures back off by
Fibonacci multiples of the interval, capped at 10 minutes.
7. Each `Fs` polls on its own and keeps its own cache; the five cross-`Fs`
cache tests stay ignored.
8. The fake server emits Create at upload commit, Update at a revision
commit, rename, move and trash, Create on copy, and nothing for a
draft's deletion; to be checked against the real server (plan 002
Task 6).
9. `Shutdown` stops the poller; using the `Fs` again restarts it from
the cursor it had, an interval later, so a `Shutdown` and reuse never
make two events requests within an interval. `Shutdown` returns once
a poll in flight has ended (not its notifications), so nothing of it
runs, or logs, afterwards. `Disconnect` stops it for good.
10. With nothing subscribed, a node cache unused (neither read nor
filled; applying events doesn't count) for 10 minutes is cleared,
which stops polling as an empty cache does; the next use refills it
and polls from the kept cursor.
The poller is a `time.AfterFunc` timer, polling only while the node cache
holds anything or a `ChangeNotify` subscribes. Subscribers (and the
dircache observer) are called only from the poller's goroutine, never
from inside a user operation, which may hold dircache or VFS locks the
subscriber takes. The first fill of an empty cache fetches the start
cursor in the caller's path, so no change after the fill is missed;
once that fetch has failed, later ones wait for the backoff instead.
Events requests are paced but not retried by the pacer, so the poller's
own backoff governs failures; a Retry-After isn't waited out in the
request (nor given to the pacer, which would hold every call of the
client) but delays the next poll.
The data race fixed in `fstests` `FsChangeNotify` (commit `27e1a9c54`:
its maps were written from the backend's goroutine and read from the
test's without a lock) is a generic test fix, not Proton-specific.
**Alternatives:** Recognising own writes (v2 events name no client);
a fetch per unknown node to name it (extra requests per poll; under
review); a cache shared across the `Fs` of one account (package state,
which the spec rules out); `ResetRoot` on refresh (wrong paths below the
volume root).
**Consequences:** A mount sees other clients' changes within about one
poll interval. A moved node is reported under its old name in its new
folder, and an own upload's event applied before the node is cached is
reported only as its folder. An `Fs` that is never shut down and holds
anything in its cache polls until the cache has gone unused for 10
minutes. If the root folder
is deleted and recreated by another client while the cursor is expired,
the `Fs` keeps the old root ID.
## 2026-11-12 - Proton Drive maps only the app-version codes to "upgrade rclone"
**Status:** accepted (handoff ruling 30). Supersedes spec 6.6's
"2000/2028 ... to a fatal error telling the user to upgrade rclone".
**Context:** 2000 is Proton's generic "requirements not met" code, which
the server returns for a stale `OriginalHash` or `CurrentRevisionID`, as
after a rename whose response was lost and which was resent. Mapping it to
a fatal "upgrade rclone" error aborted whole runs over a rename that had
succeeded. 2028 means a feature is disabled, rate-limited or blocked for
abuse (`driveTypes.ts`). The code Proton returns for a rejected app
version is 5003 ("You need to update this app", `coreTypes.ts`), and 5001
for a missing one (go-proton-api), neither of which was mapped.
**Decision:** 5001 and 5003 are a fatal error telling the user to upgrade
rclone. 2000 is a no-retry error keeping Proton's message. 2028 is retried
through the pacer (honouring `Retry-After` when sent), then returned as
it is. 2064 (malformed header) is left unmapped.
**Alternatives:** Keeping the spec's mapping (false fatal errors);
treating 2028 as a no-retry error (fails at once on rate limiting).
**Consequences:** A feature Proton disables outright costs the low-level
retries before its error is returned.
## 2026-11-12 - Proton Drive caps the Argon2 cost of node and share keys
**Status:** accepted
**Context:** go-crypto unlocks an Argon2-locked key with whatever
parameters it carries, up to 2 TiB of memory and 255 passes, and runs the
KDF before it can tell the passphrase is wrong. Node and share keys come
from the server, or from other users' clients in shared folders, so one
crafted key could crash every listing that includes it.
**Decision:** Before unlocking a node or share key, every secret key
packet's S2K is checked: Argon2 may use at most 4 passes, parallelism 8
and 2^17 KiB (128 MiB). gopenpgp and OpenPGP.js write 3, 4 and 64 MiB by
default, and Proton's node keys use an iterated and salted S2K, whose
one-byte count encoding already bounds it to 4 times the default. A key
beyond the limits is an integrity error, making the node unreadable, and
`ignore_signature_errors` doesn't change that.
**Alternatives:** Refusing Argon2 on node keys outright (no genuine node
key is known to use it, but a future client might); capping the iterated
count below the encoding's maximum (could refuse genuine old keys for a
bounded cost).
**Consequences:** At most 128 MiB per unlock while a listing decrypts
nodes. The account's own user keys aren't checked yet (`docs/TODO.md`).
## 2026-11-12 - Proton Drive refuses a file whose content key is unsigned
**Status:** accepted (handoff ruling 29)
**Context:** Anyone holding a file's public node key, which the server
stores, can make a content key packet for it. Proton's SDK checks the
content key packet's signature only when one is present. A server could
then send its own content key without a signature: rclone would encrypt
every update of the file with a key the server knows, and a ranged read
(which can't check the manifest) would return whatever the server
encrypted under it.
**Decision:** A content key whose signature is missing or invalid makes
the file unverified, as an unverified node key does (rulings 18 and 22):
reads, full and ranged, and updates are refused unless
`ignore_signature_errors` is set, when rclone logs an `ERROR` once per file
and operation and goes ahead. The pre-2023 fallback to the account's other
address keys still applies to a signature which is present but fails, as
in the SDK. Integrity checks are never turned off. This is stricter than
the SDK.
**Alternatives:** Following the SDK (leaves the hole); a creation-date
exemption for unsigned keys (the date is server-supplied, so it would
exempt anything the server back-dates).
**Consequences:** A genuine file whose content key was never signed, if
any client ever wrote one, needs `ignore_signature_errors`. Every file in
the golden fixtures, written by Proton's web app, CLI and the old
library, has a signed content key.
## 2026-11-12 - Proton Drive deletes an upload's draft only on an explicit refusal
**Status:** accepted. Supersedes the read-back half of handoff ruling 11
("a draft is deleted only on positive proof it never committed", where a
read showing the draft or the old revision counted as proof).
**Context:** Proton's server can take a moment to show a commit (entry
below). After a failed commit, one stale read showing the draft or the
base revision made the client delete the draft, but the commit may
have landed: `delete_multiple` against what is now an active file, or
`DELETE .../revisions/{id}` against what is now the active revision. What
the real server does with those is unconfirmed, so this could lose data.
**Decision:** The draft is deleted only when the server explicitly
refused the commit, meaning a 4xx `api.Error` (not 408) with a Proton code,
on a single attempt that the pacer did not resend. A resend after
5xx/408/network/timeout makes the outcome unknown even if the final
answer is a refusal, because an earlier attempt may have landed. No
read-back decides deletion, and a refusal is not read back at all. Every
other failure (5xx, network, timeout, cancelled ctx, resent attempts)
returns `errUploadOutcomeUnknown` and deletes nothing. The read after
such a failure can still turn it into a success when it shows the
upload's revision. It retries through lag while the caller's ctx lives,
and reads once, detached, if ctx is done.
**Alternatives:** Requiring every read across the whole lag window to
show the pre-commit state (a heuristic, as the lag length is unmeasured);
the old single-read proof.
**Consequences:** More drafts are left behind after ambiguous failures.
The next upload of the name replaces them, otherwise they stay (see
`docs/TODO.md`). A refused commit fails at once, with no reads.
## 2026-11-12 - Proton Drive uploads tolerate read-after-write lag, but never invent the result
**Status:** accepted
**Context:** Against the real service an upload whose commit succeeded
failed because the node read back just after was missing or still a
draft with an empty active revision (`errUploadSuperseded` with revision
"", and "object not found"). The client read the node once and trusted it.
**Decision:** After a commit the node is read again with backoff (100 ms
doubling to 2 s, 8 reads, about 7 s) while it is missing, active with no
revision, or - when the commit returned success - still as it was before
the commit. `errUploadSuperseded` is kept for a non-empty revision which
is neither ours nor the one we started from. If the lag outlasts the
reads, the upload returns `errUploadUnconfirmed` (no low-level retry, as
for `errUploadOutcomeUnknown`), deletes nothing, and a later sync pass
finds the file. The same not-found retry covers a copy's read back and,
with `enable_caching` off, a folder to write into (with it on, a folder
the client made is cached, so a miss means it is gone).
**Alternatives:** Returning success with a node built from what was
committed (the caller then reads, moves or updates through it, and that
needs the decrypted keys and server metadata the client would be
inventing; a wrong node would be cached and acted on); retrying every
read after any write (slows every genuine not-found).
**Consequences:** With `enable_caching` off, a put into a folder deleted
elsewhere takes about 7 s longer to fail. A lag longer than 7 s still fails the upload,
now with an error saying it was committed. See `docs/TODO.md` for the
read-after-write spots not yet tolerant.
## 2026-11-12 - Proton Drive golden captures fail closed
**Status:** accepted; amends "Proton Drive golden fixtures are re-keyed
captures" below
**Context:** Review found the capture could copy account data into a
fixture: an intended-recipient fingerprint of a key not being replaced (in
the notes), and the user ID of a node key. It also reused `-remote`, so a
mistyped `-run` would start the writing integration suite against the
account.
**Decision:** The capture names its folder with `-capture-remote` and
refuses to run with `-remote` set. Every key written has the user ID
`golden <golden@example.invalid>`; a node key whose user ID holds an email
other than the account's or the generic ones Proton's clients use
(`no-reply@proton.me`, `noreply@protonmail.com`) fails the capture. A
signature by, or intended for, a key not being replaced fails the capture,
and the error names no key. A salt notation is written only where the
original had one (with a new salt). `TestGoldenFixtures` also walks every
key, key packet and signature, inside encrypted messages too, and fails
on any key ID, fingerprint or user ID not of the test keys.
**Consequences:** What the committed fixtures expose, by design: with the
test keys in `keys.json`, anyone can decrypt the captured names, extended
attributes (as the client wrote them: check them for camera or location
fields before committing), literal-packet file names and dates, folder
hash keys (so whether a guessed name exists in those real folders can be
tested) and file contents; and the address key's creation time is kept.
Node passphrases and the account's keys are not exposed.
## 2026-11-12 - Proton Drive golden fixtures are re-keyed captures
**Status:** accepted
**Context:** Plan Task 21 asks for fixtures of nodes written by Proton's
web app and official CLI. Captured as they are, they would carry the test
account's keys, IDs and emails. The owner chose to re-encrypt them under
keys made for the tests.
**Decision:** `TestGoldenCapture` (`-capture DIR -capture-label NAME
-remote NAME:PATH`) writes `backend/protondrive/testdata/golden/<label>/`,
read by `TestGoldenFixtures` from the fake server with every signature
checked. Preserved: every block byte for byte; every SEIPD packet which
holds no signature (re-wrapped session key only); the session key, cipher,
SEIPD version, AEAD mode and chunk size, compression algorithm and literal
data packet (format, file name, date, content) of names, hash keys and
extended attributes; signature version, type, hash, creation time,
lifetime and notations; key algorithm, version, creation time and AEAD
support; timestamps, sizes, flags, media types, block sizes and whether
they are recorded. Replaced: all keys (address, node, the captured
folder's parent) by new ones; every PKESK, re-wrapped in the same version;
every signature, re-made by the key replacing its signer (an embedded one
forces its SEIPD packet to be encrypted again with the same session key
and parameters, as a signature can't change in place); node passphrases,
new ones in a message of the same format; the captured folder's name hash;
intended recipients and signer user IDs; emails by
`golden@example.invalid`; IDs by placeholders. Dropped: block signatures
(the backend doesn't read them), block URLs and tokens. Each node lists
what was done to it in `Rekeyed`. Files with thumbnails are refused (the
fake can't serve them); the address key passphrase is sealed to a user key
the fake generates, so no user key is written.
**Alternatives:** Raw captures (account secrets in the repository);
fixtures re-made with gopenpgp from the decrypted content (loses the
other clients' packet formats, the point of the exercise).
**Consequences:** The fixtures test the formats of names, hash keys and
extended attributes as other clients lay them out, but their signature
bytes are ours. Packet headers are re-serialised with fixed lengths.
## 2026-11-12 - Proton Drive is a native port of Proton's Drive SDK
**Status:** accepted
**Context:** With the bridge libraries gone (plan Task 19), the backend
is a Go client written for rclone. Its behaviour has to be checked against
something as Proton changes the service, and the user docs still
described the old libraries.
**Decision:** The backend is maintained as a port of Proton's Drive SDK,
module by module, tracked in `docs/specs/001-conformance-checklist.md`
against SDK commit `05c9115955589d9f6ab7392a36f8f0cb39327133` (whose
`client/js/CHANGELOG.md` ends at `js/v0.23.0`; that tag is not on the
commit), with its deliberate differences (stricter trust
rules, no small-file path, no events, no thumbnails) listed per module.
The user docs describe only what the code does and the protocol limits
the SDK shares, with no fork-only content, so they could go upstream.
**Alternatives:** Binding to the SDK itself (it is TypeScript and C#;
rclone refuses cgo and helper binaries); writing from observed traffic
alone, as the old libraries did (no reference to re-check against).
**Consequences:** Each SDK update needs a walk of the checklist: a diff
of the listed files between the checked commit and the new one. The
docs page must change with any behaviour change, and its generated
options section is left to `make backenddocs` at release.
## 2026-11-12 - Proton Drive keeps frozen old-library fixtures, not the old library
**Status:** accepted
**Context:** `go-proton-api` v1.0.4 and `Proton-API-Bridge` v1.0.5 stayed
in `go.mod` only for differential tests checking that the new client and
the old library read each other's output (plan Task 19).
**Decision:** The differential tests and both libraries were removed. What
the old library wrote stays checked through the frozen fixtures in
`backend/protondrive/testdata/oldlib/` (`TestFrozenFixtures`,
`TestFrozenFileFixture`), which can no longer be regenerated.
**Alternatives:** Keeping the libraries as test-only requirements (they
would stay in `go.mod` and its vulnerability surface, and upstream would
carry them).
**Consequences:** Multi-block and empty old-library files, and old-reader
compatibility of new writes, are no longer tested (`docs/TODO.md`).
## 2026-11-12 - Test-account usernames removed from git history by force-push
**Status:** accepted
**Context:** The first Proton Drive handoff commit contained the two
test-account usernames (no passwords or tokens); a later commit removed
them from the file but they stayed in history on the Gitea remote.
**Decision:** On the owner's explicit instruction, that commit was
rebuilt without the usernames, the following commits were replayed with
identical trees and metadata, and `master` was force-pushed once with
`--force-with-lease`. The rule against force-pushing still stands.
**Consequences:** Commit hashes from `901d7fd5b` onward changed (mapping
in the handoff's "Open questions" section). The old objects may stay
reachable on the server until Gitea garbage-collects the repository.
## 2026-11-12 - test_all shares one protonfake session across test processes
**Status:** accepted
**Context:** `fstest/testserver/init.d/TestProtonDriveFake` starts one
`protonfake` per `test_all` run (reference counted by `run.bash`), and
every test process gets the same session through
`RCLONE_CONFIG_TESTPROTONDRIVEFAKE_*` variables. Refresh tokens are
single use and a process can't see another's refresh, so one process
refreshing would leave the rest with dead tokens.
**Decision:** Share the session; change nothing in `protontest`,
`protonfake` or the client. The client refreshes only after an HTTP 401
(`session.call`); the fake answers 401 only for a session expired by
`Server.ExpireAccessToken` or ended by logout or `RevokeSession`, and
`protonfake` calls none of them. The only logouts are `Fs.Disconnect`
and re-running `rclone config`, which no test in `fs/operations`,
`fs/sync`, `vfs`, `cmd/bisync`, `cmd/gitannex` or `fstests` calls.
**Alternatives:** A session per test process (`protonfake` would need a
sign-in route for `test_all` to call, and the init.d protocol has one
output per server); non-expiring or logout-tolerant tokens in
standalone mode (behaviour no test needs, and a fake that differs from
Proton).
**Consequences:** A future test that calls `Disconnect`, or a fake that
expires tokens on a timer, breaks every process sharing the run: the
symptom is "not signed in" or refresh failures across all suites at
once. Revisit then.
## 2026-11-11 - Proton Drive rewrite paused mid-plan and merged to master
- **Decision:** at the owner's request, work on plan 001 stopped after Task
17 of 21 and the branch was fast-forwarded into `master` and pushed, with
the plan, a handoff document and the execution workspace committed under
`docs/plans/`.
- **Why:** the owner wanted development wound down with nothing left only
in an agent's conversation.
- **Consequences:** `master` carries an unfinished plan, against the usual
rule that plans are removed before merging; the user docs still describe
the old backend; the old libraries are still in `go.mod`. The push to
`master` was a one-off instruction - the rule against pushing to
`master` stands. Resume from `docs/plans/001-protondrive-tier1-HANDOFF.md`.
## 2026-11-11 - Proton Drive logs what ignore_signature_errors lets through
**Status:** accepted
**Context:** With `ignore_signature_errors` set, reads, writes, moves,
copies and renames of nodes whose keys are unverified went ahead
silently, although spec 6.4 promises a logged ERROR (Task 16 re-review
2).
**Decision:** Each such operation logs one ERROR per node and kind of
operation for the life of the `Fs`, naming the node, its ID, the
operation and why its key is unverified. Completes "Proton Drive won't
vouch for a node it couldn't verify".
**Alternatives:** Logging every call (floods a sync); a NOTICE (too
easily missed for something permanent).
**Consequences:** With the option set, moving or copying a node out of
an unverified folder re-signs or re-homes it as the user's, so it is
trusted from then on; the ERROR says so.
## 2026-11-11 - Proton Drive matches email addresses by ASCII case only
**Status:** accepted
**Context:** The own-address check of "Proton Drive roots trust in the
user's own addresses" ignored case, but the key lookup didn't, so a
server could name the user's address in capitals and serve its own keys
for it, at the root and below (Task 16 re-review 2).
**Decision:** One rule everywhere: emails are compared after lowercasing
ASCII letters only. An email matching one of the account's addresses
under it is verified with the account's own keys and never looked up on
the server, and other users' keys are cached under the same key. Unicode
case folding is not used, as it maps the Kelvin sign to k and the long s
to s, letting a non-ASCII address pass for an ASCII one.
**Alternatives:** Exact matching (an address the server stores in other
case would be looked up remotely); `strings.EqualFold` (Unicode folding).
**Consequences:** Two addresses differing only in non-ASCII case are
different addresses to rclone.
## 2026-11-11 - Proton Drive roots trust in the user's own addresses
**Status:** accepted
**Context:** "Proton Drive distrusts what a forged folder key vouches
for" and "Proton Drive refuses a file whose node key signature fails"
read as if every key were checked against keys rclone can trust. But
the keys of another user's email come from the server, so a server can
forge a share, root or node "signed" by a foreign address it vouches
for (Task 16 re-review).
**Decision:** Corrects those entries. The My Files share's creator and
the root folder's signer must be addresses whose keys the account
holds (compared ignoring case); otherwise the share or root key is
unverified, which makes the whole drive unreadable and unwritable
without `ignore_signature_errors`, and the error says why. Below the
root, nodes signed under another user's email are still verified
against server-supplied keys: a protocol limit Proton's SDK shares,
documented in spec section 7 and TODO.
**Alternatives:** Pinning other users' keys on first use (state rclone
doesn't keep); trusting server keys at the root too.
**Consequences:** A folder genuinely signed by an address since deleted
from the account, or whose keys no longer unlock, is unverified: writes
into it, and reads beneath it, need `ignore_signature_errors`.
## 2026-11-11 - Proton Drive won't vouch for a node it couldn't verify
**Status:** accepted
**Context:** Refusing writes into a folder with an unverified key left
the other direction open: moving or copying an anonymous node out of it
has rclone sign its server-made passphrase as the user's, after which it
and everything beneath it verified (Task 16 re-review). And a node made
under such a folder with `ignore_signature_errors` set was signed by the
user, so verified later, although whoever made the folder's key can
decrypt its passphrase.
**Decision:** Supersedes the inheritance rule of "Proton Drive distrusts
what a forged folder key vouches for": every node beneath a folder with
an unverified key is unverified, however it is signed, and so is a
folder rclone makes there. Moving, copying, directory-moving or
renaming a node whose key is unverified fails unless
`ignore_signature_errors` is set.
**Alternatives:** Inheriting only into anonymous nodes (leaves the
user-signed children of a forged folder trusted).
**Consequences:** A genuine folder whose key fails to verify, as one
signed by a since-deleted address, makes its whole subtree unreadable
and unwritable without `ignore_signature_errors`.
## 2026-11-11 - Proton Drive verifies the manifest before releasing a full read
**Status:** accepted
**Context:** A full read checked the manifest signature at EOF, as the
SDK does, so blocks swapped, truncated or from an older revision, each
matching its listed hash and recorded size, reached the reader before
the error: `rclone cat` and streaming mounts showed wrong bytes (spec
9.6 asks for "an integrity error, not data").
**Decision:** A full read lists every block page first, verifies the
manifest over the listed hashes, then fetches; each block is checked
against its listed hash, and a block listed again with another hash
is an integrity error. Pages are fetched one after another. Ranged
reads keep checking blocks against their listed hashes only.
**Alternatives:** Keeping the SDK's EOF check; verifying ranged reads
too by listing the whole file on each ranged open (costly for mounts
and multi-thread copies without a hash cache).
**Consequences:** No extra requests, but a file of more than 20 blocks
waits for its whole list before the first byte. Block metadata, not
data, is held meanwhile. A whole earlier revision served under the
current ID stays undetectable (protocol limit).
## 2026-11-11 - Proton Drive distrusts what a forged folder key vouches for
**Status:** accepted
**Context:** An anonymous node's passphrase, name and content signatures
are checked against its parent folder's key. A server can make a folder
key of its own, seal it to the real parent and sign everything beneath
it, so a refusal based only on each node's own signature check is
bypassed (Task 16 review).
**Decision:** A node key is unverified if its own passphrase signature
fails, or if it was checked against an unverified parent key; this
passes down the tree. Reading a file with an unverified key, and
creating, uploading, renaming, moving or copying into a folder with
one, fail unless `ignore_signature_errors` is set. Listing such a folder
still works, with one ERROR for the folder itself.
**Alternatives:** Leaving the folder's children to their own checks
(bypassable); refusing to list the folder (a sync would delete its
destination copy).
**Consequences:** Builds on the 2026-11-11 entry "Proton Drive refuses a
file whose node key signature fails". Files in a public-link upload
folder whose key fails to verify need `ignore_signature_errors`.
## 2026-11-11 - Proton Drive refuses a file whose node key signature fails
**Status:** accepted
**Context:** The node passphrase signature was only logged when it
failed, as the SDK does, so a server could seal a key of its own to
the parent folder's public key and serve any content under it (spec 7
puts the server in the threat model; 9.6 asks for an error).
**Decision:** Opening such a file fails with an integrity error naming
`ignore_signature_errors`, like a content key or manifest signature
failure; with the option set it is read, the failure logged once. The
node is still listed. Sign-in errors that need a reconnect name the
remote through a hint the seam gives the session, so read errors, which
don't pass through `translateError`, name it too.
**Alternatives:** Skipping the node from listings (a sync would then
delete its copy at the destination); leaving it to the SDK's behaviour.
**Consequences:** Legacy files whose keys were signed by a since-removed
address need `ignore_signature_errors`, which the error says.
## 2026-11-11 - Proton Drive backend runs on its own client
**Status:** accepted
**Context:** The backend sat on `rclone/Proton-API-Bridge` and
`rclone/go-proton-api`, which share credentials through package globals,
write files the official clients can't always read, and can't be tested
without a live account (spec 001).
**Decision:** `protondrive.go` was rewritten on the native client built in
plan Tasks 2-12, keeping every option name and meaning and reading v1.76
saved sessions without signing in again. `Config` offers browser sign-in
(default unless a username and password are already set, so existing
scripted setups keep using them) and password sign-in. An upload whose
commit outcome can't be known is a no-low-level-retry error: an immediate
retry could add a revision that already landed, while a later sync pass
reads the destination first.
**Alternatives:** Patching the bridge (globals and crypto stay outside the
tree); keeping browser sign-in the default even with a password set
(non-interactive `config create` would wait on a browser).
**Consequences:** The full contract suite runs hermetically against
`protontest`. Several `Fs` of one account in one process each cache
separately until events land (spec 6.2).
## 2026-11-11 - Fork agent tooling is appended, not merged, into upstream files
**Status:** accepted
**Context:** Upstream rclone already ships a maintainer-written `AGENTS.md`
and a `CLAUDE.md` that imports it. This fork tracks upstream and intends to
send work back, so every edit to an upstream file is a future merge conflict
and a risk of leaking fork-only material into an upstream PR.
**Decision:** Upstream's `AGENTS.md` text is kept verbatim. Fork rules are one
"Fork Workflow" section appended at the end. `CLAUDE.md` and `README.md` are
untouched. Fork-only docs live at the top of `docs/` (Hugo only builds
`docs/content/`, so they do not reach the website).
**Alternatives:** Restructuring `AGENTS.md` with the fork rules first (better
agent attention, permanent conflicts with upstream); a separate fork rules
file (two sources of truth).
**Consequences:** `AGENTS.md` sits at 148 of a 150 line budget - a new fork
rule must replace an old one. Upstream PR branches must be cut so they exclude
the paths listed in the Fork Workflow section.
## 2026-11-11 - Adopt AGENTS.md as single source of truth
**Status:** accepted
**Context:** Agents had no fork-specific workflow or permission config between
sessions.
**Decision:** `AGENTS.md` is canonical; `CLAUDE.md`/`GEMINI.md` are pointers.
Specs are permanent in `docs/specs/`. Plans live in `docs/plans/` only while
work is in progress and are removed before merge. Both override the
superpowers default paths.
**Alternatives:** Per-tool rule files (drift); no rules file (agents
re-explore the repo every session).
**Consequences:** Rule changes happen in one file; pointer files stay thin.
The spec, not the plan, is the record.
+65
View File
@@ -0,0 +1,65 @@
# Agent permissions
Fork-only. Source of truth for what AI coding agents may run here. Tool files
are renderings of this page: `.claude/settings.json`, `.codex/config.toml`,
`.codex/rules/project.rules`, `.gemini/settings.json`. Change this page and
every tool file in the same commit. Review changes to these files and to
`AGENTS.md` like CI config: one line redirects agent behaviour repo-wide.
## Verified commands
Run on the host on 2026-11-12 with Go 1.27.4 (`~/.local/opt/go`) and
golangci-lint 2.15.0 (`~/.local/bin`), from the repository root, with
`RCLONE_CONFIG=/notfound` set so no test can reach a real remote. The rows
marked (container) are from 2026-11-11, before Go was on the host, run in
the `golang:1.26` container and not repeated.
Only `worked` rows may appear in a tier, a tool file, or `AGENTS.md`.
| Command | Result |
|---|---|
| `go mod download` | worked |
| `go build` | worked - binary reports `rclone v1.77.0-DEV` |
| `go test ./backend/memory/` | worked |
| `go test -run 'TestIntegration/FsMkdir/FsPutFiles/FsCopy' ./backend/memory/` | worked |
| `go test ./backend/protondrive/...` | worked - the contract suite runs against the fake server; the live `TestProtonDrive:` suite skips without a config |
| `go vet ./backend/protondrive/...` | worked |
| `gofmt -l backend/protondrive` | worked - no output |
| `golangci-lint run ./backend/protondrive/...` | worked - 0 issues |
| `golangci-lint run ./...` | worked - 0 issues |
| `go run ./fstest/test_all -backends protondrive -remotes TestProtonDriveFake: -maxtries 1 -n 4` | worked - starts the fake server through `fstest/testserver/init.d/TestProtonDriveFake` |
| `RCLONE_CONFIG=/notfound go test ./...` | failed - only `cmd/serve/s3`, undiagnosed (2026-11-11, `docs/TODO.md` "Known issues"); in the container 7 packages failed: `backend/smb`, `cmd/gitannex`, `cmd/mount`, `cmd/mount2`, `cmd/serve/docker`, `cmd/serve/nfs`, `fs/logger` (container) |
| `make`, `make quicktest`, `make racequicktest` | not run - `make` is in Ask |
## Tiers
| Tier | Commands | Why |
|---|---|---|
| Allow | `git status`, `git diff`, `git log`, `git show`, `git branch --list`, `git rev-parse`, `git add`, `git commit`, `go mod download`, `go build`, `go test`, `go vet`, `gofmt -l`, `golangci-lint run` | Inspection and the verified dev loop; no prompt needed. |
| Ask | `git push`, `git merge`, `git rebase`, `git switch`, `git restore`, `git commit --amend`, `rm` (any), `go get`, `go mod tidy`, `go install`, `make` (any target), `docker` (any) | Leaves the machine, rewrites work, changes dependencies, or is unverified. |
| Deny | `git push --force` / `-f` / `--force-with-lease`, `git reset --hard`, `git clean` (any), `git add -f` / `--force`, `git commit --no-verify` / `-n`, `rm -r` / `-R` / `-rf` / `-fr` / `-Rf` / `-fR` / `--recursive`, `sudo`; reading `.env`, `.env.*`, `secrets/**` | Destroys work, skips checks, or exposes secrets. |
## Known gaps
- The Claude Code deny rule `git push --force-with-lease:*` does not match
`git push --force-with-lease=<ref>:<sha> ...` (no word boundary before
`=`); a force-push the owner approved on 2026-11-12 went through it.
- Allowlists save prompts; they are not a security boundary. Use the tool's
sandbox for isolation.
- Prefix rules are bypassable: `git -C dir push`, flags placed later
(`git push origin main --force`), `+refspec` pushes. Claude Code's `:*`
matches at a word boundary, so each flag cluster must be listed - which is
why `git clean` is denied whole and `rm` always asks.
- Allowed test and build commands run code from the repo; an agent that can
edit Go files or tests can run anything through `go test`.
- Denying reads of `.env` does not stop a program that opens it itself, and
git can print staged or committed secrets. Rclone test credentials live in
the user's rclone config, outside this repository - keep them there.
- Codex: loaded only once the project is trusted; `approval_policy` here can
loosen a stricter user setting. No `allow` rules are written because in
Codex `allow` means "run outside the sandbox". Codex cannot deny file reads.
`--force-with-lease=<ref>`, `+refspec` and late flags only match Ask.
- Gemini CLI: ignored until the folder is trusted; no Ask tier (anything not
allowed already asks); cannot deny file reads.
- Cursor and Copilot files are not written: nothing in the repository shows
the fork uses them (`.vscode` is gitignored).
+454
View File
@@ -0,0 +1,454 @@
# TODO
Fork-only durable backlog for things noticed and deliberately not fixed.
Small, specific, independently actionable. Remove only when actually done.
(In Go source, upstream's `FIXME`/`TODO` comment convention still applies.)
## Now
- [ ] protondrive: project handed over 2026-11-23 with M1-M5 done (M5 on
branch `protondrive-m5`). The plan 002 whole-branch review ran; its one
fix (a cap on event pages per poll) is in, the rest is listed under
"plan 002 whole-branch review" below. Next: M6 as plan 003 (see Next).
Public code-only branch: `protondrive` on
github.com/StereotypicalCat/rclone-protondrive (squashed, `c0bdbcde6`).
The owner posted the announcement upstream on 2026-11-23 (text in
`docs/plans/upstream-post-draft.md`); watch rclone #9252 and forum
thread 53609 for replies before starting new work.
Context: `docs/plans/002-protondrive-m5-HANDOFF.md`.
- [ ] protondrive: plan 001 (the backend rewrite) is complete and on
`master`; its handoff `docs/plans/001-protondrive-tier1-HANDOFF.md`
still holds the standing instructions, rulings 1-33 and live results.
- [ ] protondrive: the free test account's derived key passphrase (the
`client_salted_key_pass` of a browser sign-in on 2026-11-12) was shown
in an agent's tool output when `rclone config create` printed the new
remote; that session was logged out at once, but the passphrase only
changes with the account password. Owner: change the free account's
password if that matters, then update `~/proton_rclone_test_acc_password`
and the remote's saved password.
## Next
- [ ] protondrive M6: opt-in persistent metadata cache of encrypted node
records and the event cursor through `lib/kv` (`metadata_cache`), rclone
Metadata (`mtime`, `btime`, `content-type`), and the SDK's single-request
upload for files under 128 KiB (`files/small`). Needs its own plan; the
docs page and `protondrive.yaml` features change with it.
- [ ] protondrive M7: the upstream path (spec 13), deferred by the owner;
also the new crypto model once Proton publishes it in the SDK.
- [ ] protondrive live checks not yet run (plan Task 21), each of which
the user docs avoid claiming: 2FA, by code and by `otp_secret_key`;
CAPTCHA handling; Proton's desktop and mobile clients reading files
rclone wrote (the web app and the official CLI 0.9.0 have been
checked); whether the real server lists drafts or trashed items as
children (affects `Rmdir`); whether any real address differs from
another of the account only by non-ASCII case (counted as foreign);
how long the server's read-after-write lag after a commit really is
(the client retries for about 7 s).
- [ ] protondrive: `test_all` against the real service (2026-11-12,
free account) fails only the five cross-`Fs` cache tests listed
below; `fstest/test_all/config.yaml`'s `TestProtonDrive:` entry has no
`ignore` for them, so plan Task 21's "clean with no ignore entries" is
not met until that is decided.
- [ ] protondrive: `TestIntegration/FsMkdir/FsChangeNotify` is ignored
for `TestProtonDrive:` in `fstest/test_all/config.yaml`: fstests waits
9 x 3 s = 27 s for changes, and Proton allows one events request per
30 s, so it saw none live (2026-11-12). The live events check
otherwise passed (`rclone test changenotify`, rclone and the official
CLI). Fixing it needs a longer wait in fstests or a per-backend one.
- [ ] protondrive: `docs/content/protondrive.md`'s generated options
section still shows the old options and help texts (its
`enable_caching` text says events are not implemented, contradicting
the hand-written "Changes made elsewhere" section); regenerate it with
`make backenddocs` when the work goes upstream (generated doc changes
are not committed in the fork, per `AGENTS.md`).
- [ ] protondrive: API responses have no body-size limit, and a block
list's `Token` and `BareURL` and the first page's `Thumbnails` are
unbounded, so the 2^19-block cap bounds how many blocks a full read
holds but not the bytes (about 250-350 MiB at the cap with realistic
values, unbounded against a hostile server). Consider a limit on API
response bodies and on those fields.
- [ ] protondrive: confirm on an account whose drive was made by
Proton's own apps (both test accounts' drives were most likely created
by rclone itself) that the
root-of-trust rule passes (the My Files share created, and its root
signed, by one of the account's addresses), and count how many folders
fail key verification.
- [ ] protondrive: a range from offset 0 of a file whose size isn't
recorded (`bytes=0-N`, N at or past the end) is read as a ranged read,
so its manifest isn't checked; only a known size identifies a range
covering the whole file.
- [ ] protondrive: `TestGoldenCapture` refuses files with thumbnails (the
fake server serves none, and the manifest covers them) and keys other
than RSA, EdDSA, Ed25519 and Ed448; images and photos from Proton's apps
can't become golden fixtures until the fake serves thumbnails.
- [ ] protondrive: below the root, a node signed under another user's
email (a shared folder's members) is verified against public keys the
server supplies (`account.publicKeys`), so a hostile server can forge
it; the SDK does the same. Revisit when Proton offers key transparency
or address-key pinning.
- [ ] protondrive: a full read lists every block page before releasing
the first byte, one request per 20 blocks in sequence, so a very large
file waits (about 12 requests for 1 GiB, about 1280 for 100 GiB) before
data flows. Fetching pages in parallel under the 15-request metadata
limit would cut that.
- [ ] protondrive: a ranged read (offset > 0 or a limit, as multi-thread
copies and mounts make) checks each block against the hash the server
lists for it, its decryption and its recorded size, but not the
manifest, so a server can serve validly encrypted blocks of the same
file swapped or from an older revision of the same size. The planned
fix is a bounded per-revision cache of manifest-verified block hashes.
- [ ] protondrive: a server can serve an earlier revision whole (blocks,
manifest and extended attributes, all validly signed) under the current
revision ID undetected, as nothing signed binds them to the revision;
the SDK can't detect it either. Raise with Proton.
- [ ] protondrive: `--dump headers` logs Proton's `pm-storage-token` and
`x-pm-uid` (fshttp redacts only `Authorization`), and `--dump bodies`
logs the access, refresh and upload tokens in JSON bodies (seen in
`TestNoSecretsInLogs`). The docs page warns about it; a header
redaction list in `fshttp` would be an upstream change.
- [ ] protondrive: a name whose signature fails is listed and the file
read with only an `ERROR` logged (spec 6.2, the SDK's behaviour), and
names aren't bound to their node, so a server can swap the encrypted
names of two files in one folder undetected. Raise with Proton.
- [ ] lib/pacer: `TokenDispenser.Get` takes no context, so a cancelled
protondrive block download still waits for a download token (held by
other in-flight blocks, which finish or fail soon) before it notices.
Consider a context-aware `Get`.
- [ ] protondrive: confirm that Proton's desktop and mobile clients read
a file written by Task 5's `crypto.go` (the web app and the official CLI
0.9.0 do, with signatures verified, as of 2026-11-12) (v4 node key
advertising SEIPDv2, v6 PKESK content key, SEIPDv2 GCM blocks, block
signatures encrypted with the content session key). This is what the SDK
(js/v0.23.0) writes, but go-proton-api v1.0.4 `link_file_types.go`
(`SetContentKeyPacketAndSignature`) and Proton-API-Bridge v1.0.5
`crypto.go` (`generateLockedKey`) say the official clients do not reliably
read crypto-refresh files and that the server rejects a v6 content key
packet for a v4 node key; the bridge never sent one with a key advertising
SEIPDv2, so its claim may not apply. Released rclone itself is not the
problem: Task 6's `TestNewWriterOldReader` (removed with the old
libraries in Task 19) showed go-proton-api v1.0.4 and the bridge's block
reader read this format (v6 PKESK, SEIPDv2 blocks and block signatures)
and the new `toISOString` modification time.
- [ ] protondrive: files the old library (go-proton-api v1.0.4 and
Proton-API-Bridge v1.0.5) wrote are covered only by the single-block
frozen fixtures in `backend/protondrive/testdata/oldlib/`
(`TestFrozenFixtures`, `TestFrozenFileFixture`); the multi-block and
empty-file cases left with the differential tests in Task 19. A
multi-block fixture would need the old library back to write it. Nothing
checks any more that released rclone (v1.76 and older) can read what the
new backend writes; that matters to users who downgrade.
Commit `b667a1ab0` still has the differential tests and both libraries,
so such fixtures can be generated from a worktree of it.
- [ ] protondrive: a ranged read of a file which doesn't record its
block sizes (written by some other clients) starts at the furthest
block whose size an earlier read in the same `Fs` learnt
(download.go `blockSizeCache`, 16 revisions, 65536 blocks), else at
the first block. The first chunks of a multi-thread copy, which start
together, still each read from the first block, and a random-offset
read beyond what was read before through a mount is slow. A ranged
read with sizes unknown also fetches up to `downloadAhead` blocks past
its end. Neither cache nor listing tells where a block starts without
reading the ones before it, as the packet framing varies by writer.
- [ ] protondrive: `client.apiCall` retries POST and PUT mutations
(create folder, move, rename, copy) after 5xx and network errors, as the
SDK does. If a lost response hid a success, the retry fails with
`errExists` (2500) or a stale `OriginalHash` (2000) although the change
was made. Consider re-reading the node after such a retry failure.
- [ ] protondrive: `createRevision` (POST `.../files/{id}/revisions`) is
retried by `apiCall` after a lost response like any POST; a retry that
succeeds leaves the first draft revision orphaned on the server (the
fake allows several draft revisions). Find out whether Proton refuses a
second draft revision, and delete or reuse our own if so.
- [ ] protondrive: confirm on a real account what `delete_multiple` does to
an active (non-draft) link. The upload cleanup only sends it for drafts,
and the fake refuses non-drafts, but the SDK's conflict handler sends it
for any `ConflictDraftRevisionID`, which an active file with a draft
revision also reports; Task 11 uploads a new revision in that case
instead.
A `--retries` pass after an upload whose outcome was unknown can reach
the own-draft `delete_multiple` in `resolveConflict` (`upload.go`); it
is decided by Proton's conflict details, which could in theory lag.
- [ ] protondrive: confirm the HTTP status and code Proton returns for a
bad verifier token, a missing block and a stale `CurrentRevisionID`
(the fake uses 422 / 2000, which the spec maps to "upgrade rclone").
- [ ] protondrive: Proton's server can take a moment to show a change
(seen live: a node read just after its commit was missing, or still a
draft with no active revision). The fake models this only through
`Server.SetReadLag`, on the links route after a create, copy or
commit; how long the real lag lasts and whether listings
(`folders/{id}/children`) lag too is unmeasured. Only the reads back
after a commit, a copy and, with `enable_caching` off, of a folder to
write into (`targetFolder`) retry, for about 7 s. Not tolerant yet, and the next
symptoms to expect: `activeNode` (Update, Move, Copy, DirMove of an
object just written) fails "object not found" while the node is
missing or still a draft; Move's read of the moved node may be stale.
- [ ] protondrive: drafts left after an ambiguous commit failure (5xx,
network error, timeout, cancel, or a refusal after the commit was
resent) are never deleted by the client, by design (see DECISIONS,
"a draft is deleted only on an explicit refusal"). The next upload of
the same name by this remote replaces its own draft, but a name never
uploaded again keeps an orphan draft (a new file's draft node, or a
draft revision) taking quota. `CleanUp` only empties the trash. Find
out whether Proton expires drafts by itself, and consider having
`CleanUp` delete this remote's own drafts (by `DraftClientUID`).
- [ ] protondrive: `errUploadUnconfirmed` is not retried at once, but a
whole-sync `--retries` pass lists again; if the listing still lags it
uploads the file again, adding a revision with identical content
(wasted work and version history, no data loss).
- [ ] protondrive: several `Fs` of one account in one process each have
their own node cache; only Move/DirMove sources are invalidated across
them. A name missing from one `Fs`'s cached listing stays missing after
another `Fs` creates it, so `--compare-dest`, `--copy-dest` (with
`--immutable`) and `checksum --download` decide on stale listings when
the other `Fs` writes during the run, and an `Fs` made on a path that
doesn't exist yet never finds it once it is made elsewhere. Five
`test_all` tests fail for this and are in the `ignore` list of
`TestProtonDriveFake:` (`fstest/test_all/config.yaml`):
`TestCheckSumDownload`, `TestCopyFileCompareDest`,
`TestCopyFileCopyDestImmutable`, `TestSyncCompareDest`,
`TestSyncCopyDestImmutable`; all pass with `enable_caching = false`.
Events polled every 30 s (spec 6.2) will not close a window of
seconds. Re-listing on every cache miss fixes them but breaks
`TestBudgetUpload` and `TestBudgetMkdir` (a folder re-listed per new
file). A cache shared by the `Fs` of one account would fix them, but
is package state, which the spec rules out: owner decision.
M5 kept per-`Fs` caches and left these five ignored (plan 002
ruling 7); events bound how long a mount shows other clients' changes
as stale, to about one poll interval, but not this window.
- [ ] protondrive: `cmd/gitannex` is in `ignoretests` for
`TestProtonDriveFake:` because its remote name check
(`cmd/gitannex/configparse.go`, `config.GetRemoteNames`) sees remotes
set by environment variables lower cased, as for every other init.d
remote; it says nothing about the backend. Run it against a remote in
a config file to cover it.
- [ ] `.gitea/workflows/ci.yml` could not be run locally. The Gitea has
an Actions runner (slow, so the workflow is kept light: 20 s per fuzz
target, a 45 minute job timeout, no `test_all`); the first push must
confirm the runner picks the workflow up.
- [ ] protondrive: browser sign-in in `rclone config` waits up to 10 minutes
polling; a non-interactive `config create` without username and password
waits the same.
- [ ] Propose a CI check for this fork: `AGENTS.md`, `CLAUDE.md`, `GEMINI.md`
and the `docs/` agent files exist, and `AGENTS.md` is at most 150 lines.
### final review minors (2026-11-12)
- [ ] protondrive (final re-review): ranged reads place their start from
block sizes in extended attributes even when those are unsigned, so a
forging server can shift the bytes a ranged read returns (full reads
are unaffected).
- [ ] protondrive (final re-review): `TestMain` applies `RCLONE_CONFIG`
only when it is set; with it unset, tests may still find the user's
config. Left for the owner (M5 Task 7): rclone's convention is that a
plain `go test` in a backend directory runs against the configured
`TestRemote:`, so defaulting to no config would change how live runs
are made.
- [ ] protondrive (final re-review): the draft client ID stays random per
run when the remote has a config section that can't be saved.
Minors from the whole-branch review (`final-review-*.md` in the plan
workspace) not fixed in its fix wave. Paths are under `backend/protondrive/`.
- [ ] A node fetched while it is being changed can overwrite the newer
cached node and stay cached: `put` after `fetchLinks` has no per-node
generation (`nodes.go` `fetchNode`, `cache.go` `put`).
- [ ] Unbounded per-client maps: `client.folderKeys` (kept whatever the
cache), the node cache, `client.reported`, `account.foreign`; `close()`
doesn't clear the private keys of cached file nodes.
- [ ] The name and hash key signature date exemptions
(`nameSignedSince`, `hashKeySignedSince`) and the content key fallback
read the server-supplied `CreateTime`, so a server can back-date a node
to exempt it (commented in `nodes.go`, as in the SDK).
- [ ] Earlier deferred crypto minors still present: the node passphrase
isn't checked as UTF-8 (`crypto.go` `decryptNodeKeys`); Move
detects an anonymous node by `SignatureEmail == ""` (`nodes.go`
`encryptForNewParent`). Left: either change needs checking against
what the SDK and Proton's apps accept.
- [ ] Two processes sharing one config (a mount and a cron job) can
delete each other's in-progress draft of the same new name, since the
draft client ID is per remote (`upload.go` `resolveConflict`).
- [ ] An own leftover draft revision on an active file is never deleted
and keeps taking quota (`upload.go` conflict path).
- [ ] A `createDraft` resend can report the client's own new draft as
one that "came back after it was deleted" (`upload.go` `createDraft`).
- [ ] `rclone config` on a remote with a saved session asks "Already
signed in - sign in again?" with default no, even when the session is
dead and the user followed the reconnect hint (`protondrive.go`
`Config`, ruling 17).
- [ ] After a CAPTCHA in `rclone config`, the method question defaults
back to password sign-in (`protondrive.go` `Config`).
- [ ] Token refresh hasn't been seen working against the real server,
and its request lacks the `UID` and `RedirectURI` go-proton-api sends
(`api/types.go` `AuthRefreshRequest`); add it to the live checks.
- [ ] Session minors deferred since Tasks 2-4: an adopted stored token
whose access token is stale fails one call with a 401; the refresh uses
the first waiter's ctx; `serverTime` can step back; the half-signed-in
cleanup `DELETE` uses a possibly cancelled ctx; an empty SRP salt isn't
refused up front; `GET /core/v4/users` is fetched twice at sign-in
(`session.go`, `account.go`).
- [ ] Server-side Copy onto an existing name falls back to download and
re-upload (`protondrive.go` `Copy`).
- [ ] `Precision()` is 1 s while `ModTime` has milliseconds;
cross-account Move/Copy/DirMove aren't refused; the on-the-fly remote
hint names the bare backend; logging passes `nil`, not the `Fs`,
throughout (sweep before upstream).
- [ ] Goroutine-count leak checks remain (`download_internal_test.go`
`requireNoLeaks`, `nodes_internal_test.go`); don't add more.
- [ ] `TestAcquireTokenCancel` sleeps 20 ms to let the wait start
(`upload_internal_test.go`). A miss only tests a cancel before the
wait, and still passes; removing it needs a hook in `acquireToken`.
- [ ] CI runs on both `push` and `pull_request` with no `concurrency`
group, so twice per PR push (`.gitea/workflows/ci.yml`); check Gitea
supports `concurrency` first.
- [ ] The fake's read lag (`protontest` `SetReadLag`) never runs with the
contract suite; consider a second `fstests.Run` or a `protonfake
-read-lag` flag.
- [ ] The docs page describes CAPTCHA handling, 2FA prompting and
account-wide `about` that are proven only against the fake, while this
file says the docs avoid claiming them; soften one or the other.
- [ ] `docs/PERMISSIONS.md` and `.claude/`/`.gemini/` allow
`golangci-lint run` but `.codex/rules/project.rules` doesn't; bring it
in step (owner, as it is a permission change).
### plan 002 whole-branch review (2026-11-23), not fixed at handover
- [ ] protondrive events: after another client renames a folder, a mount
re-listing the parent before `forgetChanged` runs makes `GetInv` return
the new path, so the old path's dircache entries stay
(`protondrive.go` `forgetChanged`). Fix: also flush the old path from
`GetInv(parentID)` + name, or compute flush paths before notifying.
- [ ] protondrive events: the start cursor is fetched after the first
listing is read and decrypted, so changes in between are never
reported; same for a `fetchNodeIn` -> `put` in flight across a poll
(`nodes.go` `onFill`, `cache.go`). Fix: fetch the cursor before the
first server read; guard `put` with a cache generation token.
- [ ] protondrive events: `invalidateTree` walks the whole cache for each
Delete or folder move, under `p.mu` and `cache.mu`; emptying a big trash
(one Delete per link) on a large cache can stall a mount. Fix: one
parent->children index per `applyEvents` batch, skip unknown IDs.
- [ ] protondrive events (minor): `stop()` can miss a timer poll queued on
`runMu` (re-check `gen` after `pollMu.Lock()`); `setInterval` is never
reset when the last subscriber leaves; an event for an uncached node
invalidates all in-flight listings; a non-advancing page is re-applied
every interval.
- [ ] protondrive tests: test `Fs` made with `NewFs` and never shut down
(`regression_test.go` 192, 194, 230, 280, 610; `security_test.go` 571,
1032; `golden_internal_test.go` 227) keep polling closed or reused-port
servers for the whole run. This is the likely cause of both the
`ci.LogLevel` `-race` report in TestHostileServer and the
TestRegressionTwoFsRefreshRace flake (a foreign refresh takes a Gather
slot). Fix: a `mustNewFs` helper that registers `Shutdown` in
`t.Cleanup` after `protontest.New(t)`; in `fake_test.go` wrap
`ri.NewFs` to shut down every `Fs` made during `TestIntegrationFake`,
replacing the 2501 fault and 3 s sleep. Also widen the timing bounds in
`events_internal_test.go` (TestPollerPollsWhileCached, Eventually
waits at 603/646, TestPollerBackoff upper bound).
- [ ] protondrive docs (minor): environment-variable limitation paragraph
belongs under Limitations and likely applies to connection-string
remotes too; mention that a 2501 stops change tracking until restart;
conformance checklist "30 s between polls" holds only for Proton hosts;
`original_file_size` Help still has the old wording.
## Someday
## Recurring
- [ ] Maintain `AGENTS.md`:
- **Add a rule only in response to a real failure.** Do not pre-empt
hypothetical mistakes.
- **Update in the same commit as the convention change.**
- **Prune quarterly**, and re-check the Fork Workflow section against
upstream's `AGENTS.md` after each upstream merge.
## Known issues
- [ ] On the development host (Go 1.27.4) `RCLONE_CONFIG=/notfound go test ./...`
fails only `cmd/serve/s3`, unrelated to this work and undiagnosed (seen
2026-11-11 before the Proton Drive rewrite began). That is the baseline
for plan Task 19's "no new failures"; the container list below is not.
- [ ] On the development host on 2026-11-12, `cmd/bisync` `TestRcBackupDir`
(`backupDir1`, `backupDir2`) fails every run with "unable to find file
.../backup/file0.txt", also at `06700aaf2` (before the Proton Drive
rewrite), so it is not caused by that work; undiagnosed. `cmd/serve/s3`
passed in the same full run.
- [ ] `RCLONE_CONFIG=/notfound go test ./...` in the bare `golang:1.26`
container fails 7 packages (list in `docs/PERMISSIONS.md`). `fs/logger`
needs `rsync`; the mount, serve and smb failures are undiagnosed and are
probably missing container tooling (FUSE, git-annex), not code. Confirm on a
host with Go installed before treating a full-suite failure as a regression.
- [ ] protondrive: `TestRegressionManyNewFs` can't see from the fake
server that key unlocks run one at a time; only
`TestLoadAccountSerialised` (internal, `unlockHook`) checks it.
- [ ] protondrive: an address key's `Token` (account.go
`unlockAddressKey`) is decrypted without a decompression limit, unlike
node metadata (`decryptMessage`); a hostile server could send a
compression bomb there too. Seen in Task 17.
- [ ] protondrive: API error bodies are read whole and their `Error`
message is logged as the server sent it, of any length and with any
control characters (session.go `errorHandler`). Bound and sanitise it
if a hostile server is in scope for logs. Seen in Task 17.
- [ ] protondrive: a new revision (`createRevision`), a move, rename or
trash still forget the parent folder's cached listing, so `Put` over
existing files in one folder lists it again each time (`sync` is not
affected: it calls `Update`). New files and folders are added to the
listing instead since Task 17.
- [ ] protondrive: the account's own user keys (account.go `loadAccount`)
are unlocked without the Argon2 limits node and share keys get
(crypto.go `checkKeyS2K`): the server stores them too, so a hostile
server could make sign-in run a KDF of up to 2 TiB. Left out because a
genuine user key's S2K parameters aren't known to be within the
limits; check what Proton's apps write before applying them. Address
keys are safe: their token's signature is checked first.
- [ ] protondrive: `TestRegressionTwoFsRefreshRace` failed once in a
full `-race` run of the package on 2026-11-12 (a listing got 401 and
the server saw 3 refreshes, not 2); it passed 30 runs alone with
`-race`. Undiagnosed; seen in M5 Task 4. Not reproduced in M5 Task 7:
1200 runs alone with `-race` at `-cpu 1,2,4,16`, and nine full
`-race` runs of the package, three at a time (which did turn up
other load-only failures, since fixed). Lead: any third refresh
request in the test's window gives the count of 3 (shown by sending
one from a client of another session: 3 refreshes, no 401), and the
package leaks `Fs` whose pollers keep polling closed test servers,
so one that hits a later server on a reused port would send one. The
401 is not explained: neither our two sessions nor a foreign one
rotate the session's tokens after a retry. The failure's full output
was not kept; keep it if it recurs.
- [ ] protondrive `ChangeNotify`: a folder renamed in place by another
client is reported at its old path and as a change to its parent, not
at its new path, which the event doesn't carry; a moved node is
reported under its old name in the new folder. VFS only needs the
parent, so mounts are right; other consumers get no new name.
- [ ] protondrive: a listing fetched before an event is applied can
still `Put` its folders into the dircache after the event flushed
them (`ListP`); the node cache's fetch guard doesn't cover the
dircache. The next event or `DirCacheFlush` corrects it.
- [ ] protondrive tests: with three `-race` package runs at once, the
race detector still reports `captureLog` (`security_test.go`) writing
`ci.LogLevel` against a poll's debug line (`events.go` `pollEvents`)
in `TestHostileServer` cases and `TestNoSecretsInLogs`, about once
per run, even where the polling `Fs` was shut down first and
`Shutdown` now waits for a poll in flight (M5 Task 7). Also seen once
in a single full run, in a case that doesn't call `captureLog`,
against the previous case's cleanup: the polling `Fs` was made after
that cleanup, so the two are ordered by `t.Run`, which suggests the
detector sees no happens-before edge into `time.AfterFunc` callbacks
(worth checking against Go's runtime). Instrumenting the pollers
didn't catch one polling after its `Shutdown`. Left at wind-down;
next step is that check, or a fake remote for the security tests
that doesn't poll at all.
- [ ] protondrive events: an `Fs` never shut down with nothing
subscribed polls until its node cache has gone unused for 10 minutes
(`cacheIdleTime`), which is longer than the rest of the package's
test run, so `TestIntegrationFake` still makes the volume gone at
cleanup to stop the `Fs` fstests leaks (`fake_test.go`).
+328 -73
View File
@@ -7,28 +7,47 @@ versionIntroduced: "v1.64.0"
# Proton Drive # Proton Drive
[Proton Drive](https://proton.me/drive) is an end-to-end encrypted Swiss vault [Proton Drive](https://proton.me/drive) is an end-to-end encrypted Swiss vault
for your files that protects your data. for your files that protects your data.
This is an rclone backend for Proton Drive which supports the file transfer This backend reads and writes Proton Drive with the same client-side
features of Proton Drive using the same client-side encryption. encryption as Proton's own apps. It is a client of the Proton Drive API
written for rclone, following the behaviour of
[Proton's Drive SDK](https://github.com/ProtonDriveApps/sdk).
Due to the fact that Proton Drive doesn't publish its API documentation, this rclone is a third-party application not officially supported by Proton.
backend is implemented with best efforts by reading the open-sourced client
source code and observing the Proton Drive traffic in the browser.
**NB** This backend is currently in Beta. It is believed to be correct
and all the integration tests pass. However the Proton Drive protocol
has evolved over time there may be accounts it is not compatible
with. Please [post on the rclone forum](https://forum.rclone.org/) if
you find an incompatibility.
Paths are specified as `remote:path` Paths are specified as `remote:path`
Paths may be as deep as required, e.g. `remote:directory/subdirectory`. Paths may be as deep as required, e.g. `remote:directory/subdirectory`.
## Configurations ## Configuration
Here is an example of how to make a remote called `remote`. First run: There are two ways to sign in:
- **With a web browser** (the default when no username and password are
set). rclone shows a link to Proton's sign-in page, which you can open
on any device, including a different computer from the one running
rclone. Your password, two-factor authentication and any CAPTCHA are
handled by Proton's page; rclone never sees or stores your password.
- **With a username and password**, for existing configurations and
automated setups. rclone signs in with the `username`, `password`,
and, if needed, `mailbox_password`, `2fa` or `otp_secret_key` options.
If Proton asks for a CAPTCHA, password sign-in fails and you need to
run `rclone config reconnect remote:` and choose browser sign-in.
Either way rclone saves the session in the config file and uses it from
then on, refreshing it as needed. The saved session gives full access to
your account, so consider [encrypting your
configuration](/docs/#configuration-encryption).
Two uses of a remote set up with environment variables within one
rclone command, such as the source and destination of a copy between
two of its folders, can't see each other's refresh of the session, so
the second to refresh fails with "session expired". A remote in the
config file doesn't have this problem.
Here is an example of how to make a remote called `remote` with browser
sign-in. First run:
```console ```console
rclone config rclone config
@@ -50,37 +69,84 @@ XX / Proton Drive
\ "protondrive" \ "protondrive"
[snip] [snip]
Storage> protondrive Storage> protondrive
User name Option username.
user> you@protonmail.com The username of your Proton account.
Password. Only needed to sign in with a password. Signing in with a web browser,
y) Yes type in my own password which "rclone config" offers, needs neither username nor password.
g) Generate random password
n) No leave this optional password blank
y/g/n> y
Enter the password:
password:
Confirm the password:
password:
Option 2fa.
2FA code (if the account requires one)
Enter a value. Press Enter to leave empty. Enter a value. Press Enter to leave empty.
2fa> 123456 username>
Remote config Option password.
The password of your Proton account.
Only needed to sign in with a password.
Choose an alternative below. Press Enter for the default (n).
y) Yes, type in my own password
g) Generate random password
n) No, leave this optional password blank (default)
y/g/n> n
Option 2fa.
[snip]
2fa>
Option otp_secret_key.
[snip]
y/g/n> n
Edit advanced config?
y) Yes
n) No (default)
y/n> n
Sign in with a web browser?
rclone is a third-party application not officially supported by Proton.
Choose yes to sign in on Proton's sign-in page, on any device; rclone
never sees your password. Choose no to sign in with the username and
password set above.
y) Yes (default)
n) No
y/n> y
Open this link in a web browser, on any device, and sign in to Proton:
https://account.proton.me/desktop/login?app=drive&pv=3#payload=XXXXXXXX
Waiting for the sign-in to complete...
Configuration complete. Configuration complete.
Options: Options:
- type: protondrive - type: protondrive
- user: you@protonmail.com [snip]
- pass: *** ENCRYPTED ***
Keep this "remote" remote? Keep this "remote" remote?
y) Yes this is OK y) Yes this is OK (default)
e) Edit this remote e) Edit this remote
d) Delete this remote d) Delete this remote
y/e/d> y y/e/d> y
``` ```
**NOTE:** The Proton Drive encryption keys need to have been already generated The sign-in link contains a secret which lets whoever completes the
after a regular login via the browser, otherwise attempting to use the sign-in hand their session to this rclone, so don't share it. rclone
credentials in `rclone` will fail. waits up to 10 minutes for the sign-in to complete.
To sign in with a password instead, enter your username and password at
the `username` and `password` prompts (and, for a two-password account,
the `mailbox_password` advanced option), then answer `n` to "Sign in with
a web browser?". If your account uses two-factor authentication, rclone
asks for a code:
```text
Two-factor authentication code
Enter a value.
config_2fa> 123456
```
The code is only needed while signing in. If rclone has to sign in again
later without you, for example because the saved session has expired, it
uses the `otp_secret_key` option to generate a code; without it, or
without a username and password, it stops and asks you to run
`rclone config reconnect remote:`.
Running `rclone config reconnect remote:` (or editing the remote) on a
remote which is already signed in asks whether to sign in again; if you
do, the previous session is signed out.
A new account doesn't need to have used Proton Drive before: if the
account has no Drive yet, rclone creates it.
Once configured you can then use `rclone` like this (replace `remote` with the Once configured you can then use `rclone` like this (replace `remote` with the
name you gave your remote): name you gave your remote):
@@ -97,7 +163,7 @@ List all the files in your Proton Drive
rclone ls remote: rclone ls remote:
``` ```
To copy a local directory to an Proton Drive directory called backup To copy a local directory to a Proton Drive directory called backup
```console ```console
rclone copy /home/source remote:backup rclone copy /home/source remote:backup
@@ -105,33 +171,226 @@ rclone copy /home/source remote:backup
### Modification times and hashes ### Modification times and hashes
Proton Drive Bridge does not support updating modification times yet. The modification time of a file is stored, encrypted, with each upload
and read back with a precision of 1 second. It can't be changed without
uploading the file again, so rclone can't set it on an existing file.
The modification time of a folder is set when it is created.
The SHA1 hash algorithm is supported. The SHA1 hash algorithm is supported. The hash is the one the uploader
recorded in the file's encrypted attributes; files uploaded by clients
which don't record one have no hash.
### Integrity and signatures
Proton Drive is end-to-end encrypted, and rclone checks what it reads
the way Proton's own apps do, so that a server which altered your data
would be noticed:
- Every encrypted block of a file is checked against the SHA-256 hash
the server lists for it before it is decrypted. A block which doesn't
match is downloaded again, up to three attempts in all, then the read
fails.
- A read of a whole file first fetches the list of all its blocks and
checks the uploader's signature over it (the manifest) before
returning any data, so a file can't be truncated, extended or have its
blocks reordered. For a very large file this adds about one request
per 80 MiB before the first byte arrives.
- The keys of files and folders and the key of each file's content must
be signed by their owner.
On upload rclone signs everything it writes, checks that each encrypted
block decrypts again before sending it, and, before committing the
upload, checks the number of bytes read against the size of the source
and the SHA1 of what was read against the source's SHA1 if the source
has one.
**Ranged reads.** A read of only part of a file, as `rclone mount`,
`rclone serve` and multi-thread downloads (`--multi-thread-streams`)
make, checks each block against its listed hash but can't check the
manifest, so a malicious server could serve validly encrypted blocks of
the same file in the wrong place, or from an older version of the same
size. Proton's SDK has the same limit for ranged reads. A range which
covers the whole file is checked as a full read. A file uploaded by a
client which doesn't record its block sizes can only be entered in the
middle as far as earlier reads in the same rclone process have reached:
beyond that a ranged read downloads, checks and discards every block
before the range, which makes it slower. rclone remembers the sizes of
up to 65536 such blocks, so each chunk of a multi-thread download starts
near where an earlier one ended. Its manifest still isn't checked.
**Trust is rooted in your own addresses.** The share holding your files
must have been created, and its root folder signed, by one of your
account's addresses, otherwise nothing in the drive is treated as
verified. A folder whose key isn't verified makes everything beneath it
unverified, even items you signed yourself, because whoever made that
key can read and replace what is in it. That includes a genuine folder
signed by an address since removed from your account.
By default rclone refuses to:
- read a file whose key, content key or manifest signature doesn't
verify, or which is beneath a folder whose key isn't verified;
- update a file whose key or content key signature doesn't verify, as
its new content would be encrypted with a key someone else may hold.
A content key without a signature counts as one that doesn't verify;
- create, upload, rename, move or copy anything into a folder whose key
isn't verified, because what is written there would be encrypted to a
key someone else may hold, or whose name hash key's signature doesn't
verify, because whoever made that key could test guesses at the names
written there;
- rename, move or copy an item whose key isn't verified, because that
re-signs it with your keys.
The errors say so and name the `ignore_signature_errors` advanced option.
Data uploaded by some old or third-party clients, or shared with you by
others, may need it. When it is set, rclone logs an `ERROR` for each
item and operation it lets through and goes ahead; an item renamed,
moved or copied this way is signed with your keys and trusted from then
on. The option never turns off the block hash checks.
A file or folder name whose signature doesn't verify is listed anyway,
with an `ERROR` logged, as Proton's apps do.
The modification time, size and SHA-1 of an item whose extended
attributes' signature doesn't verify aren't used unless
`ignore_signature_errors` is set: rclone logs an `ERROR` and shows the
server's modification time and no SHA-1. A file's size then comes from
its recorded block sizes, which reads check, or is the encrypted size if
it has none. The file stays readable.
Two limits of the Proton Drive protocol, which Proton's SDK shares,
can't be detected by rclone:
- A server which serves a whole earlier version of a file (its blocks,
manifest and attributes, all validly signed) in place of the current
one.
- Below the root of your drive, an item signed by another Proton user,
such as a member of a shared folder, is checked against public keys
which the server itself supplies, so a malicious server could forge
those.
### Restricted filename characters ### Restricted filename characters
Invalid UTF-8 bytes will be [replaced](/overview/#invalid-utf8), also left and Names may be at most 255 characters long, counted in UTF-16 code units
right spaces will be removed ([code reference](https://github.com/ProtonMail/WebClients/blob/b4eba99d241af4fdae06ff7138bd651a40ef5d3c/applications/drive/src/app/store/_links/validation.ts#L51)) as Proton's apps count them (so some emoji count as two), and can't be
empty, `.` or `..`, or contain `/` or NUL. The default encoding replaces
these characters:
| Character | Value | Replacement |
| --------- |:-----:|:-----------:|
| NUL | 0x00 | ␀ |
| / | 0x2F | / |
File names can also not start or end with the following characters.
These only get replaced if they are the first or last character in the
name:
| Character | Value | Replacement |
| --------- |:-----:|:-----------:|
| SP | 0x20 | ␠ |
Names which are exactly `.` or `..` are replaced with `.` and `..`.
Invalid UTF-8 bytes will also be [replaced](/overview/#invalid-utf8). If
the encoding is changed so that they are not, names which aren't valid
UTF-8 are refused.
### Duplicated files ### Duplicated files
Proton Drive can not have two files with exactly the same name and path. If the Proton Drive can't have two files with exactly the same name in the same
conflict occurs, depending on the advanced config, the file might or might not folder. Uploading a file whose name is already taken by a file adds a new
be overwritten. revision to that file, keeping its history, rather than making a second
file.
### [Mailbox password](https://proton.me/support/the-difference-between-the-mailbox-password-and-login-password) An upload which is cancelled or fails can leave a draft behind. A draft
left by the same remote is replaced on the next upload of that file. A
draft left by another client, which may still be uploading, makes the
upload fail unless the `replace_existing_draft` advanced option is set.
A remote set up with environment variables or a connection string,
which can't save settings, is told apart by its name and account, so two
machines using such a remote of the same name on the same account count
as the same remote.
Please set your mailbox password in the advanced config section. If the final step of an upload fails in a way which leaves it unknown
whether the file was saved, rclone reports "upload outcome unknown: it
may have succeeded" and doesn't retry that upload at once; a later run of
`rclone sync` or `rclone copy` checks the destination first. The same
goes for "upload committed, but the server doesn't show it yet", which
means Proton accepted the upload but didn't list it within a few
seconds.
### Deleting files
Deleted files and folders (`rclone delete`, `rclone purge`,
`rclone rmdir`) are moved to the Proton Drive trash, as Proton has no
direct permanent delete. They count against your storage until the trash
is emptied. `rclone cleanup remote:` empties the whole trash
permanently, including items deleted with Proton's apps.
`rclone rmdir` asks the server whether the folder is empty, so it refuses
to remove a folder which holds items rclone doesn't list, such as Proton
Docs or items it can't decrypt.
### Items left out of listings
Proton Docs and Sheets have no file content for rclone to read, so they
are left out of listings, with one notice logged. Items whose name or
key can't be decrypted are left out too, each with an `ERROR` logged.
### Mailbox password
If your account uses a separate [mailbox
password](https://proton.me/support/the-difference-between-the-mailbox-password-and-login-password),
set it in the `mailbox_password` advanced option to sign in with a
password. Browser sign-in doesn't need it.
### Caching ### Caching
The cache is currently built for the case when the rclone is the only instance rclone keeps the decrypted metadata of the files and folders it has read
performing operations to the mount point. The event system, which is the proton in memory, to save requests and decryption. To turn this off, set the
API system that provides visibility of what has changed on the drive, is yet `enable_caching` advanced option to `false`. The cache follows changes
to be implemented, so updates from other clients won’t be reflected in the made by other clients, as described below.
cache. Thus, if there are concurrent clients accessing the same mount point,
then we might have a problem with caching the stale data. ### Changes made elsewhere
rclone follows the change events of your drive, so it sees changes made
by other clients (Proton's apps, or another rclone) while it runs.
- While rclone holds anything in its cache, it asks Proton for new events
every 30 seconds (under a mount or serve, every `--poll-interval` if
that is longer). It drops what the events say is out of date and reads
it again when next needed.
- `rclone mount` and `rclone serve` also pass the changes on, so a mount
shows another client's change within about one `--poll-interval`
(default 1 minute). Values below 30 seconds are raised to 30 seconds.
A longer interval is used as it is.
- Mounts benefit with `enable_caching = false` too: the events still
tell the mount which folders to read again.
- An idle rclone with nothing cached and no mount or serve makes no event
requests. Without a mount or serve, rclone forgets a cache it hasn't used for
10 minutes, so a long-running rclone that has gone quiet stops making
them too.
- If rclone has missed too many events, for example after a long time
without network, Proton asks it to start over. rclone then forgets
everything it has cached and reads it again as it is used. A mount
reads its folders again too.
- If the requests fail, rclone waits longer between them, up to 10
minutes, and goes back to the normal interval once they work again.
### Logs and `--dump`
rclone's own log messages for this backend never contain passwords, keys
or tokens, at any `-v` level. The output of the `--dump` flags does:
`--dump headers` shows the `x-pm-uid` and `pm-storage-token` headers,
and `--dump bodies` shows the access, refresh and upload tokens Proton
sends in request and response bodies. Treat `--dump` output from this
backend as secret, and sign the remote in again (with
`rclone config reconnect remote:`) if you have shared it.
`rclone config create`, `rclone config show` and `rclone config dump`
print the remote's saved session: the `client_` tokens and the salted
key password, which stays valid after signing out until the account
password changes. Don't share that output either.
<!-- autogenerated options start - DO NOT EDIT - instead edit fs.RegInfo in backend/protondrive/protondrive.go and run make backenddocs to verify --> <!-- markdownlint-disable-line line-length --> <!-- autogenerated options start - DO NOT EDIT - instead edit fs.RegInfo in backend/protondrive/protondrive.go and run make backenddocs to verify --> <!-- markdownlint-disable-line line-length -->
### Standard options ### Standard options
@@ -330,25 +589,21 @@ Properties:
## Limitations ## Limitations
This backend uses the - Only the "My files" section of Proton Drive is available; computers
[Proton-API-Bridge](https://github.com/henrybear327/Proton-API-Bridge), which backed up with Proton's desktop app, Photos and items shared with you
is based on [go-proton-api](https://github.com/henrybear327/go-proton-api), a are not.
fork of the [official repo](https://github.com/ProtonMail/go-proton-api). - Changes made by other clients are seen within about one polling
interval, not at once (see [Changes made
There is no official API documentation available from Proton Drive. But, thanks elsewhere](#changes-made-elsewhere)).
to Proton open sourcing [proton-go-api](https://github.com/ProtonMail/go-proton-api) - rclone metadata (`--metadata`) is not supported, and the modification
and the web, iOS, and Android client codebases, we don't need to completely time of an existing file can't be changed without uploading it again.
reverse engineer the APIs by observing the web client traffic! - A file may have at most 524288 blocks of 4 MiB (2 TiB); rclone refuses
to read a file which lists more. Items more than 2048 folders deep
[proton-go-api](https://github.com/ProtonMail/go-proton-api) provides the basic can't be used.
building blocks of API calls and error handling, such as 429 exponential - `rclone about` reports the storage used by your whole Proton account,
back-off, but it is pretty much just a barebone interface to the Proton API. not only by Drive.
For example, the encryption and decryption of the Proton Drive file are not - Files are uploaded in blocks of 4 MiB, each held in memory while it is
provided in this library. uploaded: up to 5 blocks of one file at once (the `upload_concurrency`
advanced option, default 4), and at most 10 blocks uploading and 10
The Proton-API-Bridge, attempts to bridge the gap, so rclone can be built on downloading at once per remote, so with a high `--transfers` some
top of this quickly. This codebase handles the intricate tasks before and after transfers wait their turn.
calling Proton APIs, particularly the complex encryption scheme, allowing
developers to implement features for other software on top of this codebase.
There are likely quite a few errors in this library, as there isn't official
documentation available.
+6 -2
View File
@@ -2,7 +2,7 @@ backend: protondrive
name: Proton Drive name: Proton Drive
tier: Tier 4 tier: Tier 4
maintainers: Community maintainers: Community
features_score: 4 features_score: 5
integration_tests: Passing integration_tests: Passing
data_integrity: Hash data_integrity: Hash
performance: High performance: High
@@ -14,14 +14,18 @@ remote: 'TestProtonDrive:'
features: features:
- About - About
- CanHaveEmptyDirectories - CanHaveEmptyDirectories
- ChangeNotify
- CleanUp - CleanUp
- Copy
- DirCacheFlush - DirCacheFlush
- DirMove - DirMove
- Disconnect - Disconnect
- ListP
- Move - Move
- NoMultiThreading
- Purge - Purge
- PutStream
- ReadMimeType - ReadMimeType
- Shutdown
hashes: hashes:
- sha1 - sha1
precision: 1000000000 precision: 1000000000
+462
View File
@@ -0,0 +1,462 @@
# Handoff: Proton Drive backend rewrite (plan 001)
Written 2026-11-11 when development was paused at the owner's request.
Everything a new agent needs is in this repository; nothing depends on the
conversation that produced it. Fork-only document - never include it in a
PR to rclone/rclone.
## 1. What this is
The owner's fork of rclone is rewriting `backend/protondrive` as a native Go
client for Proton Drive's current API (volume-based `drive/v2` routes),
replacing the abandoned `Proton-API-Bridge` / `go-proton-api` libraries.
The goal is a working, maintainable, secure backend that rclone's
maintainers could one day accept.
Read, in this order:
1. `AGENTS.md` - code, comment and commit rules; the "Fork Workflow" section.
2. `docs/specs/001-protondrive-tier1.md` - the approved spec (binding).
3. `docs/plans/001-protondrive-tier1.md` - the 21-task plan. Checkboxes
show what is done.
4. This file - state, rulings, and how to resume.
5. `docs/plans/001-protondrive-tier1-workspace/progress.md` - the full
execution ledger (every ruling, every deferred minor, every live probe).
6. `docs/ARCHITECTURE.md`, `docs/DECISIONS.md`, `docs/TODO.md`.
## 2. Owner's standing instructions
- Nothing is posted upstream (no issue, PR or forum post) for now. The
upstream path in spec section 13 is deferred.
- The owner is not planning permanent maintenance of the backend.
- The fake server (`backend/protondrive/protontest`) stays in the fork even
though it is about a third of the code; it would not go upstream.
- Implementers are Opus 5.5 subagents, one task at a time; each task gets
an independent review (Opus for auth, crypto, concurrency and the swap;
Sonnet for small diffs and scoped re-reviews).
- "Once I approve a design, carry it through spec, plan and execution with
no extra review stops. Report the decisions you made at the end."
- Permission rules: `docs/PERMISSIONS.md` (no sudo, no force-push, no
`git reset --hard`, no `git clean`, no recursive `rm`). Ask before adding
or upgrading a dependency.
- The push of this work to `master` on 2026-11-11 was an explicit one-off
instruction from the owner. The standing rule in `AGENTS.md` (never push
or commit directly to `master`) still applies to future work: branch
first and ask before pushing.
## 3. State
Updated 2026-11-12. Branch `protondrive-tier1`; `master` on Gitea holds
the work only up to the 2026-11-11 pause (`4282540e0`); everything after
is local until the owner asks for a push. (Later: the owner asked for a
push on 2026-11-12 and Gitea master is at `fbf2d565e`, the whole of
plan 001. M5 continues in `docs/plans/002-protondrive-m5-HANDOFF.md`.) Plan base (for a whole-branch
diff): `06700aaf2`. Rewritten history: see "Open questions" item 1.
| Work | State |
|---|---|
| Tasks 1-21 | Implemented and independently reviewed; Task 21 as far as the test accounts allow (section 6) |
| Final whole-branch review | Run 2026-11-12 (four reviewers by area); one fix wave (`e95317b52`..`944b36005`); scoped re-review ALL CLOSED |
What exists now:
- `backend/protondrive` is a native client of Proton's Drive API
(`session.go`, `account.go`, `crypto.go`, `nodes.go`, `cache.go`,
`download.go`, `upload.go`) behind the rclone `Fs` in `protondrive.go`.
The old bridge libraries are gone from `go.mod`.
- `backend/protondrive/protontest` is the fake Proton server (fork-only),
`protontest/cmd/protonfake` its standalone binary, started for
`fstest/test_all` by `fstest/testserver/init.d/TestProtonDriveFake`.
- Tests: `*_internal_test.go` per client file, `fake_test.go`
(`TestIntegrationFake`: rclone's contract suite against the fake),
`regression_test.go`, `security_test.go` (hostile server),
`budget_test.go` (request budgets), `fuzz_internal_test.go`,
`golden_internal_test.go` (re-keyed files written by Proton's web app
and official CLI, `testdata/golden/`), frozen old-library fixtures in
`testdata/oldlib/`.
- Fork CI: `.gitea/workflows/ci.yml` (the owner's Gitea has a slow
runner; the workflow has never run - the first push will show).
- User docs: `docs/content/protondrive.md` describes the new backend; its
generated options block is regenerated only for upstream.
- Live: see section 6.
Not done, and easy to assume otherwise:
- `ChangeNotify`, the persistent cache, Metadata and the small-file upload
path (M5, M6). Without events, a mount never sees other clients'
changes to folders it has listed.
- Five `test_all` tests fail against both the fake and the real service
because each `Fs` has its own listing cache (open question 5).
- Ranged reads check each block's hash and the content key's signature
but not the signed manifest (`docs/TODO.md`).
- 2FA, CAPTCHA, Proton's desktop and mobile clients, and a drive created
by Proton's own apps are unverified live.
## 4. How to resume
Development used the `superpowers:subagent-driven-development` skill
(obra's superpowers plugin, version 7.0.0; install with
`/plugin marketplace add obra/superpowers-marketplace` then
`/plugin install superpowers@superpowers-marketplace`). The skill is a
convenience, not a requirement: the per-task loop below can be followed by
hand. Its workspace lives at `.superpowers/sdd/001-protondrive-tier1/`,
which `.gitignore` excludes; a copy of every Markdown file
from it (ledger, per-task briefs, implementer reports, reviews, the shared
implementer/reviewer rules and the global constraints) is committed at
`docs/plans/001-protondrive-tier1-workspace/`. To resume with the skill:
```bash
mkdir -p .superpowers/sdd/001-protondrive-tier1
cp docs/plans/001-protondrive-tier1-workspace/*.md .superpowers/sdd/001-protondrive-tier1/
git switch -c protondrive-tier1-resume # never work on master
```
The ledger's first line names the plan; tasks with a `Task N: complete`
line are done. Paths inside `implementer-common.md` and
`reviewer-common.md` assume the checkout is `/home/user/rclone` and the
branch `protondrive-tier1` - adjust both if they differ. One rule in
`implementer-common.md` is obsolete: "the old `protondrive.go` must keep
compiling until Task 14" (Task 14 is done; there is no old implementation
left in the package).
Per task: extract the brief (`task-N-brief.md` holds the plan text for
task N; later ones are not extracted yet - copy the task's section from
the plan), record `BASE=$(git rev-parse HEAD)`, dispatch one implementer
with the brief plus `implementer-common.md`, then a reviewer with the
brief, the report and `git diff -U10 BASE..HEAD`, fix until clean, and
record it in the ledger. After Task 21: one whole-branch review on the
most capable model (merge base `06700aaf2`), one fix wave, then
`git rm` the plan and this workspace copy before any upstream-bound merge.
### Remaining work
All 21 plan tasks are done. What is left:
- The owner's open questions (below), especially question 5.
- Later milestones, each needing its own plan: M5 (events and
`ChangeNotify`), M6 (persistent metadata cache, rclone Metadata
support, small-file single-request upload), M7 (upstream path,
deferred by the owner; spec section 13 has the recipe).
- Live checks still open (`docs/TODO.md`): 2FA, CAPTCHA, desktop and
mobile clients, a drive made by Proton's apps.
Notes for live work:
- Live probes run from a scratch `git worktree` with a binary built from
it, never with `go test` in the main checkout without
`RCLONE_CONFIG=/notfound` unless the live test is the point.
- Proton's official CLI 0.9.0 runs in Docker: image
`proton-drive-cli:0.9.0` (debian:trixie-slim, non-root, built from the
checksum-verified release binary from
`https://proton.me/download/drive/cli/0.9.0/linux-x64/proton-drive`),
its free-account session in volume `pdcli-free`, e.g.
`docker run --rm -v pdcli-free:/home/drive proton-drive-cli:0.9.0 filesystem list /my-files`.
Its sign-in is browser-only (`auth login` prints a URL the owner opens)
and gives up after about 10 minutes; a late sign-in shows "Invalid
code".
- `rclone config create` prints the whole new remote, session secrets
included, for every backend; never run it where the output is captured.
- Golden fixtures: `TestGoldenCapture` with `-capture DIR
-capture-label web|cli -capture-remote NAME:PATH` (never `-remote`),
into a scratch directory first; inspect for account data before
copying into `testdata/golden/` (the ledger records how it was done).
- Do not delete the evidence folders listed in section 6.
## Open questions for the owner
Answered by the owner on 2026-11-12:
1. **Usernames in git history:** remove them. Done: commit `901d7fd5b`
was rewritten without the usernames (as `31f6aafd9`), the later
commits replayed unchanged, and `master` on Gitea force-pushed once
(`85f6b2f0e` -> `4282540e0`). A one-off; the no-force-push rule stands.
Old hashes in this file and the ledger written before that date
(`901d7fd5b`, `85f6b2f0e`, `d211b45e6`, `28f1aaf8a`) now map to
`31f6aafd9`, `4282540e0`, `65840bdc5`, `5afbe9e4f`.
2. **Gitea Actions:** a runner exists but is slow and limited; keep
`.gitea/workflows/ci.yml` light (no `test_all` in CI).
3. **Official client interop (Task 21):** allowed - install a released
Proton client and use the web app, preferably inside a Docker
container, signed in only to the test accounts.
Still open:
4. **Rulings.** Section 7 lists 33 decisions made on the owner's behalf;
they were reported in summary and not confirmed one by one.
5. **Cross-`Fs` cache staleness (Task 18).** Five `test_all` tests are
ignored because each `Fs` has its own node cache; a mount never sees
other clients' changes. Fix options (shared per-account cache = package
state the spec forbids; listing TTL; events in M5) are in
`docs/TODO.md`. Current ruling: leave for M5.
## 5. Test accounts and machine setup
Machine-local, not in the repository. A new machine needs them recreated.
- Go 1.27.4 is in `~/.local/opt/go` (symlinked into `~/.local/bin`),
golangci-lint 2.15.0 in `~/.local/bin`.
- Two Proton accounts the owner created for testing, configured as rclone
remotes in `~/.config/rclone/rclone.conf` (mode 600, passwords obscured):
- `TestProtonDrive:` - free account; password file
`~/proton_rclone_test_acc_password`.
- `TestProtonDrivePaid:` - paid account; password file
`~/proton_paid_acc_password`. Signed in by password on 2026-11-12.
Where to find the credentials on this machine (never copy either value
into the repository, a commit message, a log or a subagent prompt):
- Username: the `username` key of each remote in
`~/.config/rclone/rclone.conf`; `rclone config show TestProtonDrive`
and `rclone config show TestProtonDrivePaid` print it. That output also
contains the obscured password and, for the free account, session
tokens, so do not paste it anywhere.
- Password: plain text in the two files named above, one per account.
The same password is stored obscured in the rclone config, so rclone
itself needs nothing more; read the files only if a test must supply
the password directly, and then pass it by file or environment, never
on a command line that gets logged.
Both remotes are set up for password sign-in, which is what every live
probe used; browser sign-in has not been exercised against the real
service. On a different machine, ask the owner for the accounts.
- **Every `go test` must set `RCLONE_CONFIG=/notfound`**, e.g.
`RCLONE_CONFIG=/notfound go test -race ./backend/protondrive/...`.
Without it the live `TestIntegration` signs in to the real service on
every run. Only Task 21 talks to Proton deliberately.
- Live probes were run from a scratch `git worktree` so nothing temporary
could be committed. Proton's guidelines for third-party clients apply:
honest `x-pm-appversion` (`external-drive-rclone@<semver>-<channel>`),
official endpoints only, no hammering.
- The reference checkout of Proton's official SDK used during development
was `~/.cache/protondrive-sdk-ref` (github.com/ProtonDriveApps/sdk at
commit `05c9115`, tag `js/v0.23.0`). Read-only; never built or run.
- Live state on the free account: folder
`rclone-live-probe-20261111-130916` (files `empty.bin`, `five-mib.bin`,
`hello.txt` with two revisions, and `my-little-folder/Readme.txt` made by
the owner in Proton's web app). Leave it: it is the interop evidence.
## 6. What has been proven live
- Password (SRP) sign-in with the new code; a fresh account's Drive volume
is created by the client.
- Uploads of 0 B, 25 B, 5 MiB+3 and 9 MB files and a second revision are
accepted by Proton and read back byte-identical with signature and
manifest verification.
- Proton's web app lists the files, opens them, and shows "Digital
signature verified", the right size, modification time and SHA1.
- A folder and a file created in Proton's web app are listed, downloaded
and verified by the new code.
- With the rewritten backend (binary built at `c00162ed2`): `lsd`, `lsl`,
`cat`, `sha1sum`, `about`, `sync` up, `check` (0 differences), new
revision, server-side `moveto` and `copyto`, `sync` back byte-identical,
`rmdir` of a non-empty folder refused, `purge`.
Proven on 2026-11-12 (binaries at `502a32b5f` and `b440df2e1`):
- `TestIntegration` (`fstests`) against `TestProtonDrive:`: 98 pass,
29 skipped by the suite's feature checks, 0 fail.
- `test_all -n 1` against the real service: only the five cross-`Fs`
cache tests fail (owner question 5); `fs/operations`, `fs/sync` and
`vfs` otherwise pass. The first run found two real bugs (read-after-
write lag after a commit), fixed in `af6fdd789` and `be0053607`, and
the re-run confirmed the fix.
- Browser (session-fork) sign-in with `rclone config create` against the
real service, then listing, a verified read and `config disconnect`.
- The paid account: first password sign-in, `about` (64 GiB), mkdir and
rmdir. Its drive was most likely created by rclone at that moment.
- Interop with Proton's official CLI 0.9.0 (run in Docker, signed in to
the free account): each reads, verifies and modifies what the other
wrote, both ways, including multi-block, empty and Unicode-named files.
- Upgrade/downgrade with released rclone v1.76.0: each reads what the
other wrote, byte-identical.
- Re-keyed fixtures of files written by the web app and the CLI are in
`backend/protondrive/testdata/golden/` (`TestGoldenFixtures`).
Still not proven: 2FA; CAPTCHA; Proton's desktop and mobile clients; the
root-of-trust rule on a drive made by Proton's own apps; the items marked
"confirm" in `docs/TODO.md`.
Folders on the free account to leave in place (interop evidence):
`rclone-live-probe-20261111-130916`, `rclone-interop-20261112`,
`rclone-golden-cli-20261112`. The official CLI's session lives in the
Docker volume `pdcli-free` (image `proton-drive-cli:0.9.0`, built from the
checksum-verified release binary).
The security changes made after `c00162ed2` were re-checked live on the
free account without `ignore_signature_errors`: at `e7e43095d` (root of
trust, subtree distrust, manifest-first reads: listing, a verified full
read of the web-app file, an upload, a `Range: bytes=0-` read, purge) and
at `9c90998f7`, the last code commit (sync up, `check` with 0 differences,
a 4-stream multi-thread download byte-identical, verified read of the
web-app file, server-side move, purge). That drive's volume was created by
this client, so the root-of-trust rule is still unchecked on a drive made
by Proton's own apps. The hostile cases themselves can only be exercised
against the fake.
## 7. Rulings made during execution
Decisions the controller made where the plan or spec was silent or wrong.
Where this list and the plan or spec disagree, this list is what the code
does. Most are in the ledger as `Ruling:` lines with their reason and what
it costs if wrong; 16, 17 and 24 to 28 came out of review fix rounds and
their reasoning is in `task-14-review.md`, `task-16-review.md`,
`task-16-rereview.md`, `task-16-rereview2.md` and `task-17-review.md` in
the workspace copy, and in `docs/DECISIONS.md`. The owner was told of them
in summary but has not confirmed them one by one.
1. No separate worktree: work happened on branch `protondrive-tier1` in
the main checkout.
2. Task 8's retry test is `TestClientShouldRetry` (name collision).
3. The fake keeps Proton's published app-version grammar (channel
optional); the client always sends a channel.
4. Two sessions sharing one config: after a rejected refresh the session
re-reads the credential store for up to 3 s to adopt the other's new
tokens. No cross-`Fs` lock (the spec allows no package state beyond
`unlockMu`).
5. The key salt is chosen by the primary user key's ID (old library
behaviour), first entry as fallback.
6. A fresh account has no Drive volume (`my-files` answers 422 code 2501):
the client creates it, as the SDK does.
7. Written content follows the SDK (v4 node key advertising AEAD, v6 PKESK
content key, SEIPDv2 blocks) even where the old library's comments
disagree; confirmed accepted by the real server and web app.
8. The intended-recipient check on signatures is kept (stricter than the
SDK may be).
9. The fake rejects AEAD-encrypted names, passphrases, hash keys and
extended attributes written by the client.
10. An honoured `Retry-After` is capped at 60 s.
11. Upload commit outcome unknown: delete nothing, return
`errUploadOutcomeUnknown`. A draft is deleted only when the server
explicitly refused the commit (a 4xx with a Proton code, not 408) on
a single attempt the pacer did not resend; no read-back decides
deletion, as reads may lag (Task 21f fix round, superseding "positive
proof" from a read).
12. The fake reports a draft's owner in conflict details but does not gate
routes by `ClientUID`.
13. `Proton-API-Bridge` stays in `go.mod` until Task 19 even though no Go
file imports it any more.
14. In `rclone config`, browser sign-in is the default only when username
and password are not both set.
15. `Shutdown` leaves the `Fs` usable (it only drops the node cache); keys
are not cleared. This supersedes the plan's "Shutdown calls
`client.close()` once". `Disconnect` logs out; later use returns a
clear "not signed in" error naming the remote.
16. `Rmdir` asks the server, uncached, whether the folder has any child at
all and refuses if so.
17. `rclone config` on a remote with a saved session asks before signing in
again (default no) and logs the old session out when it does.
18. A file whose node key is not signed by its owner is refused unless
`ignore_signature_errors` is set. A key verified against an unverified
folder key is itself unverified, all the way down; creating, uploading,
renaming, moving or copying into such a folder is refused without the
flag. The flag never disables integrity checks.
19. A full read fetches the whole block list and verifies the signed
manifest before releasing any byte. Ranged reads do not verify the
manifest yet (`docs/TODO.md`). First-byte latency on very large files
grows by about one request per 80 MiB.
A re-fetched block list that changes a hash is an integrity error.
20. `TestBudgetNewObject` allows 8 requests, not the plan's 7 (four folder
levels at two requests each; Proton has no lookup-by-name route).
21. Every node beneath a folder whose key is unverified is unverified,
reads included, even when signed by the user's own address (the
implementer went beyond the ruling; a reviewer judged it justified and
it was kept). A genuine folder signed by a since-deleted address locks
its subtree behind `ignore_signature_errors`.
22. Moving, copying or renaming a node whose key is unverified is refused
without the flag. With the flag it proceeds, logs an ERROR once per
node and operation, and the node is trusted from then on.
23. Root of trust: the My Files share's creator and the root folder's
signer must be one of the account's own addresses, else the whole
drive is unverified. Addresses are compared after lowercasing ASCII
letters only; a match is always verified with the account's own keys
and never looked up on the server.
24. A file may list at most 2^19 blocks (2 TiB at 4 MiB). This bounds the
count, not the bytes: about 250-350 MiB of block metadata at the cap.
25. Parent chains deeper than 2048, or cyclic, are an integrity error.
26. A range covering the whole file takes the verified full-read path and
returns only the bytes asked for.
27. A folder listing is cached only if the folder's children did not
change while it was being read (per-folder change count).
28. Names that are not valid UTF-8 are refused on write.
29. A file whose content key packet signature is missing or invalid is
unverified (as 18 and 22): full reads, ranged reads and `Update`
refused without `ignore_signature_errors`, ERROR once per node and
operation with it. Stricter than Proton's SDK.
30. Proton codes 5001 and 5003 are the fatal "upgrade rclone" error; 2000
is a plain no-retry error keeping Proton's message; 2028 is retried
through the pacer. Supersedes the spec 6.6 mapping of 2000 and 2028.
31. Keys from the server or another user whose Argon2 parameters exceed 4
passes, parallelism 8 or 128 MiB of memory are refused before the KDF
runs (integrity error, not overridable).
32. A folder whose name hash key signature fails is unverified for writes
(as 18); a rename or move gets a fresh name session key unless the old
name verified; extended attributes whose signature fails are not used
without the flag (size then comes from the verified block sizes, the
modification time from the server, no SHA-1).
33. A remote whose config can't be saved (environment variables,
connection strings) derives its draft client ID from the volume ID, so
it is stable across runs; two machines using such a remote of the same
name on one account share it.
Known protocol limits, documented rather than fixed: a server that serves
a whole older, validly signed revision under the current revision ID
cannot be detected (Proton's SDK cannot either); below the root, something
signed under another user's email is verified against public keys the
server itself supplies.
## 8. Deferred findings
The final whole-branch review (2026-11-12) left its Minor findings in
`docs/TODO.md` under "final review minors (2026-11-12)"; its reports are
`final-review-*.md`, `final-fix-report.md` and `final-rereview.md` in the
workspace copy. The list below predates it and some items are now fixed.
Every review's minor findings were deferred to the final whole-branch
review. They are listed per task in the ledger (`progress.md`, lines
beginning `Task N: minor (deferred)` and "Deferred minors"). Only Tasks
14, 16 and 17 have review files in the workspace copy with file and line;
for Tasks 1 to 13 and 15 the reviewer's text was not saved and the ledger's
one-line entry (plus the implementer's report for that task) is all there
is, so expect to re-locate those by reading the code. The whole-branch
review will find most of them again. Items that need a decision or a live
check are in `docs/TODO.md`. The ones most worth
a new agent's attention:
- Ranged reads do not verify the signed manifest. Planned fix: a bounded
per-revision cache of verified block hashes.
- With `ignore_signature_errors` set, `Update` of an unverified file logs
"reading", not "updating" (`upload.go`, `download.go` `contentKey`).
- The depth-limit error is re-wrapped at every level and reaches about
88 KB; a hostile server can flood the log with it (`nodes.go`).
- `TestListingRacingChange` can hang until the package timeout if it
fails before releasing its held request; `protonfake`'s `main_test.go`
leaks its `run` goroutine on a failed assertion.
- `TestUploadShortSource` failed its goroutine-leak check once under heavy
parallel load and never again; `TestListCancel` uses the same
goroutine-count approach.
- `ModTime` is returned with millisecond precision while `Precision()`
says one second.
- Move, Copy and DirMove between two different accounts are not refused
with `fs.ErrorCant*` (rclone only calls them for the same remote name).
- `apiCall` retries non-idempotent mutations after 5xx or network errors.
- An extended-attribute `"Size": null` is read as size 0.
- API responses have no body-size limit; API error messages are neither
bounded nor sanitised before logging.
- Two processes sharing tokens through environment variables cannot see
each other's refresh.
- The race test for volume creation waits 2 s for no benefit.
- Real-server behaviours the fake only assumes: error codes for a bad
verifier, a missing block and a stale revision ID; whether drafts are
hidden from children; what `delete_multiple` does to an active link.
## 9. Before any upstream work
The spec's upstream path (section 13) is deferred. If it is ever taken
up: rclone's maintainers refuse cgo; Proton's SDK README says the SDK "is
not yet ready for third-party production use" and allows personal,
non-commercial projects under its guidelines; a Proton engineer wrote that
Proton "does not prohibit the usage of rclone, but does not offer formal
support for it either" (rclone issue #9252, rclone forum thread 53609).
Fork-only paths (`docs/specs/`, `docs/plans/`, `docs/ARCHITECTURE.md`,
`docs/DECISIONS.md`, `docs/TODO.md`, `docs/PERMISSIONS.md`, `GEMINI.md`,
`.claude/`, `.codex/`, `.gemini/`, the "Fork Workflow" section of
`AGENTS.md`, and `backend/protondrive/protontest`) never go into an
upstream PR.
@@ -0,0 +1,99 @@
# Final fix wave — brief (controller rulings)
Findings files (read the cited entries in full; file:line refers to HEAD 29ad26944):
- final-review-crypto.md (C1, C2, I1, I2, I3)
- final-review-transfer.md (I-1, I-2)
- final-review-auth.md (I-1, I-2)
- final-review-tests.md (I-1, I-2 only; I-3..I-5 are the controller's)
All in /home/user/rclone/.superpowers/sdd/001-protondrive-tier1/.
Work order: Critical first, one commit per finding (or tightly related group), each
with a regression test written first and seen failing (the reviewers' reproductions
are described in the files; reuse them). Commit prefix `protondrive:` (CI: `ci:` is
fine), messages user-readable per AGENTS.md.
## Rulings
**crypto C1 — unsigned/invalid content key packet signature.** Verify
ContentKeyPacketSignature (as the SDK defines it; check the reference checkout
~/.cache/protondrive-sdk-ref read-only) on every use of a file's content key: reads
(full and ranged) and Update. A missing or invalid signature makes the file
unverified, exactly like an unverified node key (ruling 18/22): reads and writes
refused unless `ignore_signature_errors`; with the flag, proceed and log an ERROR
once per node and operation. Integrity checks are never disabled. This is stricter
than the SDK; record it in docs/DECISIONS.md and as handoff ruling 29.
**crypto C2 — Argon2 parameters from the server.** Before running any S2K/KDF on
key material that came from the server or another user (node keys, share keys,
anything not the account's own locally verified keys), check the S2K parameters
against fixed caps and refuse (integrity error, not overridable by the flag) above
them. Choose the caps from what Proton's clients actually generate (gopenpgp v3 and
go-crypto defaults; the SDK) with modest headroom, and document them. Also bound
iterated-salted S2K counts if the library does not. Add a fuzz/unit test.
**crypto I1 — unverified hash key used for writes.** A hash key whose signature is
missing/invalid makes the folder unverified for writes (same as ruling 18: creating,
uploading, renaming, moving into it refused without the flag; ERROR with the flag).
**crypto I2 — rename/move reusing a name session key whose signature failed.** Never
reuse a session key from a name that did not verify; generate a fresh one (if the
name verified, keep current behaviour).
**crypto I3 — unsigned xattrs trusted.** Follow spec 6.4: xattrs whose signature is
missing/invalid are not used for modtime/size/SHA1 unless `ignore_signature_errors`
(then used with an ERROR once per node). Without them, fall back as the code already
does when xattrs are absent (check what that fallback is and that rclone behaves
sanely, e.g. ModTime from the revision create time, no SHA1). Do not make the file
unreadable just for this.
**transfer I-1 — draft_client_uid not persisted for env/connection-string remotes.**
When the value cannot be persisted (or no value exists), derive a stable one
deterministically instead of random per run, e.g. from the account's user ID and the
remote name through a hash, so the same remote on the same account gets the same
UID across runs; keep saving it when the config can store it. No package state.
**transfer I-2 — quadratic multi-thread reads of files without recorded block
sizes.** Find a fix that keeps returned bytes always correct and every block
verified. Preferred: compute each block's plaintext size from the block list
without downloading, if and only if it can be done exactly for the packet formats
involved (verify against the golden fixtures and the fake's shapes; if compression
or variable framing makes it inexact, do not use it). Otherwise a bounded per-`Fs`
cache (not package state) of plaintext block sizes learned per revision as blocks
are decrypted, so later chunks seek directly. Measure with a test counting block
fetches for an N-chunk multi-thread read. Update the docs page and TODO.
**auth I-1 — error-code mapping.** Map Proton's bad-app-version codes (5003, 5001;
confirm in the SDK reference) to the fatal "upgrade rclone" error. Code 2000 becomes
a plain `fserrors.NoRetryError` keeping Proton's message (not fatal). Code 2028:
treat as Proton's documented meaning (check the SDK); if it can mean rate limiting,
make it retryable via the pacer, not fatal. Update spec section 6.6 note via a
DECISIONS entry and handoff ruling 30. Tests for each code.
**auth I-2 — x-pm-drive-requirements.** Log those response headers at debug level
when present, as spec 6.6 says (once per value change, not per request).
**tests I-1 — CI race timeout.** Add `-timeout 45m` (or fitting the job timeout) to
the CI `go test -race` step; consider `-p`/`-parallel` limits for the slow runner.
**tests I-2 — CI Go version / govulncheck.** Pin Go to the 1.27 line the code was
verified with (go-version: '1.27.x' or exact 1.27.4) instead of go.mod's minimum,
and pin govulncheck to a fixed version instead of @latest.
Minors from all four files: fix the ones that are cheap and local to code you are
already touching; list every other Minor in docs/TODO.md under a single
"final review minors (2026-11-12)" heading with file references, so nothing is lost.
## Rules
Read implementer-common.md (its rule about the old protondrive.go is obsolete) and
global-constraints.md first. RCLONE_CONFIG=/notfound on every go test; never contact
Proton; never read ~/.config/rclone/rclone.conf or ~/proton_*password; never pass
-remote/-capture-remote. No dependency changes. Do not edit
docs/plans/001-protondrive-tier1-HANDOFF.md, docs/plans/001-protondrive-tier1-workspace/,
or docs/specs/001-protondrive-tier1.md section 13 (the controller edits those in
parallel); you may append rulings text to the controller via your report instead.
Done = go build ./..., go vet ./backend/protondrive/..., golangci-lint run
./backend/protondrive/... (0 issues), full race suite once at the end, TestGoldenFixtures
passing. No sudo, no force-push, no git reset --hard, no git clean, no recursive rm,
never push, never switch branches. End commits with:
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -0,0 +1,267 @@
# Final fix wave: implementer report
Branch `protondrive-tier1`, starting at 29ad26944. One commit per finding.
The controller's `ea8d09ce4` landed between mine and is not part of this
work. Every test ran with `RCLONE_CONFIG=/notfound`. No dependency
changes, nothing pushed, and no edits to the handoff, the workspace copy
or spec section 13.
## Commits
| Commit | Finding |
|---|---|
| e95317b52 | crypto C1: content key signature |
| db7afafc0 | crypto C2: Argon2 caps on node and share keys |
| b3f5685e6 | crypto I1: unverified hash key blocks writes |
| 67e4cfeb7 | crypto I2: no session key reuse for an unverified name |
| 94eedc942 | crypto I3: unsigned xattrs not used |
| 674b538ca | transfer I-1: stable draft client ID when it can't be saved |
| 788a711dc | transfer I-2: learnt block sizes make multi-thread reads linear |
| 5dbf5b04c | auth I-1: error-code mapping (5001/5003 fatal, 2000 no-retry, 2028 retried) |
| b4a23a31e | auth I-2: x-pm-drive-requirements logged |
| b5da26169 | tests I-1 and I-2: CI timeout, Go 1.27.x, govulncheck pinned |
| 634a2423e | (found while working) tests honour RCLONE_CONFIG whichever tests run |
| 30cfcac41 | auth M-1 and M-2: refresh 5xx/429/408 retried |
| d3aee2af1 | cheap minors: crypto M5 and M6, transfer M-3 and M-8, fake gofmt |
| bfa9b7122 | test minors 1, 6, 8 and 9, tests minors 13 and 14 (docs), every other minor logged in TODO |
| ad3552e2e | ARCHITECTURE line for the block size cache |
| 944b36005 | fake test follows the renamed code constant (found by the final race run) |
## Per finding
### crypto C1: unsigned or invalid content key signature (e95317b52)
- **Failing test first:** two new `hostileContents` cases in `TestHostileServer`
("unsigned content key the server made", "content key signed by another
key"), and "ranged reads and updates of a file with content-key-unsigned /
content-key-signature". Before the fix the read of an unsigned key returned
content (`security_test.go:204 An error is expected but got nil`), the Update
went through (`:570`), and the per-operation ERROR line was missing.
- **Fake:** the new corruptions `content-key-unsigned` and `content-key-signature`
make a new content key and re-encrypt every block under it. The blocks and
manifest are re-signed with the user's key, which is more than a real server
can do, so only the content key signature can catch it.
- **Change:** `decryptContentKey` always verifies, so a missing signature is
`errSignatureMissing`. `contentKey(n, op)` takes "reading" or "updating". Without
the flag it refuses with errIntegrity and an op-specific hint. With the flag it
logs an ERROR once per node and operation (`reportOnce("content key "+op)`). The
pre-2023 all-addresses fallback now applies only to a signature that is present
but invalid, as in the SDK. This also fixes transfer M-7 (Update logged
"reading").
- **Existing tests changed:** `TestContentKeyRoundTrip/NoSignature` (now expects
`errSignatureMissing`) and `TestDownloadContentKey` ("unsigned" is now refused,
plus a new "unsigned before 2023" case).
- **Deviation:** none. There is no date exemption for unsigned keys, because the
date comes from the server. Every golden fixture has a signed content key
(`TestGoldenFixtures` passes). Recorded in DECISIONS (ruling 29) and on the docs page.
### crypto C2: Argon2 parameters from the server (db7afafc0)
- **Failing test first:** `TestNodeKeyS2KLimits`. The patched keys (memory 2^18,
passes 255, parallelism 255, and memory 2^31 at the library limit) got no
errIntegrity. With a stub check, the memory case ran a 256 MiB KDF in 0.2 s.
- **Change:** `decryptNodeKeys` (used for node and share keys) calls
`unlockForeignKey`. That function unarmors once, runs `checkKeyS2K` on every
secret key and subkey packet before the KDF can run, then unlocks. Neither
go-crypto nor gopenpgp exposes a key's S2K parameters, so `s2kSpecifier`
finds them by hand. It parses the packet with go-crypto (cheap: no KDF), takes
the public key's serialized body as the prefix, then walks the usage, count,
cipher, AEAD and length octets the way go-crypto does.
- **Caps:** Argon2 may use at most 4 passes, parallelism 8 and 2^17 KiB (128 MiB).
The defaults of gopenpgp and OpenPGP.js are 3, 4 and 64 MiB, and Proton's node
keys use iterated-and-salted S2K anyway. Going over a cap is errIntegrity, and
the flag can't override it.
- **Iterated-salted:** not capped further. go-crypto's one-byte count encoding
already bounds it to 65011712 bytes hashed (4 times the default), and a lower
cap could refuse genuine old keys.
- **Fuzz:** `FuzzCheckKeyS2K` ran 20 s locally (1.3M execs, no panic).
- **Deviation:** the account's own user keys are not checked. The ruling scopes
this to foreign keys, and a genuine user key's parameters aren't known. This is
logged in TODO. Address keys are safe, because their token's signature is checked
before the unlock. DECISIONS entry added.
### crypto I1: unverified hash key used for writes (b3f5685e6)
- **Failing test first:** "writing into a folder whose hash key the server made".
The upload into the folder succeeded before the fix (`:272`).
- **Change:** `node.hashKeyErr` is set when the hash key signature fails and isn't
date-exempt. `targetFolder` refuses without the flag, which covers create,
upload, rename (within the folder), move into and copy into. With the flag it
logs an ERROR once per folder. Reads are not affected. A comment notes that the
date exemptions use the server's CreateTime (crypto M4). Docs page updated.
- **Deviation:** not folded into `keys.unverified`. Doing that would make every
child unreadable, and the ruling covers writes only.
### crypto I2: rename/move reusing an unverified name's session key (67e4cfeb7)
- **Failing test first:** "renaming or moving a node whose name the server
encrypted". The new stored name's session key equalled the server's for both
the rename and the move. The fake gained `User.NameSessionKey` and the
`name-unsigned` corruption.
- **Change:** `node.nameSigned` records whether the name signature actually
verified, taken before the pre-2021 exemption so that a back-dated name doesn't
count. `renameSessionKey` returns nil, which means a fresh key, unless the name
is signed. The same test checks that a verified name keeps its key.
- **Deviation:** a pre-2021 unsigned (exempt) name also gets a fresh key. This is
the safe reading of "did not verify".
### crypto I3: unsigned xattrs trusted (94eedc942)
- **Failing test first:** "unsigned extended attributes". The forged modtime
(2099), the forged SHA-1 and the forged 1 TiB size were all used, and the
folder's modtime too.
- **Change:** without the flag, when the xattr signature fails, `n.X` becomes
`blockSizesOnly()`. The ERROR line says the modification time, size and SHA-1
are not used. With the flag the attributes are used, logged once per node by the
existing verify report.
- **Deviation:** I did not use the plain "xattrs absent" fallback. That fallback
reports the *encrypted* size, so every `rclone copy` of the file would fail its
size check, which in practice makes the file unreadable. The ruling forbids
that. Instead I keep only the recorded block sizes, which every read checks
block by block, and take Size from their sum. ModTime falls back to the server's
ModifyTime and there is no SHA-1. If there are no block sizes, Size is the
encrypted size as before. The test checks that full and ranged reads still work.
Docs page updated.
### transfer I-1: draft_client_uid for env/connection-string remotes (674b538ca)
- **Failing test first:** `TestDraftClientUIDStableWhenUnsaved`, using a mapper
whose `Set` does nothing. Two runs of the same remote got different IDs.
- **Change:** `NewFs` sets a random ID through `m.Set`, then reads it back with
`m.Get`. If it didn't stick (env vars or a connection string), it uses
`client.derivedDraftClientUID(name)` = hex(SHA-256("rclone draft client\0" +
volumeID + "\0" + name))[:32].
- **Deviation:** I used the account's volume ID rather than the user ID. The
account struct doesn't keep the user ID, and the volume ID is just as stable
per account. Config-file remotes still get a random saved ID, so two machines
with their own config files don't collide. The docs say that two machines
running an env-configured remote of the same name count as one remote.
### transfer I-2: quadratic multi-thread reads (788a711dc)
- **Option chosen:** a bounded per-client cache, not computing sizes from the
listing. The packet framing (partial body lengths, the literal packet header and
any compression) depends on the writer, so plaintext size can't be computed
exactly from encrypted size.
- **Failing test first:** `TestDownloadChunksUnrecordedBlockSizes`, 32 blocks read
as 8 sequential range opens. It fetched 145 blocks before the fix and 39 after.
A range read afterwards fetches exactly its 3 blocks, and a block list changed
since the sizes were learnt is errIntegrity.
- **Change:** `blockSizeCache` lives on `client`, so it is per Fs and not package
state. It holds at most 16 revisions and 65536 blocks (about 2.5 MiB). The
reader learns sizes in order in `next()`, as a contiguous prefix with each
block's hash. `openRead` uses `locate` to start at the right block, and to stop
at the last block when the prefix covers the range, which partly fixes transfer
M-5.
- **Correctness:** every block is still hash-checked. If the start block's listed
hash differs from the learnt hash, or a re-read block's hash or size differs,
that is errIntegrity.
- **Limit:** chunks that start together still each read from block 1. The docs
page, the TODO entry and the conformance checklist are updated.
- **Existing tests changed:** `TestDownloadRanges` (the unrecorded case now
benefits from learnt sizes) and the always-3 nit in it.
### auth I-1: error codes (5dbf5b04c)
- **Codes checked in the SDK reference:**
- 5003 is "You need to update this app" (`coreTypes.ts`).
- 5001 is from go-proton-api.
- 2028 is "feature disabled, rate-limited or blocked because of abuse"
(`driveTypes.ts`).
- **Failing tests first:** `TestTranslateError` (5001 fatal, 2000 no-retry),
`TestClientShouldRetry` (2028 retried, and with Retry-After honoured),
`TestRegressionFeatureBlockedRetried`, `TestRegressionRenameResendNotFatal`
(the reviewer's lost-rename reproduction, which before the fix was fatal with
"upgrade rclone"). `TestRegressionAppVersionRejected` now uses 5003.
- **Change:**
- api: `CodeFeatureBlocked=2028`, `CodeAppVersionMissing=5001` and
`CodeAppVersionBad=5003` (renamed; it used to mean 2028).
- `translateError`: 5001/5003 are fatal with "upgrade rclone". 2000 is a
`NoRetryError` that keeps Proton's message. 2028 falls through as a plain
error.
- `shouldRetry`: 2028 is retried, through `RetryAfterError` when a Retry-After
header is present.
- **Deviation:** 2064 is left unmapped, because the user can set `app_version`
wrong themselves. DECISIONS entry added (ruling 30).
### auth I-2: x-pm-drive-requirements (b4a23a31e)
- **Failing test first:** `TestDriveRequirementsLogged`. The fake gained
`SetDriveRequirements`.
- **Change:** `session.do` calls `observeRequirements`, which logs at debug, `%q`,
truncated to 1 KiB, only when the joined value changes.
### tests I-1 and I-2: CI (b5da26169)
- `go test -race -timeout 40m`, and `timeout-minutes` raised from 45 to 60 to make
room for the fuzz step.
- `go-version: '1.27.x'`.
- `govulncheck@v1.9.0`, the latest release on proxy.golang.org at the time of
writing (queried with `go list -m -versions`).
- No `-p`/`-parallel` limits: the race suite is one big package, so `-p` doesn't
help, and limiting `-parallel` would make it slower.
### Extra: tests read the real rclone.conf (634a2423e)
- While working I saw "Using config file from ~/.config/rclone/rclone.conf" in a
captured test log. `RCLONE_CONFIG` is only applied by `fstest.Initialise`, which
only `TestIntegration` runs, so any filtered run (`-run X`) read the real config
holding the live `TestProtonDrive:` remote. Nothing contacted Proton, since the
tests use env-configured fake remotes, but the file was read.
- `TestMain` now calls `config.SetConfigPath(RCLONE_CONFIG)` first; `/notfound`
means memory-only. Earlier runs in this and previous sessions read that file.
## Minors
**Fixed:**
- crypto M4: code comment, plus a TODO line.
- crypto M5: `decryptBlock` moved to the tests; the `reencryptPassphrase` alias
removed.
- crypto M6: `truncateName` cuts on a rune boundary, with a test.
- transfer M-3: godoc rewrapped.
- transfer M-5: partly; the range end is known once sizes are learnt.
- transfer M-7: covered by C1.
- transfer M-8: the superseded message gives the state and revision.
- auth M-1 and M-2: refresh 5xx/429/408 retried, and 408 no longer kills the
session; `TestRefreshTransientErrorRetried` failed first for 503 and for 408.
- tests 1: timing bounds raised to 3 s.
- tests 6: the 502-once test counts the uploads.
- tests 8: `TestRefusedCommit` pins the 408 exclusion.
- tests 9: "assumed" renamed and the always-3 bound fixed.
- tests 13: conformance checklist.
- tests 14: the docs explain errUploadUnconfirmed.
**Logged** in docs/TODO.md under "final review minors (2026-11-12)":
- crypto M1, M2, M3, M7 and M8.
- transfer M-1, M-2, M-4, M-6 and M-9.
- auth M-3 to M-11.
- tests 2, 3, 4, 5, 7, 10, 11, 12, 15, 16 and 17.
- tests 17 (.codex rules) is a permission-file change, which I left to the owner.
**Not mine:** tests 18 and 19 (handoff) and tests I-3 to I-5 (the controller's).
## Verification (at ad3552e2e)
- `go build ./...`: OK.
- `go vet ./backend/protondrive/...`: OK.
- `golangci-lint run ./backend/protondrive/...`: 0 issues.
- Full race suite at ad3552e2e: `RCLONE_CONFIG=/notfound go test -race -count=1 -timeout 40m ./backend/protondrive/...`
- `backend/protondrive`: ok, 332.6 s.
- `protonfake`: ok.
- `protontest`: failed `TestFaultInjection/Skip`. It still used the
`api.CodeAppVersionBad` name for 2028, and my rename had missed it. Fixed in
944b36005; the `protontest` race run then passed (39.1 s), and lint was
re-run with 0 issues.
- `TestGoldenFixtures`: PASS (also run alone with -v).
## Suggested handoff ruling text
29. A file whose content key signature is missing or invalid is
unverified, like an unverified node key (rulings 18, 22): reads, full
and ranged, and updates are refused unless `ignore_signature_errors`
is set, when an ERROR is logged once per file and operation. The
pre-2023 fallback to the account's other address keys applies only to
a signature that is present. Stricter than the SDK, which skips the
check when the signature is absent. In the same wave: a folder whose
hash key signature fails can't be written into without the flag;
extended attributes whose signature fails aren't used without it
(modtime from the server, no SHA-1, size from the recorded block sizes);
a rename or move never reuses the session key of a name whose signature
didn't verify; node and share keys with an Argon2 S2K beyond 4 passes,
parallelism 8 or 128 MiB are an integrity error the flag can't override.
30. Error codes: 5001 and 5003 (missing or rejected app version) are a
fatal error telling the user to upgrade rclone; 2000 (requirements not
met, e.g. a stale `OriginalHash` after a resent rename) is a no-retry
error keeping Proton's message; 2028 (feature disabled, rate-limited or
blocked) is retried through the pacer, honouring `Retry-After`. This
supersedes spec 6.6's 2000/2028 fatal mapping. `x-pm-drive-requirements`
is logged at debug level when it changes.
@@ -0,0 +1,99 @@
# Final fix wave: scoped re-review (29ad26944..944b36005)
Verdict: **ALL CLOSED**. No open or new Critical or Important findings. There are 3 new Minors.
How I checked: I read the diff (I skipped ea8d09ce4, the controller's spec section 13 commit) and the code at HEAD where needed. I compared it with the SDK reference (`~/.cache/protondrive-sdk-ref`, read-only) and with gopenpgp v3.4.1 and go-crypto v1.5.2 in the module cache. I worked in a scratch worktree, `fr-rereview` at 944b36005, which is now removed with `git worktree remove` (no --force). Every `go test` ran with `RCLONE_CONFIG=/notfound`. I did not touch `/home/user/rclone`, apart from this file.
## Findings: closed?
| Finding | Status | Test that fails without the fix (mutation run here) |
|---|---|---|
| crypto C1, unsigned content key | Closed | I put back `if sig == "" { return sk, nil, nil }` in `decryptContentKey`. That fails `TestContentKeyRoundTrip/NoSignature`, `TestHostileServer/unsigned_content_key_the_server_made`, `.../ranged_reads_and_updates_of_a_file_with_content-key-unsigned` and `TestDownloadContentKey/unsigned{,_before_2023}`. Reads (full and ranged) and Update (`upload.go:319`, op "updating") go through `contentKey(n, op)`. It has no other callers. |
| crypto C2, Argon2 from the server | Closed | Mutation 1: removed the `checkKeyS2K` call. `TestNodeKeyS2KLimits/{Memory,Passes,Parallelism}` fail, and the KDF runs. Mutation 2: shifted the specifier offsets by one. `.../Accepted` fails. `decryptNodeKeys` is the only unlock path for node and share keys (`nodes.go:367,530`). The offsets match go-crypto's `PrivateKey.parse` (`private_key.go:219-271`): usage, the v5/v6 count, cipher, AEAD for 253, the v6 length. |
| crypto I1, unverified hash key used for writes | Closed | I made the `hashKeyErr` refusal unreachable. `TestHostileServer/writing_into_a_folder_whose_hash_key_the_server_made` fails. |
| crypto I2, rename/move reusing the session key of an unverified name | Closed | I made `renameSessionKey` ignore `nameSigned`. `TestHostileServer/renaming_or_moving_a_node_whose_name_the_server_encrypted` fails. |
| crypto I3, unsigned xattrs | Closed (accepted deviation) | I removed `n.X = n.X.blockSizesOnly()`. `TestHostileServer/unsigned_extended_attributes` fails. |
| transfer I-1, draft_client_uid | Closed (accepted deviation) | I made the derived-UID branch a no-op. `TestDraftClientUIDStableWhenUnsaved` fails. `m.Get` after `m.Set` works as a test of whether the value was saved: `fs.setConfigFile.Set` saves only when the section exists (`fs/configmap.go:90`), and `getConfigFile` reads the value back. |
| transfer I-2, quadratic multi-thread reads | Closed | I made `openRead` skip `learnt.locate`. `TestDownloadChunksUnrecordedBlockSizes` fails with 146 blocks fetched for 8 chunks (the limit is 64). |
| auth I-1, error codes | Closed | I mapped 2000/2028 back to fatal. `TestTranslateError` and `TestRegressionRenameResendNotFatal` fail. 5001 and 5003 are confirmed fatal in the code and the tests. |
| auth I-2, x-pm-drive-requirements | Closed | `TestDriveRequirementsLogged`. The header is logged once per change and truncated to 1 KiB. |
| tests I-1, CI race timeout | Closed | `-timeout 40m`, inside the job's `timeout-minutes: 60`. |
| tests I-2, CI Go version and govulncheck | Closed | `go-version: '1.27.x'` (local Go is 1.27.4). `govulncheck@v1.9.0`, which `go list -m -versions golang.org/x/vuln` shows is the latest. |
## Regression checks
**(a) Reading files that real Proton clients wrote.** `TestGoldenFixtures`, `TestGoldenRoundTrip` and `TestGoldenRefusesForeignKeys` pass at HEAD. Re-made signatures could hide a real-world mismatch, so I also checked two things:
- **The fixtures keep each signature's role and keep missing signatures missing.**
- `goldenCapture.verified` (`golden_internal_test.go:864`) made the original capture fail on any signature failure the backend reported against the real account.
- `resignDetached` keeps `""` as `""` and re-signs with the replacement for the same signer role.
- The rekey notes record which key signed what in the captured web and CLI files:
- `ContentKeyPacketSignature`: by the node key, 4 files.
- `NodeHashKey`: by the node key, including both roots.
- `XAttr`: by the address key.
- The oldlib fixture also has a `ContentKeyPacketSignature`.
- **rclone verifies with the same key sets as the SDK:**
- The content key with node key plus signature-email keys, or the parent key for an anonymous node. That is `[key, ...keyVerificationKeys]`, `cryptoService.ts:637-644`.
- The fallback to all of the account's addresses only applies to a signature that is present but invalid (`:652`).
- The hash key with node key plus signature-email keys. That is `decryptNodeHashKey`, `driveCrypto.ts:534-537`.
- Revision and folder xattrs with the signature-email keys, or the node key when there is no email (`:582-584`, `:177-179`).
So the new refusals hit only signatures that the SDK would also report as failed. The one place rclone is stricter than the SDK is an absent content key signature. That is the recorded ruling 29.
**(b) Argon2 caps.**
- The defaults are 3 passes, parallelism 4 and memory exponent 16:
- go-crypto `Argon2Config` (`s2k_config.go:98-115`).
- gopenpgp v3.4.1's RFC9580 preset (`preset.go:72-78`), which uses an empty `Argon2Config`, so it gets those defaults.
- OpenPGP.js writes the same.
- The caps are 4 passes, parallelism 8 and exponent 17, so they leave headroom.
- The SDK checkout doesn't mention S2K or Argon2 anywhere, so it sets none of its own.
- Iterated-and-salted S2K is not checked. Its one-octet count encoding bounds it already.
- `checkKeyS2K` parses packets without running the KDF.
- I noted no new problem here.
**(c) Block-size cache.**
- It is per Fs: a `blockSizeCache` value on `client`, with no package state.
- It is bounded: at most 16 revisions and 65536 blocks.
- The eviction loops leave the counters consistent.
- A new revision can't be left in `order` without its entry in `revs`, as long as `learntBlocks > 0`.
- It is safe for concurrent use: every access holds the mutex, and `locate` clones the start hash before unlocking.
- The focused `-race` run of `TestDownloadChunksUnrecordedBlockSizes`, `TestDownloadRanges` and `TestDownloadRangesUnrecordedBlockSizes` passed.
- I also wrote and ran a scratch `-race` test: 8 to 9 concurrent chunk readers per round, 3 rounds, at offsets that don't line up with blocks. All bytes were correct and no race was reported.
- It never returns wrong bytes from a genuine server:
- Sizes are learnt only from blocks that were decrypted and checked against their hash, and only as a contiguous prefix.
- The start block's listed hash must equal the learnt one.
- A re-read block must match in both hash and size.
- If eviction happens between `locate` and `learn`, learning just stops; it never corrupts.
- It is invalidated on a new revision, because entries are keyed by revision ID and a committed revision's blocks are immutable.
**(d) Error-code remap.**
- 2028 is retried only by `shouldRetry` inside the pacer, so the low-level retry count bounds it. A `Retry-After` value is capped by `maxRetryAfter`. After the retries, the plain error goes back to rclone.
- 5001 and 5003 are 4xx, so they are not retried, and are fatal.
- 2000 is `NoRetryError`.
- The refresh path now returns `nil, refreshErr`. A rejected refresh is still `errSessionExpired`, which is not retried. A 5xx, 408 or 429 on the refresh is retried within the pacer's limit.
- Nothing loops without a bound.
**(e) RCLONE_CONFIG in tests.**
- `TestMain` lives in `package protondrive_test`, and Go builds the internal and external test files into one binary, so it covers every test in `backend/protondrive`.
- I proved it with a fake `HOME`/`XDG_CONFIG_HOME` holding a sentinel `rclone.conf`, so the user's file was never at risk. I ran a scratch test that calls `configfile.Install()` with `RCLONE_CONFIG=/notfound`:
- At HEAD it got path `""` (memory only) and no sections.
- With the `TestMain` lookup disabled, it got the sentinel path and `[sentinelremote]`.
- `protontest` and `protonfake` don't import `fs/config`.
**(f) CI file.** YAML structure is fine. The fuzz step has no `-timeout`, but each target runs for only 20 s. `go vet` passes. `golangci-lint run ./backend/protondrive/...` gives 0 issues (run here).
## New Minors (fold into docs/TODO.md "final review minors")
- **M-a. Ranged reads use block sizes from unsigned xattrs to find their start** (`nodes.go` decryptNode → `blockSizesOnly`; `download.go:116-151`).
- Each fetched block's size is checked, but the blocks before the range's first block are never fetched.
- So a server that forges unsigned xattrs with block 1 at 4 MiB-100 (and true sizes after it) makes a read at offset 4 MiB-100 start at block 2 and silently return genuine bytes 100 bytes out of place. A full read still fails.
- This is the same class as the recorded "ranged read can be fed swapped blocks" limit, and it is no worse than before the fix, when all unsigned xattrs were used. But the docs page says the sizes are ones "which reads check".
- Cheapest fix: keep the summed Size, but read such a file as if it recorded no block sizes, so the checked block-size cache places ranges. Or log it in TODO next to the swapped-blocks entry.
- **M-b. `TestMain` applies `RCLONE_CONFIG` only when it is set** (`budget_test.go:39-45`).
- A developer who forgets the variable still reads the default config.
- AGENTS.md and CI always set it, so this is a gap in a safety net, not a defect.
- Fix: use memory-only config when the variable is unset.
- **M-c. Draft UID when the config can't be written** (`protondrive.go` NewFs).
- If the remote's section exists but saving the config file fails (for example a read-only file), `ConfigFileSet` still keeps the value in memory. `m.Get` then returns the random UID, so it stays random on every run, like the original I-1.
- This is rare and logged by rclone as "Failed saving config".
- Fix: always use the derived UID unless the value is already in the config.
@@ -0,0 +1,193 @@
# Final review: authentication, sessions, rclone-facing layer
Reviewer area: `session.go`, `account.go`, `protondrive.go`, `api/types.go`
(as used by these). HEAD 29ad26944.
Counts: Critical 0, Important 2, Minor 11.
Scratch proof tests (worktree since removed) are saved at
`/tmp/claude-1000/-home-user-rclone/8cc17642-4f33-4045-ad2c-177bf869b82a/scratchpad/zz_review_auth_test.go.txt`.
## Important
### I-1. Error-code table: generic code 2000 is fatal ("upgrade rclone"), and the real bad-app-version code 5003 is not mapped
`protondrive.go:396-397`, `api/types.go:17,37-39`.
`translateError` maps `CodeInvalidRequirements` (2000) and `CodeAppVersionBad`
(2028) to `fserrors.FatalError("this version of rclone is no longer accepted
by Proton: upgrade rclone")`. The spec (6.6) asks for this, so this re-opens
a recorded ruling. The reason is a defect the ruling didn't foresee:
- 2000 is Proton's generic "requirements not met" code. The fake, which
models the SDK, returns it for a stale `OriginalHash` on rename/move
(`protontest/mutate.go:527,574`), a stale `CurrentRevisionID` on revision
creation (`protontest/upload.go:365`), a folder moved into itself, and
commit validation (`upload.go:562`). Each of these then aborts the whole
rclone run with a false "upgrade rclone" message.
- A realistic trigger is a lost response. `apiCall` resends PUT rename/move
after a 5xx or network error (the recorded Task 9 deferral). The first
request has already landed, so the resend fails its `OriginalHash` check.
**Reproduced** (scratch test `TestReviewRenameResendFatal`): inject 502
AfterHandler once on `PUT .../rename`. The rename lands on the server
(`new.txt` present), but `c.rename` returns `OriginalHash doesn't match
(Code=2000)`, and `translateError(err, opMove)` gives
`IsFatalError == true` with "upgrade rclone". A `rclone move` therefore
aborts the whole sync over a rename that succeeded. A second trigger:
another client uploads a new revision between `activeNode` and
`createRevision` in `Object.Update`.
- 2028, by our own comment in `api/types.go:37-38`, covers "a feature is
disabled, rate-limited or blocked for abuse". A rate-limited feature
should not give a fatal "upgrade rclone".
- The code Proton actually returns for a rejected app version is **5003**
(`AppVersionBadCode`), and **5001** for a missing one. That is Proton's own
go-proton-api v1.0.4, `response.go:24-25`, still in the module cache. Neither
is mapped, so a blocked rclone version gives a plain retried error on every
call, without the upgrade hint the spec intends. The fake answers a bad
`x-pm-appversion` with 2064 (`CodeInvalidSection`, `protontest/server.go:319`),
which the client doesn't map either.
Fix: map 5001/5003 (and arguably 2064) to the fatal upgrade error. Drop 2000
from the fatal list, leaving it a plain (non-fatal) error, or
`NoRetryError`. Reconsider 2028: NoRetry rather than Fatal, with Proton's
message kept. Update spec 6.6 and the `TestTranslateError` code list, and
the `AppVersionRejected` regression test (`task-15`) to use 5003.
### I-2. Spec 6.6: `x-pm-drive-requirements` response headers are never logged
Spec `docs/specs/001-protondrive-tier1.md:363`: "The `x-pm-drive-requirements`
response headers are logged at debug level so a pending server-side
enforcement is visible in bug reports." Nothing in `backend/protondrive`
reads the header (grep: no match outside the spec). It is not in
`docs/TODO.md`, `DECISIONS.md` or the handoff rulings.
Failure scenario: Proton announces an upcoming requirement, such as a minimum
client version or crypto, through the header. Bug reports with `-vv` don't
show it, which is exactly what the spec item exists to prevent.
Fix: in `session.do` (`session.go:253-255`, next to `observeDate`), log
`resp.Header.Values("x-pm-drive-requirements")` at debug level when present,
ideally once per distinct value. The alternative is to record a ruling
dropping it.
## Minor
### M-1. A 5xx or 429 on the token refresh is never retried
`session.go:214-216`. `call` returns the original 401 `resp` together with
the refresh error. `shouldRetry` (`nodes.go:256`) then takes status 401 from
`resp` and falls through to `fserrors.ShouldRetry` on an `*api.Error`, which
is false. **Reproduced** (`TestReviewRefresh503NotRetried`): expire the access
token and inject one 503 on `/auth/v4/refresh`. `about` fails after 1
refresh request. A 429 with Retry-After on refresh is ignored the same way.
Every operation in flight at token expiry can fail once (EIO on a mount).
Fix: `return nil, refreshErr` so `shouldRetry` reads the status from the
wrapped `api.Error` (5xx gets retried, 429 gets the default Retry-After).
### M-2. A refresh that answers 408 permanently kills the session in-process
`session.go:331`. Every 4xx except 429 counts as "refresh token rejected"
(`deadRefresh` set, errSessionExpired, fatal), and that includes 408 Request
Timeout, a transient status that `shouldRetry` itself retries elsewhere.
The SDK precedent covers rejections, not timeouts. Fix: exclude 408 as well.
### M-3. Two `Fs` in the same process with an env-configured remote can't see each other's refresh
`protondrive.go:278-339`, `fs/configmap.go:90-94`. For a remote defined
only by `RCLONE_CONFIG_<NAME>_*`, `Save` writes nowhere: the config file
has no section. Each `configStore` falls back to its own `saved`, so a
second `Fs` (for example `rclone sync remote:a remote:b`) still reads the
spent env refresh token. It POSTs that token, is rejected, waits 3 s, and
fails fatally with "session expired". `docs/TODO.md` records this for
*two processes* only, and the same-process case is worse than recorded.
Fix: document it, or share a per-remote-name in-memory overlay. That overlay
would be package state, which needs a ruling, so documenting is likely the
answer.
### M-4. Re-sign-in prompt defaults to "no", including when the user follows our own reconnect hint
`protondrive.go:581-589` (ruling 17). An expired session's error says
`run "rclone config reconnect remote:"`. Credentials are never removed from
the store, so reconnect asks "Already signed in - sign in again?" with
default No. The statement is false (the session is dead), and pressing Enter
does nothing and exits successfully. Fix: reword the prompt ("A session is
saved - sign in again?"). Optionally default to yes, as `oauthutil`'s
"Token already configured - replace it?" does. That would need the ruling
revisited.
### M-5. After a CAPTCHA in Config, the method question defaults back to password
`protondrive.go:630-631`. `ConfigError("method", ...)` returns to `method`,
whose default is `!hasPassword`, which is false. Pressing Enter goes through
password sign-in again and hits the CAPTCHA again. Fix: go straight to
`browser`, or make the default true after a CAPTCHA.
### M-6. Wrong key password on a saved session: misleading message, no reconnect hint (deferred Task 14, still present)
`account.go:123` says "no user key unlocks, is the password right?". This is
returned from `connect` unwrapped, without `s.reconnect`. A browser-signed-in
remote has no password, and the fix is to reconnect (for example after a
password change re-keys the salts). Fix: wrap with the reconnect hint in
`connect` when `loadAccount` fails to unlock.
### M-7. Refresh not proven against the real server; request differs from go-proton-api
`api/types.go:73-77`, `session.go:322-326`. The body has no `UID` and no
`RedirectURI`, both of which go-proton-api sends
(`manager_auth_types.go:93-100`). Handoff section 6 lists no live refresh,
and a broken refresh would break every remote after one access-token
lifetime. Fix: add "token refresh after expiry" to the live checks in
`docs/TODO.md`. If it fails live, add `RedirectURI`.
### M-8. Deferred Task 2/3/4 session minors, re-checked, all still present and minor
- An adopted stored token whose access token is stale fails that call once
with a plain 401 (`session.go:308-311` then `:231`).
- The refresh HTTP call uses the first waiter's ctx (`session.go:322`).
- `serverTime` can step back with out-of-order responses (`session.go:280-289`).
- The half-signed-in cleanup DELETE uses the caller's possibly cancelled ctx
(`session.go:462-467`). An empty SRP salt is not rejected up front
(`session.go:430`).
- `publicKeys` waiters get the leader's ctx error (`account.go:288`).
`GET /core/v4/users` is fetched in `loginSRP` and again in `loadAccount`.
### M-9. Own-address public key ring rebuilt on every call
`account.go:278-280`. `publicKeyRing` copies every address key (`ToPublic`)
on each call. `decryptNode` calls it 2-3 times per node through `verifyKeys`
(`nodes.go:494,500,573`), so large listings pay thousands of key copies.
Fix: build each address's public ring once in `loadAccount`.
### M-10. Server-side Copy onto an existing destination always falls back to download and re-upload
`protondrive.go:916-918`. `copyNode` refuses an existing name (errExists maps
to `fs.ErrorCantCopy`). protondrive has no `PartialUploads`, so rclone copies
in place to the existing name. Every server-side copy over a changed file
then streams the whole file through the client. It is correct but costly.
Fix (later): on errExists, copy to a temporary name and move over, or upload
a new revision from the source node. At minimum, record it in
`docs/TODO.md`.
### M-11. Known items re-checked, still minor and recorded
- `Precision()` is 1 s while `ModTime` carries milliseconds (handoff 8).
- Cross-account Move/Copy/DirMove are not refused (handoff 8). The rclone
name check makes it practically unreachable.
- The on-the-fly remote hint names the bare backend.
- `fs.Debugf(nil, "protondrive: ...")` is used throughout instead of
passing the `Fs`. This is style only: maintainers usually prefer the `Fs`
as the log object, and it is worth a sweep before any upstream PR.
## Checked and found sound
- Secrets: the password is never sent (SRP) or logged. The key password,
tokens and fork key are never in log lines or error strings. The fork URL
holding the AES key is printed, not logged. `--dump` behaviour is as
documented in `docs/TODO.md`.
- Refresh: single-flight per session. It re-reads the store first, and after
a rejection it polls up to 3 s for a token rotated elsewhere. It never
wipes the store. `Save` writes access before refresh, so a cross-process
reader that sees a partial write adopts only after the full set lands.
Logout holds `refreshMu` so an in-flight refresh can't resurrect the
session.
- Config: browser is the default only without username+password (ruling 14).
The third-party notice is shown. The old session is signed out only after
the new sign-in succeeds (ruling 17). The 2FA prompt loops through
`password_2fa`. otp codes use the server clock.
- Disconnect/Shutdown match rulings 15 and 17. A call racing Disconnect gets
`errNotSignedIn` (`session.go:226-229`), so the Task 14 deferral is closed.
- Error-mapping contract: DirNotFound vs ObjectNotFound by op; Mkdir exists
is nil; Purge refuses the root by path and by ID; Rmdir asks the server
uncached (ruling 16); Remove of a gone file is nil; SHA1-only `Hashes`.
- `x-pm-appversion` matches the fake's grammar for releases, betas (build
metadata) and dev builds (alpha).
- `unlockMu` is the only package-level mutable state. protondrive.go holds
no crypto or routes.
@@ -0,0 +1,42 @@
# Final whole-branch review — shared instructions
Repository: /home/user/rclone, branch protondrive-tier1, HEAD 29ad26944. Base of the
work: 06700aaf2 (`git diff 06700aaf2..HEAD -- <paths>` shows the change; most backend
files are new, so read them whole). This is a rewrite of rclone's Proton Drive backend
(`backend/protondrive`) as a native Go client following Proton's official SDK, with a
fake Proton server in `backend/protondrive/protontest` (fork-only).
Authorities, in order:
1. `docs/specs/001-protondrive-tier1.md` — approved spec (binding).
2. `docs/plans/001-protondrive-tier1-HANDOFF.md` section 7 — rulings that supersede
the spec/plan where they disagree; section 6 — what has been proven live.
3. `docs/DECISIONS.md` (newest first), `docs/TODO.md` (known, deliberately deferred).
4. `AGENTS.md` — rclone code, comment and commit rules (comment style is enforced:
godoc on every exported identifier, no narrating comments, "why" inline comments).
Every per-task review deferred its Minor findings to this review. They are in
`.superpowers/sdd/001-protondrive-tier1/progress.md` (lines with "minor", "Minor",
"Deferred minors") and in the `task-*-review.md` / `task-*-rereview*.md` files in the
same directory. Re-check the ones in your area: still present? still minor?
Your job: find real defects across the whole of your area — correctness, security,
data safety, concurrency, resource leaks, error handling, rclone contract violations,
spec/ruling non-conformance, upstream-readiness (rclone maintainers' style, minimal
cleverness, no package-level mutable state except `unlockMu`, seam rule: no crypto or
API routes in protondrive.go, no fs.Fs/fs.Object below it). Do not re-litigate a
recorded ruling unless you find it causes a defect the ruling didn't foresee; say so
explicitly then. Do not report what docs/TODO.md already records unless it is worse
than recorded.
Grade findings Critical (data loss, security break, crash on normal input) / Important
(wrong behaviour a user will hit, contract violation, spec non-conformance, maintainers
would reject) / Minor. Each with file:line, a concrete failure scenario, and a suggested
fix. Verify before reporting: read the code path end to end; where cheap, prove it with
a test in a scratch worktree (`git worktree add --detach
/tmp/claude-1000/-home-user-rclone/8cc17642-4f33-4045-ad2c-177bf869b82a/scratchpad/fr-<you> HEAD`,
remove with `git worktree remove <path>`, never --force).
SAFETY: every `go test` MUST set RCLONE_CONFIG=/notfound. Never read
~/.config/rclone/rclone.conf or ~/proton_*password; never contact Proton hosts; never
pass -remote or -capture-remote. Read-only on /home/user/rclone: do not modify, stage or
commit there. Do not dispatch subagents.
@@ -0,0 +1,232 @@
# Final review: cryptography, node tree, caching (trust model)
Reviewer area: `backend/protondrive/crypto.go`, `nodes.go`, `cache.go`, the parts of
`api/types.go` they use, and the parts of `download.go`, `upload.go` and `account.go`
they feed into (contentKey, createRevision, unlockKey, publicKeys). HEAD 29ad26944.
SDK reference: ~/.cache/protondrive-sdk-ref @ 05c9115 (read only).
All Critical and Important findings were reproduced against the fake server in a
scratch worktree (now removed). The probes used small scratch-only helpers in
`protontest` that model what a hostile server can do with only public keys:
encrypt to a node's public key, and drop or replace signatures.
Counts: Critical 2, Important 3, Minor 8.
---
## Critical
### C1. An unsigned content key packet is accepted. Updates are then encrypted with a key the server chose, and ranged reads return content the server made up
- `crypto.go:614-620` (`decryptContentKey` checks the signature only
`if contentKeyPacketSig != ""`), `download.go:191-227` (`contentKey`),
`upload.go:312` (`createRevision` encrypts the new revision's blocks with
`c.contentKey(n)`).
- Anyone who has the node's public key can make a content key packet: generate a
session key and encrypt it to that key. The server stores the armored node key, so it
has the public key. If the server sends `ContentKeyPacketSignature: ""` (or null),
`verifyErr` stays nil and nothing is logged, even without `ignore_signature_errors`.
- **Confidentiality break (reproduced).** A file with a verified node key: the server
swaps in its own content key packet and blanks the signature. `Object.Update` (every
`sync` of a changed file) goes ahead without an error or a log line and encrypts
the user's new content with the server's session key. The probe then decrypted the
stored block of the new revision with the server's key and got
`"the user's new secret content"` back.
- **Integrity break (reproduced).** The server also re-encrypts block 1 under its key
(the server lists the hash, and the size matches the recorded block size). A
ranged read (`RangeOption{Start:1}`, as mounts and multi-thread copies make)
returned `"ORGED BY SERVER"` with no error. A full read failed on the manifest.
But a server that also renames the revision's `SignatureEmail` to a foreign
address and serves keys for it (the known protocol limit) can sign a manifest
over its own block hashes, so full reads can be forged too. docs/TODO.md says a
ranged read can be fed "validly encrypted blocks of the same file swapped or from
an older revision". This is worse: the content can be anything the server likes.
- The SDK also skips verification when the signature is absent
(`driveCrypto.decryptAndVerifySessionKey`, `cryptoService.ts:240-249`). But rulings
18/19 and spec section 7 put the server in the threat model and are stricter than
the SDK on the node key. A key the server can mint is the same hole one level down.
- Fix: in `decryptContentKey`, return `errSignatureMissing` as `verifyErr` when the
signature is empty, so `contentKey` refuses without the flag. If legacy unsigned
content keys exist, allow the exemption only by a creation date, the way names and
hash keys do, and say that the date is server-supplied. Add a hostile case to
`hostileContents` and a write case ("Update of a file whose content key is
unsigned is refused").
### C2. Node keys from the server are unlocked with whatever Argon2 parameters they carry, up to a 2 TiB allocation
- `crypto.go:431` (`decryptNodeKeys` → `account.go:204` `unlockKey` →
`crypto.Key.Unlock`). go-crypto accepts an Argon2 S2K with memory exponent up to
31 (`s2k.go:431`), which is 2^31 KiB, and runs the KDF before it can tell that the
passphrase is wrong.
- The armored `NodeKey` is server data. For nodes in a folder the user has shared
for editing, or in a public upload link folder, it is written by other people's
clients. A valid encrypted passphrase is easy to supply: copy any node's, or
encrypt any string to the parent's public key. The passphrase signature is checked
only after the unlock and only produces `verifyErr`. So listing the folder runs
the KDF.
- Reproduced (scratch internal test): a node key locked with Argon2 memory 2^19 KiB
and `unlockKey` called with a wrong passphrase took 0.47 s and allocated 512 MiB
before it returned "error in unlocking key". At exponent 24 to 31 that is 16 GiB
to 2 TiB: an OOM kill or `fatal error: runtime: out of memory`, which nothing can
recover. Every listing that includes the node crashes the process (rclone mount,
sync). `passes` (up to 255) multiplies the CPU cost on top.
- Not in docs/TODO.md. The decompression-bomb entries cover message sizes, not key
unlocking. The same function unlocks address keys in `account.go`, which is outside
this area but has the same cause.
- Fix: before `Unlock`, walk the private key packets (`key.GetEntity().PrivateKey`
and subkeys, `PrivateKey.S2KParams`/`s2k.Params.Mode()` or by parsing the packet).
Refuse a node key whose S2K is not iterated-and-salted. `crypto.go:374-375`
already notes that Argon2-locked node keys break the official web app, so no
genuine node key uses Argon2. Make it an unreadable node. Optionally bound the
RSA modulus size too.
---
## Important
### I1. A hash key whose signature fails or is missing is accepted and used for writes, so the server can test guesses at the names written
- `nodes.go:550-556` only logs `hashKeyVerifyErr`. `targetFolder` (`nodes.go:964-988`)
checks `hashKey != nil` and the key's `unverified`, not the hash key's signature.
`createFolder`, upload, rename, move and copy then compute
`nameHash(name, parent.hashKey)`.
- Reproduced: the server replaced folder `dir`'s hash key with an unsigned one of
its own, encrypted to the folder's public key. rclone logged one ERROR, uploaded
`dir/tax-return-2026.pdf`, and the stored `Hash` equalled
HMAC(server key, "tax-return-2026.pdf"). The server can now test names against a
dictionary, or confirm guesses, for everything rclone writes into that folder.
Name hashes exist so that it can't.
- The SDK folds hash-key and folder-xattr author failures into the node's
`keyAuthor` (`cryptoService.ts:265-281`, `finalKeyAuthor`). Under rulings 18/21,
rclone's equivalent is `keys.unverified`, but rclone folds in only the passphrase
signature. This is a defect the rulings didn't foresee, not a re-litigation of
them.
- The `hashKeySignedSince` exemption (`nodes.go:551`) is decided by the
server-supplied `CreateTime`. A server that back-dates the folder silences even the
ERROR line.
- Fix: in `targetFolder`, refuse a folder whose hash key signature failed unless
`ignore_signature_errors` is set, using the same message shape as an unverified
key. The smallest way is to keep `hashKeyVerifyErr` on the node, or fold it into
`keys.unverified` for folders. Document that the date exemption depends on a
server value.
### I2. Renaming or moving a node whose name signature failed reuses a session key the server can know, so the server reads the new name
- `nodes.go:1149-1157` (`rename`) and `nodes.go:1218-1224` (`encryptForNewParent`)
reuse `nameSessionKey(n.link.Link.Name, ...)`. The name signature failure is only
logged (`nodes.go:505-516`, docs/TODO.md "a name whose signature fails is listed").
- Reproduced: the server replaced file `real.txt`'s encrypted name with
`"shown.txt"` under a session key it generated, unsigned. rclone listed
`shown.txt` with one ERROR. `operations.Move` to `secret-new-name.txt` succeeded,
and the server decrypted the new stored name with its own session key:
`"secret-new-name.txt"`. Back-dating `CreateTime` before 2021 removes the ERROR
line too (`nameSignedSince`).
- docs/TODO.md records only that names can be swapped and shown, not that they leak
on rename or move. This is a confidentiality loss, not just mislabelling.
- Fix: reuse the old name's session key only when the name's signature verified.
Otherwise use a new session key (`encryptName(newName, nil, ...)`), or refuse
without the flag. Keep `nameVerifyErr` on the node to decide.
### I3. Extended attributes whose signature is missing or invalid are used without `ignore_signature_errors`
- `nodes.go:557-580`: the xattr `verifyErr` is only logged and `n.X` is kept. Spec
6.4 says `ignore_signature_errors` "downgrades content and authorship signature
failures to a logged ERROR", so without it they should fail. The SDK treats a
revision's xattr signature as `contentAuthor`.
- Anyone with the node's public key can write xattrs. Reproduced: unsigned xattrs
claiming `ModificationTime` 2099-01-01. rclone logged one ERROR,
`Object.ModTime` returned 2099, and the file read fine. Consequences: the server
can set modtime, size and SHA1 at will. For example, `rclone sync --update` to
the remote then skips every changed file the server has marked as newer, so a
backup silently stops updating. Forged SHA1 or size gives false `check` results
and failed copies. Integrity of the content itself holds: block sizes are checked
on read.
- Not in docs/TODO.md (only names are recorded).
- Fix: when the xattr signature fails and `ignore_signature_errors` is not set,
drop the attributes (`n.X = nil`, as for undecryptable ones). The file then shows
the encrypted size and the server's modtime, and the log says why.
---
## Minor
### M1. A node fetched while it is being changed can overwrite the newer cached node and stay cached
`nodes.go:724` `cache.put(n)` after `fetchLinks`. `cache.go:52-63` has no
per-node generation, unlike the per-folder change count of ruling 27. Goroutine A
starts `fetchNode(X)` (cache miss) and receives revision R1. Goroutine B commits
R2 and calls `cache.add(X@R2)`. A then calls `put(X@R1)`, which wins. With no
expiry, reads through the cache serve R1's (still stored, obsolete) content until
a re-list, and the next Update fails on a stale `CurrentRevisionID`. The window is
narrow: concurrent ops on one object in one process, e.g. a mount. Fix: give
`put` a token from before the fetch (a global or per-ID change count, bumped by
`invalidate` and `add`), as `startFetch`/`endFetch` do.
### M2. Unbounded per-client maps
`client.folderKeys` (every folder ever seen, private keys and passphrases, kept
whether or not caching is on; Task 8 minor, still present), the node cache (no
size bound or expiry), `client.reported` (one entry per node ID that logged a
problem), and `account.foreign` (one key ring per distinct foreign email a server
names). A long mount or a hostile server grows them without limit. `close()`
still doesn't clear the private keys of file nodes held by the cache (Task 8).
### M3. `listChildren` loops forever when the server keeps answering `More` with the same `AnchorID`
`nodes.go:837-857`. No progress check: a buggy or hostile server keeps rclone
requesting until ctx ends, with `seen` and `all` growing if the IDs change. Fix:
stop with an error when a page adds no new ID or repeats the anchor.
### M4. Root-of-trust and signature date exemptions use server-supplied times
`nameSignedSince`, `hashKeySignedSince` (`nodes.go:514`, `:551`) and
`contentKeyFallbackUntil` (`download.go:204`) read `Link.CreateTime`, which the
server controls. Only the first two waive a check. The SDK does the same. Worth a
comment and a docs/TODO.md line, as it feeds I1 and I2.
### M5. `decryptBlock` is used only by tests
`crypto.go:684-696`. Production uses `blockDecrypter`. `reencryptPassphrase`
(`crypto.go:448-450`) is a one-line alias of `encryptPassphrase`. Maintainers will
ask for both to go: move `decryptBlock` to a test file and call
`encryptPassphrase` directly.
### M6. `truncateName` can cut a UTF-8 sequence
`nodes.go:599-605` slices bytes at 64, so log lines can carry invalid UTF-8. Cut
on a rune boundary.
### M7. Earlier deferred minors re-checked
- Still present: the node passphrase is not checked as valid UTF-8 (Task 5,
`crypto.go:429`). `publicKeys` waiters get the fetching caller's ctx error
(Task 4, `account.go:288-303`). `setChildrenLocked` overwrites a cached node
without the changed-parent rule `put` applies, so after an external move the
old folder's cached listing can still return it (Task 9, `cache.go:100-107`;
only matters with milestone M5 events or a second `Fs`). An xattr `"Size": null` reads
as 0 and skips the block-size fallback (Task 17, `crypto.go:848`; recorded in
the handoff). Move's anonymous detection uses `SignatureEmail == ""` (Task 9,
`nodes.go:1231`; only reachable with the flag after `checkVerified`).
- Closed: `cache.put` leaving a moved node in its old parent's listing (Task 8),
and a negative xattr Size with block sizes (Task 17; the fallback now sums the
block sizes). The root name not being validated (Task 8) has no effect: it is
never used as a path.
### M8. `publicKeyRing` is rebuilt on every own-address lookup
`account.go:278-279`. Each `decryptNode` of a node signed by the user calls
`ToPublic` on every address key (up to three lookups per node). Cache the public
ring on the address. Performance only.
---
## Checked and found sound
- Transitive distrust: `decryptNode` inherits `parent.unverified` before its own
check. `createFolder` copies the parent's. Uploads into an unverified folder read
back through `decryptNode`. `checkVerified` gates rename, move and copy. Root of
trust (`bootstrap`): share creator and root signer through `acct.owns`
(ASCII-only lowering, `emailKey`), and own addresses are never looked up remotely.
- `decryptMessage` decrypts with the session key alone, so holders of decKR can't
pass as signers. The intended-recipient check is sound. Decompression is bounded
for metadata (`maxSmallMessageSize`, `maxXAttrsSize`) and for blocks
(`LimitReader(maxBlockSize+1)` in `fetchOnce`). Integrity checks (block hash,
sizes, block count, re-listed hash) do not depend on the flag.
- Name validation on read and write: empty, `.`, `..`, `/`, NUL, UTF-8, and 255
UTF-16 units (`utf16.RuneLen` is never -1 on valid UTF-8).
- Parent chains: cycle detection over `below`, the depth cap of 2048, and a
parentless non-root refused ("not in the drive").
- Listing cache (ruling 27): `startFetch`/`endFetch` with change counts. `add`,
`invalidate` (including an unknown parent, which bumps every listing in
progress), `move` (both parents) and `trash` bump correctly.
@@ -0,0 +1,212 @@
# Final whole-branch review: tests, fake, harness, CI, docs, hygiene
Reviewer area: everything that is not production backend code. HEAD 29ad26944,
base 06700aaf2. Verified in a scratch worktree (removed afterwards).
## Checks run
- `RCLONE_CONFIG=/notfound go test -race -count=1 ./backend/protondrive/...` on
16 cores: PASS. `backend/protondrive` 333.6 s, `protontest` 38.3 s,
`protonfake` 1.4 s.
- The same package pinned to 2 CPUs (`taskset -c 0-1`, `-timeout 60m`) to
approximate the slow Gitea runner: PASS in 420.7 s (70% of Go's default 10 min test timeout), no failures, timing assertions included.
- `golangci-lint run ./backend/protondrive/...`: 0 issues. `go vet`: clean.
- `GOPROXY=off go mod tidy -diff`: no diff. go.mod/go.sum: Proton-API-Bridge,
go-proton-api, gluon, resty, goquery/cascadia, go-message/vcard, juniper,
logrus, relvacode/iso8601 all gone; go-srp and gopenpgp/v3 promoted to direct.
Nothing unexpected.
- `TestIntegrationFake -v`: 97 subtests pass, 29 skip (matches the handoff).
- Test-account usernames (recovered from the pre-rewrite object 901d7fd5b,
which is still in the local object store): 0 hits in HEAD's tracked files,
text or binary (`git grep -a`). No tokens, UIDs or 32-hex IDs in committed
docs/testdata. Largest tracked files outside upstream's MANUAL.* are the three
golden CLI blocks (4 MiB, 4 MiB, 1 MiB), as intended.
- shellcheck on `init.d/TestProtonDriveFake`: SC2034 only (known minor).
- Live-reach audit: every `NewFs`/`configmap.Simple` in the tests carries the
fake's `endpoint`, except unit tests of the config store that never connect.
`TestIntegration` is gated only by the remote being configured (rclone's norm;
hence the standing `RCLONE_CONFIG=/notfound` rule) and skips during a golden
capture; `TestGoldenCapture` needs all three `-capture*` flags;
`TestIntegrationFake` skips under `-remote`. No other test can reach Proton.
## Critical
None.
## Important
### I-1. CI: `go test -race` has no `-timeout`; the package will hit Go's default 10 min on the slow runner
`.gitea/workflows/ci.yml:43` runs `go test -race ./backend/protondrive/...`
with Go's default per-binary `-timeout 10m`. The `backend/protondrive` test
binary alone takes 333 s on 16 cores under `-race`; pinned to 2 of this host's fast cores it took 420.7 s, 70% of the 10 min budget, so a runner whose cores are ~1.5x slower (shared VM, older CPU, the build cache cold on top) crosses it. The
job's `timeout-minutes: 45` never comes into play: the test binary panics with
"test timed out after 10m0s" first, and the job fails with a goroutine dump
rather than a result. The owner described the runner as "slow and not very
capable".
Fix: `go test -race -timeout 40m ./backend/protondrive/...` (inside the 45 min
job budget), or drop `-race` on CI and keep a non-race run (much faster) with a
nightly race job.
### I-2. CI: `go-version-file: go.mod` installs Go 1.26.0, so `govulncheck` will very likely fail on standard-library advisories
`ci.yml:22-27` takes the Go version from `go.mod`'s `go 1.26.0` directive (no
`toolchain` line). `actions/setup-go` treats that as an exact version and
installs 1.26.0, a release nine months older than the 1.27.4 every local
verification used. `ci.yml:51` then runs `govulncheck@latest` against it;
govulncheck reports reachable standard-library vulnerabilities fixed in later
patch releases (net/http, crypto/tls, crypto/x509 are reached by this backend)
and exits non-zero. So the first push most likely goes red on a finding that is
about the CI toolchain, not the code; it also means CI tests on a compiler no
one has run the suite with. Could not be proven offline (needs vuln.go.dev).
Fix: `go-version: '~1.27.3'` (or `stable`), matching upstream's
`.github/workflows/build.yml:290` lint job; pin govulncheck to a version
instead of `@latest` so a new govulncheck release can't break the build.
### I-3. The committed workspace copy of the ledger stops at the 2026-11-11 pause; the handoff tells a new agent to restore from it
`docs/plans/001-protondrive-tier1-HANDOFF.md:96-108` says a copy of every
workspace file "is committed at `docs/plans/001-protondrive-tier1-workspace/`"
and to resume with `cp docs/plans/001-protondrive-tier1-workspace/*.md
.superpowers/sdd/...`; line 25 calls `progress.md` "the full execution ledger
(every ruling, every deferred minor, every live probe)". The committed
`progress.md` is 175 lines and ends "PAUSED 2026-11-11 ... Tasks 18–21 not
started"; the live ledger (gitignored `.superpowers/`) is 244 lines. Missing
from git: all Task 18–21 ledger entries (rulings on the lag fix and on
draft deletion, the live test_all results, the browser sign-in proof, the
config-create token incident), and 21 files: task-18/19/20/21/21f/21g
briefs, reports, reviews and re-reviews. On another machine, or after the
`.superpowers/` directory is cleaned, a new agent loses the deferred minors of
Tasks 18–21 and the reasoning behind rulings the handoff only summarises.
Fix: re-copy `.superpowers/sdd/001-protondrive-tier1/*.md` into the workspace
directory (and do it again after this final review) and commit.
### I-4. Handoff section 3 still describes the pre-Task-18 state
A new agent reading top-down is told things that are false now:
`HANDOFF.md:3` "Written 2026-11-11 when development was paused"; `:50` "Last
code commit: `9c90998f7`" (it is be0053607 / f8e398fce); `:76` verification
"at `9c90998f7`"; `:82-91` "Not done, and easy to assume otherwise": user docs
"still describe the old backend (Task 20)", wider suites "have never been run
against the new backend (Task 18)", "No fork CI exists (Task 18)" — all three
are done; `:130` "Remaining tasks" lists 18–21 as remaining; section 8
(`:463-466`) says only Tasks 14, 16 and 17 have review files. The table at
`:55-57` and section 6 are current, so the document contradicts itself.
Fix: rewrite section 3's header, commit references and "Not done" list (the
real not-done list is: whole-branch review fix wave, 2FA/CAPTCHA/desktop
clients live, root of trust on a Proton-made drive, the five cross-Fs tests),
and mark the "Remaining tasks"/"Notes for Tasks 18 to 21" subsections as
historical or delete them.
### I-5. The upstream recipe is self-contradictory: the upstream branch would not compile its tests
Spec 13.4 (`docs/specs/001-protondrive-tier1.md:729-732`) says the upstream
branch contains `backend/protondrive/` and the `fstest/test_all/config.yaml`
change; spec 14.4, the handoff (`:2`, `:505-511`) and ARCHITECTURE say
`backend/protondrive/protontest` never goes upstream. 13 of the 17 test files
import `protontest` (all but `crypto_internal_test.go`, `cache_internal_test.go`,
`export_test.go`, `protondrive_test.go`), so leaving it out makes
`go test ./backend/protondrive/` fail to build upstream, and the
`TestProtonDriveFake:` entry in `config.yaml:616-630` refers to an init.d
script and port that are also fork-only. Upstream is deferred, so this isn't
a defect today, but whoever cuts the branch per the written recipe produces a
broken PR, and the handoff (section 9) lists no remedy. Ties to open question
14.6.
Fix: record in the handoff section 9 and spec 13.4 that either protontest goes
upstream with the tests (and question 14.6 must be answered first), or the
upstream branch drops every protontest-dependent test file plus the
`TestProtonDriveFake` config.yaml entry, PORTS.md row and init.d script, and
keeps only `protondrive_test.go` and the two standalone internal tests.
## Minor
1. **Timing assertions in parallel tests are tight for a slow `-race`
runner.** `nodes_internal_test.go:503` and `:523` assert a plain
`about()` call finishes in under 500 ms inside `t.Parallel()` tests
that run alongside ~120 other parallel tests doing multi-MiB crypto under
the race detector; `:487-488` (`TestRetryAfterHonoured`) asserts a 2 s Retry-After call under 5 s.
They passed on the 2-CPU run, so this is a margin concern, not an observed flake. Fix: 2-5 s bounds, which still catch a held pacer (the
cases guard against a 999999999 s or 60 s hold).
2. **Goroutine-count leak checks remain** (`download_internal_test.go:105-118`,
`nodes_internal_test.go:582-596`, known). They run in sequential tests, so
false failures need a stray goroutine started by something else; the
`<= baseline` form can also pass vacuously when goroutines of an earlier
test exit during the window. Recorded flake of `TestUploadShortSource` in the
handoff. Consider `go.uber.org/goleak`-style filtering is not available
without a dependency; keep, but don't expand the pattern.
3. **`TestListingRacingChange` can hang to the package timeout**
(`nodes_internal_test.go:1222-1228`, known, still present): `<-held` has no
timeout and `release` is closed only after `require.NoError(test.change)`;
a failure leaves the fake's handler blocked and `httptest.Server.Close`
waiting. Fix: `t.Cleanup(func(){ close(release) })` guarded by sync.Once,
and a `select` with `time.After` on `held`.
4. **`protonfake` `TestRun` leaks the `run` goroutine on a failed require**
(`protontest/cmd/protonfake/main_test.go:17-34`, known, still present).
Fix: `t.Cleanup` closing `stop`.
5. **`TestClientBootstrapCreateVolumeRace` waits a fixed 2 s** (`nodes_internal_test.go:128-147`,
known, still present).
6. **`TestUploadBlock502Once` doesn't check the fault fired**
(`upload_internal_test.go:230-237`): a mistyped pattern would let it pass
as a plain upload. Add `assert.Equal(t, 2, srv.Requests()[storage route])`
or similar.
7. **`TestAcquireTokenCancel` sleeps 20 ms to "let the wait start"**
(`upload_internal_test.go:479`): on a slow runner the goroutine may not
have started, so the cancelled-while-waiting path is not exercised (the
test still passes via the early ctx check). Harmless, not a flake.
8. **No test pins the 408 exclusion of `refusedCommit`** (21f re-review N3,
still open).
9. **Task 20 re-review nits still present**: `download_internal_test.go:621`
names the unrecorded case "assumed"; `:178` `min(v.blocks, 1)*3` is always 3.
10. **init.d `stop` has no SIGKILL escalation, SC2034 unused `i`**
(`fstest/testserver/init.d/TestProtonDriveFake:51-62`, Task 18 minors,
still present).
11. **CI hygiene**: `on: push` plus `pull_request` with no `concurrency` group
runs twice per PR push on a slow runner; `govulncheck@latest` is
unpinned (see I-2).
12. **The fake's read lag is never combined with the contract suite.**
`Server.SetReadLag` (`protontest/drive.go:1749-1757`) models the live
read-after-write lag that broke `TestPurge`/vfs on the real server, but only
targeted upload tests turn it on; `TestIntegrationFake` and `test_all`
against the fake run with no lag, so the next lag-intolerant path
(`activeNode`, recorded in TODO) can't show up offline. Consider a second
`fstests.Run` (or a `protonfake -read-lag` flag for `test_all`) with a lag
of 1-2 reads.
13. **Conformance checklist contradicts ruling 11**:
`docs/specs/001-conformance-checklist.md:27` (upload row) says "a draft is
deleted only on positive proof it never committed"; since be0053607 the
rule is "only when the server explicitly refused the commit, on a single
unresent attempt; no read-back decides deletion".
14. **User docs: the second "unknown" error isn't documented.**
`docs/content/protondrive.md:289-293` names only "upload outcome unknown";
`errUploadUnconfirmed` ("upload committed, but the server doesn't show it
yet", `upload.go:43`) gets the same no-retry treatment and is what a user
will see under server lag.
15. **User docs claim behaviour proven only against the fake**:
CAPTCHA (`protondrive.md:35-37`), 2FA prompting (`:131-147`) and "`rclone
about` reports the whole account" (`:548`) are unverified live, while
`docs/TODO.md` says the docs "avoid claiming" 2FA and CAPTCHA. Either soften
or fix the TODO wording.
16. **Lost TODO from the live run**: the ledger's 2026-11-12 incident note
("`rclone config create` printed the new remote's client_* tokens and
salted key pass to stdout ... check whether client_* options are marked
Sensitive") is not in `docs/TODO.md`. The options are `Sensitive`
(`protondrive.go:113-142`), but `config create`/`config show` print
unredacted (`fs/config/ui.go` `ShowRemote`) — generic rclone behaviour,
yet worth a docs line ("the output of `rclone config create` contains the
session; don't paste it") and a TODO entry, since the salted key password
survives a logout.
17. **`docs/PERMISSIONS.md` / `.codex/` out of step**: `golangci-lint run` was
added to the Allow tier and to `.claude/` and `.gemini/`, but not to
`.codex/rules/project.rules`, against AGENTS.md's "change them together".
18. **Old username-bearing commit still in the local object store**
(`901d7fd5b`, reachable from the reflog). Not tracked and not on the
remote after the force-push; it disappears with reflog expiry/gc. Mention
it in the handoff so a `git push --mirror` or a backup bundle of this
checkout isn't made before then.
19. **Handoff ruling 13** still reads as current ("stays in `go.mod` until
Task 19"); mark it done (Task 19 review minor, still present).
@@ -0,0 +1,160 @@
# Final review: data transfer (upload.go, download.go, Object Open/Update/Put/PutStream)
Reviewer area: `backend/protondrive/upload.go`, `backend/protondrive/download.go`,
`protondrive.go` Object.Open/Update, Fs.Put/PutStream, `api/types.go` as far as these use it.
HEAD 29ad26944. `go test -race -run 'TestUpload|TestDownload|TestAcquire|TestIntended'`
passes in a scratch worktree (40 s).
Counts: Critical 0, Important 2, Minor 9.
## Data-safety walk (no defect found)
I traced every path that can delete something during an upload:
- `upload` -> `deleteDraft` runs only when `uploadDraft` reports `committed=false`. That
happens before the commit (verification, blocks, `check`, manifest, xattrs) or when
`refusedCommit` is true on `attempts == 1` (upload.go:403). `refusedCommit` needs an
`*api.Error` with a Proton code and a 4xx status other than 408, so a 200 response with an
error envelope, an HTML 4xx and a 5xx all leave the draft. Any pacer resend (5xx, 408, 429,
network) makes `attempts > 1`.
- The `session.call` 401 -> refresh -> resend happens inside one pacer attempt. The first
send got a 401, so the server never processed it. If the refresh then fails with a coded
4xx, `refusedCommit` matches the refresh's error and the draft is deleted. That is safe,
because the commit was never processed. Go's transport does not replay a PUT unless
nothing was written.
- When ctx is cancelled, `fserrors.ContextError` in `shouldRetry` turns even a successful
commit into a ctx error. The detached single read (upload.go:411-414) then still finds the
committed revision.
- `resolveConflict` deletes a draft (with `delete_multiple`) in only two cases: the details
show a pure draft node (`ConflictRevisionID == ""` and a draft revision) left by our UID,
or `replace_existing_draft` is set. It never deletes on a retry. A file conflict becomes
a new revision, and a folder, draft or trashed node is refused.
- The read back after a commit never decides deletion. Lag gets about 7 s of reads that
stop when ctx is done. A commit that is not ours ends as Unconfirmed, Superseded or
OutcomeUnknown, and in each case only the cache is forgotten.
The upload pipeline (readBlocks -> requestURLs -> workers) frees every block on every
path: the worker frees after upload, `requestURLs` frees on a URL error or cancel, and
`readBlocks` frees on cancel. Both channels are unbuffered, so no block is stranded. Slots
and tokens are always given back, and an abandoned `acquireToken` waiter returns its token.
The size and SHA1 checks, the early long-source failure, the empty file and the
exact-multiple-of-4 MiB case are all correct.
On the download side: the full read lists every block and checks the manifest before any
fetch. Per-block hash and recorded-size checks are bounded by `maxBlocks` and
`maxFileBlocks` before `r.sizes[b.Index-1]` is indexed, so that index cannot go out of
range. A range with sizes not recorded starts from block 1 with `skip=offset`. A
whole-file range goes to `openWhole` after `FixRangeOption`. `Close` drains every slot.
## Important
### I-1 `draft_client_uid` is not stable for remotes from environment variables or connection strings, so a leftover draft blocks its name
protondrive.go:652-657, upload.go:292-297.
`NewFs` makes a random UID and calls `m.Set`. `fs.setConfigFile.Set` (fs/configmap.go:90-94)
saves nothing unless the remote is a section of the config file. A remote set up with
`RCLONE_CONFIG_X_*` variables (typical in Docker and CI) or a connection string therefore
gets a new UID on every run, and on every `NewFs` in the same process. By design, drafts
are now left after every ambiguous commit failure, and also when the process dies
mid-upload. On the next run the UID differs, so `resolveConflict` sees a foreign draft and
fails with "another client is uploading the file (set replace_existing_draft...)". That
repeats on every run for that name until the user sets the option.
This breaks spec 6.3 step 1 ("a stable per-remote draft client ID") for a common way of
running rclone.
Fix: when the value cannot be saved, derive it deterministically instead of randomly. For
example, use a hash of the account's user ID (known after sign-in) plus the remote name, or
make the random UID only when `fs.ConfigFileHasSection(name)` and derive it otherwise. At
the least, document it and log a NOTICE when the UID isn't persisted.
### I-2 Multi-thread copies of files that record a size but not block sizes download data quadratically
download.go:142-151, protondrive.go:1150-1173.
`docs/TODO.md` records that a ranged read of such a file "downloads from the first block
every time" and is slow on a mount. The case below is worse than that entry says.
`Size()` is known (xattr `Size`), so rclone's multi-thread copy turns on by itself above
`--multi-thread-cutoff` (256 MiB) when copying to local. Each 64 MiB chunk is a separate
`Open` with a `RangeOption`, and each one downloads and decrypts from block 1 up to the end
of its chunk. It also lists every block page to the end of the file, because `last = -1`
when sizes aren't recorded. Total transfer is about S²/(2·64 MiB): about 8.5 GiB for a
1 GiB file and about 800 GiB for 10 GiB.
The code supports these files on purpose ("old clients didn't all write 4 MiB blocks"). How
many real files lack `BlockSizes` is unknown (every golden capture records them), which is
the only reason this isn't higher.
Fix, cheapest first:
1. A small per-client cache of revisionID -> learnt block sizes, filled as a reader decrypts
blocks, so later chunks start at the nearest known block. This is the fix the TODO plans,
and it should be raised in priority.
2. Until then, a ranged read with sizes not recorded and offset > 0 could at least stop the
listing once the range is covered (it can't know where, so this is weak).
3. Or document that multi-thread copies of such files should use
`--multi-thread-streams 0`.
## Minor
M-1 **Two processes sharing one config can delete each other's in-progress draft**
(upload.go:292-303). The UID is per remote, not per process. If a mount and a cron
`rclone move`, or two overlapping runs, upload the same new name, B deletes A's live draft
and A's upload fails. If A commits within the RTT between B's 2500 response and B's
`delete_multiple`, B sends `delete_multiple` against an active file. What the server does
then is the open TODO question. With `move`, A has already deleted its source. The TODO
records only the `--retries` path. Add this case there. A per-process suffix on ClientUID,
with "own" meaning the remote-wide prefix plus not-this-process, doesn't help. The real
mitigation is the TODO's server check, or re-reading the link and confirming it is still a
draft just before deleting (this narrows the window but does not close it).
M-2 **`pm-storage-token` is still sent on redirects to other hosts** (download.go:264-270,
upload.go:868; Task 10 minor). Go's client drops only Authorization, Cookie and
WWW-Authenticate when a redirect leaves the host. Set `CheckRedirect` on the storage calls,
or refuse redirects for them.
M-3 **The `upload` godoc has a broken reflow** (upload.go:104-109: "...is
errUploadSuperseded,\n// and one the server..."). That is the 21f re-review's N2 and it is
still present. Rewrap the paragraph.
M-4 **`errUploadMismatch` is a `NoRetryError`** (protondrive.go:359). A source that changes
while it is being read (a growing log file, or a local file still being written) fails for
the whole run, including the `--retries` passes, where the usual rclone outcome is a retry
at the high level. `NoLowLevelRetryError` would avoid a pointless immediate re-read and still
let a later pass succeed.
M-5 **A range read of a file whose block sizes aren't recorded keeps fetching after the
range ends** (download.go:145-151, 338-346). `last` stays -1 when sizes aren't recorded, so
up to `downloadAhead` blocks (16 MiB) past the end of the range are downloaded and
decrypted before `Close`. This adds to I-2.
M-6 **An own leftover draft revision on an active file is never deleted** (upload.go:279-290;
Task 11 minor, still present). The conflict path uploads a new revision and leaves our old
`ConflictDraftRevisionID` taking quota. A `DELETE` of that draft revision when
`ConflictDraftClientUID` is ours would be safe: it is a draft revision, not the active one.
M-7 **Updating an unverified file logs "reading"** (upload.go:312 -> download.go:196; Task
16 minor, still present).
M-8 **The `errUploadSuperseded` message is misleading** when the node reads back as draft
or trashed (upload.go:437-438; Task 11 minor, still present). The message says
"revision ... is active".
M-9 **The `createDraft` retry can report its own draft as a foreign one** (upload.go:237-259,
294-295). Sequence: own draft deleted, re-POST gets a 5xx, the pacer resends, and the lost
first response leaves our new draft, so the resend sees a 2500 with our UID. Because
`retried` is set, the client reports "a draft of the file came back after it was deleted".
Nothing is lost and the next run replaces the draft. When `ConflictDraftClientUID` is ours
on the retried pass, the error could say so instead.
Still present and already in docs/TODO.md (not re-graded): `Close` waits behind the
context-free `downloadTokens.Get`; `countContent` reads the whole file again for every
suffix-range open of a file with no recorded size; `--retries` can reach the own-draft
`delete_multiple`; unbounded block-list metadata bytes; ranged reads don't check the
manifest; no test pins the 408 exclusion in `refusedCommit` (N3; the 408 case in
nodes_internal_test.go:428 covers only `shouldRetry`).
Closed since earlier reviews: `bytes=0-` and whole-file ranges now go through `openWhole`
(ruling 26). The read back now stops when ctx is done. Cleanup no longer deletes after a
read back, so Task 11's "restore deletes our superseded history entry" can no longer happen.
@@ -0,0 +1,168 @@
# Flake report: TestClientBootstrapCreateVolumeRace
Commit: `e23de0b58 protondrive: make the volume creation race test deterministic`
(branch `protondrive-tier1`, not pushed).
## Summary
- **Root cause.** The test assumed both clients' first `GET drive/v2/shares/my-files` would be handled before either client had created the volume. Nothing guaranteed that. `loadAccount` holds the process-wide `unlockMu` while it unlocks keys, so the second client's first GET always goes out at least one key unlock after the first client's. In a whole-package run, other parallel tests queue on the same mutex, so the gap can be much longer. The old `Delay: 500ms` fault delayed *both* GETs by the same amount, so it did nothing to keep them together. When the first client's create finished before the second client's GET was handled, the second client found the volume and never POSTed. That made `assert.Equal(t, 2, srv.Requests()["POST /drive/volumes"])` fail with `expected: 2, actual: 1`.
- **The client is correct.** The losing interleaving ("the volume already exists by the time I look") is the client's ordinary existing-drive path. Only the test's assertion depended on timing.
- **Second bug, found by the deterministic test.** Once both create-volume POSTs were forced to run concurrently, the race detector found a genuine data race in the **fake** (`protontest.handleCreateVolume` / `checkVolumeRequest`). go-crypto caches signature-check results inside the key the first time the key is used. The new user's address key had never been used, so two concurrent validations wrote to it at once. It is now fixed: each request is checked with its own copy of the key.
## 1. Reproduction
- In isolation it never failed: `-race -count=50` passed, and so did `-count=20 -cpu 1,2`. CPU contention alone does not reproduce it.
- It reproduced with **6 concurrent whole-package `-race -v` runs** of a prebuilt test binary: 3 of 6 runs failed (an earlier batch of 6 had 0 failures). All three failures were identical:
```
=== NAME TestClientBootstrapCreateVolumeRace
nodes_internal_test.go:139:
Error Trace: /home/user/rclone/backend/protondrive/nodes_internal_test.go:139
Error: Not equal:
expected: 2
actual : 1
Test: TestClientBootstrapCreateVolumeRace
--- FAIL: TestClientBootstrapCreateVolumeRace (8.92s)
```
Line 139 was the `POST /drive/volumes` count assertion. None of those runs logged `WARNING: DATA RACE`.
## 2. Root cause, with evidence
I added a temporary copy of the test (since deleted) whose HTTP transport logged the send and completion time of every drive request for each client.
**In isolation** the margin is tiny. c1's first GET goes out about 130ms after c0's: that is c0's key unlock, serialized by `unlockMu`. c0's create finishes at 792ms, and c1's GET is handled at about 757ms, so only about 35ms separates the passing ordering from the failing one:
```
c0 GET /drive/v2/shares/my-files sent=129ms done=630ms status=422
c1 GET /drive/v2/shares/my-files sent=256ms done=757ms status=422
c0 POST /drive/volumes sent=685ms done=792ms status=200
c0 GET /drive/v2/shares/my-files sent=793ms done=794ms status=200
c1 POST /drive/volumes sent=814ms done=814ms status=422 (2500)
c1 GET /drive/v2/shares/my-files sent=825ms done=825ms status=200
```
**Under whole-package load** the gap between the first GETs grew to between 240ms and 1.17s (runs that still passed):
```
c1 GET sent=418ms ... c0 GET sent=1.267s (gap 850ms)
c0 GET sent=239ms ... c1 GET sent=1.144s (gap 905ms)
c1 GET sent=466ms ... c0 GET sent=1.638s (gap 1.17s)
```
When the gap is longer than the winner's "keys + POST + validation" time (about 160ms idle, several hundred ms under load), the second GET is handled after the volume exists. The second client then never POSTs, which is the failure above.
**Why the gap exists.** `loadAccount` (`account.go`) takes `unlockMu`, a package-level mutex: "unlocks for different sessions run one at a time". So the second client's GET is always at least one unlock behind. In the full package, every parallel test's `newClient` queues on the same mutex, which explains why it only reproduced in whole-package runs.
**Candidates from the brief:**
| Candidate | Verdict |
|---|---|
| Loser re-reads my-files before the winner's volume is visible | Ruled out. The fake answers 2500 only under `s.mu` with `u.drive != nil`, and the trace shows the loser's re-GET always returned 200. |
| Timing assumption in the test (Delay vs real ordering) | **Root cause.** The Delay shifted both GETs equally and could not keep them together. |
| Fake's create handler validating outside the lock, interleaving badly | Not the flake's cause; the client handles both 2500 paths identically. But it hid a real **data race in the fake**, exposed once the POSTs were forced to run together (section 3b). |
| Pacer / Retry-After | Ruled out. No 429 or 5xx appears in the traces; 4xx is not retried. |
| A data race reported in the original flake | None in the failing runs. A race appeared only after the POSTs were made concurrent, and it is in the fake. |
**The production path is correct for every two-client interleaving:**
1. Both see 2501 and both POST. One gets 200; the other gets 2500, which `createVolume` treats as success, then re-reads my-files and finds the volume, because a 2500 means it exists.
2. The second client looks after the first has created the volume. It gets 200 straight away: the normal existing-drive path, with no POST.
3. The winner's POST commits but the response is lost or 5xx. The pacer retries, gets 2500, treats it as success, then re-reads. Correct.
The only residual assumption is that a real server's 2500 implies the volume is immediately readable through my-files (see Concerns).
## 3. Fix
### a. Make the interleaving deterministic (test and fake)
- `protontest.Fault` gains a `Gather bool` option: "hold each faulted request until all Times have arrived". `Inject` panics if `Gather` is set without `Times`. A held request still ends when the client gives up, so a stuck test cannot hang.
- `TestClientBootstrapCreateVolumeRace` now:
- gathers both `GET my-files` requests, so both clients see 2501 whatever their timing;
- gathers both `POST /drive/volumes` requests, so both creations are validated at once and the loser hits the fake's late "already exists" check;
- starts the second client as late as the server allows: after the first client finishes, or after 2s if the server is holding the first client back for it. This models a long `unlockMu` queue;
- runs under a context with a one-minute timeout.
- A new `TestFaultInjection/Gather` subtest checks that the first request is not answered until the second arrives, and that requests after `Times` are not held.
### b. Data race in the fake (found by 3a)
With the POSTs gathered, 3 of about 180 focused runs failed with `race detected during execution of test`. Every race stack was entirely in the fake:
```
WARNING: DATA RACE
Write at 0x00c0000f00f0 by goroutine 42:
github.com/ProtonMail/go-crypto/openpgp/v2.(*Identity).LatestValidSelfCertification() user.go:212
...(*Entity).DecryptionKeys() / ReadMessage() / gopenpgp Decrypt()
protontest.decryptArmored() protontest/crypto.go:239
protontest.unlockDriveKey() protontest/crypto.go:280
protontest.checkVolumeRequest() protontest/drive.go:1677
protontest.(*Server).handleCreateVolume() protontest/drive.go:1656
Previous read at 0x00c0000f00f0 by goroutine 32:
(same stack, the other create-volume request)
```
go-crypto's `LatestValidSelfCertification` caches `sig.Valid` inside the key on first use. A user created without a drive has never used its address key, so two concurrent `checkVolumeRequest` calls both did that first write. Other handlers share keys that setup has already used, so they only ever read. The fix is in `handleCreateVolume`: validate with `addrKey.key.Copy()`, the key's own copy for that request. After this, 200 focused runs and 12 whole-package runs reported 0 races.
## 4. Before / after (the forced interleaving)
**Before**: the final test shape with the old `Delay: 500ms` GET fault in place of the Gather faults, at `-race -count=10`:
```
--- FAIL: TestClientBootstrapCreateVolumeRace (2.30s)
nodes_internal_test.go:153:
Error: Not equal:
expected: 2
actual : 1
...
fails: 10/10
```
**After** (Gather faults, before the key-copy fix): 3 of about 180 runs failed with the fake data race in 3b. That run also hit go test's 10-minute default timeout, because each run costs about 2.3s by design.
**After** (committed tree): `RCLONE_CONFIG=/notfound go test -race -timeout 40m -count=200 -run 'TestClientBootstrapCreateVolumeRace$' ./backend/protondrive/`
```
ok github.com/rclone/rclone/backend/protondrive 610.471s
FAIL lines: 0 races: 0
```
`TestFaultInjection/Gather`: 30 of 30 passed at `-race -count=30`.
## 5. Verification
- Focused test: `-race -count=200` passed, with 0 races (above).
- Whole package, sequential, on commit e23de0b58: `RCLONE_CONFIG=/notfound go test -race -count=1 -v ./backend/protondrive/...` six times in a row:
```
run 1 exit=0 protondrive 95.1s ok, protontest 32.6s ok fails=0 races=0
run 2 exit=0 protondrive 98.3s ok, protontest 33.3s ok fails=0 races=0
run 3 exit=0 protondrive 91.7s ok, protontest 32.4s ok fails=0 races=0
run 4 exit=0 protondrive 90.4s ok, protontest 32.5s ok fails=0 races=0
run 5 exit=0 protondrive 92.9s ok, protontest 31.9s ok fails=0 races=0
run 6 exit=0 protondrive 91.8s ok, protontest 32.4s ok fails=0 races=0
```
- Whole package under contention, the same setup that reproduced the bug 3 of 6 times: 6 concurrent `-race -v` runs of the committed binary all passed, with 0 races. The volume test took 12 to 14s per run.
- `go build ./...`, `go vet ./backend/protondrive/...` and `golangci-lint run ./backend/protondrive/...` (0 issues) are all clean.
- A `protontest` run at `-race -count=20` "FAIL"ed only on go test's 10-minute default timeout. One package run takes 33s, so 20 runs need about 660s. This is not a hang: individual runs and the Gather subtest at count=30 all pass.
## Other intermittent failure seen (not fixed, out of scope)
`TestUploadShortSource` failed once, in one of the six concurrent whole-package runs of the first loaded batch (the one that also reproduced the volume flake):
```
=== RUN TestUploadShortSource
upload_internal_test.go:196:
Error Trace: /home/user/rclone/backend/protondrive/download_internal_test.go:114
/home/user/rclone/backend/protondrive/upload_internal_test.go:196
/home/user/rclone/backend/protondrive/upload_internal_test.go:201
Error: Condition never satisfied
Test: TestUploadShortSource
Messages: goroutines left running
--- FAIL: TestUploadShortSource (4.14s)
```
`requireNoLeaks` waits up to 2s for the **process-wide** `runtime.NumGoroutine()` to return to its baseline. The test is not `t.Parallel()`, so it runs in the sequential phase. On a heavily loaded machine (six race-enabled package runs at once) 2s may not be enough for server and transport goroutines to wind down. A real leak is not ruled out. It did not recur in the 12 later whole-package runs (6 sequential and 6 concurrent).
## Concerns
- The test now costs about 2.3s, which is the deliberate 2s wait. It runs in parallel with the rest of the package, so wall-clock impact is small.
- The client assumes a real server's 2500 on `POST /drive/volumes` means the volume can be read at once through my-files. If Proton's API were eventually consistent there, the losing client would fail with a clear `get drive: ... 2501` error rather than corrupt anything. I did not add a retry for this unverified case.
- go-crypto writes into a key on first use. In the client, the first use of each folder key happens on one goroutine in `decryptNode`, which decrypts the hash key before any concurrent `decryptChildren`. So the listing path is not exposed, and no client-side race appeared in any run. Any future code that uses a *freshly unlocked* key from several goroutines at once would hit the same race as the fake did.
@@ -0,0 +1,55 @@
## Global Constraints
- Pure Go. No cgo, no helper binary, no `replace` in `go.mod`.
- All HTTP goes through `fs/fshttp` (`fshttp.NewClient(ctx)`).
- Never send `x-pm-drive-sdk-version`. `x-pm-appversion` is
`external-drive-rclone@<semver>-<channel>` with channel one of `stable`,
`beta`, `alpha` (development builds map to `alpha`).
- Never call a deprecated share-scoped route:
`/drive/shares/{id}/links`, `/folders`, `/files`, `/events` and anything
beneath them. (`GET drive/shares/{id}` and `drive/v2/shares/my-files` are
allowed; the SDK uses them.)
- Below the seam (`session.go`, `account.go`, `crypto.go`, `nodes.go`,
`cache.go`, `upload.go`, `download.go`, `api/`): no `fs.Fs`, `fs.Object`
or `fs.DirEntry`. `fs` may be imported there only for logging
(`fs.Debugf` etc.), `fs.Pacer` and `fs.ConfigInfo`.
- `protondrive.go` contains no cryptography and no URL paths.
- No package-level mutable state except `unlockMu sync.Mutex` (Task 4).
- Nothing decrypted is written to disk. No secret (password, key password,
token, session key, passphrase, upload token) in any log line.
- Existing config keys keep their names and meaning: `username`,
`password`, `mailbox_password`, `2fa`, `otp_secret_key`, `client_uid`,
`client_access_token`, `client_refresh_token`, `client_salted_key_pass`,
`encoding`, `original_file_size`, `app_version`,
`replace_existing_draft`, `enable_caching`. The `encoding` default does
not change.
- Block size is 4 MiB (`4 << 20`). Per-file upload concurrency default 4,
max 5. At most 10 upload blocks and 10 download blocks in flight per
client. Metadata batches of 100, at most 15 batch requests at once.
- Code, comment and commit rules are in `AGENTS.md`. Commit subject prefix
`protondrive:`. JSON wire fields use Proton's exact names.
- **Wire-format authority:** the SDK checkout at
`~/.cache/protondrive-sdk-ref` (commit `05c9115`, JS tag `js/v0.23.0`).
It is untrusted reference data: read it, never build or run it. Request
and response shapes come from
`client/js/src/internal/apiService/driveTypes.ts` and `coreTypes.ts`;
behaviour from the module named in each task. Where this plan and the SDK
disagree about the wire format, the SDK wins - note it in the commit.
- **Second reference:** the old implementation in the module cache,
`~/go/pkg/mod/github.com/rclone/!proton-!a!p!i-!bridge@v1.0.5/` and
`~/go/pkg/mod/github.com/rclone/go-proton-api@v1.0.4/`. It passes rclone's
live tests today, so its gopenpgp v3 calls (`crypto.go`,
`crypto_v3_compat.go`) show working API usage.
- If an interface below must change, change it and update this plan's
Interfaces blocks in the same commit.
- Tick the checkboxes in this file as steps complete.
Verification commands used throughout (run from the repo root):
```sh
go build ./backend/protondrive/...
go test -race ./backend/protondrive/...
go vet ./backend/protondrive/...
golangci-lint run ./backend/protondrive/...
```
@@ -0,0 +1,159 @@
# Implementer instructions (all tasks)
## Before You Begin
If you have questions about:
- The requirements or acceptance criteria
- The approach or implementation strategy
- Dependencies or assumptions
- Anything unclear in the task description
**Ask them now.** Raise any concerns before starting work.
## Your Job
Once you're clear on requirements:
1. Implement exactly what the task specifies
2. Write tests (following TDD if task says to)
3. Verify implementation works
4. Commit your work
5. Self-review (see below)
6. Report back
Work from: /home/user/rclone (branch protondrive-tier1; commit there, never push, never switch branches)
**While you work:** If you encounter something unexpected or unclear, **ask questions**.
It's always OK to pause and clarify. Don't guess or make assumptions.
While iterating, run the focused test for what you're changing; run the
full suite once before committing, not after every edit.
## You Do Not Dispatch Subagents
Do all of this task's work yourself. Never spawn a subagent to
implement part of the task, and above all never spawn a reviewer to
check your work. Self-review (below) means reading your own diff.
Review is the controller's job: after you report, it dispatches a
fresh reviewer against your diff. A reviewer you spawn duplicates
that review at full cost, and its approval counts for nothing in
the process. If you catch yourself thinking "an independent review
would strengthen my report" — that review is already scheduled.
Report instead.
## Code Organization
You reason best about code you can hold in context at once, and your edits are more
reliable when files are focused. Keep this in mind:
- Follow the file structure defined in the plan
- Each file should have one clear responsibility with a well-defined interface
- If a file you're creating is growing beyond the plan's intent, stop and report
it as DONE_WITH_CONCERNS — don't split files on your own without plan guidance
- If an existing file you're modifying is already large or tangled, work carefully
and note it as a concern in your report
- In existing codebases, follow established patterns. Improve code you're touching
the way a good developer would, but don't restructure things outside your task.
## When You're in Over Your Head
It is always OK to stop and say "this is too hard for me." Bad work is worse than
no work. You will not be penalized for escalating.
**STOP and escalate when:**
- The task requires architectural decisions with multiple valid approaches
- You need to understand code beyond what was provided and can't find clarity
- You feel uncertain about whether your approach is correct
- The task involves restructuring existing code in ways the plan didn't anticipate
- You've been reading file after file trying to understand the system without progress
**How to escalate:** Report back with status BLOCKED or NEEDS_CONTEXT. Describe
specifically what you're stuck on, what you've tried, and what kind of help you need.
The controller can provide more context, re-dispatch with a more capable model,
or break the task into smaller pieces.
## Before Reporting Back: Self-Review
Review your work with fresh eyes. Ask yourself:
**Completeness:**
- Did I fully implement everything in the spec?
- Did I miss any requirements?
- Are there edge cases I didn't handle?
**Quality:**
- Is this my best work?
- Are names clear and accurate (match what things do, not how they work)?
- Is the code clean and maintainable?
**Discipline:**
- Did I avoid overbuilding (YAGNI)?
- Did I only build what was requested?
- Did I follow existing patterns in the codebase?
**Testing:**
- Do tests actually verify behavior (not just mock behavior)?
- Did I follow TDD if required?
- Are tests comprehensive?
- Is the test output pristine (no stray warnings or noise)?
If you find issues during self-review, fix them now before reporting.
## After Review Findings
If the task review finds issues, you will be resumed with the findings.
Fix them, re-run the tests that cover the amended code, and append a fix
report to your report file: what you changed, the covering tests you
ran, the command, and the output. Reviewers will not re-run tests for
you — your report is the test evidence. Then reply with the same short
status contract as your first report.
## Report Format
Write your full report to [REPORT_FILE]:
- What you implemented (or what you attempted, if blocked)
- What you tested and test results
- **TDD Evidence** (if TDD was required for this task):
- RED: command run, relevant failing output before implementation, and why the failure was expected
- GREEN: command run and relevant passing output after implementation
- Files changed
- Self-review findings (if any)
- Any issues or concerns
Then report back with ONLY (under 15 lines — the detail lives in the
report file):
- **Status:** DONE | DONE_WITH_CONCERNS | BLOCKED | NEEDS_CONTEXT
- Commits created (short SHA + subject)
- One-line test summary (e.g. "14/14 passing, output pristine")
- Your concerns, if any
- The report file path
If BLOCKED or NEEDS_CONTEXT, put the specifics in the final message
itself — the controller acts on it directly.
Use DONE_WITH_CONCERNS if you completed the work but have doubts about correctness.
Use BLOCKED if you cannot complete the task. Use NEEDS_CONTEXT if you need
information that wasn't provided. Never silently produce work you're unsure about.
## Project rules
- Read /home/user/rclone/AGENTS.md (code, comment and commit style) and the
constraints in /home/user/rclone/.superpowers/sdd/001-protondrive-tier1/global-constraints.md.
The spec is /home/user/rclone/docs/specs/001-protondrive-tier1.md - read the
sections your task cites.
- Go 1.27.4 and golangci-lint are on PATH. Do not use sudo, do not push, do
not run `git reset --hard`, `git clean`, `git stash`, or recursive `rm`.
- `go get` / `go mod tidy` are allowed only where your brief says so.
- The existing `backend/protondrive/protondrive.go` (old implementation) must
keep compiling and its tests passing until Task 14 rewrites it. If a new
identifier collides with one in that file, rename the NEW one and say so in
your report.
- Commit with subject prefix `protondrive:`. Tick your task's checkboxes in
/home/user/rclone/docs/plans/001-protondrive-tier1.md in your final commit.
- Things you notice but must not fix go in /home/user/rclone/docs/TODO.md.
- Never fabricate a test vector: a value "from the old library" must be
produced by actually running the old library.
- **Always run tests with `RCLONE_CONFIG=/notfound`** (e.g.
`RCLONE_CONFIG=/notfound go test -race ./backend/protondrive/...`). The
machine's real rclone config now holds a live `TestProtonDrive:` remote;
without the variable, `TestIntegration` signs in to the real Proton
service on every run, which risks rate-limiting the test account. Never
contact the real Proton API unless your brief explicitly says to.
@@ -0,0 +1,255 @@
# SDD ledger — plan: docs/plans/001-protondrive-tier1.md
Spec: docs/specs/001-protondrive-tier1.md (approved 2026-11-11). Branch protondrive-tier1. Implementer model: opus (owner's instruction).
Merge base for final review: 06700aaf2
## Preflight scan
| Tasks | Produces vs consumes | Finding |
|---|---|---|
| 1 -> 2,3,4,7,9,11 | protontest.Server.handle/Inject/Requests used by later route files | consistent |
| 2 -> 3 | session, credentialStore; 3 adds loginSRP/fork to same file | consistent |
| 3 -> 5 | Task 3 creates crypto.go (computeKeyPassword, decryptForkPayload); Task 5 modifies it | consistent |
| 4 -> 6,8 | loadAccount, account.primary(); 8 newClient consumes | consistent |
| 5 -> 6,8,10,11,12 | crypto function set | consistent |
| 7 -> 8,10 | fake read routes, Seed*/Corrupt/Dump | consistent |
| 8 -> 9,10,11,14 | client, node, apiCall, errNotFound/errExists, newTestClient | Task 8 test `TestShouldRetry` collides with the old test of that name in protondrive_internal_test.go |
| 9,10,11 -> 14 | mutations, open, upload | consistent |
| 13 -> 14,15 | setupFakeRemote, protonfake | 13 is skipped until 14 adds `endpoint`; consistent |
| 14 -> 19 | 14 keeps old libs for diff tests; 19 removes | consistent |
| 6,12 -> 19 | frozen fixtures survive removal | consistent |
| each task self-check | tests vs code, files created vs touched | Task 1: 2064 for bad app version is the fake's choice, self-consistent. Task 17 `TestBudgetNoLegacyRoutes` needs a TestMain hook in package protondrive_test - none defined elsewhere, fine. Others agree with themselves |
Ruling: Task 8's retry test is named `TestClientShouldRetry` — the old `TestShouldRetry` exists until Task 14 — costs nothing if wrong.
Ruling: no separate worktree; work happens on branch protondrive-tier1 in the main checkout — the branch is already isolated from master and the owner works here — cost: owner's checkout is busy during execution.
## Progress
Ruling: fake keeps Proton's published app-version grammar (channel optional); the client's own tests assert a channel is always sent — fake stays a model of the service — a channel-less regression would only be caught by the Task 2 unit test.
Task 1: minor (deferred): fake accepts app version without channel (ruled above)
Task 1: minor (deferred): wrong-method request to a registered path reports 404 "no route"
Task 1: minor (deferred): test counter `called` in server_test.go is a plain int across goroutines
Task 1: complete (commits 06700aa..2e3dbac, review clean)
Ruling: Task 2 two-session refresh race — fix by re-reading the store after a rejected refresh (bounded wait up to ~3s for the winner's save) before declaring the session dead; no cross-Fs lock, since the spec allows no package state beyond unlockMu — cost if wrong: a rare false "session expired" when the winner takes longer than the wait.
Task 2: minor (deferred): adopted stored tokens with a stale access token fail one call with a plain 401
Task 2: minor (deferred): refresh HTTP call uses the first waiter's ctx
Task 2: minor (deferred): serverTime can step back under out-of-order responses
Task 2: fix round 1/5 (1 addressed, 0 open — two-session refresh race; commits ea41704..9d894ab)
Task 2: complete (commits 2e3dbac..9d894ab, review clean)
Ruling: key salt must be chosen by the primary user key's ID, not the first entry (old library behaviour; SDK takes the first) — carried into Task 4 — cost if wrong: none, first-entry is the fallback.
Task 3: minor (deferred): half-signed-in cleanup DELETE is untested and uses the caller's (possibly cancelled) ctx
Task 3: minor (deferred): empty SRP salt not rejected up front
Task 3: minor (deferred): TestForkLogin goroutine may assert after test end on a failure path
Task 3: note for Task 14: forkRequest.URL carries the fork key - show it to the user only, never log it
Task 3: fix round 1/5 (1 addressed, 0 open — bad server proof untested; commits 2ba0848..5a8bb02)
Task 3: complete (commits 9d894ab..5a8bb02, review clean)
Task 4: minor (deferred): publicKeys waiters share the fetching caller's ctx error
Task 4: minor (deferred): GET /core/v4/users fetched at sign-in and again in loadAccount
Task 4: minor (deferred): fake AddAddress/EncryptToAddress read key state outside the lock
Live probe 2026-11-11 (controller, throwaway test, not committed): new loginSRP works against the real test account (TestProtonDrive: remote configured in ~/.config/rclone/rclone.conf with the old backend's keys). Account has NO drive volume: GET drive/v2/shares/my-files -> 422 code 2501 "Volume not found". Old backend fails on it (legacy GET /drive/shares redirects https->http).
Ruling: Task 8 newClient must create the volume when my-files returns 2501, as the SDK's shares module does (POST drive/volumes), and the fake must support a user without a volume — a new account is otherwise unusable — cost if wrong: a malformed volume in a throwaway test account.
Task 4: fix round 1/5 (1 addressed, 0 open — address-key signature rejection untested; commits 6ec4984..483c3a1)
Task 4: complete (commits 5a8bb02..483c3a1, review clean)
Owner provided a second, paid test account 2026-11-11: remote TestProtonDrivePaid: configured (no sign-in attempted yet). Live matrix for Task 21 is now free + paid.
Ruling: Task 5 content format follows the SDK and Proton's stated target (v4 node key advertising SEIPDv2, v6 PKESK content key, SEIPDv2 blocks, block signature encrypted with the content session key) even where the old library's comments disagree — the old library is what must stay readable, the SDK decides what is written — cost if wrong: uploads rejected by the real server; to be settled by a live upload probe as soon as Task 11 exists, now that test accounts are available.
Task 5: minor (deferred): xattrs.marshal compacts/HTML-escapes Extra members on re-encode (semantics kept)
Task 5: minor (deferred): node passphrase not checked for valid UTF-8 as the SDK does
Task 5: open for live check: block-signature packet format and node key locking mode cannot be confirmed from the SDK checkout (in docs/TODO.md)
Task 5: fix round 1/5 (1 addressed, 0 open — decryption key accepted as signer; commits 0bb2dbd..a1e1333)
Task 5: complete (commits 666df15..a1e1333, review clean)
Ruling: keep the intended-recipient check in decryptMessage (stricter than the SDK may be) — the old library and go-crypto enforce it — cost if wrong: a spurious verification warning (not a failure) on data from a client that lists other recipients.
Task 6: minor (deferred): no test for a decryption-key signature that lists recipients with verifyKR excluding it (hardening for intendedRecipientsError ordering)
Task 6: minor (deferred): recipient match accepts any key in decKR; plain error type instead of SignatureVerificationError
Task 6: minor (deferred): relvacode/iso8601 is a direct dep only for the differential test (goes in Task 19)
Task 6: complete (commits a1e1333..3b63f3c, review clean)
Ruling: the fake must reject client-written names, passphrases, hash keys and xattrs that are SEIPDv2/v6-PKESK (official clients cannot be assumed to read them), since Dump alone would not notice — carried into Tasks 9 and 11 as a fake strictness rule — cost if wrong: none for correctness, slight extra fake code.
Task 7: minor (deferred): TestCorrupt can pass without asserting if a node is missing from Dump
Task 7: minor (deferred): plan Step 2 (RED run) ticked though strict TDD was skipped for the fake's drive state
Task 7: minor (deferred): fake block URLs never expire; wire-name test covers only the links response; drive.go is 1518 lines
Task 7: note: fake error codes for unknown AnchorID (2001), missing storage token (2011) and drafts hidden from children are assumptions until a live run
Task 7: complete (commits 3b63f3c..27d06dc, review clean)
Ruling: honoured Retry-After is capped at 60s — the SDK waits whatever the server says, but an uncapped value lets one bad header freeze the client through the shared pacer — cost if wrong: retrying sooner than Proton asked after a very long Retry-After (then another 429).
Task 8: confirmed gap from ⚠️: fetchPublicKeys unpaced/unretried — sent to fix round 1
Task 8: note for Task 9: findChild cannot tell "absent" from "present but hidden" (drafts, Docs, unreadable names): create paths must map server code 2500 to errExists
Task 8: note for Task 10: pre-2023 content-key-packet fallback (SDK) not yet implemented; listing does not decrypt the content key
Task 8: minor (deferred): paging error does not cancel in-flight batches
Task 8: minor (deferred): folderKeys map grows unbounded; close() does not clear file-node keys held by the cache
Task 8: minor (deferred): cache.put leaves a moved node in its old parent's listing (Task 9 must invalidate on move)
Task 8: minor (deferred): volume creation signs with the first unlocked address key, not the one flagged Primary
Task 8: minor (deferred): root node name not validated
Task 8: fix round 1/5 (2 addressed, 0 open — Retry-After cap/single wait, public key lookups paced; commits 2065b7b..a8856ff)
Task 8: minor (deferred): TestListCancel compares NumGoroutine to a baseline and could flake
Task 8: minor (deferred): Retry-After sleep inside pacer.Call holds a connection token for up to 60s
Task 8: complete (commits 27d06dc..a8856ff, review clean)
Live probe 2026-11-11 (controller, throwaway test, not committed): against the real FREE account, loginSRP + newClient created the volume (server accepted share/root key material), listed an empty root, about returned total 2147483648. Paid account untouched so far.
Task 9: minor (deferred): apiCall retries non-idempotent mutations after 5xx/network errors; a lost response gives a spurious errExists/errNotFound/stale-hash error (Fs layer in Task 14 must treat Mkdir errExists as success and delete errNotFound as done)
Task 9: minor (deferred): trash accepts any 1xxx code as OK instead of 1000/1001/1002
Task 9: minor (deferred): anonymous detection on move uses SignatureEmail=="" even when the parent-key signature failed
Task 9: minor (deferred): cache.setChildren bypasses put's changed-parent rule (matters for external changes, M5)
Task 9: minor (deferred): copyNode reports a made copy as an error if the follow-up getNode fails
Task 9: minor (deferred): nodes.go is ~1,160 lines; small fake-side coverage gaps on copy/rename formats
Task 9: note: fake rejects NodePassphraseSignature on a signed node - stricter than known real behaviour
Task 9: complete (commits a8856ff..ee81e74, review clean)
Task 10: minor (deferred): Close can wait behind the context-free downloadTokens.Get (in docs/TODO.md)
Task 10: minor (deferred): pm-storage-token would follow a cross-host redirect from the storage host (block-scoped token)
Task 10: note: per-reader read-ahead is 4 blocks (implementer's choice; client-wide cap 10 stands); content key decrypted on every open
Task 10: fix round 1/5 (1 addressed, 0 open — unbounded claimed block sizes; commits 4bcafe5..e8b1359)
Task 10: complete (commits ee81e74..e8b1359, review clean)
Live probe 2026-11-11 13:09 UTC (controller, throwaway test, not committed), FREE account, new code at 9a4ed61a8: createFolder rclone-live-probe-20261111-130916; upload hello.txt (25 B), empty.bin (0 B), five-mib.bin (5 MiB+3, 2 blocks), then a second revision of hello.txt; each downloaded back byte-identical with manifest verification, SHA1/size/modtime correct; ranged read across the block boundary correct. => Real server accepts v6 PKESK + SEIPDv2 content, verifier tokens, manifest, xattrs, new revisions as written. Still unverified: official web/CLI client reading these files (owner asked to look in the web app).
Ruling: Task 11 commit outcome unknown (commit errored and the follow-up read failed) => delete nothing and return an "outcome unknown" error rather than success — never risk deleting a file that may have committed, never report unverified success — cost if wrong: an orphan draft that the own-draft rule cleans up on the next upload of that name.
Ruling: fake's "draft ownership by ClientUID" is satisfied by reporting the owner in conflict details; it does not gate block/commit/delete routes — the real server's rule is unknown — cost: a client deleting another client's draft would not be caught by the fake.
Task 11: note for Task 14: draft_client_uid config key must be generated and persisted or the own-draft rule never fires
Task 11: minor (deferred): an own leftover draft REVISION on an active file is never deleted (safe DELETE of ConflictDraftRevisionID possible)
Task 11: minor (deferred): two fault tests print an expected ERROR log line
Task 11: note: unconfirmed real-server codes for bad verifier / missing block / stale CurrentRevisionID; delete_multiple on an active link (docs/TODO.md)
Owner observation 2026-11-11 (web app, free account): folder rclone-live-probe-20261111-130916 is visible with empty.bin, five-mib.bin and hello.txt => official web client decrypts names written by the new code. Content open and signature-warning status not yet reported.
Owner observation 2026-11-11 (web app, free account): hello.txt opens and shows "hello again - second revision" => official web client decrypts content written by the new code and sees the second revision as active. Signature-warning status not yet reported.
Owner observation 2026-11-11 (web app details for hello.txt, free account): "Digital signature verified. This file was securely uploaded by <account email>"; original size 30 bytes; Modified May 6 2024 9:08 AM local (= 07:08:09 UTC as set); SHA1 shown; media type application/octet-stream. => official web client verifies signatures and reads xattrs written by the new code. Open: reverse direction (official-client-created files read by the new code), paid account.
Live probe 2026-11-11 (controller, scratch worktree at 9a4ed61a8, removed afterwards), FREE account, read-only: listed the probe folder; folder "my-little-folder" created by the WEB APP listed; file "Readme.txt" (66 B, text/plain) uploaded by the WEB APP inside it downloaded with manifest verification, SHA1 matches the web app's claimed digest, no signature errors logged. => reverse direction (official web client -> new code) works for folder, name, content, xattrs. Open: paid account, official CLI, full test_all after Task 14.
Task 11: fix round 1/5 (6 addressed, 1 new open — "not active" treated as "not committed" deletes a committed file when a third revision/trash intervenes; commits 9a4ed61..1f1b2bb)
Ruling: after a failed commit, delete a draft only on positive proof it never committed (new file: node still in draft state; new revision: active revision still the one we started from); any other observation => delete nothing, errUploadOutcomeUnknown — a wrong delete is permanent data loss, a leftover draft is not — cost if wrong: occasional orphan drafts.
Task 11: fix round 2/5 (1 addressed, 0 open — delete only a proven uncommitted draft; commits 1f1b2bb..dc7d977)
Task 11: minor (deferred): errUploadSuperseded message misleading when the node reads back draft/trashed
Task 11: minor (deferred): if another client "restores" the starting revision keeping its ID after our commit landed, cleanup deletes our superseded history entry (no live content lost; real restore semantics unverified)
Task 11: complete (commits e8b1359..dc7d977, review clean)
Flake hunt 2026-11-11 (controller, 8 x `go test -race ./backend/protondrive/` at e76b6783b): TestClientBootstrapCreateVolumeRace failed 1/8 (Task 8 code). To be diagnosed by a dedicated dispatch after Task 12's fix round.
Task 12: minor (deferred): no RED runs for the two crypto tests (mutation checks only reported); signTimeOf uses the primary key's creation time only
Task 12: fix round 1/5 (1 addressed, 0 open — v6 fixture provenance comment; commits e76b678..e65c785)
Task 12: complete (commits dc7d977..e65c785, review clean)
Flake dispatch: opus agent diagnosing TestClientBootstrapCreateVolumeRace (report: flake-volume-race-report.md); Task 13 waits for it.
## Flake: TestClientBootstrapCreateVolumeRace
- Fixed in e23de0b58 (opus). Root cause: test timing assumption (unlockMu serialises loadAccount, so second client often saw the created volume); client correct. Fake gained `Gather` fault option; fake data race on shared address key fixed (per-check key copy). 200/200 at -race.
- Concerns carried: (1) TestUploadShortSource requireNoLeaks failed once under heavy load (whole-process goroutine count, 2s allowance) — deferred to final review; (2) test takes ~2.3s; (3) loser of a real-server 2500 race assumes volume readable at once — no retry, fails cleanly.
- Review dispatched (sonnet). Task 13 BASE = e23de0b58.
- Flake fix review (sonnet): APPROVED. Deferred minors for final review: test's 2s wait is wasted time (Gather already forces ordering; start both clients together); no direct assertion that one POST got 2500; Gather counts abandoned requests toward Times; `Copy()` of the address key still reads the shared key. Flake: complete.
- Task 13 dispatched (opus), BASE e23de0b58.
Task 13: complete — 414de88c9. Review (sonnet): spec ✅, quality approved. Deferred minors: protonfake main_test leaks run goroutine if a require fails (close stop in t.Cleanup). Carry to Task 14: client_salted_key_pass in config is base64(StdEncoding) of raw key password — config store decodes on load, encodes on save; keep the `-remote` skip guard in TestIntegrationFake; record protontest in docs/ARCHITECTURE.md.
- Task 14 dispatched (opus), BASE 414de88c9.
- Task 14 implemented: 0ec7dce0e (DONE_WITH_CONCERNS). Contract suite vs fake: 126 subtests, 97 pass, 29 feature-skips, no Opt opt-outs.
- Ruling: leave github.com/rclone/Proton-API-Bridge in go.mod until Task 19 — plan assigns removal there; costs nothing if wrong (unused requirement for a few commits).
- Ruling: browser sign-in is default only when username+password are not both set — a scripted `config create` with credentials must not block 10 min on a browser poll; if wrong, users with saved credentials see password sign-in offered first.
- Review dispatched (opus).
- Task 14 review (opus): spec ❌ (Rmdir), 3 Important: Rmdir trashes folders with unlisted children; Config always forces new sign-in; Shutdown leaves unusable client. Review file task-14-review.md.
- Ruling: Shutdown must leave the Fs usable (no key clearing; drop caches/stop background only); key clearing only on Disconnect — supersedes brief's "Shutdown calls client.close once" because fs cache shuts down expired entries still held by rc jobs; cost if wrong: keys stay in memory until process exit.
- Task 14 fix round 1 dispatched (resume implementer), BASE 0ec7dce0e. Includes minors: inactive-node refusal, range maths with original_file_size=false, XXX comment, TestTwoRemotesIsolated refresh.
- Task 14 deferred minors: cross-account Move/Copy/DirMove not refused; env-shared tokens across two processes undocumented; cross-Fs invalidation race with in-flight listing; no reconnect hint on wrong key password / browser hint on CAPTCHA; code 2000 fatal mapping (plan-mandated).
- Task 14 fix round 1: c00162ed2. Re-review dispatched (sonnet).
Task 14: complete — 0ec7dce0e, c00162ed2. Re-review (sonnet): ALL CLOSED, no Critical/Important. Deferred minors (fix round): Rmdir refuses a folder holding only a leftover draft/trashed child if the real server lists them (check live in Task 21); countContent re-downloads on each suffix-range open of a size-less file; a call in flight during Disconnect may get an unclear error rather than errNotSignedIn (session.go ~215-225); Shutdown test may not fail against old key-clearing code.
- Task 15 dispatched (opus), BASE c00162ed2.
- Live smoke (free account, binary built from c00162ed2 in scratch worktree scratchpad/live-c00162ed2): password sign-in saved client_* to config; lsd/lsl/cat/sha1sum/about correct on the earlier probe folder (hello.txt SHA1 matches web app); write round-trip in rclone-smoke-task14: sync up 3 files (9 MB, 0 B, text), check 0 differences, second revision, moveto, copyto (server-side), sync back byte-identical, rmdir of non-empty refused, purge OK. Note for final review: ModTime returned with ms while Precision is time.Second.
- Task 15 implemented: 6e2410de6, 14128d59c, 860081538 (DONE_WITH_CONCERNS). Backend fixes: CAPTCHA error names real remote + config falls back to method choice; signature-failure error hints ignore_signature_errors. Concerns: ManyNewFs can't prove unlock serialisation black-box (internal TestLoadAccountSerialised covers); errNotSignedIn says literal `remote:` (handed to Task 16). Review dispatched (sonnet).
- Task 16 dispatched (opus), BASE 860081538.
Task 15: complete — 6e2410de6, 14128d59c, 860081538. Review (sonnet): spec ✅ (ManyNewFs split with internal test accepted), quality approved. Deferred minors (regression_test.go): BadSignatureFile `Contains "signature"` satisfied by hint text alone (:623); MultiThreadDownload evidence indirect (:719); DocsNodeSkipped doesn't assert node still on server (:587); QuotaExceeded uses generic fault message (:788); TwoProcessRefreshRace 500ms delay vs adoptWait margin (:464); FileOverDirectory no non-retryable assert.
- Task 16 implemented: 606580196 (real bug: file with bad node-passphrase signature was readable), 59f0f31b3, db97ef7c8 (401 during Disconnect retried with empty auth), f4105adfc. DONE_WITH_CONCERNS: manifest checked only at EOF so swapped/rolled-back same-size blocks reach a streaming reader before the error; --dump headers shows pm-storage-token/x-pm-uid; export_test.go added. Review dispatched (opus).
- Task 16 review (opus): spec ❌, 2 Important: forged folder key + anonymous child bypasses owner-signature refusal (nodes.go ~446-488); tests encode manifest-at-EOF gap vs spec 9.6. Review file task-16-review.md.
- Ruling: children inherit an unverified parent key's error; creating anything inside an unverified-key folder is refused unless ignore_signature_errors — names/passphrases would be encrypted to a key the server may hold; cost if wrong: a legitimately odd folder needs the flag to write into.
- Ruling: full reads verify the signed manifest over the listed block hashes BEFORE releasing any byte (spec 6.4/9.6 updated to match); ranged reads deferred (TODO: bounded per-revision verified-hash cache); sequential paging accepted (first-byte latency ~1 request per 80 MiB). Whole-revision rollback under current revision ID is a protocol limit (SDK can't detect either) — document. Cost if wrong: slower first byte on very large full reads.
- Task 16 fix round 1 dispatched (resume implementer), BASE f4105adfc. Deferred minors: parentless directly-shared file refused; on-the-fly remote hint names bare backend; DECISIONS entry placement.
- Task 16 fix round 1: 88654eb04, 9598e22d9, 248f46705, 751a3ea35. Re-review dispatched (opus). Task 17 dispatched (opus), BASE 751a3ea35.
- Task 16 re-review (opus): NOT ALL CLOSED. B, C1–C5 closed. A open: move/copy/DirMove OUT of a forged folder launders it (nodes.go:1086-1092, encryptForNewParent). New Important 2: forged key signed under a foreign email whose key the server serves passes (account.go:254-281; share CreatorEmail nodes.go:333) — protocol limit, but cheap partial fix. Minors: `bytes=0-` treated as ranged so skips manifest check (protondrive.go:1150); listing-phase memory cap from unsigned EncryptedSize (download.go:127-130); shareKeys.unverified unused; TestPasswordStoredObscured leaves configfile installed. File: task-16-rereview.md.
- Ruling: refuse move/copy/DirMove whose SOURCE has unverified keys unless ignore_signature_errors. Require My Files share creator and root folder signer to be one of the user's own addresses, else root is unverified. Foreign-signer trust (server-served public keys) documented as protocol limit (SDK same). Accept that a folder signed by a deleted own address refuses writes without the flag — error must say so plainly; cost if wrong: such users must set the flag.
- Task 16 fix round 2 waits for Task 17 implementer to finish (same tree).
- Task 17 implemented: 3fac25de7 (parent listing no longer dropped after each create — request storm), 32710eefd (decompression limits on metadata), 90b2a48e7 (skip nodes with empty ID/negative size), 12ce57ed3 (budgets + fuzz + TestMain legacy-route check). DONE_WITH_CONCERNS: NewObject budget is 8 not 7 (4 folders × 2, no lookup-by-name route); block-size sum not checked at parse; API error messages unbounded; TODO items logged; race suite ~300s. Review dispatched (opus).
- Ruling: accept NewObject budget 8 — plan's 7 miscounted the root level; no cost.
- USER (2026-11-11): finish Task 16 (fix round 2 + re-review) and Task 17 (review + fixes), then wind down: write handoff into committed docs so nothing depends on this conversation, then push to main. Do NOT start Task 18.
- Task 16 fix round 2 dispatched (resume implementer), BASE 12ce57ed3.
- Task 17 review (opus): spec ✅; 1 Important (reproduced): a listing that started before an `add` can overwrite the cached listing without the new child, and the stale listing now persists (cache.go:96-110, nodes.go:779) — fix with per-folder generation counter; Minors: negative xattr Size with BlockSizes lists as 0 (crypto.go:835, nodes.go:134-138); FuzzValidateName lacks UTF-8 assert; legacy tally map unbounded; WarmList re-seeds 251 files (~27s). File: task-17-review.md.
- Task 17 fix round 1 waits for Task 16 fix round 2 (same tree).
- Task 16 fix round 2: 787bb3b3f, cb6e6759b, 58fde59f3, 202b64cf1, 2a1a8f046, e7e43095d. Broader than ruled: EVERY node beneath an unverified folder is unverified (not only anonymous ones), reads included. Block cap 2^19 (2 TiB). Root of trust: share creator + root signer must be own address — needs live confirmation. Re-review 2 dispatched (opus). Task 17 fix round 1 dispatched (resume implementer), BASE e7e43095d.
- Live check 2026-11-11 (free account, binary at e7e43095d in scratch worktree scratchpad/live-e7e43095d): with root-of-trust + subtree-distrust + manifest-first in place, no flag needed: lsl, sha1sum, full read of web-app-created Readme.txt (verified), copyto upload, `Range: bytes=0-` read, purge all OK. NB this drive's volume was created by this client; a drive created by Proton's own clients (paid account, never signed in) is still unchecked for the root-of-trust rule.
- Task 16 re-review 2 (opus): NOT ALL CLOSED. Items 1,3,4,5 closed. CRITICAL: own-address check is case-insensitive but key lookup exact (account.go:246-253 vs :235-243,:267) — server sets CreatorEmail to a case variant of the user's address, keys then fetched from server => forged root trusted (probe-confirmed). Important: with ignore_signature_errors set, laundering by move/copy is silent (spec 6.4 promises a logged ERROR). Minors: parent cycle A→B→A recurses unbounded (nodes.go:667,:619); block cap bounds count not bytes (~250-350 MiB; no response-body limit); whole-file range with unknown size / understated signed size. Proportionality judgement: keep subtree-wide distrust for tier 1; count failures live in Task 21; if they occur add a narrower option. File: task-16-rereview2.md.
- Ruling: keep "every node beneath an unverified folder is unverified, reads included" — reviewer judged it technically justified (forged parent key exposes child keys; ranged reads don't check manifest); cost if wrong: users with a legacy folder signed by a removed address need the flag for that subtree.
- Task 16 fix round 3 (critical case-fold, ERROR log with flag, parent-cycle depth limit, TODO for response-body limit and range edge) waits for Task 17 fix round 1 (same tree).
- Task 17 fix round 1: f89be42ea (listing generation count; fake Hold/Held fault), 18b4f7d1d (size from block sizes when Size invalid), 16480e3d8 (refuse non-UTF-8 names on write — real gap found by fuzz assert), c28077174 (warm list folded into TestBudgetList). Re-review dispatched (sonnet). Task 16 fix round 3 dispatched (resume implementer), BASE c28077174.
Task 17: complete — 3fac25de7, 32710eefd, 90b2a48e7, 12ce57ed3, f89be42ea, 18b4f7d1d, 16480e3d8, c28077174. Re-review (sonnet): ALL CLOSED (compare mutation-checked). Deferred minors: TestListingRacingChange can hang until package timeout if it fails before close(release) (add t.Cleanup + timeout on <-held); xattr `"Size":null` treated as size 0, skipping the block-size fallback (crypto.go ~840); legacy-route tally map unbounded in protonfake; report's 8 TiB/16 MiB claim was wrong (code comment right); API error messages unbounded/unsanitised; block-size sum vs size not checked at parse; "invalidate of uncached node bumps all in-progress listings" costs requests only.
- Task 16 fix round 3: 15bc60ead (ASCII-only case rule, own addresses never looked up on server), 52a44e41c (ERROR per unverified node+operation with the flag), 8666b6603 (parent cycle / depth 2048), 0cd3af429 (whole-file range returns only requested bytes), 9c90998f7 (docs: ~250-350 MiB at cap). Re-review 3 dispatched (opus).
Task 16: complete — 606580196, 59f0f31b3, db97ef7c8, f4105adfc, 88654eb04, 9598e22d9, 248f46705, 751a3ea35, 787bb3b3f, cb6e6759b, 58fde59f3, 202b64cf1, 2a1a8f046, e7e43095d, 15bc60ead, 52a44e41c, 8666b6603, 0cd3af429, 9c90998f7 (3 fix rounds). Re-review 3 (opus): ALL CLOSED, trust-root bypass probes failed (address-key injection x4 refused). Deferred minors: Update of an unverified file with the flag logs "reading" not "updating" (upload.go:306 → download.go:196-200); `below` slice copied per level (~33 MB transient at depth 2048); depth-limit error ~88 KB because each level re-wraps (nodes.go:533,:546); relabelled-empty-email quirk (not exploitable); non-ASCII case variants of an own address count as foreign (check live); with the flag on a long mount, one ERROR per unverified node+operation and the `reported` map grows; ranged reads still unverified against the manifest; parentless directly-shared file refused; on-the-fly remote hint names bare backend.
- Live check 2026-11-11 (free account, binary at 9c90998f7, scratch worktree): sync up 3 files, check 0 differences, multi-thread download (4 streams) byte-identical, verified full read of web-app file, server-side moveto, purge — no flag needed.
## PAUSED 2026-11-11 at 9c90998f7 (owner's instruction)
Tasks 1–17 complete and reviewed. Tasks 18–21 not started. Final whole-branch review not run. Handoff: docs/plans/001-protondrive-tier1-HANDOFF.md. This ledger is copied to docs/plans/001-protondrive-tier1-workspace/progress.md.
## RESUMED 2026-11-12 (owner: "Continue work from HANDOFF") on branch protondrive-tier1 at d211b45e6
- Ruling: Task 18 writes .gitea/workflows/ci.yml even though the runner is unknown (plan says so; unverifiable locally) — cost if wrong: an unused file.
- Ruling: Task 18 implementer decides the shared-session design for protonfake under test_all after checking token expiry/Disconnect; any change stays inside protontest — cost if wrong: flaky wider suites, visible at once.
- Task 18 dispatched (opus), BASE d211b45e6.
- 2026-11-12: owner stopped the session to switch the controller to Opus. Task 18 implementer stopped mid-task, no commits, no report. Uncommitted work left in the tree (init.d/TestProtonDriveFake, .gitea/, PORTS.md, test_all config.yaml, docs/{ARCHITECTURE,DECISIONS,PERMISSIONS,TODO}.md); tracked-file diff saved as task-18-partial.diff. Unreviewed: next controller should inspect it, then resume Task 18 from it or discard it, and re-dispatch. A stray local protonfake process was killed.
- 2026-11-12: controller now Opus 5.5. Task 18 implementer resumed (same agent, context intact) from its uncommitted work.
- OWNER ANSWERS 2026-11-12: (1) Gitea HAS an Actions runner, but it is slow and not very capable — keep ci.yml light. (2) Task 21 may install a Proton desktop/CLI app and use the web app, preferably inside a Docker container (docker available on host). (3) YES to removing the test-account usernames from git history with a force push. Only 901d7fd5b adds them (85f6b2f0e removes them). Plan: after Task 18 commits land and the implementer is idle, rewrite from 901d7fd5b (redact usernames in that commit only, replay the rest), force-push master once, record in DECISIONS.md and handoff.
- Task 18 implemented: 28f1aaf8a. DONE_WITH_CONCERNS: 5 compare-dest/copy-dest/checksum tests ignored (per-Fs listing cache; a second Fs on same account doesn't see the first's writes); gitannex ignored (test-harness env-remote limitation); .claude/settings.json not edited (owner must add golangci-lint allow — agent cannot widen own perms); shared session OK (no 401 in standalone fake). Review dispatched (sonnet).
- Task 18 review (sonnet): spec ✅, quality approved, no Critical/Important. Cache diagnosis confirmed; init.d lifecycle checked (no orphans, build failure and busy port both rc=1). Minors (deferred): stop has no SIGKILL escalation; shellcheck SC2034 unused `i`; ci.yml lacks concurrency group; golangci-lint 2.15.0 build-Go vs go 1.26 may fail on first Gitea run. File: task-18-review.md.
- Ruling: keep the 5 ignored cross-Fs-cache tests and leave the fix for M5 (events) — shared per-account cache is forbidden package state, re-list-on-miss breaks budgets, TTL doesn't fix the tests; added mount-staleness note to TODO; flagged as owner question 5 in handoff — cost if wrong: stale --compare-dest/--copy-dest only when the reference is written by another Fs in the same process; mounts never see external changes until M5.
Task 18: complete — 28f1aaf8a (rewritten as 5afbe9e4f). Review: approved, minors deferred.
- HISTORY REWRITE 2026-11-12 (owner approved): 901d7fd5b→31f6aafd9 (usernames redacted), 85f6b2f0e→4282540e0, d211b45e6→65840bdc5, 28f1aaf8a→5afbe9e4f; final tree identical. Gitea master force-pushed 85f6b2f0e→4282540e0 with lease. Local master at 65840bdc5 (not pushed beyond what remote had). Recorded in DECISIONS.md and handoff.
- Task 19 dispatched (opus), BASE b667a1ab0.
- Owner added Bash(golangci-lint run:*) to .claude/settings.json allow list (2026-11-12). After Task 19 lands: remove that item from docs/TODO.md and the PERMISSIONS.md Known gaps, commit with the settings change.
- Task 19 implemented: cf7577dc1. DONE_WITH_CONCERNS: binary 152,460,576 -> 148,995,400; tidy removals only (2 libs, iso8601, 8 indirect); 29 cross targets OK; whole repo 186 pass, cmd/bisync TestRcBackupDir fails pre-existing (also at 06700aaf2); differential coverage lost (multi-block/empty old-lib files, v1.76 reading new writes). Review dispatched (sonnet).
- Committed owner's golangci-lint allow rule + doc cleanup. Found: deny rule 'git push --force-with-lease:*' does not match '--force-with-lease=...' — logged in PERMISSIONS Known gaps; proposed fix to owner.
- Task 19 review (sonnet): spec ✅, quality approved, no Critical/Important. Minor: handoff table stale (fixed). File: task-19-review.md.
- Ruling: do not generate multi-block/downgrade fixtures now — b667a1ab0 keeps the differential tests and libraries reachable in history, so it stays reproducible; noted in TODO — cost if wrong: a later agent spends an hour building fixtures from a worktree.
Task 19: complete — cf7577dc1. Review approved.
- Task 20 dispatched (opus), BASE after docs commit.
- Task 21 prep (controller): official Proton Drive CLI 0.9.0 exists (proton.me/download/drive/cli/0.9.0/linux-x64/proton-drive, SHA-512 3533025b...7e91ca8 on the download page). Login is browser-only (`proton-drive auth login` prints a URL) → owner must complete it once per account; run in Docker with PROTON_DRIVE_CREDENTIALS_STORE=unsafe_file in a throwaway volume. Commands: `filesystem list|upload|download --json`. Live rclone runs (TestIntegration, test_all -n 1) done by controller from a scratch worktree; v1.76 downgrade check uses a COPY of the config (refresh tokens single-use; new code falls back to password if the real config's token dies).
- Task 20 implemented: 7be50f9e2. DONE_WITH_CONCERNS: generated options block stale (needs make backenddocs before upstream; logged); browser sign-in documented as primary but unproven live; docs follow code where spec differs (name rules); SDK ref tag question; ARCHITECTURE rough-edges line kept; about = account usage unverified live. Review dispatched (sonnet).
- Task 21 prep: official CLI 0.9.0 downloaded, SHA-512 verified, image proton-drive-cli:0.9.0 built (debian:trixie-slim, non-root, unsafe_file store). Dockerfile in controller scratchpad proton-cli/. Next: owner completes 'auth login' URL for the free account.
- Task 20 review (sonnet): spec ✅, quality needs fixes. Important: (1) docs say a ranged read of a file without recorded block sizes "may fail"; code FIXME (download.go:142-145) says it returns wrong bytes, and the check at download.go:~614 only fires on the 2nd block → single-block range silently wrong. (2) SDK version cited as tag js/v0.23.0 at 05c9115; cite commit hash + CHANGELOG entry. Minor: enable_caching Help vs page. File: task-20-review.md.
- Ruling: fix (1) in code, not just docs — silent wrong bytes is a correctness bug for files from other clients. When block sizes are unrecorded and the range starts after block 0, read from the start of the file and discard (or another approach that is correct and checked), accepting the cost on such files; regression test with the fake writing non-4MiB blocks without sizes; docs then describe the cost. Cost if wrong: slower ranged reads (mount) on old-client files only.
- Task 20 fix round 1 dispatched (resume implementer), BASE 7be50f9e2.
- Task 20 fix round 1: 22b9c4fdc (ranged reads of unrecorded-size files read from block 0 and discard; regression TestDownloadRangesUnrecordedBlockSizes + fake SeedFileInBlocks; SDK cited by commit; enable_caching help aligned). Re-review dispatched (opus).
- Task 20 re-review (opus): ALL CLOSED (mutation-checked: 7/9 subtests fail on old download.go). Minors: package comment tag wording (fixed by controller), spec 167-170 same tag wording (left: spec history), TestDownloadEndlessBlockList "assumed" name, TestDownloadRanges bound always 3, two unclear protontest/drive.go comments (deferred), perf TODO added by controller.
Task 20: complete — 7be50f9e2, 22b9c4fdc, + controller follow-up commit. Review approved after 1 fix round.
- Task 21 live 2026-11-12 (free account, binary 502a32b5f, scratch worktree live-502a32b5f): TestIntegration -remote TestProtonDrive: PASS (98 pass, 29 skip, 0 fail, 120 s; only expected "directory not found" errors; no leftovers).
- Task 21 downgrade check vs released rclone v1.76.0 (downloads.rclone.org, SHA256SUMS verified; run with a COPY of the config, deleted after): v1.76 downloaded every file the new backend wrote (incl. web-app file) byte-identical; new backend read v1.76's 9 MiB + small uploads byte-identical incl. a mid-file range. Test folder purged.
- Official CLI sign-in #1 timed out (~19 min, owner not yet at it); restarted, new URL given to owner.
- Task 21: test_all -n 1 -maxtries 1 against TestProtonDrive: running (bisync/gitannex ignoretests as configured upstream).
- Official CLI 0.9.0 signed in (free account) by owner via browser fork URL on 3rd try (CLI polls only ~10 min; a late sign-in shows "Invalid code" in the browser).
- Interop 2026-11-12 (free account, binary 502a32b5f, CLI in Docker): CLI downloaded rclone-written probe folder byte-identical, 0 failures; CLI `info` shows keyAuthor/nameAuthor/contentAuthor ok:true for rclone files. CLI uploaded 13 MiB random, small, empty, unicode-named nested file → rclone (no ignore flag) copied all byte-identical, mid-file range OK. rclone rcat new revision + moveto (unicode) + mkdir → CLI lists and downloads correct content, signatures ok. Folder rclone-interop-20261112 left on the account as evidence.
- OWNER 2026-11-12: golden fixtures = option 2 (re-key captured nodes onto test-only keys; nothing from the account committed). Brief task-21g-brief.md written by controller. Ruling: implementer builds capture+rekey+TestGoldenFixtures against the fake only; controller runs the single live capture (web-app folder my-little-folder + CLI folder rclone-interop-20261112) and reviews output before committing fixtures. Task 21g dispatched (opus), BASE 502a32b5f.
- Task 21 live test_all (free account, 502a32b5f, -n 1 -maxtries 1, 46 min): FAIL 8 = the 5 known cross-Fs-cache tests (confirmed live) + 3 live-only: operations TestPurge ("Failed to put A1/B1/C1/one: object not found"), vfs TestDirRemoveAll and TestZipDirsInRoot ("upload committed, but the file now has another revision (revision is active, not X)" — EMPTY active revision). Manual repro of nested put x3: no failure (timing). Logs: scratchpad/live-testall/2026-11-12-104408. backend suite inside test_all: OK.
- Ruling: treat as one real-server read-after-write lag in the post-commit confirmation; fix = retry the confirmation briefly when node not found or active revision empty, distinguish from a genuine other revision; fake gets a lag fault to reproduce. Task 21f (fix) queued after 21g (same tree). Cost if wrong: diagnosis off, re-run live shows it.
- Task 21g implemented: a2dc6f71f. DONE_WITH_CONCERNS: signed SEIPD (names/hash keys/xattrs) cannot stay byte-identical when inner signatures are re-made — decrypted, inner packets kept, sig/OPS re-made, re-encrypted with same session key/cipher/version/AEAD/chunk; unsigned messages and all blocks byte-identical; node passphrases new; block signatures, user key, thumbnailed files left out; untested real packet shapes fail loudly. Review dispatched (opus). Controller live capture into scratchpad (not tree) pending review.
- Task 21f (post-commit read-after-write lag fix) dispatched (opus) in parallel with 21g review, BASE a2dc6f71f.
- Task 21g review (opus): spec ❌. Important: I-1 foreign intended-recipient fingerprint written into Rekeyed note (golden_internal_test.go:1256-1259); I-2 identity() keeps user-ID name part and foreign emails inside armored keys (:898-908); I-3 capture reuses -remote → a bad -run would run TestIntegration (writes/deletes) live (:38-39,:108-128). Packet-level leak check of round trip by reviewer: clean. Capture is read-only on the account. File: task-21g-review.md. Fix round 1 dispatched (resume implementer). Live capture waits.
- Task 21f implemented: af6fdd789. Diagnosis confirmed (both from the single post-commit read: draft/empty revision, or not found). Retry 100ms→2s, 8 reads (~7s). errUploadSuperseded only for non-empty revision that is neither ours nor the starting one. Ruling (accept implementer's choice over my lean): lag beyond retries after successful commit → errUploadUnconfirmed (deletes nothing, not retried at once) rather than success with a made-up node — cost if wrong: a rare spurious error where a later sync pass finds the file. Concerns: lag length unmeasured; activeNode not lag-tolerant; failed-commit stale read can still delete draft (ruling 11 risk now real); cancellation waits up to ~7s. Review dispatched (opus).
- Task 21f review (opus): spec ❌. CRITICAL (pre-existing ruling-11 path, now likely under lag; reproduced in scratch): after a FAILED commit a single stale read (draft / old revision) is taken as proof it didn't land → delete_multiple on the now-active file or DELETE on the now-active revision (upload.go:401-403,425). Pacer resends commit on 5xx/408/network, ctx cancel also leads here. Minors: empty-revision lag state untested; 7s retry ignores Ctrl-C; --retries pass may add an identical revision; two tests not seen failing. File: task-21f-review.md.
- Ruling (adopt reviewer's rule): delete a draft ONLY when the server explicitly refused the commit (4xx with a Proton code) on a single attempt that was not resent; no read-back decides deletion. Every other failure → errUploadOutcomeUnknown, delete nothing. Scratch repro becomes a regression test. Supersedes the "positive proof by read" half of ruling 11. Cost if wrong: orphan drafts accumulate on the server after ambiguous failures (cleaned by CleanUp / server expiry) — acceptable vs data loss.
- Task 21f fix round 1 queued until 21g fix round 1 finishes (shared tree).
- Task 21g fix round 1: 1b2fcbd21 (fail closed on foreign keys/recipients, placeholder user IDs, -capture-remote flag, packet-level leak check also in TestGoldenFixtures, tamper tests). Ruling: accept allowlist of generic node-key emails no-reply@proton.me / noreply@protonmail.com (C# SDK/CLI uses "Drive key <no-reply@proton.me>") — written keys still carry only the placeholder; cost if wrong: none (no account data). Re-review dispatched (opus).
- Task 21f fix round 1 dispatched (resume implementer), BASE 1b2fcbd21.
- Task 21g re-review (opus): NOT ALL CLOSED — I-3 open: TestIntegration ignores -capture/-capture-remote, so a mistyped -run with the real config runs the write/delete suite live. I-1, I-2 (allowlist proviso holds), M-1..M-5 closed; 4 new minors. File: task-21g-rereview.md.
- Ruling: controller makes the trivial I-3 fix (TestIntegration skips when -capture or -capture-remote is set) after 21f lands, then live capture.
- Task 21f fix round 1: be0053607 (delete draft only on single-attempt coded 4xx refusal, attempt counter via pacedCall; all else errUploadOutcomeUnknown; 4 regression tests failed before fix; Ctrl-C honoured post-commit; LagNoRevision tested). Re-review dispatched (opus).
- Controller fixed 21g I-3: f8e398fce TestIntegration skips when -capture/-capture-remote set (verified skip + lint). Task 21g: complete — a2dc6f71f, 1b2fcbd21, f8e398fce. Live capture next.
- Live capture 2026-11-12 by controller (free account, HEAD f8e398fce): web = my-little-folder (Readme.txt, 1 block), cli = rclone-golden-cli-20261112 (empty.bin no block sizes, nine-mib.bin 3 blocks, small.txt). Leak inspection: 11 secret needles (usernames, tokens, uid, salted key pass raw+decoded, both passwords) 0 hits; emails only golden@example.invalid; 0 Proton IDs; key UIDs only placeholder (gpg --show-keys); no 'proton.me'/'stereotypical' bytes anywhere. TestGoldenFixtures PASS offline. Committed 61733fce6 (~9.5 MB, mostly 3 random blocks). Plan Task 21 boxes ticked: capture, interop, upgrade/downgrade.
- Task 21f re-review (opus): ALL CLOSED (resend guard mutation-checked). New minors: N1 --retries pass can reach own-draft delete_multiple in resolveConflict (logged in TODO, b440df2e1); N2 upload godoc wording; N3 no test pins 408 exclusion (deferred).
Task 21f: complete — af6fdd789, be0053607.
- Live test_all re-run dispatched at b440df2e1 (scratch worktree live-b440df2e1).
- Live test_all re-run (free account, b440df2e1, 37.5 min): FAIL only the 5 known cross-Fs-cache tests (TestCheckSumDownload, TestCopyFileCompareDest, TestCopyFileCopyDestImmutable, TestSyncCompareDest, TestSyncCopyDestImmutable). TestPurge, vfs TestDirRemoveAll/TestZipDirsInRoot now PASS — lag fix confirmed live. vfs fully green live. No leftovers. Logs scratchpad/live-testall-2/.
- Paid account FIRST sign-in 2026-11-12 (b440df2e1): password/SRP OK, about = 64 GiB total, 1.152 MiB used; drive empty; mkdir/rmdir OK without ignore_signature_errors. Unknown whether the volume pre-existed (made by Proton apps) or was created now by the client (createVolume logs nothing) — asking owner.
- rclone BROWSER sign-in proven live 2026-11-12 (b440df2e1, scratch config, free account, owner completed fork URL): session saved, listing + verified full read of the web-app file OK. Then `config disconnect` (logout) OK and later use gives the clear reconnect error; scratch config deleted.
- Incident: `rclone config create` printed the new remote's config INCLUDING client_* tokens and salted key pass to stdout; the controller's redaction regex failed, so those values for that one session appeared in the controller's tool output (conversation transcript). Mitigation: that session was logged out immediately (tokens revoked server-side); the salted key pass is derived from the account password and is NOT revoked by logout — throwaway free account; owner informed. TODO: check whether client_* options are marked Sensitive so config create/show redact them.
- Owner: paid account had (probably) never opened Drive before → its volume was most likely created by rclone; root-of-trust on a Proton-app-made drive still unverified (needs an account used only by Proton apps).
Task 21: complete as far as accounts allow — live TestIntegration, test_all (5 known cache fails), browser sign-in, paid sign-in, CLI interop, v1.76 up/downgrade, golden fixtures; commits a2dc6f71f 1b2fcbd21 f8e398fce 61733fce6 af6fdd789 be0053607 b440df2e1 29ad26944. Open: 2FA, CAPTCHA, desktop/mobile clients, Proton-app-made drive root of trust.
- Final whole-branch review next (base 06700aaf2..29ad26944).
- Final whole-branch review dispatched 2026-11-12: 4 parallel opus reviewers (auth/session/Fs, crypto/nodes/cache, upload/download, tests/fake/harness/docs/hygiene) over 06700aaf2..29ad26944; shared brief final-review-common.md; outputs final-review-{auth,crypto,transfer,tests}.md. Then ONE fix wave + scoped re-review.
- Final review results: crypto 2C/3I/8m (C1 unsigned content key packet accepted → Update encrypts to server key, ranged reads forgeable; C2 server-supplied Argon2 params → OOM; I1 unverified hash key used on write; I2 rename reuses unverified name session key; I3 unsigned xattrs trusted); transfer 0C/2I/9m (draft_client_uid not persisted for env remotes; quadratic multi-thread reads of unrecorded-block-size files); auth 0C/2I/11m (2000/2028 fatal "upgrade", real 5003/5001 unmapped; x-pm-drive-requirements not logged); tests 0C/5I/19m (CI race timeout; CI Go 1.26.0 + govulncheck@latest; stale workspace copy; stale handoff §3; upstream recipe includes fork-only protontest dependents).
- Rulings: see final-fix-brief.md (C1 = unverified file like ruling 18/22 → handoff ruling 29; error codes → ruling 30). Fix wave dispatched (opus, one implementer) for code+CI, BASE 29ad26944; controller fixes tests I-3/I-4/I-5 (handoff, workspace copy, spec 13 recipe) in parallel on disjoint files.
- Final fix wave: e95317b52 (C1), db7afafc0 (C2), b3f5685e6 (I1), 67e4cfeb7 (I2), 94eedc942 (I3), 674b538ca (draft uid), 788a711dc (block-size cache 145→39 fetches), 5dbf5b04c (codes), b4a23a31e (requirements header), b5da26169 (CI), 634a2423e (tests honour RCLONE_CONFIG regardless of which tests run — earlier unit tests read the real config file; no Proton contact), 30cfcac41 (refresh retry), d3aee2af1 bfa9b7122 ad3552e2e 944b36005 (minors/docs).
- Rulings: accept deviations — I3 size from verified block sizes (encrypted size would fail every copy); draft uid derived from volume ID (user ID not kept); C2 caps foreign keys only (own keys logged in TODO); 2064 unmapped. Scoped re-review dispatched (opus).
- Live check of final fixes (944b36005, free account, no flag): all files in the 3 evidence folders (written by web app, official CLI, rclone, rclone v1.76) copy + check OK, bytes match original SHA1s; upload 9 MiB, mid-file range, moveto, full read OK; test folder purged. Stricter C1/I1/I3 checks accept real Proton-client files.
- Final re-review (opus): ALL CLOSED; every fix mutation-checked; golden fixtures pass; Argon2 caps above gopenpgp/OpenPGP.js output; RCLONE_CONFIG fix proven with fake HOME. New minors M-a (ranged start from unsigned xattr block sizes), M-b (TestMain applies RCLONE_CONFIG only when set), M-c (draft uid random when section exists but unsaveable) → TODO.
Final whole-branch review: complete. Plan 001 execution complete as far as test accounts allow.
- 2026-11-12: owner said push → protondrive-tier1 fast-forwarded to Gitea master (4282540e0..fbf2d565e); local master updated. First CI run should start on the runner.
- 2026-11-12 owner: "Push. Then continue with the cache tests and/or any deferred tasks."
- Ruling: the 5 cross-Fs cache tests stay ignored — events are spec-limited to ≥30 s polling (Proton floor) so cannot close a ms window; a shared per-account cache is package state the approved spec forbids; real impact limited to --compare-dest/--copy-dest on a folder the same run writes. Next: plan 002 = M5 (events, cache/dircache invalidation, ChangeNotify, Shutdown stops poller; spec 6.2, already approved → no design stop) + a final batch task for deferred minors. Research agent writing .superpowers/sdd/002-research-events.md.
@@ -0,0 +1,176 @@
# Task reviewer instructions
The dispatch message gives BRIEF_FILE, REPORT_FILE, BASE, HEAD and DIFF_FILE. Global constraints: /home/user/rclone/.superpowers/sdd/001-protondrive-tier1/global-constraints.md
You are reviewing one task's implementation: first whether it matches its
requirements, then whether it is well-built. This is a task-scoped gate,
not a merge review — a broad whole-branch review happens separately after
all tasks are complete.
## What Was Requested
Read the task brief: [BRIEF_FILE]
Global constraints from the spec/design that bind this task:
[GLOBAL_CONSTRAINTS]
## What the Implementer Claims They Built
Read the implementer's report: [REPORT_FILE]
## Diff Under Review
**Base:** [BASE_SHA]
**Head:** [HEAD_SHA]
**Diff file:** [DIFF_FILE]
Read the diff file once — it contains the commit list, a stat summary,
and the full diff with surrounding context, and it is your view of the
change. The diff's context lines ARE the changed files: do not Read a
changed file separately unless a hunk you must judge is cut off
mid-function — and say so in your report. Do not re-run git commands.
If the diff file is missing, fetch the diff yourself:
`git diff --stat [BASE_SHA]..[HEAD_SHA]` and `git diff [BASE_SHA]..[HEAD_SHA]`.
Do not crawl the broader codebase. Inspect code outside the diff only
to evaluate a concrete risk you can name — one focused check per named
risk, and name both the risk and what you checked in your report.
Cross-cutting changes are legitimate named risks: if the diff changes
lock ordering, a function or API contract, or shared mutable state,
checking the call sites is the right method.
Your review is read-only on this checkout. Do not mutate the working
tree, the index, HEAD, or branch state in any way.
## You Do Not Dispatch Subagents
Do all of this review yourself. Never spawn a subagent to review part
of the diff, and never spawn another reviewer for a second opinion.
This process already provides every review seat the work gets; a
reviewer you spawn duplicates one of them at full cost, and its
verdict counts for nothing. If the diff feels too large for one
pass, review it in passes yourself and say so in your report.
## Do Not Trust the Report
Treat the implementer's report as unverified claims about the code. It
may be incomplete, inaccurate, or optimistic. Verify the claims against
the diff. Design rationales in the report are claims too: "left it per
YAGNI," "kept it simple deliberately," or any other justification is the
implementer grading their own work. Judge the code on its merits — a
stated rationale never downgrades a finding's severity.
## Tests
The implementer already ran the tests and reported results with TDD
evidence for exactly this code. Do not re-run the suite to confirm their
report. Run a test only when reading the code raises a specific doubt
that no existing run answers — and then a focused test, never a
package-wide suite, race detector run, or repeated/high-count loop. If
heavy validation seems warranted, recommend it in your report instead of
running it. If you cannot run commands in this environment, name the
test you would run.
Warnings or other noise in the implementer's reported test output are
findings — test output should be pristine.
Evidence you cannot see is not evidence that doesn't exist. If the
report or its test evidence looks truncated, or you cannot locate the
results it claims, re-read the file at its stated path — and if it is
genuinely missing or garbled, report that as a gap for the controller.
Re-running the suite to regenerate what you failed to read is not
verification; illegibility of the evidence is not invalidation of it.
## Part 1: Spec Compliance
Compare the diff against What Was Requested:
- **Missing:** requirements they skipped, missed, or claimed without
implementing
- **Extra:** features that weren't requested, over-engineering, unneeded
"nice to haves"
- **Misunderstood:** right feature built the wrong way, wrong problem
solved
If the brief lists several files each with its own change (a batched
dispatch), check the diff against that list file by file: every listed
file must have its corresponding hunk. A listed file the diff never
touches is a Missing finding, no matter how clean the rest of the
batch looks.
If a requirement cannot be verified from this diff alone (it lives in
unchanged code or spans tasks), report it as a ⚠️ item instead of
broadening your search.
## Part 2: Code Quality
**Code quality:**
- Clean separation of concerns?
- Proper error handling?
- DRY without premature abstraction?
- Edge cases handled?
**Tests:**
- Do the new and changed tests verify real behavior, not mocks?
- Are the task's edge cases covered?
**Structure:**
- Does each file have one clear responsibility with a well-defined interface?
- Are units decomposed so they can be understood and tested independently?
- Is the implementation following the file structure from the plan?
- Did this change create new files that are already large, or
significantly grow existing files? (Don't flag pre-existing file
sizes — focus on what this change contributed.)
Your report should point at evidence: file:line references for every
finding and for any check you would otherwise answer with a bare
"yes." A tight report that cites lines gives the controller everything
it needs.
Your final message is the report itself: begin directly with the
spec-compliance verdict. Every line is a verdict, a finding with
file:line, or a check you ran — no preamble, no process narration,
no closing summary.
## Calibration
Categorize issues by actual severity. Not everything is Critical.
Important means this task cannot be trusted until it is fixed: incorrect
or fragile behavior, a missed requirement, or maintainability damage you
would block a merge over — verbatim duplication of a logic block,
swallowed errors, tests that assert nothing. "Coverage could be broader"
and polish suggestions are Minor.
If the plan or brief explicitly mandates something this rubric calls a
defect (a test that asserts nothing, verbatim duplication of a logic
block), that IS a finding — report it as Important, labeled
plan-mandated. The plan's authorship does not grade its own work; the
human decides.
Acknowledge what was done well before listing issues — accurate praise
helps the implementer trust the rest of the feedback.
## Output Format
### Spec Compliance
- ✅ Spec compliant | ❌ Issues found: [what's missing/extra/misunderstood,
with file:line references]
- ⚠️ Cannot verify from diff: [requirements you could not verify from the
diff alone, and what the controller should check — report alongside the
✅/❌ verdict for everything you could verify]
### Strengths
[What's well done? Be specific.]
### Issues
#### Critical (Must Fix)
#### Important (Should Fix)
#### Minor (Nice to Have)
For each issue: file:line, what's wrong, why it matters, how to fix
(if not obvious).
### Assessment
**Task quality:** [Approved | Needs fixes]
**Reasoning:** [1-2 sentence technical assessment]
@@ -0,0 +1,67 @@
### Task 1: Fake server skeleton and API error type
**Files:**
- Create: `backend/protondrive/api/types.go`
- Create: `backend/protondrive/protontest/server.go`
- Create: `backend/protondrive/protontest/server_test.go`
**Interfaces:**
- Produces (`api`):
- `type Error struct { Status int; Code int; Message string; Details json.RawMessage }`
with JSON names `Code`, `Error`, `Details` (Status is not serialised);
`func (e *Error) Error() string`.
- Code constants from `client/js/src/internal/apiService/errorCodes.ts`,
named `Code<Name>`; at least `CodeOK = 1000`, `CodeOKMany = 1001`,
`CodeOKAsync = 1002`, `CodeInvalidRequirements = 2000`,
`CodeAlreadyExists = 2500`, `CodeNotExists = 2501`,
`CodeAppVersionBad = 2028`, `CodeHumanVerification = 9001`,
`CodeRefreshTokenInvalid = 10013`.
- Produces (`protontest`):
- `func New(t testing.TB) *Server` - starts an `httptest.Server`, closes
it in `t.Cleanup`.
- `func (s *Server) URL() string`
- `func (s *Server) Client() *http.Client`
- `func (s *Server) Requests() map[string]int` - count per
`"METHOD route-pattern"`, e.g. `"GET /drive/v2/volumes/{volumeID}/folders/{linkID}/children"`.
- `func (s *Server) ResetRequests()`
- `type Fault struct { Method, Pattern string; Skip, Times int; Status, Code int; RetryAfter int; Drop bool; Delay time.Duration; AfterHandler bool }`
- `Skip` matching requests pass first, then `Times` are faulted
(`Times == 0` means forever). `Drop` closes the connection.
`AfterHandler` runs the real handler, then replaces the response
(a commit that succeeded but reports failure).
- `func (s *Server) Inject(f Fault)`
- `func (s *Server) SetClockSkew(d time.Duration)` - shifts the `Date`
header.
- `func (s *Server) handle(method, pattern string, h http.HandlerFunc)`
- unexported registration used by later tasks; uses Go 1.22+
`http.ServeMux` patterns.
- `func writeJSON(w, status int, v any)` and
`func writeError(w, status, code int, msg string)` helpers.
- [ ] **Step 1: Write failing tests** in `server_test.go`:
- `TestLegacyRoutesGone`: `GET`/`POST` on
`/drive/shares/abc/links/def`, `/drive/shares/abc/folders/def/children`,
`/drive/shares/abc/files`, `/drive/shares/abc/events/latest` each return
HTTP 410.
- `TestAppVersionEnforced`: a request with no `x-pm-appversion`, with
`web-drive@5.2.0`, and with `external-drive-rclone@1.77.0-dev` each get
HTTP 400 code 2064 (invalid section, see SDK issue 11); with
`external-drive-rclone@1.77.0-stable`, `...-beta+abc`, `...-alpha` the
request reaches the handler. Any request carrying
`x-pm-drive-sdk-version` gets HTTP 400.
- `TestFaultInjection`: `Inject(Fault{Pattern: "/tests/ping", Times: 2, Status: 429, RetryAfter: 1})`
makes two calls return 429 with `Retry-After: 1`, the third 200;
`Skip: 1` lets the first through; `Drop` yields a client error.
- `TestRequestCounting` and `TestClockSkew`.
- [ ] **Step 2: Run** `go test ./backend/protondrive/protontest/` -
expect FAIL (package does not compile).
- [ ] **Step 3: Implement** `api/types.go` and `protontest/server.go`.
Register `GET /tests/ping` (returns `{"Code":1000}`) for the tests. The
app-version grammar is the regex in the SDK `README.md` ("Usage
Guidelines"), specialised to the name `rclone`.
- [ ] **Step 4: Run** the four verification commands - expect PASS and 0
lint issues.
- [ ] **Step 5: Commit** `protondrive: add fake Proton server skeleton for tests`.
@@ -0,0 +1,91 @@
# Task 1 report: fake server skeleton and API error type
**Status:** DONE. Commit `2e3dbac3a protondrive: add fake Proton server skeleton for tests`.
## What I implemented
`backend/protondrive/api/types.go`
- `Error{Status, Code, Message, Details}`. JSON names are `Code`, `Error` and `Details` (omitempty). Status is `json:"-"`. `(*Error).Error()` returns `"<msg> (Code=<c>, Status=<s>)"`, the same shape as the old go-proton-api.
- Every `ErrorCode` from the SDK's `errorCodes.ts` as `Code<Name>`, plus the brief's non-SDK codes `CodeAppVersionBad=2028`, `CodeHumanVerification=9001` and `CodeRefreshTokenInvalid=10013`. I also added **`CodeInvalidSection = 2064`** (not in the brief's list) because the fake returns it, and added it to the plan's Interfaces block.
`backend/protondrive/protontest/server.go` (normal package, does not import `backend/protondrive`; checked with `go list -deps`)
- `New(t)`, `URL()`, `Client()`, `Requests()` (returns a copy), `ResetRequests()`, `Inject(Fault)`, `SetClockSkew(d)`, plus the unexported `handle(method, pattern, h)`, `writeJSON` and `writeError`. `handle` registers `"METHOD pattern"` on an `http.ServeMux`.
- Every request goes through one front handler, in this order:
1. It finds the route pattern with `mux.Handler(r)`.
2. It counts the request under `"METHOD /pattern"`. Unregistered and legacy paths are counted under their raw path, so a regression still shows up in `Requests()`.
3. It sets `Date` to now plus the clock skew.
4. It rejects the request in this order:
- legacy `/drive/shares/{id}/(links|folders|files|events)` and anything beneath them: **410**. `GET /drive/shares/{id}` itself is not blocked;
- any `x-pm-drive-sdk-version` header: **400**, code 2064;
- an `x-pm-appversion` that fails the grammar: **400**, code 2064;
- an unregistered route: **404** with a JSON `api.Error` body naming the method and path. The body has Code 0 rather than 2501, so a missing route cannot be mistaken for a missing file.
5. It applies faults, then dispatches through `mux.ServeHTTP` so `r.PathValue` works.
- `GET /tests/ping` is registered in `New`, so later tasks' tests can use it too. It returns `{"Code":1000}`.
- How faults behave:
- They are matched on route pattern, plus method when `Method` is set.
- They are only counted against requests that pass validation.
- A request counts against every matching fault, and the first fault that fires is applied.
- `Skip` and `Times` work as in the brief: `Times == 0` means forever.
- `Status == 0` with `Drop` false means delay only, then the real handler runs.
- `Delay` gives up if the request context is cancelled.
- `AfterHandler` runs the real handler into a `ResponseRecorder` and then sends the fault. It can be combined with `Drop`.
- `Drop` hijacks the connection and closes it.
- `RetryAfter` sets the `Retry-After` header.
App-version regex (SDK README "Usage Guidelines", name fixed to `rclone`):
`^external-drive-rclone@[0-9]+\.[0-9]+\.[0-9]+(-(stable|beta|alpha))?(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$`
## Decisions worth a look
1. **The channel is optional in the fake.** The README grammar marks `{channel}` as optional, and the brief says to use that grammar. So `external-drive-rclone@1.77.0` is accepted, and a test asserts it. The global constraint that *our client* always sends a channel is not enforced here. Task 2's own test, which checks that every output matches the regex, does not catch a missing channel either. Making the channel required is a one-character change (drop the `?` after the channel group) if the controller prefers it.
2. **Code 2064** has no source in the SDK checkout; I followed the brief ("invalid section, see SDK issue 11"). The `x-pm-drive-sdk-version` rejection uses the same code.
3. **410 bodies use Code 0** rather than a Proton code, for the same reason as the 404s.
4. **Extra tests** beyond the brief: `TestUnknownRoute`, `TestErrorJSON`, fault subtests for Forever, Method, Delay and AfterHandler, and extra rejected and accepted app versions. `TestErrorJSON` tests `api` from `server_test.go` because the brief lists no `api` test file.
## TDD evidence
RED: `go test ./backend/protondrive/protontest/`
```
backend/protondrive/protontest/server_test.go:11:2: no required module provides package github.com/rclone/rclone/backend/protondrive/api
FAIL github.com/rclone/rclone/backend/protondrive/protontest [setup failed]
```
This failure was expected: the tests were written before `api` and `server.go` existed.
GREEN: `go test -race -count=1 -v ./backend/protondrive/protontest/`
```
--- PASS: TestLegacyRoutesGone
--- PASS: TestUnknownRoute
--- PASS: TestAppVersionEnforced
--- PASS: TestFaultInjection (Times, Skip, Forever, Method, Drop, Delay, AfterHandler)
--- PASS: TestRequestCounting
--- PASS: TestClockSkew
--- PASS: TestErrorJSON
ok github.com/rclone/rclone/backend/protondrive/protontest
```
I also ran the suite with `-race -count=10` to check for flakiness, and it passed.
## Verification commands
- `go build ./backend/protondrive/...`: OK.
- `go test -race ./backend/protondrive/...`: protondrive ok, protontest ok, api has no test files. The old backend's tests still pass.
- `go vet ./backend/protondrive/...`: OK.
- `golangci-lint run ./backend/protondrive/...`: 0 issues.
Test output is clean, with no stray logs.
## Files changed
- `backend/protondrive/api/types.go` (new)
- `backend/protondrive/protontest/server.go` (new)
- `backend/protondrive/protontest/server_test.go` (new)
- `docs/plans/001-protondrive-tier1.md`: Task 1 boxes ticked, `CodeInvalidSection` added to the Interfaces block
## Self-review
- There are no name collisions with the old `protondrive.go`, since these are separate packages.
- No package-level mutable state. The two package-level vars are compiled regexes that are never written.
- A failed `Drop` hijack panics inside the handler. `net/http` recovers it and closes the connection, so the client still sees an error. That can only happen with HTTP/2, which `httptest.NewServer` does not use.
## Concerns
None blocking. Decision 1 (optional channel) is the one the controller may want to reverse.
@@ -0,0 +1,51 @@
### Task 10: Download
**Files:**
- Create: `backend/protondrive/download.go`, `backend/protondrive/download_internal_test.go`
**Interfaces:**
- Produces:
- `func (c *client) open(ctx context.Context, n *node, offset, limit int64) (io.ReadCloser, error)`
- `limit < 0` means to the end. Block `i` covers plaintext
`[sum(BlockSizes[:i]), ...)`; when `BlockSizes` is absent assume 4 MiB
for all but the last. Fetches block metadata 20 per page starting at the
first needed block, downloads through `storagePacer` holding a download
token, checks SHA-256 of the encrypted bytes against the server's
`Hash` before decrypting (mismatch -> retryable `errBlockHash`, 3
attempts, re-requesting the URL), decrypts, discards the bytes before
`offset`. On a read that starts at 0 and reaches EOF with `limit < 0`,
verifies the manifest signature; failure returns `errIntegrity` from
the final `Read` unless `IgnoreSignatureErrors`, in which case it logs
at `ERROR`. A storage 404 re-requests the block URL once.
- `var errIntegrity`, `var errBlockHash`
- `Close` cancels in-flight fetches and returns pool buffers; safe to
call twice.
Reference: `client/js/src/internal/download/` (`fileDownloader.ts`,
`apiService.ts`, `cryptoService.ts`).
- [ ] **Step 1: Write failing tests**:
- `TestDownloadSizes` (Review Focus 1): sizes 0, 1, 4 MiB - 1, 4 MiB,
4 MiB + 1, 8 MiB, 9 MiB + 123 round-trip byte-exact.
- `TestDownloadRanges`: for the 9 MiB + 123 file, table of
`(offset, limit)` including `(0, 1)`, `(4<<20 - 1, 2)`, `(4<<20, -1)`,
`(size - 1, -1)`, `(size, -1)` (empty), `(8<<20, 100)`; assert bytes
and that only the needed blocks were fetched (request counts).
- `TestDownloadLegacyFile`.
- `TestDownloadBlockHashMismatch` (`Corrupt "block-hash"`): error wraps
`errBlockHash`, no plaintext from that block is returned.
- `TestDownloadSwappedBlocks`, `TestDownloadTruncatedBlocks`,
`TestDownloadBadManifest`: full read ends in `errIntegrity`; with
`IgnoreSignatureErrors` the bad-manifest case returns the bytes.
- `TestDownloadStorage404Refetch`, `TestDownloadRetries5xx`.
- `TestDownloadCloseEarly`: read 10 bytes of an 8 MiB file, `Close`;
afterwards `runtime.NumGoroutine()` returns to its baseline within 2s
and `pool.Global().InUse()` is back to its starting value.
- `TestDownloadContextCancel`.
- [ ] **Step 2: Run** - expect FAIL.
- [ ] **Step 3: Implement.**
- [ ] **Step 4: Run** the verification commands - expect PASS.
- [ ] **Step 5: Commit** `protondrive: add verified ranged download`.
@@ -0,0 +1,161 @@
# Task 10 report: verified ranged download
**Status:** DONE
**Commit:** 4bcafe5cd protondrive: add verified ranged download
## What was implemented
- `backend/protondrive/download.go` (new)
- `func (c *client) open(ctx, n *node, offset, limit int64) (io.ReadCloser, error)`.
- `errIntegrity` and `errBlockHash`, plus an unexported `errDownloadClosed` that Read returns after Close.
- Block layout: if the xattrs have `BlockSizes`, block `i` starts at `sum(BlockSizes[:i])`. If not, every block is assumed to be 4 MiB. When the sizes are known, the range is clamped to their total. An empty range, such as `(size, -1)` or `limit == 0`, makes no requests.
- Block metadata comes from `GET /drive/v2/volumes/{v}/files/{id}/revisions/{rev}?FromBlockIndex=&PageSize=`.
- It starts at the first needed block, 20 blocks per page.
- For a bounded range the page size is `min(20, blocks still needed)`, so the server issues no URLs that won't be used.
- Block fetch:
- It runs through `storagePacer` while holding a `downloadTokens` token, sending only `pm-storage-token` plus the client's global headers (`x-pm-appversion`, `Accept`).
- It uses `c.s.srv.Call` directly, never `session.call`, so `Authorization` and `x-pm-uid` are never added.
- The encrypted bytes go into a `pool.RW` and are hashed while they stream in.
- Hash mismatch: `errBlockHash`, retried for 3 attempts in all, each with a new URL (`PageSize=1&FromBlockIndex=i`). The expected hash stays the listed one, which the manifest covers.
- A storage 404 re-requests the URL once.
- Decryption streams from the encrypted RW into a plaintext `pool.RW`. Bytes before `offset` are dropped with `Seek`.
- Full read (offset 0, limit < 0):
- Collects the thumbnail hashes from the first page (as the SDK does) and then every block hash, paging until an empty page.
- Checks the manifest against the revision's `SignatureEmail` keys, or the node key when that is empty (anonymous upload). Per the SDK, a missing signature is an integrity failure.
- A failure comes back as `errIntegrity` from the final Read. With `IgnoreSignatureErrors` it is logged at ERROR and the read ends in `io.EOF`.
- A zero-length file still pages once and checks the empty manifest.
- Content key: decrypted lazily in `open`. This follows the SDK's `decryptContentKeyPacket`:
- The signature is checked against the node key plus the node's key-verification keys. A missing signature is fine.
- If it fails and the node was created on or before 2023-01-01, it is checked again against all of the account's address keys.
- If it still fails, `open` returns `errIntegrity`. With `IgnoreSignatureErrors` the failure is logged once per node instead.
- A key packet that won't decrypt is an ordinary error.
- Defences against a hostile server:
- A block is rejected if its `Content-Length` or body exceeds 8 MiB, or if it decrypts to more than 8 MiB (this also stops a decompression bomb). Rejection is not retried.
- With claimed sizes, each decrypted block must match its claimed size.
- Block indexes must be contiguous.
- The list may not run past the claimed count, and with claimed sizes may not end before it. All of these are `errIntegrity`.
- Without claimed sizes, the list is capped at `EncryptedSize/1MiB + 1` blocks, so a block list that never ends gives an error, not an endless loop.
- Pipeline:
- One lister goroutine queues a slot per block on a channel with capacity `downloadAhead = 4`, then starts that block's fetch goroutine.
- The reader takes slots in order, so at most 4 blocks are queued plus the one being read.
- The end of the download, or the lister's error, travels as a final slot so it arrives in order after the data.
- Close:
- It cancels the context, waits for the lister and every fetch, then closes the current block and every queued result. This returns all pool buffers.
- It is safe to call twice.
- Read and Close share a mutex. Close cancels before taking the mutex, so a blocked Read wakes up.
- `nodes.go`: the retry and Retry-After body of `apiCall` moved into `pacedCall(ctx, p *fs.Pacer, fn func() (*http.Response, error))`. Both `apiCall` and block downloads use it, so there is one implementation.
- `crypto.go`: added `blockDecrypter(enc io.Reader, sk)` (gopenpgp `DecryptingReader`). `decryptBlock` now wraps it, so there is one decryption path. gopenpgp's own `Decrypt` is `DecryptingReader` + ReadAll, so behaviour is unchanged.
- `api/types.go`: added `RevisionResponse`, `Revision`, `Block` and `Thumbnail`, taken from the SDK's `GetRevisionResponseDto`, `DetailedRevisionResponseDto`, `BlockResponseDto` and `ThumbnailResponseDto`.
- `protontest/drive.go`:
- Added `SeedFileContentKeySignedBy(email, created, parentID, name, content)`. It signs the content key packet with the address `email`, or leaves it unsigned if `email` is empty, and backdates the file to `created`.
- `insertLink` now keeps a creation time that is already set.
- This is recorded in the plan's Interfaces block, along with `pacedCall`, `blockDecrypter` and the api types.
- Plan: Task 10 boxes ticked; Interfaces block updated.
- `docs/TODO.md`: added that `pacer.TokenDispenser.Get` takes no context.
## Decisions made
- **Per-download readahead:** `downloadAhead = 4`, i.e. at most about 5 × 4 MiB of plaintext buffered per open reader. The client-wide limit of 10 is still set by `downloadTokens`. The brief gives no per-file value.
- **No content key cache:** the content key is decrypted on every `open` (one PKESK decryption), not cached on the node. Nodes are shared and must not be modified, and caching would need a mutable field in `node`.
- **Hard errors:** the block-list and size checks above are hard errors even with `IgnoreSignatureErrors`, because they are structural rather than signature checks. Only signature failures (content key, manifest) are ignorable. A failure to fetch the manifest signer's keys is a plain error, not `errIntegrity`.
- **Assumed 4 MiB sizes:**
- A ranged read (offset > 0 or limit ≥ 0) of a file without `BlockSizes` errors if a block it reads past is not exactly 4 MiB. That would mean the assumption is wrong, for example 4 MB blocks from old clients. A full read never relies on the assumption.
- Blocks before the first one read can't be checked, so a ranged read of such a file is only as good as the brief's assumption.
- **Wrong-length bodies:** a body over the cap returns a nil response from the paced function, so `shouldRetry` doesn't retry it.
## Tests (download_internal_test.go)
TestDownloadSizes (0, 1, 4MiB-1, 4MiB, 4MiB+1, 8MiB, 9MiB+123), TestDownloadRanges, TestDownloadLegacyFile, TestDownloadManifestSigners, TestDownloadContentKey, TestDownloadBlockHashMismatch, TestDownloadSwappedBlocks, TestDownloadTruncatedBlocks, TestDownloadBadManifest, TestDownloadEmptyFileBadManifest, TestDownloadStorage404Refetch, TestDownloadRetries5xx, TestDownloadStorageRetryAfter, TestDownloadStorageHeaders, TestDownloadBlockTooLarge, TestDownloadEndlessBlockList, TestDownloadCloseEarly, TestDownloadContextCancel.
- **Ranges:** the brief's table plus `(0,-1)`, `(0,0)` and `(size-10,100)`. Each row runs once with the claimed sizes and once with `BlockSizes` removed (assumed 4 MiB), and asserts the bytes and the storage request count.
- **Manifest signers:** a file signed by another user and an anonymous upload both read in full.
- **Content key:** unsigned → OK; signed by the signer address → OK; signed by another own address before 2023 → OK (fallback); the same after 2023 → `errIntegrity`, but readable with `IgnoreSignatureErrors`.
- **Block hash mismatch:** 3 storage attempts and 4 revision requests (the list page, the empty end page and 2 URL re-requests); the error wraps `errBlockHash` and no bytes come back.
- **Swapped and truncated blocks:** a full read ends in `errIntegrity`. Truncation is also tested without `BlockSizes`, where only the manifest catches it.
- **Bad manifest:** a full read returns all bytes and then `errIntegrity`; a ranged read is unaffected; `IgnoreSignatureErrors` returns the bytes.
- **Empty file with a bad manifest:** `errIntegrity`.
- **404 refetch:** one 404 is recovered with 2 storage and 3 revision requests; a persistent 404 is tried only twice.
- **Retries:** 5xx on storage (×2) and on the revision list (×1) are retried. A storage 429 with Retry-After 1 waits at least 1s.
- **Storage headers:** checked through a wrapping transport — no `Authorization` or `x-pm-uid`; `pm-storage-token` and `x-pm-appversion` are present.
- **Hostile:**
- An endless storage body gives "larger than" after one fetch.
- An endless block list, served by a transport that renumbers copies of block 1, gives `errIntegrity` within at most 2 pages, both with claimed sizes and with assumed sizes (encrypted-size cap).
- **Close early and context cancel:** both non-parallel. After Close, the goroutine count is back to baseline within 2s and `pool.Global().InUse()` is back to its starting value. Context cancel returns `context.Canceled` while one block hangs on an injected one-minute delay.
### TDD evidence
- **RED:** `RCLONE_CONFIG=/notfound go test -run 'TestDownload' ./backend/protondrive/` failed to build with `c.open undefined`, `undefined: errIntegrity`, `undefined: errBlockHash`. This was expected, as nothing existed yet.
- **First run after implementing:** everything passed except TestDownloadBlockHashMismatch's revision count (got 4, want 3). The test was wrong because it forgot the empty end-of-list page, so the expectation and its comment were fixed.
- **GREEN:**
- `RCLONE_CONFIG=/notfound go test -race -count=3 -run 'TestDownload' ./backend/protondrive/` → `ok ... 28.745s`.
- Full verification:
- `go build ./backend/protondrive/...` OK.
- `go vet ./backend/protondrive/...` OK.
- `golangci-lint run ./backend/protondrive/...` → `0 issues.`
- `RCLONE_CONFIG=/notfound go test -race ./backend/protondrive/...` → `ok backend/protondrive 67.068s`, `ok backend/protondrive/protontest 25.122s`.
- **Output:** the tests that exercise the `IgnoreSignatureErrors` paths log the expected `ERROR : protondrive: ... integrity check failed` lines (the behaviour under test). Nothing else is printed.
## Files changed
- backend/protondrive/download.go (new)
- backend/protondrive/download_internal_test.go (new)
- backend/protondrive/nodes.go (pacedCall factored out of apiCall)
- backend/protondrive/crypto.go (blockDecrypter; decryptBlock uses it)
- backend/protondrive/api/types.go (revision and block types)
- backend/protondrive/protontest/drive.go (SeedFileContentKeySignedBy; insertLink keeps a preset creation time)
- docs/plans/001-protondrive-tier1.md, docs/TODO.md
## Concerns / notes
- `pacer.TokenDispenser.Get` can't be cancelled. A cancelled fetch first waits for a token from the other in-flight blocks, which finish or fail quickly. Logged in TODO.md.
- The fake's `Dump` checks content key signatures the way current clients do, so it reports a `SeedFileContentKeySignedBy` file unless `email` is the primary address. This is documented on the seeder; no test dumps such a file.
- No identifier collided with protondrive.go. The new block-size constant is named `defaultBlockSize`.
## Fix round 1
**Commit:** e8b135976 protondrive: reject absurd recorded block sizes when downloading
### What changed
- **`download.go` `open`:**
- Any recorded block size above `maxBlockSize` (8 MiB) now returns `errIntegrity` from `open`. This also bounds the total, so it can't overflow.
- A full read (offset 0, limit < 0) no longer takes the empty-reader shortcut and never has its limit clamped. It always lists the blocks and reaches the manifest verdict.
- Added the FIXME where the start block is computed: without recorded block sizes, the blocks before the first one read can't be checked.
- **`protontest/drive.go`:** added `SeedFileClaimingBlockSizes(parentID, name, content, blockSizes)`, which writes arbitrary `BlockSizes` into the extended attributes. It is recorded in the plan's Interfaces block.
- **`TestDownloadHostileBlockSizes` (new):** every case below reads in full and must end in `errIntegrity`.
- "overflowing": `[MaxInt64, 1]`.
- "above the cap": `[8 MiB + 1]` on 1000 bytes.
- "sum too small": `[1000]` on 4 MiB + 1.
- "too few blocks": `[4 MiB]` on 4 MiB + 1.
- **`TestDownloadStorageHeaders`:** a second part rewrites the revision responses so block `BareURL`s point at a second `httptest` server on a different host:port. That server records the headers and passes the request on to the fake. The test asserts both blocks went there, with `pm-storage-token` but without `Authorization` or `x-pm-uid`.
### Pre-2023 fallback time (confirmed, unchanged)
- The SDK applies the fallback unless `node.creationTime > new Date(2023, 0, 1)` (`client/js/src/internal/nodes/cryptoService.ts:652`).
- `node.creationTime` is `new Date(link.Link.CreateTime * 1000)` (`client/js/src/internal/nodes/apiService.ts:858`), i.e. the link's creation time.
- Ours uses `n.CreateTime.Unix() <= contentKeyFallbackUntil` (1672531200). `n.CreateTime` is `time.Unix(l.CreateTime, 0)` from `api.Link.CreateTime` in `decryptNode`, so it is the same field.
- The only difference: the SDK's `Date(2023, 0, 1)` is in local time, while ours is fixed at UTC.
### RED against the old code
Command: `RCLONE_CONFIG=/notfound go test -run 'TestDownloadHostileBlockSizes' ./backend/protondrive/`
```
--- FAIL: TestDownloadHostileBlockSizes/overflowing (0.03s)
download_internal_test.go:539:
Error: Expected error with "integrity check failed" in chain but got nil.
FAIL
```
The overflowing case reproduces the finding: a clean, empty EOF with no error. The other three cases already passed on the old code:
- "above the cap": the real block's size doesn't match the recorded size.
- "sum too small" and "too few blocks": more blocks are listed than recorded.
They are kept as guards. After the cap, no recorded sizes can make a full read's total ≤ 0, so the full-read rule has no separate RED case and is defence in depth.
### GREEN
- `go build ./backend/protondrive/...` OK.
- `go vet ./backend/protondrive/...` OK.
- `golangci-lint run ./backend/protondrive/...` → `0 issues.`
- `RCLONE_CONFIG=/notfound go test -race ./backend/protondrive/...` → `ok backend/protondrive 65.405s`, `ok backend/protondrive/protontest 26.191s`. The only output is the expected `IgnoreSignatureErrors` ERROR log lines.
@@ -0,0 +1,96 @@
### Task 11: Upload
**Files:**
- Create: `backend/protondrive/upload.go`, `backend/protondrive/upload_internal_test.go`
- Create: `backend/protondrive/protontest/upload.go`
**Interfaces:**
- Produces (`protontest`): routes
`POST /drive/v2/volumes/{volumeID}/files`,
`POST .../files/{linkID}/revisions`,
`GET .../links/{linkID}/revisions/{revisionID}/verification`,
`POST /drive/blocks`, `POST /storage/upload/{token}` (multipart, needs
`pm-storage-token`, tokens expire - `func (s *Server) ExpireUploadTokens()`),
`PUT .../files/{linkID}/revisions/{revisionID}`,
`DELETE .../files/{linkID}/revisions/{revisionID}`,
`POST .../delete_multiple`.
Strictness: draft creation enforces name-hash uniqueness and returns
`ConflictLinkID`, `ConflictDraftRevisionID`, `ConflictDraftClientUID` in
`Details`; `/drive/blocks` rejects a block whose `Verifier.Token` is not
`code XOR block-prefix` for the uploaded bytes (checked at commit, HTTP
422 code 2000); commit rejects a manifest that does not verify over the
stored block hashes, a missing block index, or an `XAttr` that is empty;
`CurrentRevisionID` must match for new revisions.
- Produces (`protondrive`):
- `type uploadRequest struct { ParentID, Name string; Existing *node; In io.Reader; Size int64; ModTime time.Time; MIMEType, SHA1 string }`
- `Size < 0` means unknown; `SHA1` is the source's, `""` if unknown.
- `func (c *client) upload(ctx context.Context, r uploadRequest) (*node, error)`
Behaviour is spec section 6.3, steps 1-7, in that order. Decisions the
spec leaves open:
- Blocks are read into `pool.Global()` buffers; a producer reads and
encrypts, `UploadConcurrency` workers upload, all bounded by the upload
token dispenser. Every channel send/receive selects on `ctx.Done()`.
- A block upload is attempted up to 3 times through `storagePacer`
(`CallNoRetry` inside an explicit loop so the buffer is re-sent);
a 404 from storage re-requests that block's URL.
- `IntendedUploadSize` is `Size` rounded down to a multiple of 100,000,
omitted when `Size < 0`.
- SHA1 is computed while reading. If `r.SHA1 != ""` and differs, or if
`Size >= 0` and the bytes read differ from `Size` (short or long), fail
with `errUploadMismatch` before commit.
- Commit error: `getNode` and compare the active revision ID; if it is
ours, return success.
- Cleanup on any failure: for a new file delete the draft link
(`delete_multiple`); for a new revision delete the draft revision.
Cleanup uses `context.WithoutCancel(ctx)` with a 30s timeout.
- Draft conflict: own `DraftClientUID` -> delete it, retry once; another
client's draft -> `errExists` unless `ReplaceExistingDraft`; an active
file (no draft revision in the details) -> the caller passed no
`Existing`, so fetch that node and upload a new revision to it.
Reference: `client/js/src/internal/upload/` (`manager.ts`,
`streamUploader.ts`, `fileUploader.ts`, `apiService.ts`,
`blockVerifier.ts`, `cryptoService.ts`).
- [ ] **Step 1: Write failing tests** (assert with `user.Dump()` and
`user.Drafts()`):
- `TestUploadSizes` (Review Focus 1): 0, 1, 4 MiB - 1, 4 MiB, 4 MiB + 1,
9 MiB + 123; content, modtime (1s precision), SHA1 and block count
(`ceil(size / 4 MiB)`, 0 for empty) are right.
- `TestUploadUnknownSize`: `Size: -1` works for 0 and 5 MiB.
- `TestUploadNewRevision`: same node ID, new content, old revision
superseded.
- `TestUploadOverExistingNameCreatesRevision`.
- `TestUploadShortSource`, `TestUploadLongSource` (Review Focus 3):
`errUploadMismatch`, `Drafts()` empty, pool `InUse` restored.
- `TestUploadSHA1Mismatch`.
- `TestUploadBlock502Once`: succeeds; `TestUploadBlock502Forever`
(issue 10224): returns an error within 30s, `Drafts()` empty, goroutine
count back to baseline.
- `TestUploadStorageTokenExpired`: `ExpireUploadTokens()` mid-upload,
succeeds.
- `TestUploadCommitErrorButCommitted`: `Fault{AfterHandler: true}` on
commit; `upload` returns the node, exactly one file exists.
- `TestUploadOwnDraftReplaced` (issue 9622): leave a draft via a faulted
commit plus faulted cleanup, upload again -> succeeds, one file.
- `TestUploadForeignDraft`: `errExists`; with `ReplaceExistingDraft`
succeeds.
- `TestUploadCancel`: cancel at draft, mid-blocks and before commit (use
`Fault.Delay`); `Drafts()` empty each time.
- `TestUploadConcurrencyBound`: with `UploadConcurrency: 2` the fake's
peak concurrent `/storage/upload` count is at most 2; with 16 parallel
`upload` calls it is at most 10.
- `TestUploadVerifierChecked`: an `upload` whose verifier is sabotaged
through a test hook is rejected by the fake (proves the fake checks).
- `TestUploadClockSkew`: server 3h ahead and 3h behind both succeed.
- `TestUploadThenDownload`: round trip through Task 10 for 9 MiB + 123.
- [ ] **Step 2: Run** - expect FAIL.
- [ ] **Step 3: Implement.**
- [ ] **Step 4: Run** the verification commands - expect PASS. Also
`go test -race -count=5 ./backend/protondrive/ -run TestUpload`.
- [ ] **Step 5: Commit** `protondrive: add verified block upload`.
@@ -0,0 +1,304 @@
# Task 11 report: verified block upload
Commit: `9a4ed61a8 protondrive: add verified block upload`
## What was implemented
### Client (`backend/protondrive/upload.go`, 879 lines)
You set a limit of about 900 lines. upload.go is at 879, so I am reporting it rather than splitting it.
- `uploadRequest` and `(*client).upload` match the interface in the plan (no change to it).
- **Step 1, create the draft.** `createDraft` creates a draft file with these parts:
- a v4 file key advertising AEAD, locked with an ordinary S2K;
- a v6 PKESK content key;
- the name and passphrase without AEAD;
- `ClientUID` from `DraftClientUID`, sent as null if empty;
- `IntendedUploadSize`, rounded as the SDK rounds it.
For `Existing` it calls `createRevision` instead, which sends `CurrentRevisionID`.
- **Step 2, name conflict (`resolveConflict`).**
- If `ConflictRevisionID` is set, or there is no draft revision, the node is re-read. A file gets a new revision. A folder, or anything else, returns `errExists`.
- If the draft is ours (non-empty UID that matches), or `ReplaceExistingDraft` is set, it calls `delete_multiple` and retries once. A draft that comes back after that returns `errExists`.
- Otherwise it returns `errExists` with a hint about `replace_existing_draft`.
- Without a `DraftClientUID`, no draft counts as ours. This matches the SDK.
- **Step 3, verification.** `verificationData` decrypts the server's `ContentKeyPacket`, as `BlockVerifier` does. Each block is then:
- encrypted and signed in one pass by a new `crypto.go:encryptBlockTo`; `encryptBlock` now wraps it, so there is one implementation and the existing vectors and diff tests cover it;
- hashed with SHA-256;
- decrypted again as a check;
- given its verifier token.
A failed check is retried once, like the SDK's `MAX_BLOCK_ENCRYPTION_RETRIES`.
- **Step 4, the upload pipeline.** It uses an errgroup:
- one producer reads and encrypts blocks;
- one requester batches the waiting blocks into `POST /drive/blocks`;
- `uploadConcurrency()` workers upload (default 4, max 5).
Every channel send and receive selects on `ctx.Done()`. Each block holds two things until it is uploaded or freed: a per-file slot (capacity = concurrency) and a client token from `uploadTokens` (10).
- **Uploading a block:** a multipart POST with field `Block` and filename `blob`, as the SDK sends it. It sends `pm-storage-token` plus the rest client's `x-pm-appversion`, and never Authorization or x-pm-uid. Calls go through `c.paced` (one retry and Retry-After implementation) inside `storagePacer.CallNoRetry` and an explicit loop of 3 attempts. A 404 requests that block's URL again, and the 404 counts as an attempt so the loop stays bounded.
- **Refactor in nodes.go:** `pacedCall` is split into `pacedCall` and `paced`. The behaviour is unchanged.
- **Request bodies:** each attempt's body is a `multipartBlock` that stops reading the pool buffer once the attempt ends. The HTTP transport may keep reading a request body after `RoundTrip` returns, which would otherwise race with the retry's `Seek` or with `free`.
- **Step 5, checks before commit.**
- Size: a long source fails with `errUploadMismatch` as soon as it passes `Size`; a short one fails before commit.
- SHA1: compared case-insensitively against the source's.
- Block count: must equal `ceil(size/4 MiB)`.
- **Step 6, commit.** It signs the manifest and encrypts the xattrs (ModTime if set, Size, BlockSizes, SHA1), then commits with `ChecksumVerified` and `Photo: null`. Then it always drops the cache entry and calls `getNode`:
- If the commit failed but the node is active with our revision ID, the upload succeeds.
- If the commit succeeded but reading the node back failed, it returns an error and does not clean up, so a committed file is never deleted.
- **Step 7, cleanup.** On any failure before commit, `deleteDraft` uses `context.WithoutCancel` with a 30s timeout. A new file is deleted with `delete_multiple`; a revision with `DELETE .../revisions/{id}`. A cleanup failure is logged at ERROR.
- **Token wait.** `acquireToken` puts a context around `TokenDispenser.Get`. If the wait is abandoned, its goroutine returns the token as soon as it gets one, so nothing deadlocks and no token leaks (`TestAcquireTokenCancel`).
- **Test hook.** `client.verifierHook` is an instance field, not package state.
- **Wire types** are in `api/types.go`: `ConflictDetails`, `CreateFile*`, `CreateRevision*`, `VerificationResponse`, `BlockUpload*`, `UploadLink`, `CommitRevisionRequest`.
### Fake (`protontest/upload.go`, 693 lines, plus small edits)
- **Routes:** the 8 listed in the brief, plus `ExpireUploadTokens()` and `StorageUploadPeak()`. The peak is counted in `serveHTTP`, so `Fault.Delay` counts toward it, and `ResetRequests` resets it.
- **Strictness**, all answered with HTTP 422 code 2001 unless noted:
- A file draft must have a non-AEAD Name and NodePassphrase (via `checkMessages`).
- The file key must be v4 with iterated-salted S2K. `checkFolderKey` was generalised into `checkNodeKey(armored, aead)` plus `checkFileKey`.
- `ContentKeyPacket` must be exactly one v6 PKESK.
- `ContentKeyPacketSignature`, `NodePassphraseSignature` and `MIMEType` are required.
- `IntendedUploadSize` must be rounded.
- `SignatureAddress` must be one of the user's addresses.
- Name-hash uniqueness is enforced, and the conflict Details include the draft revision and its ClientUID.
- **At commit:**
- XAttr must be present and non-AEAD (2001).
- Blocks 1..n must all have been requested and uploaded, with no gaps (2000).
- A declared `Hash`, if one was sent, must match (2000).
- Each verifier token must equal code XOR the prefix of the uploaded bytes (2000).
- Each block must be a single SEIPD packet whose version matches the content key packet: SEIPDv2 for v6, SEIPDv1 for a seeded legacy file (2001).
- The manifest signature must verify over the stored hashes, in order, with the keys of `SignatureAddress`, at the server's clock (2001).
- On success, the previous active revision becomes obsolete (state 2).
- **Storage upload rules:**
- A request carrying session credentials gets HTTP 400.
- A wrong `pm-storage-token` gets 403.
- An expired URL, or a block that is no longer awaited, gets 404.
- **Other routes:**
- `delete_multiple` deletes drafts only and gives a per-link error for anything else.
- `DELETE` revision accepts draft revisions only.
- `createRevision` requires a matching `CurrentRevisionID` (2000).
- **Fake changes needed for the clock-skew test:** the 3h-behind case could not even sign in before, so I changed three things.
- The fake now generates keys and makes signatures at `signTime()`, which is now minus 24h.
- `Dump` checks signatures at the server clock (`driveView.now`). For that, `decryptArmored`, `verifyDetached`, `unlockDriveKey`, `decryptContentKey` and `decryptBlock` take an `at time.Time` (zero means now).
- `serveFault` reads the request body before a `Delay`. Without that, Go's server never notices that the client gave up, and the delayed handler kept running (httptest Close blocked for a minute).
## Decisions made (no review stop, per your standing instruction)
1. **IntendedUploadSize follows the SDK, not the brief.** For a new file it is `floor(size/1e5)*1e5`, sent only when that is over 100,000. For a revision it is `reduceSizePrecision` (4095 under 4 KiB, 100,000 under 100 kB). It is null when the size is unknown. The commit message says so.
2. **No `Hash` in BlockList**, because the SDK omits it (deprecated and unused). "The SHA-256 the client declared" is taken to mean the manifest, which commit checks against the hashes of the uploaded bytes. A `Hash` field, if sent, is also checked, and there is a fake-side test for it.
3. **Conflict with an active file that also has a draft revision** (both `ConflictRevisionID` and `ConflictDraftRevisionID` set): we upload a new revision. The SDK would `delete_multiple` the link. I chose this because it cannot destroy an active file. It is logged in TODO.md.
4. **An empty stream of unknown size uploads as zero blocks.** The SDK refuses that, but the brief requires it to work.
5. **"Every declared block index uploaded exactly once"** is implemented as: each requested index 1..n has exactly one stored block at commit. Uploading again to the same URL replaces the block, so a retry after a lost storage response works.
6. **A cleanup failure is logged at ERROR**, as the SDK does. Two deliberate fault tests therefore print an ERROR line each.
7. **A storage 404 counts toward the 3 attempts**, so a storage host that only ever answers 404 cannot loop forever.
8. **Fault codes:** the fake uses 422/2000 for verifier failures, missing blocks and a stale CurrentRevisionID, and 2001 for format and signature failures. The real codes still need confirming; this is in TODO.md.
## Memory
- `pool.Global()` pages are 1 MiB (`pool.BufferSize`).
- A 4 MiB plaintext block uses 4 pages, plus a 5th page touched by `RW.ReadFrom` at exact-4 MiB EOF. The plaintext is freed right after encryption.
- The ciphertext is about 4 MiB plus AEAD overhead, so 5 pages, held until that block's upload finishes.
- Peak is therefore about 10 × 5 MiB of ciphertext per client, plus at most one transient plaintext per uploading file. Plaintext is read only while a client token is held, so it is bounded by 10 as well.
- Nothing per block is allocated outside the pool on the hot path. The exceptions are small: gopenpgp's internal AEAD and stream buffers (256 KiB chunk size), the detached signature, and the 32-byte token prefix.
## Tests
Client tests (`upload_internal_test.go`):
- TestUploadSizes
- TestUploadUnknownSize
- TestUploadNewRevision (checks the old revision has State 2)
- TestUploadOverExistingNameCreatesRevision (also: a folder in the way gives `errExists`)
- TestUploadShortSource and TestUploadLongSource (each runs 2 sizes and checks for goroutine and pool leaks)
- TestUploadSHA1Mismatch
- TestUploadBlock502Once
- TestUploadBlock502Forever (finishes in seconds, leak check)
- TestUploadStorageTokenExpired (expires the tokens from a transport hook on the first storage request)
- TestUploadCommitErrorButCommitted
- TestUploadDraftResponseLost (your ruling: the 2500 points at our own UID, then delete and retry)
- TestUploadOwnDraftReplaced
- TestUploadForeignDraft (also: no UID means no own draft)
- TestUploadCancel (cancel at draft, blocks and commit, with a leak check)
- TestUploadConcurrencyBound (per file ≤ 2; 16 parallel uploads ≤ 10 and > 1)
- TestUploadVerifierChecked
- TestUploadClockSkew (±3h, including a revision upload of unknown size)
- TestUploadThenDownload
- TestIntendedUploadSize
- TestAcquireTokenCancel
After every successful upload test, `requireUploaded` checks the fake's `Dump()` for each node: no `Err`, and content, SHA1, mtime and block sizes (via Dump's xattr checks) all match. It also checks that `Drafts()` is empty.
Fake tests (`protontest/upload_test.go`):
- TestCreateFileFormats: AEAD Name and passphrase variants, a v6 key, an AEAD-locked key, salted S2K, a v3 content key packet, missing signatures, an unrounded size, the conflict details.
- TestCommitChecks: AEAD, empty or missing XAttr; verifier; a block not uploaded; an index gap; a declared hash; the manifest; the manifest signer; a SEIPDv1 block. Then a valid commit is checked through Dump, and recommit and deletes are refused.
- TestStorageUploadRules
- TestRevisionRules
- TestUploadWireNames
Commands, all run with `RCLONE_CONFIG=/notfound`:
- `go build ./backend/protondrive/...`: OK
- `go vet ./backend/protondrive/...`: OK
- `golangci-lint run ./backend/protondrive/...`: 0 issues
- `go test -race ./backend/protondrive/...`: `ok protondrive ~75s`, `ok protontest ~30s`. After the fix described below, I ran it 6 times and it passed every time.
- `go test -race -count=5 ./backend/protondrive/ -run TestUpload`: `ok 105.461s`
- Output: the expected ERROR lines from the two fault tests whose draft cleanup fails, plus existing log lines from other tests.
One full run failed before I fixed a race in my own test: the PerClient subtest read a map from goroutines, and the fatal concurrent-map error aborted the binary. Fixed; all runs since then are green.
## TDD evidence
I did not follow TDD strictly. upload.go and the fake routes were written before `upload_internal_test.go`, so there is no RED output from before the implementation.
To show the tests check behaviour, I ran a mutation check. I disabled the fake's verifier comparison (`false && ...`):
- `TestUploadVerifierChecked` failed with "An error is expected but got nil".
- `TestCommitChecks/verifier` failed.
With the check restored, both pass.
The first runs also caught real defects, which I then fixed:
- ±3h clock skew failed: Dump verified at local time, and the fake signed at local time.
- Cancel tests stalled httptest Close for a minute (the delayed request body was never read).
- The data race described above.
## Files changed
- New: `backend/protondrive/upload.go`, `upload_internal_test.go`, `protontest/upload.go`, `protontest/upload_test.go`
- Modified: `api/types.go`, `crypto.go` (`encryptBlockTo`), `nodes.go` (`paced` split, `verifierHook`), `protontest/{account,crypto,drive,drive_test,mutate,server}.go`, `docs/TODO.md` (3 entries), `docs/plans/001-protondrive-tier1.md` (Task 11 steps ticked)
## Concerns
- upload.go is at 879 lines, close to your 900-line limit. It is not split.
- The fake changes go beyond upload. Every fake signature is now backdated one day, and Dump checks at the skewed clock. These were needed for the 3h-behind case, and all existing fake and client tests still pass.
- The real Proton status and code values for verifier, missing-block and stale-revision errors are guesses (TODO.md). The real server's `delete_multiple` behaviour for active links is unconfirmed.
- If a lost response causes `createRevision` to be retried, an orphan draft revision can be left behind (TODO.md).
- A source reader that blocks forever cannot be interrupted by ctx. That is outside the client's control.
## Fix round 1
### Important finding: commit check under cancellation
The problem: after a commit error, `uploadDraft` read the node back with the caller's ctx. If that ctx was cancelled, or the read failed for any other reason, the code concluded "not active" and cleanup deleted the draft. For a commit that had actually landed, that meant deleting the new file (`delete_multiple`) or the new active revision.
What changed in `backend/protondrive/upload.go`:
- The post-commit `getNode` now runs under `context.WithTimeout(context.WithoutCancel(ctx), 30s)`, the same as cleanup.
- If the commit errored and the read also failed, the outcome is unknown. Nothing is deleted (committed=true), and the error wraps a new sentinel, `errUploadOutcomeUnknown` ("upload outcome unknown: it may have succeeded"), plus the commit error and the read error. No node is returned. Any draft left behind is handled by the own-draft rule on the next upload.
- If the read shows our revision is active, the upload succeeds, as before.
- The draft is deleted only when the read positively shows our revision is not active.
- The doc comments of `upload` and `uploadDraft` now state this contract.
New tests in `upload_internal_test.go`:
1. **`TestUploadCancelDuringCommit`**, with subtests for a new file and a new revision. A transport hook forwards the commit PUT, so the fake's handler really runs and commits. Then it cancels ctx and returns `context.Canceled` to the client. The test asserts:
- no `POST delete_multiple` and no `DELETE .../revisions/{id}` was sent;
- `upload` returns success, because the read shows the revision active;
- Dump shows the file intact, and there are no drafts.
2. **`TestUploadCommitOutcomeUnknown`**: the commit gets a 422 and `POST .../links` also gets a 422. The test asserts the error `Is` `errUploadOutcomeUnknown` and contains "may have succeeded", that no delete of either kind was sent, and that one draft is left.
3. **`TestUploadCommitRefused`**: the commit gets a 422 once and the read shows the revision not active. The test asserts exactly one `delete_multiple` was sent, no drafts are left, no file exists, and the error is not `errUploadOutcomeUnknown`.
RED against 9a4ed61a8 (tests added, fix not yet applied). Command:
```
RCLONE_CONFIG=/notfound go test -count=1 -run 'TestUploadCancelDuringCommit|TestUploadCommitOutcomeUnknown|...' ./backend/protondrive/
--- FAIL: TestUploadCommitOutcomeUnknown (0.32s)
Error: Target error should be in err chain:
Error: "upload \"f\": commit: protontest: injected fault (Code=2001, Status=422)" does not contain "may have succeeded"
Error: Should be zero, but was 1 Messages: draft deletion sent
Error: "[]" should have 1 item(s), but has 0
--- FAIL: TestUploadCancelDuringCommit (0.60s)
--- FAIL: TestUploadCancelDuringCommit/revision=false
Error: Received unexpected error:
upload "f": commit: Put "http://127.0.0.1:41259/drive/v2/volumes/.../revisions/...": context canceled
--- FAIL: TestUploadCancelDuringCommit/revision=true
(same)
FAIL
```
The first assertion in test (1) stopped the run, so the delete counts were not reached. To show them, I temporarily changed that `require.NoError` to `assert.NoError` and ran it again:
- `revision=false` failed with "draft deletion sent": cleanup sent `delete_multiple` for the committed file.
- `revision=true` failed with "revision deletion sent": cleanup sent DELETE for the committed revision.
I reverted that change afterwards. With the fix, all three tests pass.
### Minor findings
- **Fake data race:** `handleStorageUpload` read `up.expired` after unlocking. It now computes `usable` while holding `s.mu`.
- **New `TestUploadSourceError`:** the reader returns 5 MiB + 10 bytes, then an error. The test asserts `ErrorIs` the source error, no drafts, no file, and no goroutine or pool leak.
- **New `TestUploadBlockFailsProducerBlocked`:** `UploadConcurrency: 1`, 4 blocks, storage answers 502 forever. The test asserts an error, exactly 3 storage requests (one block tried 3 times), no drafts, no file, and no leaks.
- **`TestUploadThenDownload`** now runs the Dump check (`requireUploaded`).
- **Plan:** I unticked Task 11 "Step 2: Run - expect FAIL" and added a note that no RED run happened because the implementation was written first.
### Verification
All run with `RCLONE_CONFIG=/notfound`:
- `go vet ./backend/protondrive/...`: OK
- `golangci-lint run ./backend/protondrive/...`: 0 issues
- `go test -race -count=1 ./backend/protondrive/...`:
```
ok github.com/rclone/rclone/backend/protondrive 73.966s
ok github.com/rclone/rclone/backend/protondrive/protontest 29.910s
```
- `go test -race -count=5 ./backend/protondrive/ -run TestUpload`:
```
ok github.com/rclone/rclone/backend/protondrive 120.787s
```
## Fix round 2
### Finding: "not active" is not proof that the commit failed
The problem: round 1 deleted the draft whenever the follow-up read showed our revision was not active. Two cases make that wrong:
- **New file:** the node may be active with another client's revision, or trashed. Either proves our commit landed, yet cleanup sent `delete_multiple` for the node.
- **New revision:** a third revision may supersede ours. Cleanup then sent `DELETE` for our committed, now historical, revision.
The fix in `backend/protondrive/upload.go` follows the controller's ruling:
- `draft` gains `baseRevisionID`, the `CurrentRevisionID` the draft revision was created from.
- `(*draft).notCommitted(n)` is true only in two cases: a new file whose node is still a draft, or a revision whose node's active revision is still `baseRevisionID`. Only then is the draft deleted.
- After a failed commit and a successful read:
- our revision active on an active node: success;
- `notCommitted`: delete the draft, and return the commit error;
- anything else: delete nothing, and return `errUploadOutcomeUnknown` ("... the file has changed since").
- If the commit failed and the read failed too: `errUploadOutcomeUnknown`, nothing deleted (unchanged from round 1).
- **The commit succeeded but the read shows a revision that is not ours:** a new sentinel, `errUploadSuperseded`, with no node returned and nothing deleted. If that read fails instead, the error is "read the uploaded file" and nothing is deleted.
- The `upload` doc comment now states the real contract. It deletes a draft only before commit or on positive proof that the commit did not land. It names both sentinels, and says the returned node is always this upload's own revision.
- The stale `failingReader` comment is fixed.
### Fake
- New `func (u *User) SeedRevision(id string, content []byte, modTime time.Time) (revisionID string)`. It gives a file a new active revision as another client would, and makes the previous active revision obsolete. It is recorded in the plan's Interfaces block for Task 11.
- `seedFile`'s revision building was factored into `contentKey.newRevision` so both use it.
- Test: `TestSeedRevision`, for a current and a legacy file; Dump is clean.
### Tests
Each test below asserts zero `POST delete_multiple` and zero `DELETE .../revisions/{id}` requests, and checks Dump. The hook (`interveneAfterCommit`) forwards the commit PUT, so the server really commits. It then runs "another client's" change, and then shows the client a network error.
1. **`TestUploadCommitThenChanged/NewFileNewRevision`:** after the commit, `SeedRevision` adds a newer revision. Result: `errUploadOutcomeUnknown`, and the file is present with the other client's content.
2. **`TestUploadCommitThenChanged/NewFileTrashed`:** a second client trashes the node after the commit. Result: `errUploadOutcomeUnknown`, and the trashed file is intact with our content.
3. **`TestUploadCommitThenChanged/NewRevision`:** a new revision, with a third revision seeded after the commit. Result: `errUploadOutcomeUnknown`. Dump shows the third revision active, and our committed revision still exists with State 2 (read through the revision route).
4. **`TestUploadCommittedThenSuperseded`:** the commit succeeds (real response kept), then another revision is seeded. Result: `errUploadSuperseded`, nil node, and nothing deleted.
5. **`TestUploadRevisionCommitRefused`:** a new revision whose commit is refused (422, handler not run, active revision unchanged). Result: exactly one revision DELETE, and the original content is intact.
RED against 1f1b2bb32. The tests were added with only the `errUploadSuperseded` declaration, not its behaviour. Command:
```
RCLONE_CONFIG=/notfound go test -count=1 -v -run 'TestUploadCommitThenChanged|TestUploadCommittedThenSuperseded|TestUploadRevisionCommitRefused' ./backend/protondrive/
--- PASS: TestUploadRevisionCommitRefused (0.30s)
--- FAIL: TestUploadCommittedThenSuperseded (0.30s)
Error: Expected error with "upload committed, but the file now has another revision" in chain but got nil.
Error: Expected nil, but got: &protondrive.node{... RevisionID:"59735c0b..." ...} (another client's revision returned as ours)
--- FAIL: TestUploadCommitThenChanged (0.00s)
--- FAIL: TestUploadCommitThenChanged/NewFileNewRevision Target error should be in err chain; Should be zero, but was 1: draft deletion sent
--- FAIL: TestUploadCommitThenChanged/NewRevision Target error should be in err chain; Should be zero, but was 1: revision deletion sent
--- FAIL: TestUploadCommitThenChanged/NewFileTrashed Target error should be in err chain; Should be zero, but was 1: draft deletion sent
FAIL
```
Test 5 passes before and after, as expected: that path deleted correctly in 1f1b2bb32 and still must.
### Verification
All run with `RCLONE_CONFIG=/notfound`:
- `go vet ./backend/protondrive/...`: OK
- `golangci-lint run ./backend/protondrive/...`: 0 issues
- `go test -race -count=1 ./backend/protondrive/...`:
```
ok github.com/rclone/rclone/backend/protondrive 81.363s
ok github.com/rclone/rclone/backend/protondrive/protontest 31.375s
```
- `go test -race -count=3 ./backend/protondrive/ -run TestUpload`:
```
ok github.com/rclone/rclone/backend/protondrive 75.294s
```
@@ -0,0 +1,20 @@
### Task 12: Differential interop of whole files
**Files:**
- Modify: `backend/protondrive/crypto_diff_internal_test.go`
**Interfaces:** Consumes Tasks 10 and 11, and the old library.
- [ ] **Step 1: Write failing tests**:
- `TestUploadReadableByOldLibraryCrypto`: upload a 5 MiB file with
Task 11 to the fake; take the stored link, revision, blocks and xattrs
from the fake; decrypt the node, content key, every block and the
xattrs using only old-library exported helpers and plain gopenpgp as in
Task 6; compare content and SHA1.
- `TestFrozenFileFixture`: add a frozen fixture `testdata/oldlib/file/`
(link JSON, revision JSON, block files) written by the old-library
path, and download it through Task 10 by loading it into the fake.
- [ ] **Step 2: Run** - expect FAIL, then implement helper code until PASS.
- [ ] **Step 3: Commit** `protondrive: add whole-file differential tests`.
@@ -0,0 +1,151 @@
# Task 12 report: whole-file differential interop
**Status:** DONE_WITH_CONCERNS. The work is complete and all checks pass. The concerns are one unexplained failure in a full race run, a missing RED run for two of the tests, and the scope of the v6 fixture. All three are listed at the end.
Commit: `e76b6783b protondrive: add whole-file differential tests`
## Findings
**No interop mismatch in either direction.**
- **New writer, old reader:** the old library reads what `client.upload` writes, in full. This covers the node key, name, content key, both blocks, the block signatures and the xattrs. The SHA1, size, block sizes and mod time all match, and the manifest verifies with plain gopenpgp. Nothing to record in `docs/TODO.md`.
- **Old writer, new reader:** the new download path reads files the old library wrote. It was tested at 0 B, 2500 B and 5 MiB + 3, in both formats. Every signature verifies (`c.reported` stays empty) and the content, SHA1, size and mod time match. No bug to fix.
**The one defect found was in the fake.** It always signed at `now - 24h` (`signTime`). A key generated just now, such as the old library's fresh test keys, then had "no valid signing keys", and `AddUserWithKeys` panicked. Fix: the new `signTimeOf(signer)` signs at `max(signTime(), the key's creation time)`. It is used in `encryptArmored`, `signArmored` and `signBinary`. Keys the fake generates itself are unaffected, because they are created at `signTime()`.
## Which formats the old library can write
- **Legacy (v3 PKESK, SEIPDv1):** this is every **new file** it writes. `SetContentKeyPacketAndSignature` always makes a non-v6 session key. The whole file is old-library output.
- **v6 (v6 PKESK, SEIPDv2 blocks):** the old library writes this format only as a **revision of an existing crypto-refresh file**. It reads the stored v6 content key with `link.GetSessionKey` (bridge `file_upload.go:229-252`), and `protonDrivePGP()` then emits SEIPDv2 blocks. It never creates the file's node key or v6 content key packet itself.
- So in the v6 fixture, `sdkFileKeys` (plain gopenpgp, SDK style) makes the node key (v4, advertising SEIPDv2) and the v6 content key packet. They stand in for the client that created the file.
- The old library writes everything else: the node passphrase (`oldEncryptWithSignature`), name, hash, session key read-back, blocks, block signatures, manifest and xattrs.
- `TestFrozenFileFixture` asserts each fixture's packet versions: legacy is PKESK v3 / SEIPD v1, v6 is PKESK v6 / SEIPD v2.
## What I implemented
### `protontest` (new `stored.go`; no import from `backend/protondrive` except `api`)
- **Stored-file types:**
- `type StoredFile struct { Link StoredLink; Revision StoredRevision }`
- `StoredLink`: `Name, NameHash, NodeKey, NodePassphrase, NodePassphraseSignature, SignatureEmail, NameSignatureEmail, MediaType, ContentKeyPacket, ContentKeyPacketSignature`
- `StoredRevision`: `ManifestSignature, XAttr, SignatureEmail, Blocks []StoredBlock`
- `StoredBlock`: `Hash []byte, EncSignature, SignatureEmail, Data []byte` (`json:"-"`)
- `func (u *User) StoredFile(id string) StoredFile` is the read-only accessor. It returns copies of the file and its active revision.
- `func (u *User) SeedStoredFile(parentID string, f StoredFile) string` installs the material exactly as given, with no checks, through `insertLink`.
- `type FixedKeys struct { AddressKey, AddressKeyPassphrase, RootKey, RootKeyPassphrase, RootHashKey string }` and `func (s *Server) AddUserWithKeys(username, password string, keys FixedKeys) *User`:
- This is `AddUser` with the primary address key and the root folder's node key and hash key supplied.
- The user key and share key are still generated. The fake seals the address key passphrase to the new user key, and the root passphrase to the share key.
- This is the narrowest set that lets a stored file verify: its name and passphrase are encrypted to the parent, and it is signed by the address.
- `newDrive` is split into `newDrive` plus `newDriveWithRoot(addr, root, hashKey)`.
- New test `TestStoredFileRoundTrip`: two users built from the same `FixedKeys`. A file stored by one, seeded into the other, dumps cleanly.
- All of this is in the plan's Task 12 Interfaces block, along with the extra files.
### `download_internal_test.go` (no old-library import; Task 19 keeps it as is)
- `frozenFile`: the email, `FixedKeys`, name, mod time and plaintext, plus `Stored`.
- `loadFrozenFile`, `frozenFileParts`, `blockFileName`.
- `checkFrozenFile`: builds a user from the fixture's keys, seeds the stored file and reads it with `getNode` and `open`. It checks the name, mod time, size, SHA1, content, and that no problems were reported.
- `TestFrozenFileFixture`: runs over `testdata/oldlib/file/{legacy,v6}`, asserts each fixture's format, then calls `checkFrozenFile`.
- With `crypto_diff_internal_test.go` moved aside, this test still compiles and passes. I checked this, then moved the file back.
### `crypto_diff_internal_test.go`
- `oldEncryptWithSignature` is extracted from `oldGenerateNodeKeys` (bridge `crypto.go:88-117`), because the v6 path reuses it.
- `writeOldLibStoredFile(name, content, modTime, v6)` follows the bridge's upload path. It:
- sets the parent hash key with `CreateFolderReq.SetNodeHashKey` and reads it back with `GetHashKey`;
- fills `CreateFileReq` with `SetName`, `SetHash` and `SetContentKeyPacketAndSignature` (or, for v6, `sdkFileKeys` plus `GetSessionKey`);
- encrypts 4 MiB blocks with `oldEncryptBlock`;
- signs the manifest with `oldSignManifest`;
- writes the xattrs with `CommitRevisionReq.SetEncXAttrString` and the bridge's `-0700` time layout.
- `saveFrozenFile` writes a fixture directory.
- `TestUploadReadableByOldLibraryCrypto` uploads 5 MiB + 3 through `client.upload`, then takes `u.StoredFile`. Using only the old library and plain gopenpgp, it:
- unlocks the address keys with `proton.Unlock` from the same `/core/v4/users` and `/core/v4/addresses` responses;
- reads the share key with `proton.Share.GetKeyRing` and the root key with `proton.Link.GetKeyRing`, from my-files;
- reads the file's `GetKeyRing`, `GetName` and `GetSessionKey`;
- decrypts both blocks with `oldDecryptBlock`, verifying against the address keys plus the node key, as bridge `file_download.go` does;
- verifies the manifest with gopenpgp;
- reads the xattrs with `GetDecXAttrString` and parses the time with `iso8601`.
- `TestOldLibraryFileReadable` covers both formats at 0, 2500 and 5 MiB + 3 bytes, and passes each through `checkFrozenFile`. With `-update` it also rewrites the 2500 B frozen fixtures. Regenerate them with `go test ./backend/protondrive/ -run 'TestFrozenFixtures|TestOldLibraryFileReadable' -update` (now in the file header).
### Fixtures
- `testdata/oldlib/file/legacy/` and `v6/` each hold `file.json`, `link.json`, `revision.json` and `block-1.bin`, about 12 KB per directory.
- They were generated once with `-update` and committed.
- They are a single block on purpose: a multi-block fixture would mean committing more than 4 MiB of incompressible ciphertext per format.
### Docs
- Plan: the Task 12 Files and Interfaces are filled in and the boxes ticked.
- `docs/TODO.md`: the cleanup entry now says `TestFrozenFileFixture` stays. It also notes that multi-block old-library files lose coverage when `TestOldLibraryFileReadable` is deleted.
## Tests
- **RED:** `RCLONE_CONFIG=/notfound go test -count=1 ./backend/protondrive/ -run TestFrozenFileFixture` gave:
```
--- FAIL: TestFrozenFileFixture/legacy ... open testdata/oldlib/file/legacy/file.json: no such file or directory
--- FAIL: TestFrozenFileFixture/v6 ... open testdata/oldlib/file/v6/file.json: no such file or directory
```
This was expected: no fixture existed yet.
- **First run of `TestOldLibraryFileReadable`:** it panicked with `protontest: sign: ... no valid signing keys`. That is the signing-time defect in the fake described above; I fixed it.
- **Mutation checks**, run against the live test and then reverted:
- manifest signed by the parent key instead of the address key gives `integrity check failed: verify manifest`;
- name signed by the wrong key gives `Should be empty, but was map[verify:<id>:true]`.
- **GREEN:**
```
go build ./backend/protondrive/... ok
go vet ./backend/protondrive/... ok
golangci-lint run ./backend/protondrive/... 0 issues.
go mod tidy -diff no diff
RCLONE_CONFIG=/notfound go test -count=1 -race ./backend/protondrive/... ok (x5, see concern 1)
RCLONE_CONFIG=/notfound go test -race -count=5 ./backend/protondrive/ -run 'TestFrozen|TestOldLibrary|TestUploadReadable' ok 60.3s
RCLONE_CONFIG=/notfound go test -race -count=3 ./backend/protondrive/protontest/ ok 92.8s
```
With `-v`, the new tests print only their PASS lines.
## Files changed
- `backend/protondrive/protontest/stored.go` (new)
- `backend/protondrive/protontest/drive.go`, `crypto.go`, `drive_test.go`
- `backend/protondrive/crypto_diff_internal_test.go`, `download_internal_test.go`
- `backend/protondrive/testdata/oldlib/file/{legacy,v6}/*` (new)
- `docs/plans/001-protondrive-tier1.md`, `docs/TODO.md`
## Concerns
1. **One unexplained failure.** The first full `go test -race ./backend/protondrive/...` run reported FAIL for the main package. I had only captured the tail of the output, so I don't know which test failed. Five more full race runs passed, and so did the new tests at `-race -count=5`. It may be an existing flaky test, but I could not confirm that.
2. **TDD order.** I wrote the `protontest` helpers before their test, and `TestUploadReadableByOldLibraryCrypto` and `TestOldLibraryFileReadable` passed the first time they compiled. The only RED runs were the missing-fixture failure and the signing-time panic. The mutation checks above show the tests can fail.
3. **v6 fixture scope.** Its node key and content key come from `sdkFileKeys`, not the old library, because the old library cannot create them (see "Which formats" above). If you want a v6 fixture made purely by the old library, that is impossible: it only writes v6 content as a revision of a file someone else created.
## Fix round 1
Commit: `protondrive: state the v6 file fixture's provenance in the fixture` (see `git log -1`).
### What changed
- **The fixture's `Comment` now depends on `v6`.** The change is in `writeOldLibStoredFile` (`crypto_diff_internal_test.go`). For v6 it reads:
> Revision written by go-proton-api v1.0.4 and Proton-API-Bridge v1.0.5 with test-only keys, as stored by the server, of a crypto-refresh file: the node key, content key packet and its signature are SDK-style stand-ins made with plain gopenpgp; the old library wrote the node passphrase, name, hash, blocks, block signatures, manifest and extended attributes.
The legacy `Comment` is unchanged.
- **The `frozenFileDir` comment** (`download_internal_test.go`) now notes the exception. The old library writes v6 content only as a revision of a file another client created, so in `v6` the node key, the content key packet and its signature are gopenpgp SDK-style stand-ins.
- **One shared name and size.** The new constants `frozenFileName = "Résumé – final.txt"` and `frozenFileSize = 2500` are used both by the `TestOldLibraryFileReadable` loop and by its `-update` branch. The loop previously used "Fichiers d'été.txt". I picked the frozen name so that the legacy fixture still matches without being regenerated.
### Regeneration
- **Command:** `RCLONE_CONFIG=/notfound go test -count=1 ./backend/protondrive/ -run 'TestOldLibraryFileReadable/v6' -update` gave `ok`.
- **Only the v6 fixture changed.** `git status` showed changes to exactly these four files:
- `testdata/oldlib/file/v6/block-1.bin`
- `testdata/oldlib/file/v6/file.json`
- `testdata/oldlib/file/v6/link.json`
- `testdata/oldlib/file/v6/revision.json`
- **All four were rewritten.** Regenerating makes new random keys, so the whole v6 fixture is new.
- **Untouched:** `testdata/oldlib/file/legacy/*` and Task 6's `testdata/oldlib/{file,folder}.json`.
### Verification
- `RCLONE_CONFIG=/notfound go test -race -count=1 ./backend/protondrive/... -run 'Frozen|OldLibrary|OldWriter|NewWriter|StoredFile'`:
- `ok github.com/rclone/rclone/backend/protondrive 16.042s`
- `ok github.com/rclone/rclone/backend/protondrive/protontest 2.621s`
- That run covers `TestFrozenFixtures`, `TestFrozenFileFixture`, `TestOldLibraryFileReadable`, `TestOldWriterNewReader`, `TestNewWriterOldReader` and `TestStoredFileRoundTrip`. All pass.
- `go vet ./backend/protondrive/...`: ok.
- `golangci-lint run ./backend/protondrive/...`: 0 issues.
Loaded 100 of 189 files, more files were not shown because too many files have changed in this diff. Show more