Skip to content
This repository was archived by the owner on Mar 29, 2025. It is now read-only.

Commit 6707511

Browse files
authored
Merge pull request #15 from randlabs/updated_deps
Updated deps and propagate sanitize url errors
2 parents c3c9320 + 1f811a8 commit 6707511

File tree

6 files changed

+27
-12
lines changed

6 files changed

+27
-12
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@
187187
same "printed page" as the copyright notice for easier
188188
identification within third-party archives.
189189

190-
Copyright (C) 2022 RandLabs
190+
Copyright (C) 2022-2023 RandLabs
191191

192192
Licensed under the Apache License, Version 2.0 (the "License");
193193
you may not use this file except in compliance with the License.

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ go 1.19
44

55
require (
66
github.com/fasthttp/router v1.4.20
7-
github.com/valyala/fasthttp v1.48.0
7+
github.com/valyala/fasthttp v1.50.0
88
)
99

1010
require (
1111
github.com/andybalholm/brotli v1.0.5 // indirect
12-
github.com/klauspost/compress v1.16.7 // indirect
12+
github.com/klauspost/compress v1.17.0 // indirect
1313
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
1414
github.com/valyala/bytebufferpool v1.0.0 // indirect
1515
github.com/valyala/tcplisten v1.0.0 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/
22
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
33
github.com/fasthttp/router v1.4.20 h1:yPeNxz5WxZGojzolKqiP15DTXnxZce9Drv577GBrDgU=
44
github.com/fasthttp/router v1.4.20/go.mod h1:um867yNQKtERxBm+C+yzgWxjspTiQoA8z86Ec3fK/tc=
5-
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
6-
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
5+
github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM=
6+
github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
77
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee h1:8Iv5m6xEo1NR1AvpV+7XmhI4r39LGNzwUL4YpMuL5vk=
88
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee/go.mod h1:qwtSXrKuJh/zsFQ12yEE89xfCrGKK63Rr7ctU/uCo4g=
99
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
1010
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
11-
github.com/valyala/fasthttp v1.48.0 h1:oJWvHb9BIZToTQS3MuQ2R3bJZiNSa2KiNdeI8A+79Tc=
12-
github.com/valyala/fasthttp v1.48.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA=
11+
github.com/valyala/fasthttp v1.50.0 h1:H7fweIlBm0rXLs2q0XbalvJ6r0CUPFWK3/bB4N13e9M=
12+
github.com/valyala/fasthttp v1.50.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA=
1313
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
1414
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
1515
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=

middleware/trailingslash.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,14 @@ func NewTrailingSlash(opts TrailingSlashOptions) webserver.MiddlewareFunc {
4444
}
4545
}
4646
if modified {
47-
uri.SetPath(util.SanitizeUrlPath(path))
47+
var err error
48+
49+
path, err = util.SanitizeUrlPath(path)
50+
if err != nil {
51+
req.Error(err.Error(), 400)
52+
return nil
53+
}
54+
uri.SetPath(path)
4855

4956
// Redirect
5057
if opts.RedirectCode != 0 {

util/uri.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package util
22

33
import (
4+
"errors"
45
"strings"
56
)
67

78
// -----------------------------------------------------------------------------
89

9-
func SanitizeUrlPath(path string) string {
10+
func SanitizeUrlPath(path string) (string, error) {
1011
// Nothing to sanitize?
1112
if len(path) == 0 {
12-
return "/"
13+
return "/", nil
1314
}
1415

1516
// Convert backslashes
@@ -28,6 +29,8 @@ func SanitizeUrlPath(path string) string {
2829
} else {
2930
if len(newPathFragments) > 0 {
3031
newPathFragments = newPathFragments[0 : len(newPathFragments)-1]
32+
} else {
33+
return "", errors.New("invalid path")
3134
}
3235
}
3336
}
@@ -40,5 +43,5 @@ func SanitizeUrlPath(path string) string {
4043
}
4144

4245
// Done
43-
return path
46+
return path, nil
4447
}

webserver.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,13 +370,18 @@ func (srv *Server) CustomMethod(method string, path string, handler HandlerFunc,
370370

371371
// ServeFiles adds custom filesystem handler for the specified route
372372
func (srv *Server) ServeFiles(path string, opts ServerFilesOptions, middlewares ...MiddlewareFunc) error {
373+
var err error
374+
373375
// Check some options
374376
if !filepath.IsAbs(opts.RootDirectory) {
375377
return errors.New("absolute path must be provided")
376378
}
377379

378380
// Normalize path
379-
path = util.SanitizeUrlPath(path + "/" + serveFilesSuffix)
381+
path, err = util.SanitizeUrlPath(path + "/" + serveFilesSuffix)
382+
if err != nil {
383+
return err
384+
}
380385

381386
indexNames := make([]string, 0)
382387
if !opts.DisableDefaultIndexPages {

0 commit comments

Comments
 (0)