From 5e38c8e479bc3702b8f98e431f0c35f778b152be Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Sat, 31 Jan 2026 10:27:57 +0100 Subject: [PATCH] add path as body injection method --- internals/proxy/middlewares/template.go | 71 ++++++++++++++++++------- 1 file changed, 53 insertions(+), 18 deletions(-) diff --git a/internals/proxy/middlewares/template.go b/internals/proxy/middlewares/template.go index af7d1a2e..ef1235be 100644 --- a/internals/proxy/middlewares/template.go +++ b/internals/proxy/middlewares/template.go @@ -10,6 +10,7 @@ import ( jsonutils "github.com/codeshelldev/gotl/pkg/jsonutils" query "github.com/codeshelldev/gotl/pkg/query" request "github.com/codeshelldev/gotl/pkg/request" + "github.com/codeshelldev/gotl/pkg/stringutils" templating "github.com/codeshelldev/gotl/pkg/templating" "github.com/codeshelldev/secured-signal-api/utils/requestkeys" ) @@ -73,32 +74,37 @@ func templateHandler(next http.Handler) http.Handler { } } - if modifiedBody { - body.Data = bodyData + if req.URL.Path != "" { + var modified bool + var templated bool - err := body.Write(req) + req.URL.Path, bodyData, modified, templated, err = TemplatePath(req.URL, bodyData, variables) if err != nil { - logger.Error("Could not write to Request Body: ", err.Error()) - http.Error(w, "Internal Server Error", http.StatusInternalServerError) - return + logger.Error("Error Templating Path: ", err.Error()) } - logger.Debug("Applied Body Templating: ", body.Data) + if modified { + logger.Debug("Applied Path Templating: ", req.URL.Path) + } + + if templated { + modifiedBody = true + } } - if req.URL.Path != "" { - var modified bool + if modifiedBody { + body.Data = bodyData - req.URL.Path, modified, err = TemplatePath(req.URL, variables) + err := body.Write(req) if err != nil { - logger.Error("Error Templating Path: ", err.Error()) + logger.Error("Could not write to Request Body: ", err.Error()) + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return } - if modified { - logger.Debug("Applied Path Templating: ", req.URL.Path) - } + logger.Debug("Applied Body Templating: ", body.Data) } next.ServeHTTP(w, req) @@ -208,26 +214,55 @@ func TemplateBody(body map[string]any, headers map[string][]string, VARIABLES ma return templatedData, modified, nil } -func TemplatePath(reqUrl *url.URL, VARIABLES any) (string, bool, error) { +func TemplatePath(reqUrl *url.URL, data map[string]any, VARIABLES any) (string, map[string]any, bool, bool, error) { var modified bool + var modifiedBody bool reqPath, err := url.PathUnescape(reqUrl.Path) if err != nil { - return reqUrl.Path, modified, err + return reqUrl.Path, data, false, false, err } reqPath, err = templating.RenderNormalizedTemplate("path", reqPath, VARIABLES) if err != nil { - return reqUrl.Path, modified, err + return reqUrl.Path, data, false, false, err + } + + parts := strings.Split(reqPath, "/") + newParts := []string{} + + for _, part := range parts { + newParts = append(newParts, part) + + keyValuePair := strings.SplitN(part, "=", 2) + + if len(keyValuePair) != 2 { + continue + } + + keyWithoutPrefix, match := strings.CutPrefix(keyValuePair[0], "@") + + if !match { + continue + } + + value := stringutils.ToType(keyValuePair[1]) + + data[keyWithoutPrefix] = value + modifiedBody = true + + newParts = newParts[:len(newParts) - 1] } + reqPath = strings.Join(newParts, "/") + if reqUrl.Path != reqPath { modified = true } - return reqPath, modified, nil + return reqPath, data, modified, modifiedBody, nil } func TemplateQuery(reqUrl *url.URL, data map[string]any, VARIABLES any) (string, map[string]any, bool, error) {