Skip to content

Commit 86b51f3

Browse files
committed
Escape user input for logs
1 parent 785dce9 commit 86b51f3

File tree

7 files changed

+85
-41
lines changed

7 files changed

+85
-41
lines changed

.gitattributes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test=auto
2+
3+
# gofmt enforces LF line endings
4+
*.go text eol=lf

.github/workflows/build.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ jobs:
2626
go-version: ${{ matrix.go }}
2727

2828
- name: Vet
29-
run: go vet
29+
run: go vet ./...
30+
31+
- name: Test
32+
run: go test ./...
3033

3134
- name: Build
3235
run: go build

fs.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"context"
99
"database/sql"
1010
"encoding/json"
11-
"net"
1211
"net/http"
1312
"net/url"
1413
"os"
@@ -255,9 +254,9 @@ func (fs *CachedFS) DBReady() bool {
255254
}
256255

257256
var (
258-
escGlob = regexp.MustCompile("[][*?]")
257+
escGlob = regexp.MustCompile(`[][*?]`)
259258
escLike = regexp.MustCompile("[%_`]")
260-
escSpace = regexp.MustCompile("\\s+")
259+
escSpace = regexp.MustCompile(`\s+`)
261260
)
262261

263262
func escapeGlob(s string) string {
@@ -377,9 +376,7 @@ func (fs *CachedFS) serveCache(w http.ResponseWriter, r *http.Request) {
377376
return
378377

379378
interr:
380-
h, _, _ := net.SplitHostPort(r.RemoteAddr)
381-
logErr.Printf("%s \"%s %s %s\" \"%s\"\n", h, r.Method, r.URL, r.Proto, err.Error())
382-
http.Error(w, err.Error(), http.StatusInternalServerError)
379+
logError(http.StatusInternalServerError, err, w, r)
383380
}
384381

385382
func (fs *CachedFS) serveLive(w http.ResponseWriter, r *http.Request) {
@@ -432,14 +429,11 @@ func (fs *CachedFS) serveLive(w http.ResponseWriter, r *http.Request) {
432429
})
433430
}
434431

435-
if err != nil {
436-
h, _, _ := net.SplitHostPort(r.RemoteAddr)
437-
if err == walk.ErrNonDir || os.IsNotExist(err) || os.IsPermission(err) {
438-
http.NotFound(w, r)
439-
} else {
440-
logErr.Printf("%s \"%s %s %s\" \"%s\"\n", h, r.Method, r.URL, r.Proto, err.Error())
441-
http.Error(w, err.Error(), http.StatusInternalServerError)
442-
}
432+
if err == walk.ErrNonDir || os.IsNotExist(err) || os.IsPermission(err) {
433+
http.NotFound(w, r)
434+
return
435+
} else if err != nil {
436+
logError(http.StatusInternalServerError, err, w, r)
443437
return
444438
}
445439

@@ -502,7 +496,5 @@ func (fs *CachedFS) Sitemap(w http.ResponseWriter, r *http.Request) {
502496
return
503497

504498
interr:
505-
h, _, _ := net.SplitHostPort(r.RemoteAddr)
506-
logErr.Printf("%s \"%s %s %s\" \"%s\"\n", h, r.Method, r.URL, r.Proto, err.Error())
507-
http.Error(w, err.Error(), http.StatusInternalServerError)
499+
logError(http.StatusInternalServerError, err, w, r)
508500
}

main.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func main() {
9898

9999
go func() {
100100
sig := make(chan os.Signal, 1)
101-
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
101+
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
102102
<-sig
103103

104104
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
@@ -148,15 +148,6 @@ func realIP(trustForward bool, han http.Handler) http.Handler {
148148
})
149149
}
150150

151-
func logRequest(han http.Handler) http.Handler {
152-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
153-
u, _, _ := r.BasicAuth()
154-
h, _, _ := net.SplitHostPort(r.RemoteAddr)
155-
logOut.Printf("%s - %s [%s] \"%s %s %s\" 0 0 \"%s\" \"%s\"\n", h, orHyphen(u), time.Now().Format("02/Jan/2006:15:04:05 -0700"), r.Method, r.URL, r.Proto, orHyphen(r.Referer()), orHyphen(r.UserAgent()))
156-
han.ServeHTTP(w, r)
157-
})
158-
}
159-
160151
func nodir(han http.Handler) http.Handler {
161152
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
162153
if r.URL.Path == "" || strings.HasSuffix(r.URL.Path, "/") {
@@ -167,3 +158,30 @@ func nodir(han http.Handler) http.Handler {
167158
han.ServeHTTP(w, r)
168159
})
169160
}
161+
162+
func logRequest(han http.Handler) http.Handler {
163+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
164+
u, _, _ := r.BasicAuth()
165+
h, _, _ := net.SplitHostPort(r.RemoteAddr)
166+
logOut.Printf("%.128s - %.256s [%s] %.2048q 0 0 %.2048q %.1024q\n",
167+
h,
168+
orHyphen(u),
169+
time.Now().Format("02/Jan/2006:15:04:05 -0700"),
170+
r.Method+" "+r.URL.String()+" "+r.Proto,
171+
orHyphen(r.Referer()),
172+
orHyphen(r.UserAgent()),
173+
)
174+
han.ServeHTTP(w, r)
175+
})
176+
}
177+
178+
func logError(code int, err error, w http.ResponseWriter, r *http.Request) {
179+
h, _, _ := net.SplitHostPort(r.RemoteAddr)
180+
logErr.Printf("%.128s %.2048q %q\n",
181+
h,
182+
r.Method+" "+r.URL.String()+" "+r.Proto,
183+
err.Error(),
184+
)
185+
186+
http.Error(w, err.Error(), code)
187+
}

