Files
mesh/encrypted_record.zi
T
waozi cd5bd26508 Send deletions without content only to clients that read them
Deleted records keep no content since the last deploy. Released Breeze
reads such a deletion of its own collections as a record it cannot decrypt
and refuses the whole sync, so a full snapshot ends in a review that cannot
merge. Sync replies and record pages now leave these deletions out unless
the client sends the content-free-deletions capability; the cursor still
moves past them. Entity data keeps its deletions for every client.
2026-10-29 14:37:46 -03:00

104 lines
3.7 KiB
Ziran

#import "std/go_types"
#import "protocol"
identity :: #import "identity";
signing :: #import "signing";
errors :: #import "std/errors_go";
go_strings :: #system_library "go:strings";
builtin :: #system_library "go:builtin";
#scope_file
TrimSpace :: (value: string) -> string #foreign go_strings;
Join :: (values: []string, separator: string) -> string #foreign go_strings;
HasPrefix :: (value: string, prefix: string) -> bool #foreign go_strings;
AllocateRecords :: (count: isize, capacity: isize) -> []EncryptedRecord #foreign builtin "make";
AppendRecord :: (values: []EncryptedRecord, value: EncryptedRecord) -> []EncryptedRecord #foreign builtin "append";
#scope_export
ValidateAccountKey :: (userID: string, publicKey: []u8) -> Error {
if userID != signing.SHA256Hex(publicKey) {
return errors.New("public key hash mismatch")
}
return null
}
ValidMetadata :: (item: EncryptedRecord) -> bool {
if item.FreeBytes < 0 || item.FreeBytes > 524288 {
return false
}
hash := TrimSpace(item.ContentHash)
parent := TrimSpace(item.ParentID)
if hash != "" && !identity.ValidUserID(hash) {
return false
}
if parent != "" && !identity.ValidEncryptedRecordID(parent) {
return false
}
return item.SchemaVersion >= cast(isize)0 && item.SchemaVersion <= cast(isize)65535
}
Valid :: (item: EncryptedRecord) -> bool {
if !identity.ValidNamespace(TrimSpace(item.Collection)) ||
!identity.ValidEncryptedRecordID(TrimSpace(item.ID)) {
return false
}
if TrimSpace(item.UpdatedAt) == "" {
return false
}
if item.DeletedAt == 0 && TrimSpace(item.Ciphertext) == "" {
return false
}
if (item.PackedNonce == "") != (item.PackedCiphertext == "") ||
item.PackedNonce.count > 256 || item.PackedCiphertext.count > 262144 ||
(item.DeletedAt != 0 && item.PackedCiphertext != "") {
return false
}
return item.Ciphertext.count <= 262144 && item.Nonce.count <= 256 &&
item.KeyID.count <= 128 && ValidMetadata(item)
}
ValidForProtocol :: (item: EncryptedRecord, version: isize) -> bool {
if !Valid(item) {
return false
}
if version < cast(isize)6 && item.PackedCiphertext != "" {
return false
}
if version >= cast(isize)5 {
return identity.ValidEncryptedHierarchyCollection(item.Collection) ||
identity.ValidLegacyEncryptedCollection(item.Collection)
}
return true
}
// A client that reads a deleted record without content as a deletion says
// so with this capability. Deleted records keep no content on the node, and
// released clients read one of their app's own collections as a record they
// cannot decrypt, then refuse the whole sync.
ContentFreeDeletions :: "content-free-deletions"
// The records a client can read: without the capability, deleted records of
// apps' own collections that keep no content stay out, as their content is
// gone anyway. Entity data always had such deletions, which every client
// reads.
ReadableBy :: (records: []EncryptedRecord, capabilities: []string) -> []EncryptedRecord {
for capability: capabilities {
if capability == ContentFreeDeletions {
return records
}
}
readable := AllocateRecords(cast(isize)0, cast(isize)records.count)
for item: records {
bare := item.DeletedAt != 0 && item.Nonce == "" && item.Ciphertext == ""
if bare && !HasPrefix(item.Collection, "private.entity.") {
continue
}
readable = AppendRecord(readable, item)
}
return readable
}
ContentKey :: (item: EncryptedRecord) -> string {
parts: [3]string = .[item.ContentHash, "\x00", item.Ciphertext]
return Join(parts[:], "")
}