Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

rc: limit request size when the rc server has no authentication GHSA-f8p4-8g4j-q3m2 CVE-PENDING

Nick Craig-Wood17ebd339da05
diff --git a/docs/content/rc.md b/docs/content/rc.md
index 5fb5f62a5..309437921 100644
--- a/docs/content/rc.md
+++ b/docs/content/rc.md
@@ -232,6 +232,9 @@ If this is set then no authorisation will be required on the server to
use these methods. The alternative is to use `--rc-user` and
`--rc-pass` and use these credentials in the request.
+Without authorisation or this flag, POST request bodies are limited to
+64 KiB.
+
Default Off.
### --rc-baseurl
diff --git a/fs/rc/rcserver/rcserver.go b/fs/rc/rcserver/rcserver.go
index b3251dabb..1eda5c1a2 100644
--- a/fs/rc/rcserver/rcserver.go
+++ b/fs/rc/rcserver/rcserver.go
@@ -45,6 +45,11 @@ func Start(ctx context.Context, opt *rc.Options) (*Server, error) {
return nil, nil
}
+// maxUnauthenticatedBodySize is the largest POST body accepted when
+// the rc server has no authentication. It is kept small as decoding
+// JSON can use many times the size of its input in memory.
+const maxUnauthenticatedBodySize = 64 << 10
+
// Server contains everything to run the rc server
type Server struct {
ctx context.Context // for global config
@@ -180,10 +185,24 @@ func (s *Server) handler(w http.ResponseWriter, r *http.Request) {
}
}
+// bodyErrorStatus returns the HTTP status for an error reading the request body
+func bodyErrorStatus(err error) int {
+ if _, ok := errors.AsType[*http.MaxBytesError](err); ok {
+ return http.StatusRequestEntityTooLarge
+ }
+ return http.StatusBadRequest
+}
+
func (s *Server) handlePost(w http.ResponseWriter, r *http.Request, path string) {
ctx := r.Context()
contentType := r.Header.Get("Content-Type")
+ // Without auth anyone who can connect may POST, so bound the
+ // memory a request can make the server use
+ if !s.noAuth && !s.server.UsingAuth() {
+ r.Body = http.MaxBytesReader(w, r.Body, maxUnauthenticatedBodySize)
+ }
+
var (
contentTypeMediaType string
contentTypeParams map[string]string
@@ -202,7 +221,7 @@ func (s *Server) handlePost(w http.ResponseWriter, r *http.Request, path string)
// Parse the POST and URL parameters into r.Form, for others r.Form will be empty value
err := r.ParseForm()
if err != nil {
- writeError(path, nil, w, fmt.Errorf("failed to parse form/URL parameters: %w", err), http.StatusBadRequest)
+ writeError(path, nil, w, fmt.Errorf("failed to parse form/URL parameters: %w", err), bodyErrorStatus(err))
return
}
values = r.Form
@@ -226,7 +245,11 @@ func (s *Server) handlePost(w http.ResponseWriter, r *http.Request, path string)
err := json.NewDecoder(r.Body).Decode(&in)
if err != nil {
- writeError(path, in, w, fmt.Errorf("failed to read input JSON: %w", err), http.StatusBadRequest)
+ status := bodyErrorStatus(err)
+ if status == http.StatusRequestEntityTooLarge {
+ in = nil // don't echo back the partial input
+ }
+ writeError(path, in, w, fmt.Errorf("failed to read input JSON: %w", err), status)
return
}
}
diff --git a/fs/rc/rcserver/rcserver_test.go b/fs/rc/rcserver/rcserver_test.go
index 16ace6d10..42050bf3f 100644
--- a/fs/rc/rcserver/rcserver_test.go
+++ b/fs/rc/rcserver/rcserver_test.go
@@ -910,6 +910,75 @@ func TestAuthRequired(t *testing.T) {
testServer(t, tests, &opt)
}
+func TestBodyLimitWithoutAuth(t *testing.T) {
+ bigBody := `{"payload":"` + strings.Repeat("A", maxUnauthenticatedBodySize) + `"}`
+ tooLarge := regexp.MustCompile(`(?s)"error": "failed to read input JSON: http: request body too large",\s*"input": null,.*"status": 413`)
+ tests := []testRun{{
+ Name: "noAuthCall",
+ URL: "rc/noop",
+ Method: "POST",
+ Body: bigBody,
+ ContentType: "application/json",
+ Status: http.StatusRequestEntityTooLarge,
+ Contains: tooLarge,
+ }, {
+ Name: "authCall",
+ URL: "rc/noopauth",
+ Method: "POST",
+ Body: bigBody,
+ ContentType: "application/json",
+ Status: http.StatusRequestEntityTooLarge,
+ Contains: tooLarge,
+ }, {
+ Name: "form",
+ URL: "rc/noop",
+ Method: "POST",
+ Body: "payload=" + strings.Repeat("A", maxUnauthenticatedBodySize),
+ ContentType: "application/x-www-form-urlencoded",
+ Status: http.StatusRequestEntityTooLarge,
+ Contains: regexp.MustCompile(`"error": "failed to parse form/URL parameters: http: request body too large"`),
+ }, {
+ Name: "small",
+ URL: "rc/noop",
+ Method: "POST",
+ Body: `{"potato":1}`,
+ ContentType: "application/json",
+ Status: http.StatusOK,
+ Expected: `{
+ "potato": 1
+}
+`,
+ }}
+ opt := newTestOpt()
+ opt.Serve = false
+ opt.Files = ""
+ opt.NoAuth = false
+ testServer(t, tests, &opt)
+
+ // --rc-no-auth explicitly allows everything so isn't limited
+ bigOK := testRun{
+ Name: "noAuthFlag",
+ URL: "rc/noop",
+ Method: "POST",
+ Body: bigBody,
+ ContentType: "application/json",
+ Status: http.StatusOK,
+ Contains: regexp.MustCompile(`"payload": "AAAA`),
+ }
+ opt.NoAuth = true
+ testServer(t, []testRun{bigOK}, &opt)
+
+ // Authenticated requests aren't limited either
+ opt.NoAuth = false
+ opt.Auth.BasicUser = "user"
+ opt.Auth.BasicPass = "pass"
+ bigOK.Name = "withAuth"
+ bigOK.URL = "rc/noopauth"
+ bigOK.User = "user"
+ bigOK.Pass = "pass"
+ testServer(t, []testRun{bigOK}, &opt)
+}
+
// job/status returns the output of any job so it must need
// authentication, e.g. so an unauthenticated user can't read the
// output of config/dump started with _async by an authenticated one.