caddyhttp: reject opaque targets before routing and proxying
Reject opaque HTTP request targets before either normal or error routes can interpret them. Clear URL.Opaque defensively on proxy clones. Verify HTTP/1 parsing, path authentication, supported target forms and outbound serialization without contacting an upstream. Addresses scan report e17d2fcd-d50c-4d68-82d1-96519a437a24. Code written by GitHub Copilot (GPT-6.1 Sol, Claude Opus 5.5); reviewed by mholt. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
2ee6bdf837
commit
13ae3457cf
4 changed files with 113 additions and 0 deletions
43
modules/caddyhttp/reverseproxy/request_target_test.go
Normal file
43
modules/caddyhttp/reverseproxy/request_target_test.go
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package reverseproxy
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCloneRequestClearsOpaqueTarget(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
target, want string
|
||||
}{
|
||||
{"/public?x=1", "/public?x=1"},
|
||||
{"http://public.example/public?x=1", "/public?x=1"},
|
||||
{"http:http://private.example/secret", "/"},
|
||||
} {
|
||||
t.Run(tc.target, func(t *testing.T) {
|
||||
original, err := http.ReadRequest(bufio.NewReader(strings.NewReader(
|
||||
"GET " + tc.target + " HTTP/1.1\r\nHost: public.example\r\n\r\n")))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer original.Body.Close()
|
||||
originalURL := *original.URL
|
||||
cloned := cloneRequest(original)
|
||||
cloned.URL.Scheme = "http"
|
||||
cloned.URL.Host = "selected-upstream.example"
|
||||
var wire bytes.Buffer
|
||||
if err := cloned.Write(&wire); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := "GET " + tc.want + " HTTP/1.1\r\n"
|
||||
if !strings.HasPrefix(wire.String(), want) {
|
||||
t.Fatalf("outbound target = %q, want prefix %q", wire.String(), want)
|
||||
}
|
||||
if *original.URL != originalURL {
|
||||
t.Fatal("original request URL was mutated")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -1827,6 +1827,7 @@ func cloneRequest(origReq *http.Request) *http.Request {
|
|||
// dialer will behave. See #4237 for context.
|
||||
newURL.Scheme = ""
|
||||
newURL.Host = ""
|
||||
newURL.Opaque = ""
|
||||
req.URL = newURL
|
||||
}
|
||||
if origReq.Header != nil {
|
||||
|
|
|
|||
|
|
@ -599,6 +599,14 @@ var (
|
|||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
|
||||
// Opaque URIs have no routable path but can override the serialized target
|
||||
// when forwarded. Reject them before either normal or error routes run.
|
||||
if r.URL.Opaque != "" {
|
||||
s.logger.Debug("rejecting opaque HTTP request target")
|
||||
http.Error(w, "invalid request target", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Count HTTP/3 requests on connections ServeQUICConn owns. http3.Server
|
||||
// does not track those, so shutdown uses this to tell an idle keepalive
|
||||
// from work that still has to drain.
|
||||
|
|
|
|||
61
modules/caddyhttp/server_request_target_test.go
Normal file
61
modules/caddyhttp/server_request_target_test.go
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
package caddyhttp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func TestServerRequestTargetForms(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
method, target string
|
||||
code int
|
||||
routed bool
|
||||
}{
|
||||
{http.MethodGet, "/public", http.StatusOK, true},
|
||||
{http.MethodGet, "/admin/secret", http.StatusUnauthorized, true},
|
||||
{http.MethodGet, "http://public.example/public", http.StatusOK, true},
|
||||
{http.MethodGet, "http://public.example/admin/secret", http.StatusUnauthorized, true},
|
||||
{http.MethodConnect, "public.example:443", http.StatusOK, true},
|
||||
{http.MethodOptions, "*", http.StatusOK, true},
|
||||
{http.MethodGet, "http:http://upstream.example/admin/secret", http.StatusBadRequest, false},
|
||||
{http.MethodGet, "http:http://private.example/secret", http.StatusBadRequest, false},
|
||||
{http.MethodGet, "http:secret", http.StatusBadRequest, false},
|
||||
} {
|
||||
t.Run(tc.method+" "+tc.target, func(t *testing.T) {
|
||||
raw := tc.method + " " + tc.target + " HTTP/1.1\r\nHost: public.example\r\n\r\n"
|
||||
req, err := http.ReadRequest(bufio.NewReader(strings.NewReader(raw)))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer req.Body.Close()
|
||||
routed := false
|
||||
server := &Server{
|
||||
logger: zap.NewNop(), errorLogger: zap.NewNop(), accessLogger: zap.NewNop(),
|
||||
Errors: &HTTPErrorConfig{Routes: RouteList{{}}},
|
||||
errorHandlerChain: HandlerFunc(func(http.ResponseWriter, *http.Request) error {
|
||||
t.Fatal("opaque target reached error routes")
|
||||
return nil
|
||||
}),
|
||||
primaryHandlerChain: HandlerFunc(func(w http.ResponseWriter, req *http.Request) error {
|
||||
routed = true
|
||||
if (MatchPath{"/admin/*"}).Match(req) {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
return nil
|
||||
}),
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
server.ServeHTTP(rec, req)
|
||||
if rec.Code != tc.code || routed != tc.routed {
|
||||
t.Fatalf("got status=%d routed=%v, want status=%d routed=%v", rec.Code, routed, tc.code, tc.routed)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue