Files
StereotypicalCatandClaude Opus 5.5 c0bdbcde6b
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
protondrive: rewrite the backend as a native Go client of the Proton Drive API
Replace Proton-API-Bridge and go-proton-api with a client in
backend/protondrive written against Proton's current Drive API, following
the behaviour of Proton's official Drive SDK:

- volume-based and v2 routes only
- upload block verification, signed manifests and extended attributes
- current content encryption (v4 node keys, v6 PKESK, SEIPDv2)
- browser (session fork) sign-in, with username and password still
  supported
- the volume's change events, used to keep the metadata cache current and
  for ChangeNotify, polled at most every 30 s
- an in-memory fake Proton server (protontest) so the tests run without
  an account

Uses ProtonMail/gopenpgp/v3 and ProtonMail/go-srp directly; no cgo.

Written with heavy AI assistance (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 23:22:46 +01:00

192 lines
7.0 KiB
Go

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
}