| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"os" |
| 7 |
"path/filepath" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/pkg/errors" |
| 11 |
) |
| 12 |
|
| 13 |
// errInvalidIndexName is returned when an index name is not safe to use as a |
| 14 |
// filename in a filesystem-backed index store. |
| 15 |
var errInvalidIndexName = errors.New("invalid index name") |
| 16 |
|
| 17 |
// validIndexName reports whether name is safe to use as an index filename in |
| 18 |
// filesystem-backed stores. It rejects empty names, "." and "..", names |
| 19 |
// containing a path separator (forward slash or backslash, regardless of the |
| 20 |
// host OS so a server is protected from cross-platform payloads) and names |
| 21 |
// that aren't local per the OS rules (e.g. Windows reserved device names or |
| 22 |
// volume/absolute paths). This prevents path traversal from |
| 23 |
// attacker-controlled index names, e.g. via the index server. |
| 24 |
func validIndexName(name string) bool { |
| 25 |
if name == "" || name == "." || name == ".." { |
| 26 |
return false |
| 27 |
} |
| 28 |
if strings.ContainsAny(name, `/\`) { |
| 29 |
return false |
| 30 |
} |
| 31 |
return filepath.IsLocal(name) |
| 32 |
} |
| 33 |
|
| 34 |
// LocalIndexStore is used to read/write index files on local disk |
| 35 |
type LocalIndexStore struct { |
| 36 |
Path string |
| 37 |
} |
| 38 |
|
| 39 |
// NewLocalIndexStore creates an instance of a local index store, it only checks presence |
| 40 |
// of the store |
| 41 |
func NewLocalIndexStore(path string) (LocalIndexStore, error) { |
| 42 |
info, err := os.Stat(path) |
| 43 |
if err != nil { |
| 44 |
return LocalIndexStore{}, err |
| 45 |
} |
| 46 |
if !info.IsDir() { |
| 47 |
return LocalIndexStore{}, fmt.Errorf("%s is not a directory", path) |
| 48 |
} |
| 49 |
if !strings.HasSuffix(path, "/") { |
| 50 |
path = path + "/" |
| 51 |
} |
| 52 |
return LocalIndexStore{Path: path}, nil |
| 53 |
} |
| 54 |
|
| 55 |
// GetIndexReader returns a reader of an index file in the store or an error if |
| 56 |
// the specified index file does not exist. |
| 57 |
func (s LocalIndexStore) GetIndexReader(name string) (rdr io.ReadCloser, e error) { |
| 58 |
if !validIndexName(name) { |
| 59 |
return nil, errInvalidIndexName |
| 60 |
} |
| 61 |
return os.Open(s.Path + name) |
| 62 |
} |
| 63 |
|
| 64 |
// GetIndex returns an Index structure from the store |
| 65 |
func (s LocalIndexStore) GetIndex(name string) (i Index, e error) { |
| 66 |
f, err := s.GetIndexReader(name) |
| 67 |
if err != nil { |
| 68 |
return i, err |
| 69 |
} |
| 70 |
defer f.Close() |
| 71 |
idx, err := IndexFromReader(f) |
| 72 |
if os.IsNotExist(err) { |
| 73 |
err = errors.Errorf("Index file does not exist: %v", err) |
| 74 |
} |
| 75 |
return idx, err |
| 76 |
} |
| 77 |
|
| 78 |
// StoreIndex stores an index in the index store with the given name. |
| 79 |
func (s LocalIndexStore) StoreIndex(name string, idx Index) error { |
| 80 |
if !validIndexName(name) { |
| 81 |
return errInvalidIndexName |
| 82 |
} |
| 83 |
// Write the index to file |
| 84 |
i, err := os.Create(s.Path + name) |
| 85 |
if err != nil { |
| 86 |
return err |
| 87 |
} |
| 88 |
defer i.Close() |
| 89 |
_, err = idx.WriteTo(i) |
| 90 |
return err |
| 91 |
} |
| 92 |
|
| 93 |
func (s LocalIndexStore) String() string { |
| 94 |
return s.Path |
| 95 |
} |
| 96 |
|
| 97 |
// Close the index store. NOP operation, needed to implement IndexStore interface |
| 98 |
func (s LocalIndexStore) Close() error { return nil } |