fix(egress): name the denied target when refusing a tunnel CONNECT (#1808)
This commit is contained in:
1 parent
72929c36af
commit
3a0c52dcb3
2 files changed
+50
-1
No files matched your search
@@ -46,7 +46,7 @@ func (t *Tunnel) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
target := net.JoinHostPort(host, strconv.Itoa(port))
|
||||
if !t.rules.allows(host, port) {
|
||||
http.Error(w, "egress to "+target+" is not allowed", http.StatusForbidden)
|
||||
deny(w, r, "egress to "+target+" is not allowed")
|
||||
return
|
||||
}
|
||||
if r.Method == http.MethodConnect {
|
||||
@@ -98,6 +98,23 @@ func (t *Tunnel) splice(w http.ResponseWriter, r *http.Request, target string) {
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
// deny refuses a request. Go's client discards the body of a refused CONNECT
|
||||
// and reports only the reason phrase, so for CONNECT the message is written
|
||||
// as the reason phrase; otherwise a denial reads as a bare "Forbidden".
|
||||
func deny(w http.ResponseWriter, r *http.Request, msg string) {
|
||||
hijacker, ok := w.(http.Hijacker)
|
||||
if r.Method != http.MethodConnect || !ok {
|
||||
http.Error(w, msg, http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
conn, _, err := hijacker.Hijack()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer func() { _ = conn.Close() }()
|
||||
_, _ = fmt.Fprintf(conn, "HTTP/1.1 403 %s\r\nContent-Length: 0\r\nConnection: close\r\n\r\n", msg)
|
||||
}
|
||||
|
||||
func closeWrite(c net.Conn) {
|
||||
if cw, ok := c.(interface{ CloseWrite() error }); ok {
|
||||
_ = cw.CloseWrite()
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package egress
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTunnelDenialNamesTargetToGoClients(t *testing.T) {
|
||||
proxy := httptest.NewServer(NewTunnel([]string{"api.example.com"}))
|
||||
defer proxy.Close()
|
||||
proxyURL, err := url.Parse(proxy.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyURL)}}
|
||||
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, "https://denied.example.com/archive.tar.gz", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err == nil {
|
||||
_ = resp.Body.Close()
|
||||
t.Fatal("request to a denied host succeeded")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "egress to denied.example.com:443 is not allowed") {
|
||||
t.Fatalf("error does not name the denied target: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user