Skip to content

Commit

Permalink
add request body size limit (#913)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas7788 authored and laizy committed May 21, 2019
1 parent 6686600 commit 75960f1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
1 change: 1 addition & 0 deletions http/base/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
)

const MAX_SEARCH_HEIGHT uint32 = 100
const MAX_REQUEST_BODY_SIZE = 1 << 20

type BalanceOfRsp struct {
Ont string `json:"ont"`
Expand Down
14 changes: 5 additions & 9 deletions http/base/rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
"encoding/json"
"fmt"
"github.com/ontio/ontology/common/log"
"github.com/ontio/ontology/http/base/common"
berr "github.com/ontio/ontology/http/base/error"
"io"
"io/ioutil"
"net/http"
"os"
Expand Down Expand Up @@ -79,7 +81,6 @@ func Handle(w http.ResponseWriter, r *http.Request) {
return
}
}

//check if there is Request Body to read
if r.Body == nil {
if mainMux.defaultFunction != nil {
Expand All @@ -91,15 +92,10 @@ func Handle(w http.ResponseWriter, r *http.Request) {
return
}
}

//read the body of the request
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Error("HTTP JSON RPC Handle - ioutil.ReadAll: ", err)
return
}
request := make(map[string]interface{})
err = json.Unmarshal(body, &request)
defer r.Body.Close()
decoder := json.NewDecoder(io.LimitReader(r.Body, common.MAX_REQUEST_BODY_SIZE))
err := decoder.Decode(&request)
if err != nil {
log.Error("HTTP JSON RPC Handle - json.Unmarshal: ", err)
return
Expand Down
9 changes: 4 additions & 5 deletions http/restful/restful/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import (
"encoding/json"
cfg "github.com/ontio/ontology/common/config"
"github.com/ontio/ontology/common/log"
"github.com/ontio/ontology/http/base/common"
berr "github.com/ontio/ontology/http/base/error"
"github.com/ontio/ontology/http/base/rest"
"golang.org/x/net/netutil"
"io/ioutil"
"io"
"net"
"net/http"
"strconv"
Expand Down Expand Up @@ -271,16 +272,14 @@ func (this *restServer) initGetHandler() {
func (this *restServer) initPostHandler() {
for k, _ := range this.postMap {
this.router.Post(k, func(w http.ResponseWriter, r *http.Request) {

body, _ := ioutil.ReadAll(r.Body)
decoder := json.NewDecoder(io.LimitReader(r.Body, common.MAX_REQUEST_BODY_SIZE))
defer r.Body.Close()

var req = make(map[string]interface{})
var resp map[string]interface{}

url := this.getPath(r.URL.Path)
if h, ok := this.postMap[url]; ok {
if err := json.Unmarshal(body, &req); err == nil {
if err := decoder.Decode(&req); err == nil {
req = this.getParams(r, url, req)
resp = h.handler(req)
resp["Action"] = h.name
Expand Down
2 changes: 1 addition & 1 deletion http/websocket/websocket/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (self *WsServer) checkSessionsTimeout(done chan bool) {

func (self *WsServer) webSocketHandler(w http.ResponseWriter, r *http.Request) {
wsConn, err := self.Upgrader.Upgrade(w, r, nil)

wsConn.SetReadLimit(1024 * 1024)
if err != nil {
log.Error("websocket Upgrader: ", err)
return
Expand Down

0 comments on commit 75960f1

Please sign in to comment.