Skip to content

Commit

Permalink
home: imp large req handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Feb 15, 2021
1 parent 8a0bc54 commit dad39ae
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ and this project adheres to
longer prevent the DHCP server from starting ([#2667]).
- The server name sent by clients of TLS APIs is not only checked when
`strict_sni_check` is enabled ([#2664]).
- HTTP API request body size limit for the `POST /control/access/set` HTTP API
is increased ([#2666]).
- HTTP API request body size limit for the `POST /control/access/set` and `POST
/control/filtering/set_rules` HTTP APIs is increased ([#2666], [#2675]).

### Fixed

Expand All @@ -45,6 +45,7 @@ and this project adheres to
[#2664]: https://github.com/AdguardTeam/AdGuardHome/issues/2664
[#2666]: https://github.com/AdguardTeam/AdGuardHome/issues/2666
[#2667]: https://github.com/AdguardTeam/AdGuardHome/issues/2667
[#2675]: https://github.com/AdguardTeam/AdGuardHome/issues/2675
[#2678]: https://github.com/AdguardTeam/AdGuardHome/issues/2678


Expand Down
36 changes: 27 additions & 9 deletions internal/home/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,43 @@ func withMiddlewares(h http.Handler, middlewares ...middleware) (wrapped http.Ha
return wrapped
}

// RequestBodySizeLimit is maximum request body length in bytes.
// RequestBodySizeLimit is the maximum request body size in bytes.
const RequestBodySizeLimit = 64 * 1024

// largerRequestBodySizeLimit is the maximum request body size for APIs
// expecting larger requests.
const largerRequestBodySizeLimit = 4 * 1024 * 1024

// expectsLargerRequests shows if this request should use a larger body size
// limit. These are exceptions for poorly designed current APIs as well as APIs
// that are designed to expect large files and requests. Remove once the new,
// better APIs are up.
//
// See https://github.com/AdguardTeam/AdGuardHome/issues/2666 and
// https://github.com/AdguardTeam/AdGuardHome/issues/2675.
func expectsLargerRequests(r *http.Request) (ok bool) {
m := r.Method
if m != http.MethodPost {
return false
}

p := r.URL.Path
return p == "/control/access/set" ||
p == "/control/filtering/set_rules"
}

// limitRequestBody wraps underlying handler h, making it's request's body Read
// method limited.
func limitRequestBody(h http.Handler) (limited http.Handler) {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var err error

var bodySizeLimit int64 = RequestBodySizeLimit
if u := r.URL; u.Path == "/control/access/set" {
// An exception for a poorly designed API. Remove once
// the new, better API is up.
//
// See https://github.com/AdguardTeam/AdGuardHome/issues/2666.
bodySizeLimit *= 4
var szLim int64 = RequestBodySizeLimit
if expectsLargerRequests(r) {
szLim = largerRequestBodySizeLimit
}

r.Body, err = aghio.LimitReadCloser(r.Body, bodySizeLimit)
r.Body, err = aghio.LimitReadCloser(r.Body, szLim)
if err != nil {
log.Error("limitRequestBody: %s", err)

Expand Down

0 comments on commit dad39ae

Please sign in to comment.