lib/protocol: Ensure correct blocksize on enc. fileinfo (ref #7861) (#7870)

This commit is contained in:
Simon Frei 2021-08-04 23:12:01 +02:00 committed by GitHub
parent e56e8b7aa1
commit 50aacdf1f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 14 deletions

View File

@ -122,10 +122,10 @@ func (f FileInfo) FileSize() int64 {
}
func (f FileInfo) BlockSize() int {
if f.RawBlockSize == 0 {
if f.RawBlockSize < MinBlockSize {
return MinBlockSize
}
return int(f.RawBlockSize)
return f.RawBlockSize
}
func (f FileInfo) FileName() string {

View File

@ -317,18 +317,20 @@ func encryptFileInfo(fi FileInfo, folderKey *[keySize]byte) FileInfo {
typ = FileInfoTypeDirectory
}
enc := FileInfo{
Name: encryptName(fi.Name, folderKey),
Type: typ,
Size: offset, // new total file size
Permissions: 0644,
ModifiedS: 1234567890, // Sat Feb 14 00:31:30 CET 2009
Deleted: fi.Deleted,
RawInvalid: fi.IsInvalid(),
Version: version,
Sequence: fi.Sequence,
RawBlockSize: fi.RawBlockSize + blockOverhead,
Blocks: blocks,
Encrypted: encryptedFI,
Name: encryptName(fi.Name, folderKey),
Type: typ,
Permissions: 0644,
ModifiedS: 1234567890, // Sat Feb 14 00:31:30 CET 2009
Deleted: fi.Deleted,
RawInvalid: fi.IsInvalid(),
Version: version,
Sequence: fi.Sequence,
Encrypted: encryptedFI,
}
if typ == FileInfoTypeFile {
enc.Size = offset // new total file size
enc.Blocks = blocks
enc.RawBlockSize = fi.BlockSize() + blockOverhead
}
return enc

View File

@ -142,6 +142,9 @@ func TestEnDecryptFileInfo(t *testing.T) {
if bytes.Equal(enc.Blocks[0].Hash, enc.Blocks[1].Hash) {
t.Error("block hashes should not repeat when on different offsets")
}
if enc.RawBlockSize < MinBlockSize {
t.Error("Too small raw block size:", enc.RawBlockSize)
}
again := encryptFileInfo(fi, &key)
if !bytes.Equal(enc.Blocks[0].Hash, again.Blocks[0].Hash) {
t.Error("block hashes should remain stable (0)")