Skip to content

Commit acfa74d

Browse files
authored
fix(dnsserver): abort dropped DoH responses after cancellation (#431)
1 parent 42f349a commit acfa74d

2 files changed

Lines changed: 73 additions & 6 deletions

File tree

‎internal/dnsserver/server.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,9 @@ func (s *Server) DoHHandler(h Handler) http.Handler {
383383
if resp == nil {
384384
// A dropped query: hold the request until the client gives up.
385385
<-r.Context().Done()
386-
return
386+
// Returning would let net/http send an implicit 200 if the server
387+
// sees cancellation first. Abort without writing any response.
388+
panic(http.ErrAbortHandler)
387389
}
388390
w.Header().Set("Content-Type", "application/dns-message")
389391
_, _ = w.Write(resp)

‎internal/dnsserver/transport_test.go‎

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"crypto/tls"
77
"crypto/x509"
88
"encoding/binary"
9+
"errors"
910
"io"
1011
"net"
1112
"net/http"
@@ -229,8 +230,10 @@ func TestDoHHandler(t *testing.T) {
229230
t.Fatal(err)
230231
}
231232
defer srv.Close()
233+
dropped := make(chan struct{})
232234
ts := httptest.NewUnstartedServer(srv.DoHHandler(func(q Query) Reply {
233235
if q.Name == "dropped.example.com" {
236+
close(dropped)
234237
return Dropped
235238
}
236239
if !q.TCP {
@@ -277,17 +280,45 @@ func TestDoHHandler(t *testing.T) {
277280
}
278281

279282
t.Run("dropped", func(t *testing.T) {
280-
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
283+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
281284
defer cancel()
282285
req, err := http.NewRequestWithContext(ctx, http.MethodPost, ts.URL, bytes.NewReader(query(t, "dropped.example.com.")))
283286
if err != nil {
284287
t.Fatal(err)
285288
}
286289
req.Header.Set("Content-Type", "application/dns-message")
287-
resp, err := client.Do(req)
288-
if err == nil {
289-
_ = resp.Body.Close()
290-
t.Fatalf("dropped DoH query answered with status %d; want the request held until the client gives up", resp.StatusCode)
290+
type result struct {
291+
resp *http.Response
292+
err error
293+
}
294+
done := make(chan result, 1)
295+
go func() {
296+
resp, err := client.Do(req)
297+
if resp != nil {
298+
_ = resp.Body.Close()
299+
}
300+
done <- result{resp, err}
301+
}()
302+
// Wait for the query to reach the handler before canceling it. A short
303+
// client deadline could expire before delivery on a loaded runner.
304+
select {
305+
case <-dropped:
306+
case <-ctx.Done():
307+
t.Fatal("dropped query never reached the handler")
308+
}
309+
select {
310+
case got := <-done:
311+
t.Fatalf("dropped request completed before cancellation: response=%v, error=%v", got.resp, got.err)
312+
case <-time.After(10 * time.Millisecond):
313+
}
314+
cancel()
315+
select {
316+
case got := <-done:
317+
if got.resp != nil || !errors.Is(got.err, context.Canceled) {
318+
t.Fatalf("dropped request: response=%v, error=%v; want only context.Canceled", got.resp, got.err)
319+
}
320+
case <-time.After(10 * time.Second):
321+
t.Fatal("dropped request did not stop after cancellation")
291322
}
292323
})
293324

@@ -297,6 +328,40 @@ func TestDoHHandler(t *testing.T) {
297328
}
298329
}
299330

331+
// A canceled server context can be observed before the client gives up. The
332+
// dropped request must still receive no HTTP response, not an implicit 200.
333+
func TestDoHHandlerCanceledServerContext(t *testing.T) {
334+
var srv Server
335+
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
336+
ctx, cancel := context.WithCancel(r.Context())
337+
defer cancel()
338+
srv.DoHHandler(func(Query) Reply {
339+
cancel()
340+
return Dropped
341+
}).ServeHTTP(w, r.WithContext(ctx))
342+
}))
343+
ts.StartTLS()
344+
defer ts.Close()
345+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
346+
defer cancel()
347+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, ts.URL, bytes.NewReader(query(t, "dropped.example.com.")))
348+
if err != nil {
349+
t.Fatal(err)
350+
}
351+
req.Header.Set("Content-Type", "application/dns-message")
352+
resp, err := ts.Client().Do(req)
353+
if resp != nil {
354+
_ = resp.Body.Close()
355+
t.Fatalf("dropped request answered with status %d after server cancellation", resp.StatusCode)
356+
}
357+
if !errors.Is(err, io.EOF) {
358+
t.Fatalf("request error = %v, want EOF without a response", err)
359+
}
360+
if got := srv.Queries(); got != 1 {
361+
t.Errorf("Queries() = %d, want 1", got)
362+
}
363+
}
364+
300365
// TestQueriesForCountsEveryTransport: the same name asked over UDP, TCP, DoT
301366
// and DoH lands in one QueriesFor count, matched case-insensitively and with
302367
// or without the trailing dot.

0 commit comments

Comments
 (0)