Skip to content

Commit 74c20ce

Browse files
committed
adding additional checks
1 parent 5a9a363 commit 74c20ce

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

internal/runner/options.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ func ParseOptions() *Options {
4040
flag.BoolVar(&options.EnableTCP, "tcp", false, "TCP Server")
4141
flag.BoolVar(&options.TCPWithTLS, "tls", false, "Enable TCP TLS")
4242
flag.StringVar(&options.RulesFile, "rules", "", "Rules yaml file")
43-
flag.StringVar(&options.Folder, "path", ".", "Folder")
43+
currentPath := "."
44+
if p, err := os.Getwd(); err == nil {
45+
currentPath = p
46+
}
47+
flag.StringVar(&options.Folder, "path", currentPath, "Folder")
4448
flag.BoolVar(&options.EnableUpload, "upload", false, "Enable upload via PUT")
4549
flag.BoolVar(&options.HTTPS, "https", false, "HTTPS")
4650
flag.StringVar(&options.TLSCertificate, "cert", "", "HTTPS Certificate")

pkg/httpserver/loglayer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (t *HTTPServer) loglayer(handler http.Handler) http.Handler {
6868
w.WriteHeader(http.StatusInternalServerError)
6969
return
7070
}
71-
err = handleUpload(path.Base(r.URL.Path), data)
71+
err = handleUpload(t.options.Folder, path.Base(r.URL.Path), data)
7272
if err != nil {
7373
gologger.Print().Msgf("%s\n", err)
7474
w.WriteHeader(http.StatusInternalServerError)

pkg/httpserver/uploadlayer.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
package httpserver
22

3-
import "io/ioutil"
3+
import (
4+
"errors"
5+
"io/ioutil"
6+
"path/filepath"
7+
"strings"
8+
)
9+
10+
func handleUpload(base, file string, data []byte) error {
11+
// rejects all paths containing a non exhaustive list of invalid characters - This is only a best effort as the tool is meant for development
12+
if strings.ContainsAny(file, "\\`\"':") {
13+
return errors.New("invalid character")
14+
}
15+
16+
// allow upload only in subfolders
17+
rel, err := filepath.Rel(base, file)
18+
if rel == "" || err != nil {
19+
return err
20+
}
421

5-
func handleUpload(file string, data []byte) error {
622
return ioutil.WriteFile(file, data, 0655)
723
}

0 commit comments

Comments
 (0)