fix: #8 content size lies

master
Caj Larsson 3 years ago
parent 82067ca87c
commit 67e137a474

@ -6,7 +6,11 @@ type FileSizeQuota struct {
}
func (f *FileSizeQuota) Allows(size int64) bool {
return f.CurrentUsage+size <= f.AllowanceKB
return f.Remaining() >= size
}
func (f *FileSizeQuota) Remaining() int64 {
return f.AllowanceKB - f.CurrentUsage
}
func (f *FileSizeQuota) Add(size int64) error {

@ -73,7 +73,22 @@ func (s SwampFileService) SaveFile(ref swampfile.FileReference, src io.Reader, s
return err
}
io.Copy(f, src)
written, err := io.CopyN(f, src, size)
if written < size {
s.swamp_file_repo.Delete(ref.Path, strconv.FormatInt(ns.ID, 10))
return swampfile.ErrContentSizeExaggerated
}
var buf = make([]byte, 1)
overread, err :=src.Read(buf)
if overread > 0 || err != io.EOF {
s.swamp_file_repo.Delete(ref.Path, strconv.FormatInt(ns.ID, 10))
return swampfile.ErrContentSizeExceeded
}
f.Close()
ns.FileQuota.Add(size)
s.namespace_repo.Update(ns.ID, *ns)

@ -105,3 +105,31 @@ func TestPathStrictMode(t *testing.T) {
err = s.SaveFile(ref, ns_file, int64(ns_file.Len()))
is.Equal(err, swampfile.ErrUnacceptablePath)
}
func TestQuotaWithContenSizeLieOver(t *testing.T) {
is := is.New(t)
s := NewTestSwampFileService()
largefakefile := bytes.NewBufferString("")
for largefakefile.Len() < 64000 {
_, _ = largefakefile.WriteString("A very repetitive file")
}
err := s.SaveFile(file_ref3, largefakefile, int64(10))
is.Equal(err, swampfile.ErrContentSizeExceeded)
}
func TestQuotaWithContenSizeLieUnder(t *testing.T) {
is := is.New(t)
s := NewTestSwampFileService()
largefakefile := bytes.NewBufferString("small")
err := s.SaveFile(file_ref3, largefakefile, int64(1024))
is.Equal(err, swampfile.ErrContentSizeExaggerated)
}

@ -16,6 +16,8 @@ var (
ErrDuplicate = errors.New("record already exists")
ErrExceedQuota = errors.New("file too large")
ErrQuotaInvalid = errors.New("quota invalid")
ErrContentSizeExceeded = errors.New("content size exceeded")
ErrContentSizeExaggerated = errors.New("content size exaggerated")
ErrNotExists = errors.New("row not exists")
ErrUpdateFailed = errors.New("update failed")
ErrDeleteFailed = errors.New("delete failed")

Loading…
Cancel
Save