fix: backend error interceptor
This commit is contained in:
@@ -30,7 +30,7 @@ func (blockList AccessListMiddleware) AccessMiddleware(next http.Handler) http.H
|
||||
for _, block := range blockList.List {
|
||||
if isPathBlocked(r.URL.Path, util.ParseURLPath(blockList.Path+block)) {
|
||||
logger.Error("%s: %s access forbidden", getRealIP(r), r.URL.Path)
|
||||
RespondWithError(w, http.StatusForbidden, fmt.Sprintf("%d you do not have permission to access this resource"))
|
||||
RespondWithError(w, http.StatusForbidden, fmt.Sprintf("%d you do not have permission to access this resource", http.StatusForbidden))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,18 +19,13 @@ package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
errorinterceptor "github.com/jkaninda/goma-gateway/pkg/errorinterceptor"
|
||||
"github.com/jkaninda/goma-gateway/pkg/logger"
|
||||
"net/http"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type BlockCommon struct {
|
||||
ErrorInterceptor errorinterceptor.ErrorInterceptor
|
||||
}
|
||||
|
||||
// BlockExploitsMiddleware Middleware to block common exploits
|
||||
func (blockCommon BlockCommon) BlockExploitsMiddleware(next http.Handler) http.Handler {
|
||||
func BlockExploitsMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Patterns to detect SQL injection attempts
|
||||
sqlInjectionPattern := regexp.MustCompile(sqlPatterns)
|
||||
|
||||
@@ -45,15 +45,12 @@ func (intercept InterceptErrors) ErrorInterceptor(next http.Handler) http.Handle
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
rec := newResponseRecorder(w)
|
||||
next.ServeHTTP(rec, r)
|
||||
w.Header().Set("Proxied-By", "Goma Gateway")
|
||||
w.Header().Del("Server") //Delete server name
|
||||
if canIntercept(rec.statusCode, intercept.Errors) {
|
||||
logger.Debug("Backend error")
|
||||
logger.Error("An error occurred from the backend with the status code: %d", rec.statusCode)
|
||||
//Update Origin Cors Headers
|
||||
if allowedOrigin(intercept.Origins, r.Header.Get("Origin")) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
|
||||
}
|
||||
logger.Debug("An error occurred in the backend, %d", rec.statusCode)
|
||||
logger.Error("Backend error: %d", rec.statusCode)
|
||||
RespondWithError(w, rec.statusCode, http.StatusText(rec.statusCode))
|
||||
return
|
||||
} else {
|
||||
// No error: write buffered response to client
|
||||
w.WriteHeader(rec.statusCode)
|
||||
@@ -61,7 +58,6 @@ func (intercept InterceptErrors) ErrorInterceptor(next http.Handler) http.Handle
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -75,6 +75,5 @@ func RespondWithError(w http.ResponseWriter, statusCode int, logMessage string)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ import (
|
||||
// RateLimiter defines requests limit properties.
|
||||
type RateLimiter struct {
|
||||
requests int
|
||||
id int
|
||||
id string
|
||||
window time.Duration
|
||||
clientMap map[string]*Client
|
||||
mu sync.Mutex
|
||||
@@ -42,7 +42,7 @@ type Client struct {
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
type RateLimit struct {
|
||||
Id int
|
||||
Id string
|
||||
Requests int
|
||||
Window time.Duration
|
||||
Origins []string
|
||||
|
||||
Reference in New Issue
Block a user