Skip to content

Commit e0585b8

Browse files
committed
sanitize url path
1 parent 0e23f08 commit e0585b8

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

auth.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/subtle"
55
"net"
66
"slices"
7+
"strings"
78

89
"github.com/labstack/echo/v4"
910
"github.com/labstack/echo/v4/middleware"
@@ -13,7 +14,7 @@ import (
1314
type Auth struct {
1415
Login string `json:"login" yaml:"login"` // Basic auth login
1516
Password string `json:"password" yaml:"password"` // Basic auth password
16-
IPs []string `json:"ips" yaml:"ips"` // Allowed IPs
17+
IPs []string `json:"ips" yaml:"ips"` // Allowed IPs and CIDRs
1718
}
1819

1920
// ContextLoginKey is the key used to store the login after successful auth in the context
@@ -27,16 +28,18 @@ func NewValidator(auths ...*Auth) middleware.BasicAuthValidator {
2728
validIPs, validCIDRs := parseIPs(auths...)
2829

2930
return func(login, password string, c echo.Context) (bool, error) {
31+
sanitizedPath := strings.ReplaceAll(c.Request().URL.Path, "\n", "")
32+
sanitizedPath = strings.ReplaceAll(sanitizedPath, "\r", "")
3033
for idx, auth := range auths {
3134
allowedIP := isIPAllowed(validIPs[idx], validCIDRs[idx], c.RealIP())
3235
match := Equals(auth.Login, login) && Equals(auth.Password, password)
3336
if match && allowedIP {
3437
c.Set(ContextLoginKey, login)
35-
c.Logger().Infof("authorization attempt from %s to %s (allowed_ip=%t allowed_credentials=%t)", c.RealIP(), c.Request().URL.Path, allowedIP, match)
38+
c.Logger().Infof("authorization attempt from %s to %s (allowed_ip==%t and allowed_credentials==%t)", c.RealIP(), sanitizedPath, allowedIP, match)
3639
return true, nil
3740
}
3841
}
39-
c.Logger().Infof("authorization attempt from %s to %s (allowed_ip=%t allowed_credentials=%t)", c.RealIP(), c.Request().URL.Path, false, false)
42+
c.Logger().Infof("authorization attempt from %s to %s (allowed_ip==%t or allowed_credentials==%t)", c.RealIP(), sanitizedPath, false, false)
4043

4144
return false, nil
4245
}

0 commit comments

Comments
 (0)