diff --git a/backend/protondrive/export_test.go b/backend/protondrive/export_test.go new file mode 100644 index 000000000..a7d9cd1e1 --- /dev/null +++ b/backend/protondrive/export_test.go @@ -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 +} diff --git a/backend/protondrive/protontest/auth.go b/backend/protondrive/protontest/auth.go index 91b4b9a06..fd8953b89 100644 --- a/backend/protondrive/protontest/auth.go +++ b/backend/protondrive/protontest/auth.go @@ -239,7 +239,7 @@ func (s *Server) NewSession(u *User) 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: randomID(), RefreshToken: randomID()}, + Session: Session{UID: randomID(), AccessToken: s.newSecretLocked(), RefreshToken: s.newSecretLocked()}, user: u, locked: locked, } @@ -344,8 +344,8 @@ func (s *Server) handleRefresh(w http.ResponseWriter, r *http.Request) { writeError(w, http.StatusUnprocessableEntity, api.CodeRefreshTokenInvalid, "protontest: invalid refresh token") return } - sess.AccessToken = randomID() - sess.RefreshToken = randomID() + sess.AccessToken = s.newSecretLocked() + sess.RefreshToken = s.newSecretLocked() sess.expired = false writeJSON(w, http.StatusOK, &api.AuthRefreshResponse{ Code: api.CodeOK, @@ -497,8 +497,8 @@ func (s *Server) handleKeySalts(w http.ResponseWriter, r *http.Request, u *User) // 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)))} - selector := randomID() s.mu.Lock() + selector := s.newSecretLocked() s.forks[selector] = f s.mu.Unlock() writeJSON(w, http.StatusOK, &api.ForkInitResponse{Code: api.CodeOK, Selector: selector, UserCode: f.userCode}) diff --git a/backend/protondrive/protontest/drive.go b/backend/protondrive/protontest/drive.go index 165c39b9a..ede862ab5 100644 --- a/backend/protondrive/protontest/drive.go +++ b/backend/protondrive/protontest/drive.go @@ -715,14 +715,7 @@ func (u *User) SeedRevision(id string, content []byte, modTime time.Time) (revis if l == nil || l.typ != linkTypeFile { panic("protontest: no file " + id) } - key, err := v.unlock(id) - if key == nil { - panic(fmt.Sprintf("protontest: unlock file %s: %v", id, err)) - } - sk, err := decryptContentKey(l.contentKeyPacket, l.contentKeyPacketSignature, key, v.verifier(l.signatureEmail, key), v.now) - if sk == nil { - panic(fmt.Sprintf("protontest: content key of %s: %v", id, err)) - } + key, sk := v.contentKey(l) ck := contentKey{sk: sk, aead: sk.IsV6()} rev := ck.newRevision(content, key, v.addrKey, v.email, modTime.UTC().Format("2006-01-02T15:04:05.000Z"), false, nil) u.srv.mu.Lock() @@ -774,6 +767,12 @@ func (u *User) insertLink(parentID string, l *link) string { // - "swap-blocks": the first two blocks trade places, each still // matching its own hash // - "truncate-blocks": the last block is dropped from the revision +// - "rollback": the active revision serves the blocks of the revision +// before it, which SeedRevision makes, under its own ID, manifest +// signature and extended attributes +// - "foreign-block": the first block is the same plaintext encrypted +// with another content key, with its hash and block signature to +// match func (u *User) Corrupt(id string, what string) { impostor := generateKey("impostor", "impostor@proton.test") v := u.view() @@ -821,6 +820,33 @@ func (u *User) Corrupt(id string, what string) { needBlocks(r, 1) r.blocks = r.blocks[:len(r.blocks)-1] }) + case "rollback": + var previous *revision + for _, r := range l.revisions { + if r.state == revisionStateObsolete { + previous = r + } + } + if previous == nil { + panic("protontest: no earlier revision of " + id) + } + change = changeActiveRevision(func(r *revision) { r.blocks = slices.Clone(previous.blocks) }) + case "foreign-block": + key, sk := v.contentKey(l) + rev := l.activeRevision() + needBlocks(rev, 1) + first := rev.blocks[0] + plain, err := decryptBlock(first.data, first.encSignature, sk, key, v.verifier(first.signatureEmail, key), v.now) + if plain == nil { + mustCrypto("decrypt block", err) + } + data, encSig := newContentKey(key, sk.IsV6()).encryptBlock(plain, key, v.addrKey) + sum := sha256.Sum256(data) + change = changeActiveRevision(func(r *revision) { + b := *r.blocks[0] + b.data, b.hash, b.encSignature = data, sum[:], encSig + r.blocks[0] = &b + }) default: panic("protontest: unknown corruption " + what) } @@ -829,6 +855,39 @@ func (u *User) Corrupt(id string, what string) { change(u.mustDriveLocked().links[id]) } +// contentKey returns the node key and content session key of file l, +// ignoring signature errors. It panics if either can't be decrypted. +func (v *driveView) contentKey(l *link) (*crypto.Key, *crypto.SessionKey) { + key, err := v.unlock(l.id) + if key == nil { + panic(fmt.Sprintf("protontest: unlock file %s: %v", l.id, err)) + } + sk, err := decryptContentKey(l.contentKeyPacket, l.contentKeyPacketSignature, key, v.verifier(l.signatureEmail, key), v.now) + if sk == nil { + panic(fmt.Sprintf("protontest: content key of %s: %v", l.id, err)) + } + return key, sk +} + +// NodeSecrets returns the decrypted node passphrase and content session +// key of file id, for tests to check a client never reveals them. It +// panics if id is not a file or they can't be decrypted. +func (u *User) NodeSecrets(id string) (passphrase, sessionKey []byte) { + v := u.view() + l := v.d.links[id] + if l == nil || l.typ != linkTypeFile { + panic("protontest: no file " + id) + } + _, sk := v.contentKey(l) + parent, err := v.parentKey(l) + if parent == nil { + panic(fmt.Sprintf("protontest: parent key of %s: %v", id, err)) + } + passphrase, err = decryptArmored(l.nodePass.armored, parent, nil, v.now) + mustCrypto("decrypt passphrase", err) + return passphrase, sk.Key +} + // needBlocks panics unless r has at least n blocks. func needBlocks(r *revision, n int) { if len(r.blocks) < n { @@ -1590,7 +1649,7 @@ func (s *Server) handleRevision(w http.ResponseWriter, r *http.Request, u *User, SignatureEmail: nullable(b.signatureEmail), } if !noURLs { - id, token := randomID(), randomID() + id, token := randomID(), s.newSecretLocked() s.downloads[id] = &download{token: token, block: b} bareURL := s.URL() + "/storage/blocks/" + id bj.Token, bj.BareURL = &token, &bareURL diff --git a/backend/protondrive/protontest/drive_test.go b/backend/protondrive/protontest/drive_test.go index 0f17ebd6f..aeddee863 100644 --- a/backend/protondrive/protontest/drive_test.go +++ b/backend/protondrive/protontest/drive_test.go @@ -197,11 +197,14 @@ func TestSeedFormats(t *testing.T) { } func TestCorrupt(t *testing.T) { - for _, what := range []string{"name", "name-signature", "passphrase-signature", "block-hash", "manifest", "swap-blocks", "truncate-blocks"} { + for _, what := range []string{"name", "name-signature", "passphrase-signature", "block-hash", "manifest", "swap-blocks", "truncate-blocks", "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 == "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() { @@ -588,6 +591,34 @@ func TestSeedRevision(t *testing.T) { } } +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) + assert.Contains(t, s.IssuedSecrets(), *rev.Revision.Blocks[0].Token) +} + func TestStoredFileRoundTrip(t *testing.T) { s := New(t) addrPass := newPassphrase() diff --git a/backend/protondrive/protontest/server.go b/backend/protondrive/protontest/server.go index f691d4452..1b18cef86 100644 --- a/backend/protondrive/protontest/server.go +++ b/backend/protondrive/protontest/server.go @@ -12,6 +12,7 @@ import ( "net/http" "net/http/httptest" "regexp" + "slices" "strconv" "strings" "sync" @@ -84,6 +85,7 @@ type Server struct { 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 []string // every secret issued, in order } // New starts a fake server which is closed when the test ends. @@ -173,6 +175,23 @@ func (s *Server) ResetRequests() { s.storagePeak = s.storageUploads } +// newSecretLocked returns a new random secret and records it for +// IssuedSecrets. s.mu must be held. +func (s *Server) newSecretLocked() string { + secret := randomID() + s.secrets = append(s.secrets, secret) + return secret +} + +// IssuedSecrets returns every secret the server has handed to a +// client: session access and refresh tokens, fork selectors, and block +// upload and download tokens. +func (s *Server) IssuedSecrets() []string { + s.mu.Lock() + defer s.mu.Unlock() + return slices.Clone(s.secrets) +} + // 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) { diff --git a/backend/protondrive/protontest/upload.go b/backend/protondrive/protontest/upload.go index 81358a1b0..b6cd1d566 100644 --- a/backend/protondrive/protontest/upload.go +++ b/backend/protondrive/protontest/upload.go @@ -426,7 +426,7 @@ func (s *Server) handleRequestUpload(w http.ResponseWriter, r *http.Request, u * resp := &blockUploadResponse{UploadLinks: []blockURLJSON{}, ThumbnailLinks: []struct{}{}, Code: api.CodeOK} for _, b := range blocks { rev.pending[b.index] = b - id, token := randomID(), randomID() + id, token := randomID(), s.newSecretLocked() 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}) diff --git a/backend/protondrive/security_test.go b/backend/protondrive/security_test.go new file mode 100644 index 000000000..4af2e4197 --- /dev/null +++ b/backend/protondrive/security_test.go @@ -0,0 +1,528 @@ +package protondrive_test + +import ( + "bytes" + "context" + "encoding/base64" + "encoding/hex" + "fmt" + "io" + "log/slog" + "os" + "path/filepath" + "runtime" + "strings" + "sync" + "testing" + "time" + + "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/log" + "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" +) + +// blockSize is the plaintext size of a full block. +const blockSize = 4 << 20 + +// patternedContent returns n bytes, seeded by seed, which differ per +// block. +func patternedContent(n int, seed byte) []byte { + b := make([]byte, n) + for i := range b { + b[i] = byte(i*7+i/blockSize) ^ seed + } + return b +} + +// readAll opens o and reads it to the end, returning every byte the +// reader released and the first error, from Open or Read. +func readAll(ctx context.Context, o fs.Object) ([]byte, error) { + rc, err := o.Open(ctx) + if err != nil { + return nil, err + } + got, err := io.ReadAll(rc) + if closeErr := rc.Close(); err == nil { + err = closeErr + } + return got, err +} + +// requireCopyFails copies remote from src to a new local directory and +// checks the copy fails and leaves nothing behind. +func requireCopyFails(ctx context.Context, t *testing.T, src fs.Fs, remote string) { + t.Helper() + dst := newFakeFs(t, "", t.TempDir()) + err := operations.CopyFile(ctx, dst, src, remote, remote) + require.Error(t, err, "copied %s", remote) + entries, err := os.ReadDir(dst.Root()) + require.NoError(t, err) + assert.Empty(t, entries, "the failed copy left files behind") +} + +// hostileContent is a file whose content a hostile server tampers with. +type hostileContent struct { + name string + content []byte + // prepare seeds the file in u's root and tampers with it, + // returning its ID. + prepare func(u *protontest.User, content []byte) string + // signatureOnly is set if only a signature check catches the + // tampering, which ignore_signature_errors turns off. + signatureOnly bool + // released is how many bytes the reader may hand out before its + // error: those of the blocks read before the check which fails. + released int +} + +// seedCorrupted returns a prepare which seeds the file and applies the +// fake's corruption what. +func seedCorrupted(what string) func(u *protontest.User, content []byte) string { + return func(u *protontest.User, content []byte) string { + id := u.SeedFile(u.RootID(), "victim.bin", content, time.Now()) + u.Corrupt(id, what) + return id + } +} + +// seedRolledBack returns a prepare which seeds the file with old +// content, gives it a new revision with the content, then serves the +// old revision's blocks under the new revision. +func seedRolledBack(old []byte) func(u *protontest.User, content []byte) string { + return func(u *protontest.User, content []byte) string { + id := u.SeedFile(u.RootID(), "victim.bin", old, time.Now()) + u.SeedRevision(id, content, time.Now()) + u.Corrupt(id, "rollback") + return id + } +} + +// hostileContents are the hostile content cases of spec section 9.6. +var hostileContents = []hostileContent{{ + name: "wrong block hash", + content: patternedContent(blockSize+10, 1), + prepare: seedCorrupted("block-hash"), +}, { + name: "swapped blocks", + content: patternedContent(2*blockSize+10, 2), + prepare: seedCorrupted("swap-blocks"), + signatureOnly: true, + released: 2*blockSize + 10, +}, { + name: "swapped blocks of different sizes", + content: patternedContent(blockSize+10, 3), + prepare: seedCorrupted("swap-blocks"), +}, { + name: "truncated block list", + content: patternedContent(blockSize+10, 4), + prepare: seedCorrupted("truncate-blocks"), + released: blockSize, +}, { + name: "older revision content under the current revision ID", + content: patternedContent(blockSize+10, 5), + prepare: seedRolledBack(patternedContent(blockSize+10, 6)), + signatureOnly: true, + released: blockSize + 10, +}, { + name: "older revision content of another size under the current revision ID", + content: patternedContent(20, 7), + prepare: seedRolledBack(patternedContent(10, 8)), +}, { + name: "block from another file", + content: patternedContent(blockSize+10, 9), + prepare: seedCorrupted("foreign-block"), +}, { + name: "attacker-signed passphrase", + content: patternedContent(100, 10), + prepare: seedCorrupted("passphrase-signature"), + signatureOnly: true, +}} + +// hostileNames are names a hostile server gives nodes, each of which +// must be left out of listings. +var hostileNames = []string{"../x", "a/b", "nul\x00name", "..", ".", strings.Repeat("x", 10<<20)} + +// TestHostileServer checks a hostile server can't get rclone to return +// tampered content as the file's, or a path outside the listed +// directory. +func TestHostileServer(t *testing.T) { + for _, tc := range hostileContents { + t.Run(tc.name, func(t *testing.T) { + ctx := context.Background() + remote, _, u := setupFakeRemote(t) + tc.prepare(u, tc.content) + + f := newFakeFs(t, remote, "") + o, err := f.NewObject(ctx, "victim.bin") + require.NoError(t, err, "the file is listed") + got, err := readAll(ctx, o) + require.Error(t, err, "read without error") + assert.LessOrEqual(t, len(got), tc.released, "bytes released before the error") + assert.False(t, bytes.Equal(got, tc.content), "the content was returned") + requireCopyFails(ctx, t, f, "victim.bin") + + // Turning signature checks off leaves the integrity + // checks, and the hash check of a copy. + f = newFakeFs(t, strings.TrimSuffix(remote, ":")+",ignore_signature_errors=true:", "") + o, err = f.NewObject(ctx, "victim.bin") + require.NoError(t, err) + got, err = readAll(ctx, o) + if !tc.signatureOnly { + require.Error(t, err, "read without error with signature checks off") + assert.LessOrEqual(t, len(got), tc.released, "bytes released before the error") + } + if bytes.Equal(got, tc.content) { + // Only a signature was tampered with, and the content + // is the file's. + return + } + requireCopyFails(ctx, t, f, "victim.bin") + }) + } + + t.Run("names", func(t *testing.T) { + // Its own stats, so the sync sees only its own errors. + ctx := accounting.WithStatsGroup(context.Background(), "hostile names") + remote, _, u := setupFakeRemote(t) + dir := u.SeedFolder(u.RootID(), "dir") + for i, name := range hostileNames { + u.SeedFile(dir, name, []byte("hostile file"), time.Now()) + // At the root, "../x" would land beside a sync's destination. + u.SeedFile(u.RootID(), name, []byte("hostile file"), time.Now()) + sub := u.SeedFolder(u.RootID(), fmt.Sprintf("sub %d", i)) + u.SeedFolder(sub, name) + } + // A legitimate name rclone shows with a lookalike of /. + u.SeedFile(dir, "a/b", []byte("fine"), time.Now()) + u.SeedFile(dir, "ok.txt", []byte("fine"), time.Now()) + f := newFakeFs(t, remote, "") + + var before runtime.MemStats + runtime.ReadMemStats(&before) + start := time.Now() + entries, err := f.List(ctx, "dir") + elapsed := time.Since(start) + var after runtime.MemStats + runtime.ReadMemStats(&after) + require.NoError(t, err) + t.Logf("listing took %v and allocated %d MiB, the fake's encoding included", elapsed, (after.TotalAlloc-before.TotalAlloc)>>20) + assert.Less(t, elapsed, 30*time.Second) + + var remotes []string + for _, entry := range entries { + remotes = append(remotes, entry.Remote()) + leaf, ok := strings.CutPrefix(entry.Remote(), "dir/") + assert.True(t, ok, "%q is outside dir", entry.Remote()) + assert.NotContains(t, leaf, "/") + assert.NotContains(t, leaf, "\x00") + assert.NotEqual(t, "..", leaf) + assert.NotEqual(t, ".", leaf) + } + assert.Len(t, remotes, 2, "%q", remotes) + assert.Contains(t, remotes, "dir/ok.txt") + + for _, entry := range mustList(ctx, t, f, "") { + if d, ok := entry.(fs.Directory); ok && strings.HasPrefix(d.Remote(), "sub ") { + assert.Empty(t, mustList(ctx, t, f, d.Remote()), "hostile folder listed in %q", d.Remote()) + } + } + + // A sync of the whole remote writes only inside its + // destination. + parent := t.TempDir() + dst := newFakeFs(t, "", filepath.Join(parent, "dst")) + require.NoError(t, fssync.Sync(ctx, dst, newFakeFs(t, remote, ""), false)) + var written []string + require.NoError(t, filepath.WalkDir(parent, func(path string, d os.DirEntry, err error) error { + if err != nil { + return err + } + rel, err := filepath.Rel(parent, path) + written = append(written, filepath.ToSlash(rel)) + return err + })) + assert.NotContains(t, written, "x") + for _, path := range written { + assert.True(t, path == "." || path == "dst" || strings.HasPrefix(path, "dst/"), "%q written outside the destination", path) + } + assert.Contains(t, written, "dst/dir/ok.txt") + }) +} + +// mustList lists dir of f. +func mustList(ctx context.Context, t *testing.T, f fs.Fs, dir string) fs.DirEntries { + t.Helper() + entries, err := f.List(ctx, dir) + require.NoError(t, err) + return entries +} + +// logCapture holds rclone's log output while it is captured. +type logCapture struct { + mu sync.Mutex + buf strings.Builder +} + +// String returns the log output captured so far. +func (c *logCapture) String() string { + c.mu.Lock() + defer c.mu.Unlock() + return c.buf.String() +} + +// captureLog captures rclone's log output at debug level, with the HTTP +// dumps of dump, until the test ends. +func captureLog(t *testing.T, dump fs.DumpFlags) *logCapture { + c := &logCapture{} + log.Handler.SetOutput(func(level slog.Level, text string) { + c.mu.Lock() + defer c.mu.Unlock() + c.buf.WriteString(text) + }) + oldLevel := log.Handler.SetLevel(slog.LevelDebug) + ci := fs.GetConfig(context.Background()) + oldLogLevel, oldDump := ci.LogLevel, ci.Dump + ci.LogLevel, ci.Dump = fs.LogLevelDebug, dump + t.Cleanup(func() { + ci.LogLevel, ci.Dump = oldLogLevel, oldDump + log.Handler.SetLevel(oldLevel) + log.Handler.ResetOutput() + }) + return c +} + +// secretForms returns the forms a secret could take in a log line. +func secretForms(secret []byte) []string { + forms := []string{base64.StdEncoding.EncodeToString(secret), base64.RawURLEncoding.EncodeToString(secret), hex.EncodeToString(secret)} + if len(secret) >= 8 { + forms = append(forms, string(secret)) + } + return forms +} + +// assertNoSecrets checks log contains no form of any of secrets, +// described by what. +func assertNoSecrets(t *testing.T, log, what string, secrets ...[]byte) { + t.Helper() + for i, secret := range secrets { + require.NotEmpty(t, secret, "%s %d is empty", what, i) + for _, form := range secretForms(secret) { + assert.False(t, strings.Contains(log, form), "the log holds %s %d as %q", what, i, form) + } + } +} + +// secretRun is what a run of signing in, uploading, downloading and +// disconnecting revealed to the test. +type secretRun struct { + log string + passwords [][]byte // the password and mailbox password + keyPassword []byte + sessionTokens [][]byte // every access and refresh token saved + storageTokens [][]byte // every block upload and download token + nodeSecrets [][]byte // node passphrases and content session keys +} + +// runSignedInSession signs in with a password, uploads a 5 MiB file, +// refreshes the session, downloads files and disconnects, with rclone's +// log captured with dump. +func runSignedInSession(t *testing.T, dump fs.DumpFlags) secretRun { + ctx := context.Background() + const password, mailboxPassword = "password-Hunter2-3f9c1a", "mailbox-Hunter2-7b2d4e" + srv := protontest.New(t) + u := srv.AddTwoPasswordUser("user", password, mailboxPassword) + stored := u.SeedFile(u.RootID(), "stored.bin", patternedContent(blockSize+10, 11), time.Now()) + damaged := u.SeedFile(u.RootID(), "damaged.bin", patternedContent(100, 12), time.Now()) + u.Corrupt(damaged, "block-hash") + m := newLockedMap(configmap.Simple{ + "endpoint": srv.URL(), + "username": "user", + "password": obscure.MustObscure(password), + "mailbox_password": obscure.MustObscure(mailboxPassword), + "original_file_size": "true", + }) + + capture := captureLog(t, dump) + f, err := protondrive.NewFs(ctx, "secrets", "", m) + require.NoError(t, err) + content := []byte(random.String(5 << 20)) + uploaded, err := operations.Copy(ctx, f, nil, "upload.bin", object.NewMemoryObject("upload.bin", time.Now(), content)) + require.NoError(t, err) + + uid, _ := m.Get("client_uid") + srv.ExpireAccessToken(uid) + f.(fs.DirCacheFlusher).DirCacheFlush() + for _, name := range []string{"upload.bin", "stored.bin"} { + o, err := f.NewObject(ctx, name) + require.NoError(t, err) + _, err = readAll(ctx, o) + require.NoError(t, err, name) + } + o, err := f.NewObject(ctx, "damaged.bin") + require.NoError(t, err) + _, err = readAll(ctx, o) + require.Error(t, err) + require.NoError(t, f.Features().Disconnect(ctx)) + + run := secretRun{ + log: capture.String(), + passwords: [][]byte{[]byte(password), []byte(mailboxPassword)}, + keyPassword: []byte(u.KeyPassword()), + } + saved := map[string]bool{} + for _, key := range []string{"client_access_token", "client_refresh_token"} { + for _, token := range m.values(key) { + if token != "" { + saved[token] = true + run.sessionTokens = append(run.sessionTokens, []byte(token)) + } + } + } + require.NotEmpty(t, run.sessionTokens) + for _, secret := range srv.IssuedSecrets() { + if !saved[secret] { + run.storageTokens = append(run.storageTokens, []byte(secret)) + } + } + require.NotEmpty(t, run.storageTokens) + for _, id := range []string{uploaded.(fs.IDer).ID(), stored, damaged} { + passphrase, sessionKey := u.NodeSecrets(id) + run.nodeSecrets = append(run.nodeSecrets, passphrase, sessionKey) + } + require.Contains(t, run.log, "refreshing session", "debug output not captured") + return run +} + +// TestNoSecretsInLogs checks no secret reaches rclone's log at debug +// level across sign-in, upload, download and Disconnect. +func TestNoSecretsInLogs(t *testing.T) { + t.Run("debug", func(t *testing.T) { + run := runSignedInSession(t, 0) + assertNoSecrets(t, run.log, "password", run.passwords...) + assertNoSecrets(t, run.log, "key password", run.keyPassword) + assertNoSecrets(t, run.log, "session token", run.sessionTokens...) + assertNoSecrets(t, run.log, "pm-storage-token", run.storageTokens...) + assertNoSecrets(t, run.log, "node secret", run.nodeSecrets...) + }) + + // --dump headers shows the headers fshttp doesn't redact, of + // which pm-storage-token and x-pm-uid are Proton's, but never the + // Authorization header with the access token. --dump bodies shows + // the tokens in sign-in and refresh responses, as spec section 7 + // says, so isn't checked. + t.Run("dump headers", func(t *testing.T) { + run := runSignedInSession(t, fs.DumpHeaders) + require.Contains(t, run.log, "HTTP REQUEST", "headers not dumped") + assertNoSecrets(t, run.log, "password", run.passwords...) + assertNoSecrets(t, run.log, "key password", run.keyPassword) + assertNoSecrets(t, run.log, "session token", run.sessionTokens...) + assertNoSecrets(t, run.log, "node secret", run.nodeSecrets...) + }) +} + +// allValues returns every value m held or was set to, by key. +func allValues(m *lockedMap) map[string][]string { + m.mu.Lock() + defer m.mu.Unlock() + all := map[string][]string{} + for k, v := range m.m { + all[k] = append(all[k], v) + } + for k, vs := range m.set { + all[k] = append(all[k], vs...) + } + return all +} + +// TestConfigHoldsNoPasswordAfterBrowserSignIn checks browser sign-in +// leaves no password in the config, and logs none of its secrets. +func TestConfigHoldsNoPasswordAfterBrowserSignIn(t *testing.T) { + ctx := context.Background() + const password = "browser-Hunter2-9e8d7c" + srv := protontest.New(t) + u := srv.AddUser("user", password) + m := newLockedMap(configmap.Simple{"endpoint": srv.URL()}) + capture := captureLog(t, 0) + var signInURL string + require.NoError(t, protondrive.ConfigBrowserSignIn(ctx, "browser", m, func(shown string) { + signInURL = shown + require.NoError(t, srv.ApproveFork(shown, u)) + })) + + _, err := protondrive.NewFs(ctx, "browser", "", m) + require.NoError(t, err, "the session works") + for _, key := range []string{"password", "mailbox_password"} { + assert.Empty(t, m.values(key), "%s set", key) + } + for key, values := range allValues(m) { + for _, v := range values { + assert.NotContains(t, v, password, "%s holds the password", key) + if v != "" { + revealed, err := obscure.Reveal(v) + assert.False(t, err == nil && strings.Contains(revealed, password), "%s holds the password obscured", key) + } + } + } + + log := capture.String() + fragment := signInURL[strings.Index(signInURL, "#")+1:] + assert.NotContains(t, log, fragment, "the sign-in URL's secret is logged") + var secrets [][]byte + for _, secret := range srv.IssuedSecrets() { + secrets = append(secrets, []byte(secret)) + } + assertNoSecrets(t, log, "issued secret", secrets...) + assertNoSecrets(t, log, "key password", []byte(u.KeyPassword())) +} + +// TestPasswordStoredObscured checks password sign-in keeps the password +// obscured in the config, as rclone config stores it, and writes no +// password to the config itself. +func TestPasswordStoredObscured(t *testing.T) { + ctx := context.Background() + const password, mailboxPassword = "stored-Hunter2-1a2b3c", "stored-mailbox-4d5e6f" + srv := protontest.New(t) + srv.AddTwoPasswordUser("user", password, mailboxPassword) + + // rclone config obscures the options marked as passwords. + info, err := fs.Find("protondrive") + require.NoError(t, err) + for _, key := range []string{"password", "mailbox_password", "otp_secret_key"} { + opt := info.Options.Get(key) + require.NotNil(t, opt, key) + assert.True(t, opt.IsPassword, "%s is not obscured", key) + } + + m := newLockedMap(configmap.Simple{ + "endpoint": srv.URL(), + "username": "user", + "password": obscure.MustObscure(password), + "mailbox_password": obscure.MustObscure(mailboxPassword), + }) + _, err = protondrive.NewFs(ctx, "obscured", "", m) + require.NoError(t, err) + for key, plain := range map[string]string{"password": password, "mailbox_password": mailboxPassword} { + stored, _ := m.Get(key) + assert.NotEqual(t, plain, stored, key) + revealed, err := obscure.Reveal(stored) + require.NoError(t, err, key) + assert.Equal(t, plain, revealed, key) + } + for key, values := range allValues(m) { + for _, v := range values { + assert.NotContains(t, v, password, "%s holds the password", key) + assert.NotContains(t, v, mailboxPassword, "%s holds the mailbox password", key) + } + } + assert.NotEmpty(t, m.values("client_uid"), "the session is saved") +} diff --git a/docs/DECISIONS.md b/docs/DECISIONS.md index 51a18cf6a..63f24fe62 100644 --- a/docs/DECISIONS.md +++ b/docs/DECISIONS.md @@ -5,6 +5,24 @@ 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-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 diff --git a/docs/TODO.md b/docs/TODO.md index 51bbda452..2262e8a87 100644 --- a/docs/TODO.md +++ b/docs/TODO.md @@ -8,6 +8,30 @@ Small, specific, independently actionable. Remove only when actually done. ## Next +- [ ] protondrive: a full read checks the manifest signature only at EOF + (spec 6.4, as the SDK does), so a hostile server serving blocks + swapped, or an older revision's blocks, of the recorded sizes gets + that wrong plaintext handed to the reader before the final `Read` + fails (`TestHostileServer` "swapped blocks", "older revision + content"). `operations.Copy` discards it, but `rclone cat` and a + streaming mount don't. Consider listing every block page and checking + the manifest before releasing the first byte, at least when the block + list fits in one page; with `ignore_signature_errors` only the SHA1 + check of a copy catches these. +- [ ] 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`). Task 20's docs must carry the spec 7 warning; + 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. +- [ ] protondrive: a hostile 10 MiB name is decrypted in full before + `validateName` rejects it (about 230 MiB allocated listing it, the + fake included). Consider refusing an encrypted name over a few KiB + before decrypting it. + - [ ] 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. diff --git a/docs/plans/001-protondrive-tier1.md b/docs/plans/001-protondrive-tier1.md index 8ade72215..6269bae48 100644 --- a/docs/plans/001-protondrive-tier1.md +++ b/docs/plans/001-protondrive-tier1.md @@ -572,7 +572,14 @@ library what must stay readable. (v3 PKESK, SEIPDv1 blocks), `func (u *User) Corrupt(id string, what string)` with `what` one of `"name"`, `"name-signature"`, `"passphrase-signature"`, `"block-hash"`, - `"manifest"`, `"swap-blocks"`, `"truncate-blocks"`. + `"manifest"`, `"swap-blocks"`, `"truncate-blocks"`, and (Task 16) + `"rollback"` (the active revision serves the previous revision's + blocks) and `"foreign-block"` (the first block encrypted with another + content key). + - Secrets for log hygiene tests (Task 16): + `func (s *Server) IssuedSecrets() []string` (every access and refresh + token, fork selector and block upload or download token issued) and + `func (u *User) NodeSecrets(id string) (passphrase, sessionKey []byte)`. - `func (u *User) Dump() []DumpedNode` - decrypted tree for assertions (`ID`, `Path`, `IsFolder`, `Content`, `ModTime`, `SHA1`, `State`, and `Err` joining every decryption, signature, block-hash, manifest and @@ -1154,7 +1161,7 @@ Decisions: - Create: `backend/protondrive/security_test.go` -- [ ] **Step 1: Write the tests** (spec 9.6): +- [x] **Step 1: Write the tests** (spec 9.6): - `TestHostileServer` subtests: wrong block hash, swapped blocks, truncated block list, older revision content under the current revision ID, attacker-signed passphrase, names `"../x"`, `"a/b"`, NUL, @@ -1169,8 +1176,8 @@ Decisions: - `TestConfigHoldsNoPasswordAfterBrowserSignIn`, and `TestPasswordStoredObscured` (the stored value differs from the plaintext and `obscure.Reveal` restores it). -- [ ] **Step 2: Run** - fix until PASS. -- [ ] **Step 3: Commit** `protondrive: add hostile-server and secret-hygiene tests`. +- [x] **Step 2: Run** - fix until PASS. +- [x] **Step 3: Commit** `protondrive: add hostile-server and secret-hygiene tests`. ### Task 17: Request budgets and fuzzing