Skip to content

Commit c9df357

Browse files
authored
Merge pull request #195 from m13253/fix/wrr-selector
nginxWRRSelector: Limit HTTP body length
2 parents 6440815 + 6e49461 commit c9df357

3 files changed

Lines changed: 10 additions & 6 deletions

File tree

‎doh-client/google.go‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,7 @@ func (c *Client) parseResponseGoogle(ctx context.Context, w dns.ResponseWriter,
141141
}
142142
}
143143

144-
// The largest well-formed 64 KiB DNS response can be serialized into ~18 MiB of JSON, if not pretty-printed.
145-
// (4079 MINFO records, each with NAME = RMAILBX = EMAILBX = ("<"*63 + ".") * 3 + "<"*61 + ".")
146-
// That is too large as a practical size limit. Also, there is actually no upper limit if pretty-printed.
147-
// So, I'll set the limit to 1 MiB as for now. We can increase it if we do need more.
148-
body, err := io.ReadAll(http.MaxBytesReader(nil, req.response.Body, 1048576))
144+
body, err := io.ReadAll(http.MaxBytesReader(nil, req.response.Body, jsondns.ReasonableJSONResponseLengthLimit))
149145
if err != nil {
150146
log.Println(err)
151147
req.reply.Rcode = dns.RcodeServerFailure

‎doh-client/selector/nginxWRRSelector.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 NginxWRRSelector struct {
@@ -163,7 +165,7 @@ func (ws *NginxWRRSelector) checkGoogleResponse(resp *http.Response, upstream *U
163165
}
164166

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

‎json-dns/response.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ import (
2828
"time"
2929
)
3030

31+
// The largest well-formed 64 KiB DNS response can be serialized into ~18 MiB of JSON, if not pretty-printed.
32+
// (4079 MINFO records, each with NAME = RMAILBX = EMAILBX = ("<"*63 + ".") * 3 + "<"*61 + ".")
33+
// That is too large as a practical size limit. Also, there is actually no upper limit if pretty-printed.
34+
// So, I'll set the limit to 1 MiB as for now. We can increase it if we do need more.
35+
const ReasonableJSONResponseLengthLimit = 1048576
36+
3137
type QuestionList []Question
3238

3339
func (ql *QuestionList) UnmarshalJSON(b []byte) error {

0 commit comments

Comments
 (0)