允许80、443以外的端口
This commit is contained in:
+2
-11
@@ -394,28 +394,19 @@ func mustParseCIDR(s string) *net.IPNet {
|
||||
return network
|
||||
}
|
||||
|
||||
// isSafeRedirect 检查重定向目标是否安全(非内网 IP、非非标端口)。
|
||||
// isSafeRedirect 检查重定向目标是否安全(非内网 IP)。
|
||||
// 用于防止 SSRF 攻击:恶意服务器将爬虫重定向到内网服务。
|
||||
func isSafeRedirect(u *url.URL) error {
|
||||
host := u.Hostname()
|
||||
port := u.Port()
|
||||
// 解析 IP 地址
|
||||
ip := net.ParseIP(host)
|
||||
if ip == nil {
|
||||
// 域名(非 IP),允许(DNS 解析由系统处理)
|
||||
// 但非标端口仍需检查
|
||||
if port != "" && port != "80" && port != "443" {
|
||||
return fmt.Errorf("blocked: non-standard port %s", port)
|
||||
}
|
||||
// 域名(非 IP),允许(DNS 解析由系统处理,端口不限)
|
||||
return nil
|
||||
}
|
||||
// IP 直连:检查是否为私有地址
|
||||
if isPrivateIP(ip) {
|
||||
return fmt.Errorf("blocked: private IP %s", ip)
|
||||
}
|
||||
// 非标端口检查
|
||||
if port != "" && port != "80" && port != "443" {
|
||||
return fmt.Errorf("blocked: non-standard port %s", port)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user