From 8088002ba6e7ec578be93ef3731bb5f28873ec73 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Thu, 15 Oct 2020 16:03:43 +1100 Subject: [PATCH] Fix error handling for bad torrent adds with safe file handling --- client.go | 3 +++ cmd/torrent/main.go | 7 ++++++- storage/file.go | 2 +- storage/safe-path.go | 2 -- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index 25f7a860..111812c3 100644 --- a/client.go +++ b/client.go @@ -1150,6 +1150,9 @@ func (cl *Client) AddTorrentInfoHashWithStorage(infoHash metainfo.Hash, specStor func (cl *Client) AddTorrentSpec(spec *TorrentSpec) (t *Torrent, new bool, err error) { t, new = cl.AddTorrentInfoHashWithStorage(spec.InfoHash, spec.Storage) err = t.MergeSpec(spec) + if err != nil && new { + t.Drop() + } return } diff --git a/cmd/torrent/main.go b/cmd/torrent/main.go index 83783766..c684b092 100644 --- a/cmd/torrent/main.go +++ b/cmd/torrent/main.go @@ -4,6 +4,7 @@ package main import ( "expvar" "fmt" + stdLog "log" "net" "net/http" "os" @@ -210,6 +211,7 @@ func main() { } func mainErr() error { + stdLog.SetFlags(stdLog.Flags() | stdLog.Lshortfile) var flags struct { tagflag.StartPos Command string @@ -288,7 +290,10 @@ func downloadErr(args []string, parent *tagflag.Parser) error { http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { client.WriteStatus(w) }) - addTorrents(client) + err = addTorrents(client) + if err != nil { + return fmt.Errorf("adding torrents: %w", err) + } defer outputStats(client) if client.WaitAll() { log.Print("downloaded ALL the torrents") diff --git a/storage/file.go b/storage/file.go index 35f1cc40..66b0debf 100644 --- a/storage/file.go +++ b/storage/file.go @@ -73,7 +73,7 @@ func (fs *fileClientImpl) OpenTorrent(info *metainfo.Info, infoHash metainfo.Has for i, fileInfo := range upvertedFiles { s, err := ToSafeFilePath(append([]string{info.Name}, fileInfo.Path...)...) if err != nil { - return nil, fmt.Errorf("file %v has unsafe path %q", i, fileInfo.Path) + return nil, fmt.Errorf("file %v has unsafe path %q: %w", i, fileInfo.Path, err) } f := file{ path: filepath.Join(dir, s), diff --git a/storage/safe-path.go b/storage/safe-path.go index 89796ad6..9e50b7e3 100644 --- a/storage/safe-path.go +++ b/storage/safe-path.go @@ -2,7 +2,6 @@ package storage import ( "errors" - "log" "path/filepath" "strings" ) @@ -21,7 +20,6 @@ func ToSafeFilePath(fileInfoComponents ...string) (string, error) { } safeFilePath := filepath.Join(safeComps...) fc := firstComponent(safeFilePath) - log.Printf("%q", fc) switch fc { case "..": return "", errors.New("escapes root dir")