Skip to content

Commit

Permalink
feat: do not display internal information on error (#3)
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Chenebault <benjamin.chenebault@keeneye.tech>

Co-authored-by: Benjamin Chenebault <benjamin.chenebault@keeneye.tech>
  • Loading branch information
bench and Benjamin Chenebault committed Mar 16, 2022
1 parent b9981c4 commit 19cdb47
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions modsecurity.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"time"
)

Expand All @@ -31,6 +33,7 @@ type Modsecurity struct {
next http.Handler
modSecurityUrl string
name string
logger *log.Logger
}

// New created a new Modsecurity plugin.
Expand All @@ -43,6 +46,7 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
modSecurityUrl: config.ModSecurityUrl,
next: next,
name: name,
logger: log.New(os.Stdout, "", log.LstdFlags),
}, nil
}

Expand All @@ -58,7 +62,8 @@ func (a *Modsecurity) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// in the request.
body, err := ioutil.ReadAll(req.Body)
if err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
a.logger.Printf("fail to read incoming request: %s", err.Error())
http.Error(rw, "", http.StatusBadGateway)
return
}

Expand All @@ -71,7 +76,8 @@ func (a *Modsecurity) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
proxyReq, err := http.NewRequest(req.Method, url, bytes.NewReader(body))

if err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
a.logger.Printf("fail to prepare forwarded request: %s", err.Error())
http.Error(rw, "", http.StatusBadGateway)
return
}

Expand All @@ -84,7 +90,8 @@ func (a *Modsecurity) ServeHTTP(rw http.ResponseWriter, req *http.Request) {

resp, err := httpClient.Do(proxyReq)
if err != nil {
http.Error(rw, err.Error(), http.StatusBadGateway)
a.logger.Printf("fail to send HTTP request to modsec: %s", err.Error())
http.Error(rw, "", http.StatusBadGateway)
return
}
defer resp.Body.Close()
Expand Down

0 comments on commit 19cdb47

Please sign in to comment.