mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-12-06 16:22:15 +00:00
fix(gotenberg): match tunnel allow lists on the root URL
This commit is contained in:
@@ -378,14 +378,17 @@ var tunnelSchemes = []struct {
|
||||
// it. Rules that name a path cannot match here: only a layer that sees the
|
||||
// full URL, such as Chromium's request interception, can apply them.
|
||||
//
|
||||
// The allow-list and the IP-class checks then run on the https:// form. The
|
||||
// scheme does not influence the CONNECT handling beyond filtering.
|
||||
// The allow-list and the IP-class checks then run on the https:// root URL,
|
||||
// so an allow-list rule such as "^https://cdn\.example\.com/" clears the tunnel
|
||||
// to cdn.example.com:443 as it clears the ordinary request. The scheme does not
|
||||
// influence the CONNECT handling beyond filtering.
|
||||
func (p *OutboundProxy) DecideTunnel(ctx context.Context, hostport string, deadline time.Time) (OutboundDecision, error) {
|
||||
host, port, err := net.SplitHostPort(hostport)
|
||||
if err != nil {
|
||||
return OutboundDecision{}, fmt.Errorf("split CONNECT target %q: %v: %w", hostport, err, ErrFiltered)
|
||||
}
|
||||
|
||||
var httpsRoot string
|
||||
for _, ts := range tunnelSchemes {
|
||||
authority := net.JoinHostPort(host, port)
|
||||
if port == ts.defaultPort {
|
||||
@@ -395,13 +398,18 @@ func (p *OutboundProxy) DecideTunnel(ctx context.Context, hostport string, deadl
|
||||
}
|
||||
}
|
||||
|
||||
err = FilterDenyList(ts.scheme+"://"+authority+"/", p.denyList, deadline)
|
||||
root := ts.scheme + "://" + authority + "/"
|
||||
if ts.scheme == "https" {
|
||||
httpsRoot = root
|
||||
}
|
||||
|
||||
err = FilterDenyList(root, p.denyList, deadline)
|
||||
if err != nil {
|
||||
return OutboundDecision{}, err
|
||||
}
|
||||
}
|
||||
|
||||
return p.decide(ctx, "https://"+hostport, p.allowList, p.denyList, deadline)
|
||||
return p.decide(ctx, httpsRoot, p.allowList, p.denyList, deadline)
|
||||
}
|
||||
|
||||
// maxConcurrentTunnels is the default for [OutboundProxy.maxTunnels]. See
|
||||
|
||||
@@ -1241,7 +1241,7 @@ func TestOutboundProxy_DecideTunnel_DenyRuleShapes(t *testing.T) {
|
||||
|
||||
_, err := p.DecideTunnel(context.Background(), tc.hostport, time.Now().Add(time.Minute))
|
||||
if tc.denied != (err != nil) {
|
||||
t.Fatalf("decideTunnel(%q) with rule %q: error = %v, want denied: %t", tc.hostport, tc.rule, err, tc.denied)
|
||||
t.Fatalf("DecideTunnel(%q) with rule %q: error = %v, want denied: %t", tc.hostport, tc.rule, err, tc.denied)
|
||||
}
|
||||
if err != nil && !errors.Is(err, ErrFiltered) {
|
||||
t.Errorf("error %v does not wrap ErrFiltered", err)
|
||||
@@ -1250,6 +1250,36 @@ func TestOutboundProxy_DecideTunnel_DenyRuleShapes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The allow-list sees the https:// root URL of a tunnel, as it sees an
|
||||
// ordinary request: a rule ending in "/" clears the tunnel to its host on the
|
||||
// default port, and only there.
|
||||
func TestOutboundProxy_DecideTunnel_AllowRuleShapes(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
rule string
|
||||
hostport string
|
||||
allowed bool
|
||||
}{
|
||||
{"path terminated", `^https://cdn\.example\.com/`, "cdn.example.com:443", true},
|
||||
{"host terminated", `^https://cdn\.example\.com(:|/|$)`, "cdn.example.com:443", true},
|
||||
{"non-default port", `^https://cdn\.example\.com:8443/`, "cdn.example.com:8443", true},
|
||||
{"rule on the default port only", `^https://cdn\.example\.com/`, "cdn.example.com:8443", false},
|
||||
{"other host", `^https://cdn\.example\.com/`, "other.example.com:443", false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
p := NewOutboundProxy(OutboundProxyOptions{AllowList: []*regexp2.Regexp{regexp2.MustCompile(tc.rule, regexp2.None)}})
|
||||
|
||||
decision, err := p.DecideTunnel(context.Background(), tc.hostport, time.Now().Add(time.Minute))
|
||||
if tc.allowed && (err != nil || !decision.Bypass) {
|
||||
t.Fatalf("DecideTunnel(%q) with rule %q: decision = %+v, error = %v, want an allow-list match", tc.hostport, tc.rule, decision, err)
|
||||
}
|
||||
if !tc.allowed && !errors.Is(err, ErrFiltered) {
|
||||
t.Fatalf("DecideTunnel(%q) with rule %q: error = %v, want a refusal", tc.hostport, tc.rule, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A tunnel whose upstream never answers must end with the proxy that accepted
|
||||
// it, including across a restart: Shutdown does not reach hijacked
|
||||
// connections.
|
||||
|
||||
Reference in New Issue
Block a user