11package server
22
33import (
4- "bytes "
4+ "bufio "
55 "encoding/json"
6- "io/ioutil "
6+ "io"
77 "net/http"
88 "runtime"
99 "strings"
@@ -14,6 +14,7 @@ import (
1414 "github.com/docker/docker/dockerversion"
1515 "github.com/docker/docker/errors"
1616 "github.com/docker/docker/pkg/authorization"
17+ "github.com/docker/docker/pkg/ioutils"
1718 "github.com/docker/docker/pkg/version"
1819 "golang.org/x/net/context"
1920)
@@ -27,25 +28,37 @@ func debugRequestMiddleware(handler httputils.APIFunc) httputils.APIFunc {
2728 return func (ctx context.Context , w http.ResponseWriter , r * http.Request , vars map [string ]string ) error {
2829 logrus .Debugf ("%s %s" , r .Method , r .RequestURI )
2930
30- if r .Method == "POST" {
31- if err := httputils .CheckForJSON (r ); err == nil {
32- var buf bytes.Buffer
33- if _ , err := buf .ReadFrom (r .Body ); err == nil {
34- r .Body .Close ()
35- r .Body = ioutil .NopCloser (& buf )
36- var postForm map [string ]interface {}
37- if err := json .Unmarshal (buf .Bytes (), & postForm ); err == nil {
38- if _ , exists := postForm ["password" ]; exists {
39- postForm ["password" ] = "*****"
40- }
41- formStr , errMarshal := json .Marshal (postForm )
42- if errMarshal == nil {
43- logrus .Debugf ("form data: %s" , string (formStr ))
44- } else {
45- logrus .Debugf ("form data: %q" , postForm )
46- }
47- }
48- }
31+ if r .Method != "POST" {
32+ return handler (ctx , w , r , vars )
33+ }
34+ if err := httputils .CheckForJSON (r ); err != nil {
35+ return handler (ctx , w , r , vars )
36+ }
37+ maxBodySize := 4096 // 4KB
38+ if r .ContentLength > int64 (maxBodySize ) {
39+ return handler (ctx , w , r , vars )
40+ }
41+
42+ body := r .Body
43+ bufReader := bufio .NewReaderSize (body , maxBodySize )
44+ r .Body = ioutils .NewReadCloserWrapper (bufReader , func () error { return body .Close () })
45+
46+ b , err := bufReader .Peek (maxBodySize )
47+ if err != io .EOF {
48+ // either there was an error reading, or the buffer is full (in which case the request is too large)
49+ return handler (ctx , w , r , vars )
50+ }
51+
52+ var postForm map [string ]interface {}
53+ if err := json .Unmarshal (b , & postForm ); err == nil {
54+ if _ , exists := postForm ["password" ]; exists {
55+ postForm ["password" ] = "*****"
56+ }
57+ formStr , errMarshal := json .Marshal (postForm )
58+ if errMarshal == nil {
59+ logrus .Debugf ("form data: %s" , string (formStr ))
60+ } else {
61+ logrus .Debugf ("form data: %q" , postForm )
4962 }
5063 }
5164
0 commit comments