feat: support system proxy for remote API
This commit is contained in:
@@ -22,6 +22,7 @@ import (
|
|||||||
// Uses SSRFSafeDialContext to prevent DNS rebinding attacks at the connection layer.
|
// Uses SSRFSafeDialContext to prevent DNS rebinding attacks at the connection layer.
|
||||||
var rawHTTPClient = &http.Client{
|
var rawHTTPClient = &http.Client{
|
||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
|
Proxy: http.ProxyFromEnvironment,
|
||||||
DialContext: secutils.SSRFSafeDialContext,
|
DialContext: secutils.SSRFSafeDialContext,
|
||||||
TLSHandshakeTimeout: 10 * time.Second,
|
TLSHandshakeTimeout: 10 * time.Second,
|
||||||
IdleConnTimeout: 90 * time.Second,
|
IdleConnTimeout: 90 * time.Second,
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"golang.org/x/net/http/httpproxy"
|
||||||
)
|
)
|
||||||
|
|
||||||
// XSS 防护相关正则表达式
|
// XSS 防护相关正则表达式
|
||||||
@@ -781,7 +783,7 @@ func SSRFSafeDialContext(ctx context.Context, network, addr string) (net.Conn, e
|
|||||||
|
|
||||||
// Whitelisted hosts bypass all dial-time SSRF checks, consistent with
|
// Whitelisted hosts bypass all dial-time SSRF checks, consistent with
|
||||||
// ValidateURLForSSRF which skips IsSSRFSafeURL for whitelisted hosts.
|
// ValidateURLForSSRF which skips IsSSRFSafeURL for whitelisted hosts.
|
||||||
if IsSSRFWhitelisted(host) {
|
if IsSystemProxy(addr) || IsSSRFWhitelisted(host) {
|
||||||
dialer := &net.Dialer{
|
dialer := &net.Dialer{
|
||||||
Timeout: 30 * time.Second,
|
Timeout: 30 * time.Second,
|
||||||
KeepAlive: 30 * time.Second,
|
KeepAlive: 30 * time.Second,
|
||||||
@@ -978,3 +980,22 @@ func ValidateURLForSSRF(rawURL string) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsSystemProxy 判断是否为系统代理
|
||||||
|
func IsSystemProxy(host string) bool {
|
||||||
|
proxyCfg := httpproxy.FromEnvironment()
|
||||||
|
for _, proxyUrl := range []string{
|
||||||
|
proxyCfg.HTTPProxy,
|
||||||
|
proxyCfg.HTTPSProxy,
|
||||||
|
} {
|
||||||
|
if proxyUrl == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if parse, err := url.Parse(proxyUrl); err == nil {
|
||||||
|
if parse.Host == host {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user