Fix error handling for bad torrent adds with safe file handling

This commit is contained in:
Matt Joiner 2020-10-15 16:03:43 +11:00
parent 89235e180f
commit 8088002ba6
4 changed files with 10 additions and 4 deletions

View File

@ -1150,6 +1150,9 @@ func (cl *Client) AddTorrentInfoHashWithStorage(infoHash metainfo.Hash, specStor
func (cl *Client) AddTorrentSpec(spec *TorrentSpec) (t *Torrent, new bool, err error) { func (cl *Client) AddTorrentSpec(spec *TorrentSpec) (t *Torrent, new bool, err error) {
t, new = cl.AddTorrentInfoHashWithStorage(spec.InfoHash, spec.Storage) t, new = cl.AddTorrentInfoHashWithStorage(spec.InfoHash, spec.Storage)
err = t.MergeSpec(spec) err = t.MergeSpec(spec)
if err != nil && new {
t.Drop()
}
return return
} }

View File

@ -4,6 +4,7 @@ package main
import ( import (
"expvar" "expvar"
"fmt" "fmt"
stdLog "log"
"net" "net"
"net/http" "net/http"
"os" "os"
@ -210,6 +211,7 @@ func main() {
} }
func mainErr() error { func mainErr() error {
stdLog.SetFlags(stdLog.Flags() | stdLog.Lshortfile)
var flags struct { var flags struct {
tagflag.StartPos tagflag.StartPos
Command string Command string
@ -288,7 +290,10 @@ func downloadErr(args []string, parent *tagflag.Parser) error {
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
client.WriteStatus(w) client.WriteStatus(w)
}) })
addTorrents(client) err = addTorrents(client)
if err != nil {
return fmt.Errorf("adding torrents: %w", err)
}
defer outputStats(client) defer outputStats(client)
if client.WaitAll() { if client.WaitAll() {
log.Print("downloaded ALL the torrents") log.Print("downloaded ALL the torrents")

View File

@ -73,7 +73,7 @@ func (fs *fileClientImpl) OpenTorrent(info *metainfo.Info, infoHash metainfo.Has
for i, fileInfo := range upvertedFiles { for i, fileInfo := range upvertedFiles {
s, err := ToSafeFilePath(append([]string{info.Name}, fileInfo.Path...)...) s, err := ToSafeFilePath(append([]string{info.Name}, fileInfo.Path...)...)
if err != nil { 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{ f := file{
path: filepath.Join(dir, s), path: filepath.Join(dir, s),

View File

@ -2,7 +2,6 @@ package storage
import ( import (
"errors" "errors"
"log"
"path/filepath" "path/filepath"
"strings" "strings"
) )
@ -21,7 +20,6 @@ func ToSafeFilePath(fileInfoComponents ...string) (string, error) {
} }
safeFilePath := filepath.Join(safeComps...) safeFilePath := filepath.Join(safeComps...)
fc := firstComponent(safeFilePath) fc := firstComponent(safeFilePath)
log.Printf("%q", fc)
switch fc { switch fc {
case "..": case "..":
return "", errors.New("escapes root dir") return "", errors.New("escapes root dir")