Skip to content

Commit 441f671

Browse files
authored
Merge pull request #196 from m13253/fix/http-body-size-validation
Limit HTTP body length
2 parents c9df357 + 99bbaef commit 441f671

3 files changed

Lines changed: 6 additions & 5 deletions

File tree

‎doh-client/ietf.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func (c *Client) parseResponseIETF(ctx context.Context, w dns.ResponseWriter, r
179179
}
180180
}
181181

182-
body, err := io.ReadAll(req.response.Body)
182+
body, err := io.ReadAll(http.MaxBytesReader(nil, req.response.Body, dns.MaxMsgSize))
183183
if err != nil {
184184
log.Printf("read error from upstream %s: %v\n", req.currentUpstream, err)
185185
req.reply.Rcode = dns.RcodeServerFailure

‎doh-client/selector/lvsWRRSelector.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"sync"
99
"sync/atomic"
1010
"time"
11+
12+
jsondns "github.com/m13253/dns-over-https/v2/json-dns"
1113
)
1214

1315
type LVSWRRSelector struct {
@@ -210,7 +212,7 @@ func (ls *LVSWRRSelector) checkGoogleResponse(resp *http.Response, upstream *Ups
210212
}
211213

212214
m := make(map[string]interface{})
213-
if err := json.NewDecoder(resp.Body).Decode(&m); err != nil {
215+
if err := json.NewDecoder(http.MaxBytesReader(nil, resp.Body, jsondns.ReasonableJSONResponseLengthLimit)).Decode(&m); err != nil {
214216
// should I check error in detail?
215217
if atomic.AddInt32(&upstream.effectiveWeight, -2) < 1 {
216218
atomic.StoreInt32(&upstream.effectiveWeight, 1)

‎doh-server/ietf.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ func (s *Server) parseRequestIETF(ctx context.Context, w http.ResponseWriter, r
5252
}
5353
}
5454
if len(requestBinary) == 0 && (r.Header.Get("Content-Type") == "application/dns-message" || r.Header.Get("Content-Type") == "application/dns-udpwireformat") {
55-
// DNS protocol gives 64 KiB bound.
56-
// We will use our own FormatError, so not passing http.ResponseWriter into http.MaxBytesReader.
57-
requestBinary, err = io.ReadAll(http.MaxBytesReader(nil, r.Body, 65536))
55+
// We will use our own FormatError, thus not passing http.ResponseWriter into http.MaxBytesReader.
56+
requestBinary, err = io.ReadAll(http.MaxBytesReader(nil, r.Body, dns.MaxMsgSize))
5857
if _, ok := errors.AsType[*http.MaxBytesError](err); ok {
5958
return &DNSRequest{
6059
errcode: 413,

0 commit comments

Comments
 (0)