4
4
"crypto/subtle"
5
5
"net"
6
6
"slices"
7
+ "strings"
7
8
8
9
"github.com/labstack/echo/v4"
9
10
"github.com/labstack/echo/v4/middleware"
@@ -13,7 +14,7 @@ import (
13
14
type Auth struct {
14
15
Login string `json:"login" yaml:"login"` // Basic auth login
15
16
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
17
18
}
18
19
19
20
// 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 {
27
28
validIPs , validCIDRs := parseIPs (auths ... )
28
29
29
30
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 " , "" )
30
33
for idx , auth := range auths {
31
34
allowedIP := isIPAllowed (validIPs [idx ], validCIDRs [idx ], c .RealIP ())
32
35
match := Equals (auth .Login , login ) && Equals (auth .Password , password )
33
36
if match && allowedIP {
34
37
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 )
36
39
return true , nil
37
40
}
38
41
}
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 )
40
43
41
44
return false , nil
42
45
}
0 commit comments