mirror of
https://github.com/hacdias/webdav.git
synced 2026-09-22 03:20:41 +08:00
fix: spoofing of X-Forwarded-For
This commit is contained in:
@@ -71,6 +71,11 @@ debug: false
|
|||||||
# Disable sniffing the files to detect their content type. Default is 'false'.
|
# Disable sniffing the files to detect their content type. Default is 'false'.
|
||||||
noSniff: false
|
noSniff: false
|
||||||
|
|
||||||
|
# Whether the server runs behind a trusted proxy or not. When this is true,
|
||||||
|
# the header X-Forwarded-For will be used for logging the remote addresses
|
||||||
|
# of logging attempts (if available).
|
||||||
|
behindProxy: false
|
||||||
|
|
||||||
# The directory that will be able to be accessed by the users when connecting.
|
# The directory that will be able to be accessed by the users when connecting.
|
||||||
# This directory will be used by users unless they have their own 'directory' defined.
|
# This directory will be used by users unless they have their own 'directory' defined.
|
||||||
# Default is '.' (current directory).
|
# Default is '.' (current directory).
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ type Config struct {
|
|||||||
Prefix string
|
Prefix string
|
||||||
NoSniff bool
|
NoSniff bool
|
||||||
NoPassword bool
|
NoPassword bool
|
||||||
|
BehindProxy bool
|
||||||
Log Log
|
Log Log
|
||||||
CORS CORS
|
CORS CORS
|
||||||
Users []User
|
Users []User
|
||||||
|
|||||||
+14
-11
@@ -17,14 +17,16 @@ type handlerUser struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
noPassword bool
|
noPassword bool
|
||||||
user *handlerUser
|
behindProxy bool
|
||||||
users map[string]*handlerUser
|
user *handlerUser
|
||||||
|
users map[string]*handlerUser
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHandler(c *Config) (http.Handler, error) {
|
func NewHandler(c *Config) (http.Handler, error) {
|
||||||
h := &Handler{
|
h := &Handler{
|
||||||
noPassword: c.NoPassword,
|
noPassword: c.NoPassword,
|
||||||
|
behindProxy: c.BehindProxy,
|
||||||
user: &handlerUser{
|
user: &handlerUser{
|
||||||
User: User{
|
User: User{
|
||||||
UserPermissions: c.UserPermissions,
|
UserPermissions: c.UserPermissions,
|
||||||
@@ -85,7 +87,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
|
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
|
||||||
|
|
||||||
// Retrieve the real client IP address using the updated helper function
|
// Retrieve the real client IP address using the updated helper function
|
||||||
remoteAddr := getRealRemoteIP(r)
|
remoteAddr := getRealRemoteIP(r, h.behindProxy)
|
||||||
|
|
||||||
// Gets the correct user for this request.
|
// Gets the correct user for this request.
|
||||||
username, password, ok := r.BasicAuth()
|
username, password, ok := r.BasicAuth()
|
||||||
@@ -166,12 +168,13 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// getRealRemoteIP retrieves the client's actual IP address, considering reverse proxies.
|
// getRealRemoteIP retrieves the client's actual IP address, considering reverse proxies.
|
||||||
func getRealRemoteIP(r *http.Request) string {
|
func getRealRemoteIP(r *http.Request, behindProxy bool) string {
|
||||||
ip := r.Header.Get("X-Forwarded-For")
|
if behindProxy {
|
||||||
if ip == "" {
|
if ip := r.Header.Get("X-Forwarded-For"); ip != "" {
|
||||||
ip = r.RemoteAddr
|
return ip
|
||||||
}
|
}
|
||||||
return ip
|
}
|
||||||
|
return r.RemoteAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
type responseWriterNoBody struct {
|
type responseWriterNoBody struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user