From 2e89edf2ed7af332813a377e0243ed2a3db7d606 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 7 Nov 2025 18:33:03 +0100 Subject: [PATCH 1/7] add new `Write()` func for Writing the body --- utils/request/request.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/utils/request/request.go b/utils/request/request.go index d3a5241c..e3e64a67 100644 --- a/utils/request/request.go +++ b/utils/request/request.go @@ -6,6 +6,7 @@ import ( "errors" "io" "net/http" + "strconv" "strings" "github.com/codeshelldev/secured-signal-api/utils/query" @@ -29,6 +30,17 @@ func (body Body) ToString() string { return string(body.Raw) } +func (body Body) Write(req *http.Request) { + bodyLength := len(body.Raw) + + if req.ContentLength != int64(bodyLength) { + req.ContentLength = int64(bodyLength) + req.Header.Set("Content-Length", strconv.Itoa(bodyLength)) + } + + req.Body = io.NopCloser(bytes.NewReader(body.Raw)) +} + func CreateBody(data map[string]any) (Body, error) { if len(data) <= 0 { err := errors.New("empty data map") @@ -175,4 +187,4 @@ func GetBodyType(req *http.Request) BodyType { default: return Unknown } -} +} \ No newline at end of file From cd6d010cf6d5633dcae131c316f05a7e596b28db Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 7 Nov 2025 18:51:12 +0100 Subject: [PATCH 2/7] limit reader to 5MB --- utils/request/request.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/request/request.go b/utils/request/request.go index e3e64a67..22e53847 100644 --- a/utils/request/request.go +++ b/utils/request/request.go @@ -96,7 +96,7 @@ func GetFormData(body []byte) (map[string]any, error) { } func GetBody(req *http.Request) ([]byte, error) { - bodyBytes, err := io.ReadAll(req.Body) + bodyBytes, err := io.ReadAll(io.LimitReader(req.Body, 1<<20)) req.Body.Close() From c03ed979af14703617b0f3e9a20214f680cca9f7 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 7 Nov 2025 18:51:40 +0100 Subject: [PATCH 3/7] change stream size limit to 5MB --- utils/request/request.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/request/request.go b/utils/request/request.go index 22e53847..d068e791 100644 --- a/utils/request/request.go +++ b/utils/request/request.go @@ -96,7 +96,7 @@ func GetFormData(body []byte) (map[string]any, error) { } func GetBody(req *http.Request) ([]byte, error) { - bodyBytes, err := io.ReadAll(io.LimitReader(req.Body, 1<<20)) + bodyBytes, err := io.ReadAll(io.LimitReader(req.Body, 5<<20)) req.Body.Close() From ac38c0786672e66fae30b002b8ce41082a76921d Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 7 Nov 2025 18:52:23 +0100 Subject: [PATCH 4/7] removed additional bodywrite --- internals/proxy/middlewares/mapping.go | 21 +++------------------ internals/proxy/middlewares/message.go | 21 +++------------------ internals/proxy/middlewares/template.go | 21 +++------------------ 3 files changed, 9 insertions(+), 54 deletions(-) diff --git a/internals/proxy/middlewares/mapping.go b/internals/proxy/middlewares/mapping.go index f2d7fdf6..5f47666f 100644 --- a/internals/proxy/middlewares/mapping.go +++ b/internals/proxy/middlewares/mapping.go @@ -1,10 +1,7 @@ package middlewares import ( - "bytes" - "io" "net/http" - "strconv" "github.com/codeshelldev/secured-signal-api/internals/config/structure" jsonutils "github.com/codeshelldev/secured-signal-api/utils/jsonutils" @@ -62,25 +59,13 @@ func mappingHandler(next http.Handler) http.Handler { } if modifiedBody { - modifiedBody, err := request.CreateBody(bodyData) + body.Data = bodyData - if err != nil { - http.Error(w, "Internal Error", http.StatusInternalServerError) - return - } - - body = modifiedBody - - strData := body.ToString() + log.Debug("Applied Data Aliasing: ", body.Data) - log.Debug("Applied Data Aliasing: ", strData) - - req.ContentLength = int64(len(strData)) - req.Header.Set("Content-Length", strconv.Itoa(len(strData))) + body.Write(req) } - req.Body = io.NopCloser(bytes.NewReader(body.Raw)) - next.ServeHTTP(w, req) }) } diff --git a/internals/proxy/middlewares/message.go b/internals/proxy/middlewares/message.go index 47662085..eb624519 100644 --- a/internals/proxy/middlewares/message.go +++ b/internals/proxy/middlewares/message.go @@ -1,10 +1,7 @@ package middlewares import ( - "bytes" - "io" "net/http" - "strconv" log "github.com/codeshelldev/secured-signal-api/utils/logger" request "github.com/codeshelldev/secured-signal-api/utils/request" @@ -61,25 +58,13 @@ func messageHandler(next http.Handler) http.Handler { } if modifiedBody { - modifiedBody, err := request.CreateBody(bodyData) + body.Data = bodyData - if err != nil { - http.Error(w, "Internal Error", http.StatusInternalServerError) - return - } - - body = modifiedBody - - strData := body.ToString() + log.Debug("Applied Message Templating: ", body.Data) - log.Debug("Applied Message Templating: ", strData) - - req.ContentLength = int64(len(strData)) - req.Header.Set("Content-Length", strconv.Itoa(len(strData))) + body.Write(req) } - req.Body = io.NopCloser(bytes.NewReader(body.Raw)) - next.ServeHTTP(w, req) }) } diff --git a/internals/proxy/middlewares/template.go b/internals/proxy/middlewares/template.go index 1577d363..633ddb42 100644 --- a/internals/proxy/middlewares/template.go +++ b/internals/proxy/middlewares/template.go @@ -1,13 +1,10 @@ package middlewares import ( - "bytes" - "io" "maps" "net/http" "net/url" "regexp" - "strconv" "strings" jsonutils "github.com/codeshelldev/secured-signal-api/utils/jsonutils" @@ -73,25 +70,13 @@ func templateHandler(next http.Handler) http.Handler { } if modifiedBody { - modifiedBody, err := request.CreateBody(bodyData) + body.Data = bodyData - if err != nil { - http.Error(w, "Internal Error", http.StatusInternalServerError) - return - } - - body = modifiedBody + log.Debug("Applied Body Templating: ", body.Data) - strData := body.ToString() - - log.Debug("Applied Body Templating: ", strData) - - req.ContentLength = int64(len(strData)) - req.Header.Set("Content-Length", strconv.Itoa(len(strData))) + body.Write(req) } - req.Body = io.NopCloser(bytes.NewReader(body.Raw)) - if req.URL.Path != "" { var modified bool From 682aa10e308fc6d619b9a5423c8a87cd63a7a622 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 7 Nov 2025 22:19:59 +0100 Subject: [PATCH 5/7] debug --- internals/proxy/middlewares/template.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internals/proxy/middlewares/template.go b/internals/proxy/middlewares/template.go index 633ddb42..f4b27a3a 100644 --- a/internals/proxy/middlewares/template.go +++ b/internals/proxy/middlewares/template.go @@ -75,6 +75,10 @@ func templateHandler(next http.Handler) http.Handler { log.Debug("Applied Body Templating: ", body.Data) body.Write(req) + + test, _ := request.GetReqBody(req) + + log.Dev("TEST:", test) } if req.URL.Path != "" { From 32464d8ca3266800d16de5a1a8895385820fa320 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 7 Nov 2025 22:30:03 +0100 Subject: [PATCH 6/7] fix body serialisation --- internals/proxy/middlewares/mapping.go | 9 +++++++-- internals/proxy/middlewares/message.go | 9 +++++++-- internals/proxy/middlewares/template.go | 11 ++++++----- utils/request/request.go | 10 +++++++++- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/internals/proxy/middlewares/mapping.go b/internals/proxy/middlewares/mapping.go index 5f47666f..53f7a010 100644 --- a/internals/proxy/middlewares/mapping.go +++ b/internals/proxy/middlewares/mapping.go @@ -61,9 +61,14 @@ func mappingHandler(next http.Handler) http.Handler { if modifiedBody { body.Data = bodyData - log.Debug("Applied Data Aliasing: ", body.Data) + err := body.Write(req) + + if err != nil { + http.Error(w, "Internal Error", http.StatusInternalServerError) + return + } - body.Write(req) + log.Debug("Applied Data Aliasing: ", body.Data) } next.ServeHTTP(w, req) diff --git a/internals/proxy/middlewares/message.go b/internals/proxy/middlewares/message.go index eb624519..481c2167 100644 --- a/internals/proxy/middlewares/message.go +++ b/internals/proxy/middlewares/message.go @@ -60,9 +60,14 @@ func messageHandler(next http.Handler) http.Handler { if modifiedBody { body.Data = bodyData - log.Debug("Applied Message Templating: ", body.Data) + err := body.Write(req) + + if err != nil { + http.Error(w, "Internal Error", http.StatusInternalServerError) + return + } - body.Write(req) + log.Debug("Applied Message Templating: ", body.Data) } next.ServeHTTP(w, req) diff --git a/internals/proxy/middlewares/template.go b/internals/proxy/middlewares/template.go index f4b27a3a..9ab980d8 100644 --- a/internals/proxy/middlewares/template.go +++ b/internals/proxy/middlewares/template.go @@ -72,13 +72,14 @@ func templateHandler(next http.Handler) http.Handler { if modifiedBody { body.Data = bodyData - log.Debug("Applied Body Templating: ", body.Data) - - body.Write(req) + err := body.Write(req) - test, _ := request.GetReqBody(req) + if err != nil { + http.Error(w, "Internal Error", http.StatusInternalServerError) + return + } - log.Dev("TEST:", test) + log.Debug("Applied Body Templating: ", body.Data) } if req.URL.Path != "" { diff --git a/utils/request/request.go b/utils/request/request.go index d068e791..22c71dcf 100644 --- a/utils/request/request.go +++ b/utils/request/request.go @@ -30,7 +30,15 @@ func (body Body) ToString() string { return string(body.Raw) } -func (body Body) Write(req *http.Request) { +func (body *Body) Write(req *http.Request) error { + newBody, err := CreateBody(body.Data) + + if err != nil { + return err + } + + body = &newBody + bodyLength := len(body.Raw) if req.ContentLength != int64(bodyLength) { From fe888c0483eb7c1dd80f2230804865a84afd787f Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 7 Nov 2025 22:31:42 +0100 Subject: [PATCH 7/7] added missing return --- utils/request/request.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/utils/request/request.go b/utils/request/request.go index 22c71dcf..8dd85141 100644 --- a/utils/request/request.go +++ b/utils/request/request.go @@ -47,6 +47,8 @@ func (body *Body) Write(req *http.Request) error { } req.Body = io.NopCloser(bytes.NewReader(body.Raw)) + + return nil } func CreateBody(data map[string]any) (Body, error) {