walk/getdents_stdlib.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Project: autoindex (https://github.com/nielsAD/autoindex)
33
// License: Mozilla Public License, v2.0
44

5+
//go:build !freebsd && !linux && !netbsd && !openbsd
56
// +build !freebsd,!linux,!netbsd,!openbsd
67

78
package walk
@@ -11,23 +12,14 @@ import (
1112
)
1213

1314
func getdents(name string, _ []byte) ([]Dirent, error) {
14-
dir, err := os.Open(name)
15+
dir, err := os.ReadDir(name)
1516
if err != nil {
1617
return nil, err
1718
}
1819

19-
r, err := dir.Readdir(0)
20-
if err != nil {
21-
dir.Close()
22-
return nil, err
23-
}
24-
if err := dir.Close(); err != nil {
25-
return nil, err
26-
}
27-
28-
res := make([]Dirent, len(r))
29-
for i, info := range r {
30-
res[i] = Dirent{name: info.Name(), modeType: info.Mode() & os.ModeType}
20+
res := make([]Dirent, len(dir))
21+
for i, info := range dir {
22+
res[i] = Dirent{name: info.Name(), modeType: info.Type() & os.ModeType}
3123
}
3224

3325
return res, nil

walk/getdents_unix.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Project: autoindex (https://github.com/nielsAD/autoindex)
33
// License: Mozilla Public License, v2.0
44

5+
//go:build freebsd || linux || netbsd || openbsd
56
// +build freebsd linux netbsd openbsd
67

78
package walk

walk/walk_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Author: Niels A.D.
2+
// Project: autoindex (https://github.com/nielsAD/autoindex)
3+
// License: Mozilla Public License, v2.0
4+
5+
package walk_test
6+
7+
import (
8+
"reflect"
9+
"sort"
10+
"testing"
11+
12+
"github.com/nielsAD/autoindex/walk"
13+
)
14+
15+
func TestWalk(t *testing.T) {
16+
var expected = []string{".", "dirent.go", "getdents_stdlib.go", "getdents_unix.go", "walk.go", "walk_test.go"}
17+
18+
var files []string
19+
walk.Walk(".", &walk.Options{
20+
Visit: func(dir string, entry *walk.Dirent) error {
21+
files = append(files, entry.Name())
22+
return nil
23+
},
24+
Error: func(dir string, entry *walk.Dirent, err error) error {
25+
t.Errorf("Error walking `%s`: %s\n", dir, err.Error())
26+
return err
27+
},
28+
})
29+
30+
sort.Strings(files)
31+
if !reflect.DeepEqual(files, expected) {
32+
t.Errorf("Unexpected directory contents: %s\n", files)
33+
}
34+
}

0 commit comments

Comments
 (0)