check content length before reading
This commit is contained in:
@@ -3,6 +3,7 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -13,6 +14,8 @@ import (
|
|||||||
|
|
||||||
const maxBytes = 1 << 20 // 1 MiB
|
const maxBytes = 1 << 20 // 1 MiB
|
||||||
|
|
||||||
|
var errContentLengthExceeded = errors.New("content length exceeded")
|
||||||
|
|
||||||
func CryptionHandler(key []byte) func(http.Handler) http.Handler {
|
func CryptionHandler(key []byte) func(http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -35,6 +38,10 @@ func CryptionHandler(key []byte) func(http.Handler) http.Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func decryptBody(key []byte, r *http.Request) error {
|
func decryptBody(key []byte, r *http.Request) error {
|
||||||
|
if r.ContentLength > maxBytes {
|
||||||
|
return errContentLengthExceeded
|
||||||
|
}
|
||||||
|
|
||||||
content, err := ioutil.ReadAll(io.LimitReader(r.Body, maxBytes))
|
content, err := ioutil.ReadAll(io.LimitReader(r.Body, maxBytes))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user