Files
Robin Jarry 635f1fb499 jmap: avoid crash when server returns garbage
For some reason, a JMAP server may send message details with empty
blobId values. This is invalid but it should not cause a crash.

Error: runtime error: slice bounds out of range [-2:]

goroutine 16 [running]:
git.sr.ht/~rjarry/aerc/worker/jmap/cache.(*JMAPCache).blobPath()
	git.sr.ht/~rjarry/aerc/worker/jmap/cache/blob.go:43 +0x95
git.sr.ht/~rjarry/aerc/worker/jmap/cache.(*JMAPCache).GetBlob()
	git.sr.ht/~rjarry/aerc/worker/jmap/cache/blob.go:11 +0x18
git.sr.ht/~rjarry/aerc/worker/jmap.(*JMAPWorker).handleFetchMessageBodyPart()
	git.sr.ht/~rjarry/aerc/worker/jmap/fetch.go:116 +0x26f
git.sr.ht/~rjarry/aerc/worker/jmap.(*JMAPWorker).handleMessage()
	git.sr.ht/~rjarry/aerc/worker/jmap/worker.go:142 +0x25f
git.sr.ht/~rjarry/aerc/worker/jmap.(*JMAPWorker).Run()
	git.sr.ht/~rjarry/aerc/worker/jmap/worker.go:177 +0x105
git.sr.ht/~rjarry/aerc/app.NewAccountView.func3()
	git.sr.ht/~rjarry/aerc/app/account.go:105 +0x57
created by git.sr.ht/~rjarry/aerc/app.NewAccountView in goroutine 1
	git.sr.ht/~rjarry/aerc/app/account.go:98 +0x468

Ignore a blobId when it is an empty string.

Signed-off-by: Robin Jarry <robin@jarry.cc>
Reviewed-by: Tristan Partin <tristan@partin.io>
2024-01-20 00:29:19 +01:00

46 lines
817 B
Go

package cache
import (
"os"
"path"
"git.sr.ht/~rockorager/go-jmap"
)
func (c *JMAPCache) GetBlob(id jmap.ID) ([]byte, error) {
fpath := c.blobPath(id)
if fpath == "" {
return nil, notfound
}
return os.ReadFile(fpath)
}
func (c *JMAPCache) PutBlob(id jmap.ID, buf []byte) error {
fpath := c.blobPath(id)
if fpath == "" {
return nil
}
_ = os.MkdirAll(path.Dir(fpath), 0o700)
return os.WriteFile(fpath, buf, 0o600)
}
func (c *JMAPCache) DeleteBlob(id jmap.ID) error {
fpath := c.blobPath(id)
if fpath == "" {
return nil
}
defer func() {
_ = os.Remove(path.Dir(fpath))
}()
return os.Remove(fpath)
}
func (c *JMAPCache) blobPath(id jmap.ID) string {
if c.blobsDir == "" || id == "" {
return ""
}
name := string(id)
sub := name[len(name)-2:]
return path.Join(c.blobsDir, sub, name)
}