fix: validate proxy IP headers and force attachment for unsafe MIME types (BUG-112, BUG-118)

BUG-112: clientIPWithProxies now validates extracted X-Real-IP and
X-Forwarded-For values with net.ParseIP. Non-IP strings are rejected,
falling back to RemoteAddr. Prevents attackers from choosing arbitrary
rate-limit bucket keys via header injection.

BUG-118: Files with MIME types that could execute active content
(HTML, SVG, XML, PDF) are now served with Content-Disposition: attachment
instead of inline, preventing content hosting under the OwnCord origin.
This commit is contained in:
J3vb
2026-04-02 13:23:09 +02:00
parent fcffdb8d6b
commit d5ec7ca877
2 changed files with 30 additions and 3 deletions
+7 -2
View File
@@ -211,15 +211,20 @@ func clientIPWithProxies(r *http.Request, trustedCIDRs []string) string {
}
// Prefer X-Real-IP when coming from a trusted proxy.
// BUG-112: Validate extracted IP to prevent spoofed rate-limit keys.
if xri := strings.TrimSpace(r.Header.Get("X-Real-IP")); xri != "" {
return xri
if net.ParseIP(xri) != nil {
return xri
}
}
// Fall back to the leftmost (client) entry in X-Forwarded-For.
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
parts := strings.SplitN(xff, ",", 2)
if client := strings.TrimSpace(parts[0]); client != "" {
return client
if net.ParseIP(client) != nil {
return client
}
}
}