From a9858a90c61d7d065f35db47c53f036625ba6f3b Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Wed, 17 Sep 2025 19:14:54 +0200 Subject: [PATCH 01/42] hint new feat in readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index bae148d..79b2100 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ endpoint restrictions, placeholders, and flexible configuration. - [Configuration](#configuration) - [Endpoints](#endpoints) - [Variables](#variables) + - [Message Templates](#message-templates) - [Contributing](#contributing) - [Support](#support) - [License](#license) From f4cde7b3f714e2fead2b02f470b60830a74cead4 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Wed, 17 Sep 2025 20:08:50 +0200 Subject: [PATCH 02/42] remove `ls` --- Dockerfile | 2 -- 1 file changed, 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 109e2c5..a8f6c15 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,6 +17,4 @@ COPY . . COPY dist/${TARGETOS}/${TARGETARCH}/app . -RUN ls - CMD ["./app"] From 0f63ec828d65780f60163f8d7b2a1f155c181c93 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 17:35:13 +0200 Subject: [PATCH 03/42] remove aliases --- README.md | 43 ++++--------------------------------------- 1 file changed, 4 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index c992331..98d03c5 100644 --- a/README.md +++ b/README.md @@ -66,10 +66,9 @@ services: container_name: secured-signal environment: API__URL: http://signal-api:8080 - SETTINGS__VARIABLES__RECIPIENTS: - '[+123400002, +123400003, +123400004]' + SETTINGS__VARIABLES__RECIPIENTS: "[+123400002, +123400003, +123400004]" SETTINGS__VARIABLES__NUMBER: "+123400001" - API__TOKENS: '[LOOOOOONG_STRING]' + API__TOKENS: "[LOOOOOONG_STRING]" ports: - "8880:8880" restart: unless-stopped @@ -101,10 +100,9 @@ services: container_name: secured-signal environment: API__URL: http://signal-api:8080 - SETTINGS__VARIABLES__RECIPIENTS: - '[+123400002,+123400003,+123400004]' + SETTINGS__VARIABLES__RECIPIENTS: "[+123400002,+123400003,+123400004]" SETTINGS__VARIABLES__NUMBER: "+123400001" - API__TOKENS: '[LOOOOOONG_STRING]' + API__TOKENS: "[LOOOOOONG_STRING]" labels: - traefik.enable=true - traefik.http.routers.signal-api.rule=Host(`signal-api.mydomain.com`) @@ -439,39 +437,6 @@ settings: recipients: ["+123400002", "group.id", "user.id"] ``` -### Message Aliases - -To improve compatibility with other services Secured Signal API provides **Message Aliases** for the `message` attribute. - -
-Default Message Aliases - -| Alias | Score | Alias | Score | -| ------------ | ----- | ---------------- | ----- | -| msg | 100 | data.content | 9 | -| content | 99 | data.description | 8 | -| description | 98 | data.text | 7 | -| text | 20 | data.summary | 6 | -| summary | 15 | data.details | 5 | -| details | 14 | body | 2 | -| data.message | 10 | data | 1 | - -
- -Secured Signal API will pick the best scoring Message Alias (if available) to extract the correct message from the Request Body. - -Message Aliases can be added by setting `messageAliases` in your config: - -```yaml -settings: - messageAliases: - [ - { alias: "msg", score: 80 }, - { alias: "data.message", score: 79 }, - { alias: "array[0].message", score: 78 }, - ] -``` - ### Port To change the Port which Secured Signal API uses, you need to set `server.port` in your config. (default: `8880`) From cb2673fcee59310752512bc2dcca4f71264ae944 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 17:36:33 +0200 Subject: [PATCH 04/42] testing --- main.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index da4c76a..c160846 100644 --- a/main.go +++ b/main.go @@ -75,4 +75,6 @@ func main() { <-stop docker.Shutdown(server) -} \ No newline at end of file +} + +// TESTING \ No newline at end of file From 3846eba041797833b260271d0f0b476315beab87 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 21:02:08 +0200 Subject: [PATCH 05/42] improved json templating --- tests/json_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/json_test.go b/tests/json_test.go index 399ba48..87f10bb 100644 --- a/tests/json_test.go +++ b/tests/json_test.go @@ -19,6 +19,7 @@ func TestJsonTemplating(t *testing.T) { json := ` { + "multiple": "{{.key}}, {{.int}}", "dict": { "key": "{{.key}}" }, "dictArray": [ { "key": "{{.key}}" }, @@ -31,6 +32,7 @@ func TestJsonTemplating(t *testing.T) { data := jsonutils.GetJson[map[string]any](json) expected := map[string]any{ + "multiple": "val, 4", "dict": map[string]any{ "key": "val", }, From f7fb6989b3de4f9fed011c00b6603ee59c450861 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 21:02:36 +0200 Subject: [PATCH 06/42] added aliases middleware --- .../proxy/middlewares/{body.go => aliases.go} | 71 +++++++++++++------ main.go | 8 ++- 2 files changed, 56 insertions(+), 23 deletions(-) rename internals/proxy/middlewares/{body.go => aliases.go} (50%) diff --git a/internals/proxy/middlewares/body.go b/internals/proxy/middlewares/aliases.go similarity index 50% rename from internals/proxy/middlewares/body.go rename to internals/proxy/middlewares/aliases.go index 25f01fe..c3221ca 100644 --- a/internals/proxy/middlewares/body.go +++ b/internals/proxy/middlewares/aliases.go @@ -12,18 +12,24 @@ import ( request "github.com/codeshelldev/secured-signal-api/utils/request" ) -type BodyMiddleware struct { +type AliasMiddleware struct { Next http.Handler } -func (data BodyMiddleware) Use() http.Handler { +func (data AliasMiddleware) Use() http.Handler { next := data.Next return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - messageAliases := getSettingsByReq(req).MESSAGE_ALIASES + settings := getSettingsByReq(req) - if messageAliases == nil { - messageAliases = getSettings("*").MESSAGE_ALIASES + dataAliases := settings.DATA_ALIASES + + if dataAliases == nil { + dataAliases = getSettings("*").DATA_ALIASES + } + + if settings.VARIABLES == nil { + settings.VARIABLES = getSettings("*").VARIABLES } body, err := request.GetReqBody(w, req) @@ -38,13 +44,21 @@ func (data BodyMiddleware) Use() http.Handler { if !body.Empty { bodyData = body.Data - content, ok := bodyData["message"] + var aliasData map[string]any + + aliasData, modifiedBody = processDataAliases(dataAliases, bodyData) - if !ok || content == "" { + for key, value := range aliasData { + prefix := key[:1] - bodyData["message"], bodyData = getMessage(messageAliases, bodyData) + keyWithoutPrefix := key[1:] - modifiedBody = true + switch prefix { + case "@": + bodyData[keyWithoutPrefix] = value + case ".": + settings.VARIABLES[keyWithoutPrefix] = value + } } } @@ -70,32 +84,47 @@ func (data BodyMiddleware) Use() http.Handler { }) } -func getMessage(aliases []middlewareTypes.MessageAlias, data map[string]any) (string, map[string]any) { - var content string +func processDataAliases(aliases map[string][]middlewareTypes.DataAlias, data map[string]any) (map[string]any, bool) { + var modified bool + + var newData map[string]any + + for key, alias := range aliases { + newData = getData(key, alias, data) + + if data[key] != newData[key] { + modified = true + } + } + + return newData, modified +} + +func getData(key string, aliases []middlewareTypes.DataAlias, data map[string]any) (map[string]any) { var best int for _, alias := range aliases { aliasValue, score, ok := processAlias(alias, data) - if ok && score > best { - content = aliasValue - } + if ok { + if score > best { + data[key] = aliasValue + } - data[alias.Alias] = nil + data[alias.Alias] = nil + } } - return content, data + return data } -func processAlias(alias middlewareTypes.MessageAlias, data map[string]any) (string, int, bool) { +func processAlias(alias middlewareTypes.DataAlias, data map[string]any) (any, int, bool) { aliasKey := alias.Alias value, ok := jsonutils.GetByPath(aliasKey, data) - aliasValue, isStr := value.(string) - - if isStr && ok && aliasValue != "" { - return aliasValue, alias.Score, true + if ok && value != nil { + return value, alias.Score, true } else { return "", 0, false } diff --git a/main.go b/main.go index c160846..93fdf80 100644 --- a/main.go +++ b/main.go @@ -33,12 +33,16 @@ func main() { initHandler = proxy.Create(ENV.API_URL) - body_m4 := middlewares.BodyMiddleware{ + mesg_m5 := middlewares.MessageMiddleware{ Next: initHandler, } + alias_m4 := middlewares.AliasMiddleware{ + Next: mesg_m5.Use(), + } + temp_m3 := middlewares.TemplateMiddleware{ - Next: body_m4.Use(), + Next: alias_m4.Use(), } endp_m2 := middlewares.EndpointsMiddleware{ From c2e5b51414c76fd803c7ae1c0d71304f6b1fbaab Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 21:02:57 +0200 Subject: [PATCH 07/42] temporarily updated defaults --- config/defaults.yml | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/config/defaults.yml b/config/defaults.yml index 5b2a0fa..ac9a327 100644 --- a/config/defaults.yml +++ b/config/defaults.yml @@ -4,25 +4,7 @@ server: logLevel: info settings: - messageAliases: - [ - { alias: msg, score: 100 }, - { alias: content, score: 99 }, - { alias: description, score: 98 }, - { alias: text, score: 20 }, - { alias: summary, score: 15 }, - { alias: details, score: 14 }, - - { alias: data.message, score: 10 }, - { alias: data.content, score: 9 }, - { alias: data.description, score: 8 }, - { alias: data.text, score: 7 }, - { alias: data.summary, score: 6 }, - { alias: data.details, score: 5 }, - - { alias: body, score: 2 }, - { alias: data, score: 1 }, - ] + dataAliases: ["@message": [{ alias: "data.msg", score: 100 }]] variables: recipients: ${RECIPIENTS} From 8f4bf51cf89491e5e5c13fac1c77f9eb980eb8f9 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 21:03:22 +0200 Subject: [PATCH 08/42] added message templates --- internals/proxy/middlewares/message.go | 97 ++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 internals/proxy/middlewares/message.go diff --git a/internals/proxy/middlewares/message.go b/internals/proxy/middlewares/message.go new file mode 100644 index 0000000..f7c9fe2 --- /dev/null +++ b/internals/proxy/middlewares/message.go @@ -0,0 +1,97 @@ +package middlewares + +import ( + "bytes" + "io" + "net/http" + "strconv" + + "github.com/codeshelldev/secured-signal-api/utils/jsonutils" + log "github.com/codeshelldev/secured-signal-api/utils/logger" + request "github.com/codeshelldev/secured-signal-api/utils/request" +) + +type MessageMiddleware struct { + Next http.Handler +} + +func (data MessageMiddleware) Use() http.Handler { + next := data.Next + + return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + settings := getSettingsByReq(req) + + variables := settings.VARIABLES + messageTemplate := settings.MESSAGE_TEMPLATE + + if variables == nil { + variables = getSettings("*").VARIABLES + } + + if messageTemplate == "" { + messageTemplate = getSettings("*").MESSAGE_TEMPLATE + } + + + body, err := request.GetReqBody(w, req) + + if err != nil { + log.Error("Could not get Request Body: ", err.Error()) + } + + bodyData := map[string]any{} + + var modifiedBody bool + + if !body.Empty { + templatedMessage, err := TemplateMessage(messageTemplate, bodyData, variables) + + if err != nil { + log.Error("Error Templating Message: ", err.Error()) + } + + if templatedMessage != bodyData["message"] && templatedMessage != "" { + bodyData["message"] = templatedMessage + modifiedBody = true + } + } + + if modifiedBody { + modifiedBody, err := request.CreateBody(bodyData) + + if err != nil { + http.Error(w, "Internal Error", http.StatusInternalServerError) + return + } + + body = modifiedBody + + strData := body.ToString() + + log.Debug("Applied Body Templating: ", strData) + + req.ContentLength = int64(len(strData)) + req.Header.Set("Content-Length", strconv.Itoa(len(strData))) + } + + req.Body = io.NopCloser(bytes.NewReader(body.Raw)) + + next.ServeHTTP(w, req) + }) +} + +func TemplateMessage(template string, data map[string]any, VARIABLES any) (string, error) { + data, ok, err := TemplateBody(data, VARIABLES) + + if err != nil || !ok || data == nil { + return template, err + } + + jsonStr, err := jsonutils.ToJsonSafe(data) + + if err != nil { + return template, err + } + + return jsonStr, nil +} \ No newline at end of file From e5f104e80c00d7e113f12ed53bb6ba333032ef3d Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 21:03:38 +0200 Subject: [PATCH 09/42] updated template middleware --- internals/proxy/middlewares/template.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/internals/proxy/middlewares/template.go b/internals/proxy/middlewares/template.go index 25946ba..9249e76 100644 --- a/internals/proxy/middlewares/template.go +++ b/internals/proxy/middlewares/template.go @@ -5,6 +5,7 @@ import ( "io" "net/http" "net/url" + "regexp" "strconv" jsonutils "github.com/codeshelldev/secured-signal-api/utils/jsonutils" @@ -107,7 +108,25 @@ func (data TemplateMiddleware) Use() http.Handler { func TemplateBody(data map[string]any, VARIABLES any) (map[string]any, bool, error) { var modified bool - templatedData, err := templating.RenderJSONTemplate("body", data, VARIABLES) + jsonStr := jsonutils.ToJson(data) + + if jsonStr != "" { + re, err := regexp.Compile(`{{\s*(\@)[a-zA-Z0-9_.]+\s*}}`) + + if err != nil { + return data, false, err + } + + jsonStr = re.ReplaceAllString(jsonStr, ".") + + normalizedData, err := jsonutils.GetJsonSafe[map[string]any](jsonStr) + + if err == nil { + data = normalizedData + } + } + + templatedData, err := templating.RenderJSON("body", data, VARIABLES) if err != nil { return data, false, err From 805a19fa77a725d181fe3f139da156ccf1197b95 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 21:03:52 +0200 Subject: [PATCH 10/42] renamed messageAlias to alias --- internals/proxy/middlewares/types/types.go | 2 +- utils/config/loader.go | 17 ++++++++++------- utils/config/tokens.go | 5 +---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/internals/proxy/middlewares/types/types.go b/internals/proxy/middlewares/types/types.go index ed96549..359f49b 100644 --- a/internals/proxy/middlewares/types/types.go +++ b/internals/proxy/middlewares/types/types.go @@ -3,7 +3,7 @@ package middlewareTypes -type MessageAlias struct { +type DataAlias struct { Alias string `koanf:"alias"` Score int `koanf:"score"` } \ No newline at end of file diff --git a/utils/config/loader.go b/utils/config/loader.go index 0258b6d..d6d8b3b 100644 --- a/utils/config/loader.go +++ b/utils/config/loader.go @@ -27,10 +27,11 @@ type ENV_ struct { } type SETTING_ struct { - BLOCKED_ENDPOINTS []string `koanf:"blockedendpoints"` - ALLOWED_ENDPOINTS []string `koanf:"allowedendpoints"` - VARIABLES map[string]any `koanf:"variables"` - MESSAGE_ALIASES []middlewareTypes.MessageAlias `koanf:"messagealiases"` + BLOCKED_ENDPOINTS []string `koanf:"blockedendpoints"` + ALLOWED_ENDPOINTS []string `koanf:"allowedendpoints"` + VARIABLES map[string]any `koanf:"variables"` + DATA_ALIASES map[string][]middlewareTypes.DataAlias `koanf:"dataaliases"` + MESSAGE_TEMPLATE string `koanf:"messagetemplate"` } var ENV *ENV_ = &ENV_{ @@ -77,9 +78,7 @@ func InitEnv() { var settings SETTING_ - transformChildren(config, "settings.variables", func(key string, value any) (string, any) { - return strings.ToUpper(key), value - }) + transformChildren(config, "settings.variables", transformVariables) config.Unmarshal("settings", &settings) @@ -106,4 +105,8 @@ func LoadConfig() { log.Error("Could not Load Config ", ENV.CONFIG_PATH, ": ", conErr.Error()) } } +} + +func transformVariables(key string, value any) (string, any) { + return strings.ToUpper(key), value } \ No newline at end of file diff --git a/utils/config/tokens.go b/utils/config/tokens.go index ce995a0..b271579 100644 --- a/utils/config/tokens.go +++ b/utils/config/tokens.go @@ -2,7 +2,6 @@ package config import ( "strconv" - "strings" log "github.com/codeshelldev/secured-signal-api/utils/logger" "github.com/knadh/koanf/parsers/yaml" @@ -28,9 +27,7 @@ func InitTokens() { var tokenConfigs []TOKEN_CONFIG_ - transformChildrenUnderArray(tokensLayer, "tokenconfigs", "overrides.variables", func(key string, value any) (string, any) { - return strings.ToUpper(key), value - }) + transformChildrenUnderArray(tokensLayer, "tokenconfigs", "overrides.variables", transformVariables) tokensLayer.Unmarshal("tokenconfigs", &tokenConfigs) From 57db640d46e2d06e85c4335038f0cf339028b54a Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 21:04:02 +0200 Subject: [PATCH 11/42] updated json templating --- utils/templating/templating.go | 52 ++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/utils/templating/templating.go b/utils/templating/templating.go index 37d4ec3..5887ded 100644 --- a/utils/templating/templating.go +++ b/utils/templating/templating.go @@ -13,7 +13,6 @@ func normalize(value any) string { switch str := value.(type) { case []string: return "[" + strings.Join(str, ",") + "]" - case []any: items := make([]string, len(str)) @@ -43,6 +42,37 @@ func normalizeJSON(value any) string { } } +func cleanQuotedPairsJSON(s string) string { + quoteRe, err := regexp.Compile(`"([^"]*?)"`) + + if err != nil { + return s + } + + pairRe, err := regexp.Compile(`<<([^<>]+)>>`) + + if err != nil { + return s + } + + return quoteRe.ReplaceAllStringFunc(s, func(container string) string { + inner := container[1 : len(container)-1] // remove quotes + + matches := pairRe.FindAllStringSubmatchIndex(inner, -1) + + // ONE pair which fills whole "" + if len(matches) == 1 && matches[0][0] == 0 && matches[0][1] == len(inner) { + return container // keep <<...>> untouched + } + + // MULTIPLE pairs || that do not fill whole "" + inner = pairRe.ReplaceAllString(inner, "$1") + inner = strings.ReplaceAll(inner, `"`, `'`) + + return `"` + inner + `"` + }) +} + func ParseTemplate(templt *template.Template, tmplStr string, variables any) (string, error) { tmpl, err := templt.Parse(tmplStr) @@ -69,6 +99,22 @@ func CreateTemplateWithFunc(name string, funcMap template.FuncMap) (*template.Te return template.New(name).Funcs(funcMap) } +func RenderJSON(name string, data map[string]any, variables any) (map[string]any, error) { + data, err := RenderJSONTemplate(name + ":json_path", data, data) + + if err != nil { + return data, err + } + + data, err = RenderJSONTemplate(name + ":variables", data, variables) + + if err != nil { + return data, err + } + + return data, nil +} + func RenderJSONTemplate(name string, data map[string]any, variables any) (map[string]any, error) { jsonBytes, err := json.Marshal(data) @@ -78,7 +124,7 @@ func RenderJSONTemplate(name string, data map[string]any, variables any) (map[st tmplStr := string(jsonBytes) - re, err := regexp.Compile(`{{\s*\.(\w+)\s*}}`) + re, err := regexp.Compile(`{{\s*\.([a-zA-Z0-9_.]+)\s*}}`) // Add normalize() to be able to remove Quotes from Arrays if err == nil { @@ -95,6 +141,8 @@ func RenderJSONTemplate(name string, data map[string]any, variables any) (map[st return nil, err } + jsonStr = cleanQuotedPairsJSON(jsonStr) + // Remove the Quotes around "<<[item1,item2]>>" re, err = regexp.Compile(`"<<(.*?)>>"`) From 290db8f5845bf45a659fbe98f7312c1d59f66776 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 21:21:28 +0200 Subject: [PATCH 12/42] possible fix by updating regex --- internals/proxy/middlewares/template.go | 4 ++-- utils/templating/templating.go | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/internals/proxy/middlewares/template.go b/internals/proxy/middlewares/template.go index 9249e76..84c820f 100644 --- a/internals/proxy/middlewares/template.go +++ b/internals/proxy/middlewares/template.go @@ -111,13 +111,13 @@ func TemplateBody(data map[string]any, VARIABLES any) (map[string]any, bool, err jsonStr := jsonutils.ToJson(data) if jsonStr != "" { - re, err := regexp.Compile(`{{\s*(\@)[a-zA-Z0-9_.]+\s*}}`) + re, err := regexp.Compile(`{{\s*\@([a-zA-Z0-9_.]+)\s*}}`) if err != nil { return data, false, err } - jsonStr = re.ReplaceAllString(jsonStr, ".") + jsonStr = re.ReplaceAllString(jsonStr, "{{.$1}}") normalizedData, err := jsonutils.GetJsonSafe[map[string]any](jsonStr) diff --git a/utils/templating/templating.go b/utils/templating/templating.go index 5887ded..3fa7da6 100644 --- a/utils/templating/templating.go +++ b/utils/templating/templating.go @@ -35,6 +35,10 @@ func normalizeJSON(value any) string { case []any, []string, map[string]any, int, float64, bool: object, _ := json.Marshal(value) + if string(object) == "{}" { + return value.(string) + } + return "<<" + string(object) + ">>" default: From 6bbf7f12fe3cd4679bedf5c6011d340961833dae Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 21:27:04 +0200 Subject: [PATCH 13/42] debugging issue with `{{@key}}` not working --- 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 84c820f..4421170 100644 --- a/internals/proxy/middlewares/template.go +++ b/internals/proxy/middlewares/template.go @@ -126,8 +126,12 @@ func TemplateBody(data map[string]any, VARIABLES any) (map[string]any, bool, err } } + log.Dev("after @ replacement:\n" + jsonStr) + templatedData, err := templating.RenderJSON("body", data, VARIABLES) + log.Dev("after render:\n", jsonutils.ToJson(templatedData)) + if err != nil { return data, false, err } From bb4efc29f02b5527659fe10a0318d7a9338c21f4 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 21:31:06 +0200 Subject: [PATCH 14/42] added missing message templating --- internals/proxy/middlewares/message.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internals/proxy/middlewares/message.go b/internals/proxy/middlewares/message.go index f7c9fe2..ff07fc8 100644 --- a/internals/proxy/middlewares/message.go +++ b/internals/proxy/middlewares/message.go @@ -81,6 +81,8 @@ func (data MessageMiddleware) Use() http.Handler { } func TemplateMessage(template string, data map[string]any, VARIABLES any) (string, error) { + data["message"] = template + data, ok, err := TemplateBody(data, VARIABLES) if err != nil || !ok || data == nil { From dd711ab95ecfd72712ae36b69d72c3f3409fe588 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 21:34:04 +0200 Subject: [PATCH 15/42] debugging issue --- config/defaults.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/config/defaults.yml b/config/defaults.yml index ac9a327..db8b3cf 100644 --- a/config/defaults.yml +++ b/config/defaults.yml @@ -4,8 +4,6 @@ server: logLevel: info settings: - dataAliases: ["@message": [{ alias: "data.msg", score: 100 }]] - variables: recipients: ${RECIPIENTS} number: ${NUMBER} From 96e6525c3e56c505924c4f75d5bfc36f772540b9 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 21:42:03 +0200 Subject: [PATCH 16/42] testing --- utils/templating/templating.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/utils/templating/templating.go b/utils/templating/templating.go index 3fa7da6..ce7c3a6 100644 --- a/utils/templating/templating.go +++ b/utils/templating/templating.go @@ -7,6 +7,9 @@ import ( "regexp" "strings" "text/template" + + "github.com/codeshelldev/secured-signal-api/utils/jsonutils" + "github.com/codeshelldev/secured-signal-api/utils/logger" ) func normalize(value any) string { @@ -110,12 +113,16 @@ func RenderJSON(name string, data map[string]any, variables any) (map[string]any return data, err } + logger.Dev("after json_path:\n" + jsonutils.ToJson(data)) + data, err = RenderJSONTemplate(name + ":variables", data, variables) if err != nil { return data, err } + logger.Dev("after variables:\n" + jsonutils.ToJson(data)) + return data, nil } From fa6e5e43caf3eff65d116f2bda22bee371ab5c1b Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 21:45:58 +0200 Subject: [PATCH 17/42] debugging --- utils/templating/templating.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/utils/templating/templating.go b/utils/templating/templating.go index ce7c3a6..d610333 100644 --- a/utils/templating/templating.go +++ b/utils/templating/templating.go @@ -8,7 +8,6 @@ import ( "strings" "text/template" - "github.com/codeshelldev/secured-signal-api/utils/jsonutils" "github.com/codeshelldev/secured-signal-api/utils/logger" ) @@ -113,16 +112,12 @@ func RenderJSON(name string, data map[string]any, variables any) (map[string]any return data, err } - logger.Dev("after json_path:\n" + jsonutils.ToJson(data)) - data, err = RenderJSONTemplate(name + ":variables", data, variables) if err != nil { return data, err } - logger.Dev("after variables:\n" + jsonutils.ToJson(data)) - return data, nil } @@ -152,6 +147,8 @@ func RenderJSONTemplate(name string, data map[string]any, variables any) (map[st return nil, err } + logger.Dev("after template:\n" + jsonStr) + jsonStr = cleanQuotedPairsJSON(jsonStr) // Remove the Quotes around "<<[item1,item2]>>" From 2fb052e5e041971d4a918720afb3c2b47c632eae Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 21:48:58 +0200 Subject: [PATCH 18/42] debugging incoming data --- utils/templating/templating.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/utils/templating/templating.go b/utils/templating/templating.go index d610333..d52a992 100644 --- a/utils/templating/templating.go +++ b/utils/templating/templating.go @@ -8,6 +8,7 @@ import ( "strings" "text/template" + "github.com/codeshelldev/secured-signal-api/utils/jsonutils" "github.com/codeshelldev/secured-signal-api/utils/logger" ) @@ -141,14 +142,14 @@ func RenderJSONTemplate(name string, data map[string]any, variables any) (map[st "normalize": normalizeJSON, }) + logger.Dev(jsonutils.ToJson(variables)) + jsonStr, err := ParseTemplate(templt, tmplStr, variables) if err != nil { return nil, err } - logger.Dev("after template:\n" + jsonStr) - jsonStr = cleanQuotedPairsJSON(jsonStr) // Remove the Quotes around "<<[item1,item2]>>" From aad457ab5f0dfa48adae6befa4d2972a8cec6c95 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 21:54:25 +0200 Subject: [PATCH 19/42] debugging incoming body --- internals/proxy/middlewares/message.go | 4 +++- internals/proxy/middlewares/template.go | 4 ---- utils/templating/templating.go | 5 ----- 3 files changed, 3 insertions(+), 10 deletions(-) diff --git a/internals/proxy/middlewares/message.go b/internals/proxy/middlewares/message.go index ff07fc8..30e79af 100644 --- a/internals/proxy/middlewares/message.go +++ b/internals/proxy/middlewares/message.go @@ -44,6 +44,8 @@ func (data MessageMiddleware) Use() http.Handler { var modifiedBody bool if !body.Empty { + log.Dev(body.ToString()) + templatedMessage, err := TemplateMessage(messageTemplate, bodyData, variables) if err != nil { @@ -68,7 +70,7 @@ func (data MessageMiddleware) Use() http.Handler { strData := body.ToString() - log.Debug("Applied Body Templating: ", strData) + log.Debug("Applied Message Templating: ", strData) req.ContentLength = int64(len(strData)) req.Header.Set("Content-Length", strconv.Itoa(len(strData))) diff --git a/internals/proxy/middlewares/template.go b/internals/proxy/middlewares/template.go index 4421170..84c820f 100644 --- a/internals/proxy/middlewares/template.go +++ b/internals/proxy/middlewares/template.go @@ -126,12 +126,8 @@ func TemplateBody(data map[string]any, VARIABLES any) (map[string]any, bool, err } } - log.Dev("after @ replacement:\n" + jsonStr) - templatedData, err := templating.RenderJSON("body", data, VARIABLES) - log.Dev("after render:\n", jsonutils.ToJson(templatedData)) - if err != nil { return data, false, err } diff --git a/utils/templating/templating.go b/utils/templating/templating.go index d52a992..3fa7da6 100644 --- a/utils/templating/templating.go +++ b/utils/templating/templating.go @@ -7,9 +7,6 @@ import ( "regexp" "strings" "text/template" - - "github.com/codeshelldev/secured-signal-api/utils/jsonutils" - "github.com/codeshelldev/secured-signal-api/utils/logger" ) func normalize(value any) string { @@ -142,8 +139,6 @@ func RenderJSONTemplate(name string, data map[string]any, variables any) (map[st "normalize": normalizeJSON, }) - logger.Dev(jsonutils.ToJson(variables)) - jsonStr, err := ParseTemplate(templt, tmplStr, variables) if err != nil { From a08813fe9c2b9d4153294c5c387ae7abe2dfe8cb Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 21:58:57 +0200 Subject: [PATCH 20/42] extensive debugging --- internals/proxy/middlewares/message.go | 2 +- utils/templating/templating.go | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/internals/proxy/middlewares/message.go b/internals/proxy/middlewares/message.go index 30e79af..b01036c 100644 --- a/internals/proxy/middlewares/message.go +++ b/internals/proxy/middlewares/message.go @@ -44,7 +44,7 @@ func (data MessageMiddleware) Use() http.Handler { var modifiedBody bool if !body.Empty { - log.Dev(body.ToString()) + log.Dev("Body at MSG Middleware:\n" + body.ToString()) templatedMessage, err := TemplateMessage(messageTemplate, bodyData, variables) diff --git a/utils/templating/templating.go b/utils/templating/templating.go index 3fa7da6..4490f35 100644 --- a/utils/templating/templating.go +++ b/utils/templating/templating.go @@ -7,6 +7,9 @@ import ( "regexp" "strings" "text/template" + + "github.com/codeshelldev/secured-signal-api/utils/jsonutils" + "github.com/codeshelldev/secured-signal-api/utils/logger" ) func normalize(value any) string { @@ -104,6 +107,8 @@ func CreateTemplateWithFunc(name string, funcMap template.FuncMap) (*template.Te } func RenderJSON(name string, data map[string]any, variables any) (map[string]any, error) { + logger.Dev("Incoming data:\n", jsonutils.ToJson(data)) + data, err := RenderJSONTemplate(name + ":json_path", data, data) if err != nil { @@ -139,8 +144,14 @@ func RenderJSONTemplate(name string, data map[string]any, variables any) (map[st "normalize": normalizeJSON, }) + logger.Dev("Variables before templating:\n", jsonutils.ToJson(variables)) + + logger.Dev("Before templating:\n", jsonutils.ToJson(data)) + jsonStr, err := ParseTemplate(templt, tmplStr, variables) + logger.Dev("After templating:\n", jsonStr) + if err != nil { return nil, err } From 0464a52ef86d55642f7161103661988f09cd41d5 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 22:02:35 +0200 Subject: [PATCH 21/42] narrowing down root of issue --- 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 84c820f..4d661a0 100644 --- a/internals/proxy/middlewares/template.go +++ b/internals/proxy/middlewares/template.go @@ -110,6 +110,8 @@ func TemplateBody(data map[string]any, VARIABLES any) (map[string]any, bool, err jsonStr := jsonutils.ToJson(data) + log.Dev("Body before @:\n", jsonStr) + if jsonStr != "" { re, err := regexp.Compile(`{{\s*\@([a-zA-Z0-9_.]+)\s*}}`) @@ -126,6 +128,8 @@ func TemplateBody(data map[string]any, VARIABLES any) (map[string]any, bool, err } } + log.Dev("Body after @:\n", jsonStr) + templatedData, err := templating.RenderJSON("body", data, VARIABLES) if err != nil { From 8dd1f92ef3c0681a7a86df30065ee77ce76d4b80 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 22:07:30 +0200 Subject: [PATCH 22/42] =?UTF-8?q?actually=20assigned=20bodyData=20to=20the?= =?UTF-8?q?=20Body=20=F0=9F=A4=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internals/proxy/middlewares/message.go | 2 +- internals/proxy/middlewares/template.go | 4 ---- utils/templating/templating.go | 11 ----------- 3 files changed, 1 insertion(+), 16 deletions(-) diff --git a/internals/proxy/middlewares/message.go b/internals/proxy/middlewares/message.go index b01036c..10e4c62 100644 --- a/internals/proxy/middlewares/message.go +++ b/internals/proxy/middlewares/message.go @@ -44,7 +44,7 @@ func (data MessageMiddleware) Use() http.Handler { var modifiedBody bool if !body.Empty { - log.Dev("Body at MSG Middleware:\n" + body.ToString()) + bodyData = body.Data templatedMessage, err := TemplateMessage(messageTemplate, bodyData, variables) diff --git a/internals/proxy/middlewares/template.go b/internals/proxy/middlewares/template.go index 4d661a0..84c820f 100644 --- a/internals/proxy/middlewares/template.go +++ b/internals/proxy/middlewares/template.go @@ -110,8 +110,6 @@ func TemplateBody(data map[string]any, VARIABLES any) (map[string]any, bool, err jsonStr := jsonutils.ToJson(data) - log.Dev("Body before @:\n", jsonStr) - if jsonStr != "" { re, err := regexp.Compile(`{{\s*\@([a-zA-Z0-9_.]+)\s*}}`) @@ -128,8 +126,6 @@ func TemplateBody(data map[string]any, VARIABLES any) (map[string]any, bool, err } } - log.Dev("Body after @:\n", jsonStr) - templatedData, err := templating.RenderJSON("body", data, VARIABLES) if err != nil { diff --git a/utils/templating/templating.go b/utils/templating/templating.go index 4490f35..3fa7da6 100644 --- a/utils/templating/templating.go +++ b/utils/templating/templating.go @@ -7,9 +7,6 @@ import ( "regexp" "strings" "text/template" - - "github.com/codeshelldev/secured-signal-api/utils/jsonutils" - "github.com/codeshelldev/secured-signal-api/utils/logger" ) func normalize(value any) string { @@ -107,8 +104,6 @@ func CreateTemplateWithFunc(name string, funcMap template.FuncMap) (*template.Te } func RenderJSON(name string, data map[string]any, variables any) (map[string]any, error) { - logger.Dev("Incoming data:\n", jsonutils.ToJson(data)) - data, err := RenderJSONTemplate(name + ":json_path", data, data) if err != nil { @@ -144,14 +139,8 @@ func RenderJSONTemplate(name string, data map[string]any, variables any) (map[st "normalize": normalizeJSON, }) - logger.Dev("Variables before templating:\n", jsonutils.ToJson(variables)) - - logger.Dev("Before templating:\n", jsonutils.ToJson(data)) - jsonStr, err := ParseTemplate(templt, tmplStr, variables) - logger.Dev("After templating:\n", jsonStr) - if err != nil { return nil, err } From 1778e975d9f7ade8f855cc216ea3771b2e385c14 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 22:10:28 +0200 Subject: [PATCH 23/42] on the hunt for a new issue, @var expanding to full json body --- utils/templating/templating.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/utils/templating/templating.go b/utils/templating/templating.go index 3fa7da6..4490f35 100644 --- a/utils/templating/templating.go +++ b/utils/templating/templating.go @@ -7,6 +7,9 @@ import ( "regexp" "strings" "text/template" + + "github.com/codeshelldev/secured-signal-api/utils/jsonutils" + "github.com/codeshelldev/secured-signal-api/utils/logger" ) func normalize(value any) string { @@ -104,6 +107,8 @@ func CreateTemplateWithFunc(name string, funcMap template.FuncMap) (*template.Te } func RenderJSON(name string, data map[string]any, variables any) (map[string]any, error) { + logger.Dev("Incoming data:\n", jsonutils.ToJson(data)) + data, err := RenderJSONTemplate(name + ":json_path", data, data) if err != nil { @@ -139,8 +144,14 @@ func RenderJSONTemplate(name string, data map[string]any, variables any) (map[st "normalize": normalizeJSON, }) + logger.Dev("Variables before templating:\n", jsonutils.ToJson(variables)) + + logger.Dev("Before templating:\n", jsonutils.ToJson(data)) + jsonStr, err := ParseTemplate(templt, tmplStr, variables) + logger.Dev("After templating:\n", jsonStr) + if err != nil { return nil, err } From 577a30df0a6c4d05aa4a35ea5b2116259b06c8c5 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 22:14:27 +0200 Subject: [PATCH 24/42] fixed body getting tostring'ed and put into `message` --- internals/proxy/middlewares/message.go | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/internals/proxy/middlewares/message.go b/internals/proxy/middlewares/message.go index 10e4c62..43d5d92 100644 --- a/internals/proxy/middlewares/message.go +++ b/internals/proxy/middlewares/message.go @@ -6,7 +6,6 @@ import ( "net/http" "strconv" - "github.com/codeshelldev/secured-signal-api/utils/jsonutils" log "github.com/codeshelldev/secured-signal-api/utils/logger" request "github.com/codeshelldev/secured-signal-api/utils/request" ) @@ -46,14 +45,14 @@ func (data MessageMiddleware) Use() http.Handler { if !body.Empty { bodyData = body.Data - templatedMessage, err := TemplateMessage(messageTemplate, bodyData, variables) + newData, err := TemplateMessage(messageTemplate, bodyData, variables) if err != nil { log.Error("Error Templating Message: ", err.Error()) } - if templatedMessage != bodyData["message"] && templatedMessage != "" { - bodyData["message"] = templatedMessage + if newData["message"] != bodyData["message"] && newData["message"] != "" { + bodyData = newData modifiedBody = true } } @@ -82,20 +81,14 @@ func (data MessageMiddleware) Use() http.Handler { }) } -func TemplateMessage(template string, data map[string]any, VARIABLES any) (string, error) { +func TemplateMessage(template string, data map[string]any, VARIABLES any) (map[string]any, error) { data["message"] = template data, ok, err := TemplateBody(data, VARIABLES) if err != nil || !ok || data == nil { - return template, err + return data, err } - jsonStr, err := jsonutils.ToJsonSafe(data) - - if err != nil { - return template, err - } - - return jsonStr, nil + return data, nil } \ No newline at end of file From c068d387ec4767060f9cc5190f62c8c98045b95a Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 22:22:53 +0200 Subject: [PATCH 25/42] process json templating in one combined render --- internals/proxy/middlewares/message.go | 2 +- internals/proxy/middlewares/template.go | 2 +- utils/templating/templating.go | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/internals/proxy/middlewares/message.go b/internals/proxy/middlewares/message.go index 43d5d92..5761a7a 100644 --- a/internals/proxy/middlewares/message.go +++ b/internals/proxy/middlewares/message.go @@ -81,7 +81,7 @@ func (data MessageMiddleware) Use() http.Handler { }) } -func TemplateMessage(template string, data map[string]any, VARIABLES any) (map[string]any, error) { +func TemplateMessage(template string, data map[string]any, VARIABLES map[string]any) (map[string]any, error) { data["message"] = template data, ok, err := TemplateBody(data, VARIABLES) diff --git a/internals/proxy/middlewares/template.go b/internals/proxy/middlewares/template.go index 84c820f..4365185 100644 --- a/internals/proxy/middlewares/template.go +++ b/internals/proxy/middlewares/template.go @@ -105,7 +105,7 @@ func (data TemplateMiddleware) Use() http.Handler { }) } -func TemplateBody(data map[string]any, VARIABLES any) (map[string]any, bool, error) { +func TemplateBody(data map[string]any, VARIABLES map[string]any) (map[string]any, bool, error) { var modified bool jsonStr := jsonutils.ToJson(data) diff --git a/utils/templating/templating.go b/utils/templating/templating.go index 4490f35..0913e68 100644 --- a/utils/templating/templating.go +++ b/utils/templating/templating.go @@ -106,16 +106,16 @@ func CreateTemplateWithFunc(name string, funcMap template.FuncMap) (*template.Te return template.New(name).Funcs(funcMap) } -func RenderJSON(name string, data map[string]any, variables any) (map[string]any, error) { +func RenderJSON(name string, data map[string]any, variables map[string]any) (map[string]any, error) { logger.Dev("Incoming data:\n", jsonutils.ToJson(data)) - data, err := RenderJSONTemplate(name + ":json_path", data, data) + combinedData := data - if err != nil { - return data, err + for key, value := range variables { + combinedData[key] = value } - data, err = RenderJSONTemplate(name + ":variables", data, variables) + data, err := RenderJSONTemplate(name, data, combinedData) if err != nil { return data, err @@ -124,7 +124,7 @@ func RenderJSON(name string, data map[string]any, variables any) (map[string]any return data, nil } -func RenderJSONTemplate(name string, data map[string]any, variables any) (map[string]any, error) { +func RenderJSONTemplate(name string, data map[string]any, variables map[string]any) (map[string]any, error) { jsonBytes, err := json.Marshal(data) if err != nil { From afd9ba5b54b566ac5a44f74835ef034554a29fa1 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 22:28:35 +0200 Subject: [PATCH 26/42] set modifiedBody flag --- internals/proxy/middlewares/aliases.go | 1 + utils/templating/templating.go | 11 ----------- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/internals/proxy/middlewares/aliases.go b/internals/proxy/middlewares/aliases.go index c3221ca..1151685 100644 --- a/internals/proxy/middlewares/aliases.go +++ b/internals/proxy/middlewares/aliases.go @@ -56,6 +56,7 @@ func (data AliasMiddleware) Use() http.Handler { switch prefix { case "@": bodyData[keyWithoutPrefix] = value + modifiedBody = true case ".": settings.VARIABLES[keyWithoutPrefix] = value } diff --git a/utils/templating/templating.go b/utils/templating/templating.go index 0913e68..3f66c90 100644 --- a/utils/templating/templating.go +++ b/utils/templating/templating.go @@ -7,9 +7,6 @@ import ( "regexp" "strings" "text/template" - - "github.com/codeshelldev/secured-signal-api/utils/jsonutils" - "github.com/codeshelldev/secured-signal-api/utils/logger" ) func normalize(value any) string { @@ -107,8 +104,6 @@ func CreateTemplateWithFunc(name string, funcMap template.FuncMap) (*template.Te } func RenderJSON(name string, data map[string]any, variables map[string]any) (map[string]any, error) { - logger.Dev("Incoming data:\n", jsonutils.ToJson(data)) - combinedData := data for key, value := range variables { @@ -144,14 +139,8 @@ func RenderJSONTemplate(name string, data map[string]any, variables map[string]a "normalize": normalizeJSON, }) - logger.Dev("Variables before templating:\n", jsonutils.ToJson(variables)) - - logger.Dev("Before templating:\n", jsonutils.ToJson(data)) - jsonStr, err := ParseTemplate(templt, tmplStr, variables) - logger.Dev("After templating:\n", jsonStr) - if err != nil { return nil, err } From f7dd0592c4f9a615d8f8b516801e24877ec23f6f Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 22:33:22 +0200 Subject: [PATCH 27/42] fixed aliases processing --- internals/proxy/middlewares/aliases.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/internals/proxy/middlewares/aliases.go b/internals/proxy/middlewares/aliases.go index 1151685..0cc6917 100644 --- a/internals/proxy/middlewares/aliases.go +++ b/internals/proxy/middlewares/aliases.go @@ -56,7 +56,6 @@ func (data AliasMiddleware) Use() http.Handler { switch prefix { case "@": bodyData[keyWithoutPrefix] = value - modifiedBody = true case ".": settings.VARIABLES[keyWithoutPrefix] = value } @@ -88,17 +87,17 @@ func (data AliasMiddleware) Use() http.Handler { func processDataAliases(aliases map[string][]middlewareTypes.DataAlias, data map[string]any) (map[string]any, bool) { var modified bool - var newData map[string]any - for key, alias := range aliases { - newData = getData(key, alias, data) + oldData := data + + data = getData(key, alias, data) - if data[key] != newData[key] { + if data[key] != oldData[key] { modified = true } } - return newData, modified + return data, modified } func getData(key string, aliases []middlewareTypes.DataAlias, data map[string]any) (map[string]any) { From c34d67c45b35e671aef6fefd0092142401864e80 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 22:33:46 +0200 Subject: [PATCH 28/42] debug --- internals/proxy/middlewares/aliases.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internals/proxy/middlewares/aliases.go b/internals/proxy/middlewares/aliases.go index 0cc6917..b05fec9 100644 --- a/internals/proxy/middlewares/aliases.go +++ b/internals/proxy/middlewares/aliases.go @@ -53,6 +53,8 @@ func (data AliasMiddleware) Use() http.Handler { keyWithoutPrefix := key[1:] + log.Dev("Key: ", keyWithoutPrefix) + switch prefix { case "@": bodyData[keyWithoutPrefix] = value From 3693c9165ba0a77d98b14fc833716b60bd6b4585 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 22:43:27 +0200 Subject: [PATCH 29/42] fixed misintepretation of data alias logic --- internals/proxy/middlewares/aliases.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/internals/proxy/middlewares/aliases.go b/internals/proxy/middlewares/aliases.go index b05fec9..4dd4cd9 100644 --- a/internals/proxy/middlewares/aliases.go +++ b/internals/proxy/middlewares/aliases.go @@ -89,35 +89,34 @@ func (data AliasMiddleware) Use() http.Handler { func processDataAliases(aliases map[string][]middlewareTypes.DataAlias, data map[string]any) (map[string]any, bool) { var modified bool - for key, alias := range aliases { - oldData := data + aliasData := map[string]any{} - data = getData(key, alias, data) + for key, alias := range aliases { + key, value := getData(key, alias, data) - if data[key] != oldData[key] { - modified = true - } + aliasData[key] = value } - return data, modified + return aliasData, modified } -func getData(key string, aliases []middlewareTypes.DataAlias, data map[string]any) (map[string]any) { +func getData(key string, aliases []middlewareTypes.DataAlias, data map[string]any) (string, any) { var best int + var value any for _, alias := range aliases { aliasValue, score, ok := processAlias(alias, data) if ok { if score > best { - data[key] = aliasValue + value = aliasValue } data[alias.Alias] = nil } } - return data + return key, value } func processAlias(alias middlewareTypes.DataAlias, data map[string]any) (any, int, bool) { From f38215b0aaf23829903db0c576101280ded1994d Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 22:46:00 +0200 Subject: [PATCH 30/42] moved modifiedBody flag --- internals/proxy/middlewares/aliases.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/internals/proxy/middlewares/aliases.go b/internals/proxy/middlewares/aliases.go index 4dd4cd9..508a7f5 100644 --- a/internals/proxy/middlewares/aliases.go +++ b/internals/proxy/middlewares/aliases.go @@ -46,7 +46,7 @@ func (data AliasMiddleware) Use() http.Handler { var aliasData map[string]any - aliasData, modifiedBody = processDataAliases(dataAliases, bodyData) + aliasData = processDataAliases(dataAliases, bodyData) for key, value := range aliasData { prefix := key[:1] @@ -58,6 +58,7 @@ func (data AliasMiddleware) Use() http.Handler { switch prefix { case "@": bodyData[keyWithoutPrefix] = value + modifiedBody = true case ".": settings.VARIABLES[keyWithoutPrefix] = value } @@ -86,9 +87,7 @@ func (data AliasMiddleware) Use() http.Handler { }) } -func processDataAliases(aliases map[string][]middlewareTypes.DataAlias, data map[string]any) (map[string]any, bool) { - var modified bool - +func processDataAliases(aliases map[string][]middlewareTypes.DataAlias, data map[string]any) (map[string]any) { aliasData := map[string]any{} for key, alias := range aliases { @@ -97,7 +96,7 @@ func processDataAliases(aliases map[string][]middlewareTypes.DataAlias, data map aliasData[key] = value } - return aliasData, modified + return aliasData } func getData(key string, aliases []middlewareTypes.DataAlias, data map[string]any) (string, any) { From d668f2b30845addf2a5907a6fed534dbf0b94c0c Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 22:46:04 +0200 Subject: [PATCH 31/42] add --- internals/proxy/middlewares/aliases.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/internals/proxy/middlewares/aliases.go b/internals/proxy/middlewares/aliases.go index 508a7f5..deb500b 100644 --- a/internals/proxy/middlewares/aliases.go +++ b/internals/proxy/middlewares/aliases.go @@ -44,9 +44,7 @@ func (data AliasMiddleware) Use() http.Handler { if !body.Empty { bodyData = body.Data - var aliasData map[string]any - - aliasData = processDataAliases(dataAliases, bodyData) + aliasData := processDataAliases(dataAliases, bodyData) for key, value := range aliasData { prefix := key[:1] From edd2f96283a28e2d26b901cf15ca1c27c06388ed Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 22:48:52 +0200 Subject: [PATCH 32/42] debugging missing aliases --- internals/proxy/middlewares/aliases.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internals/proxy/middlewares/aliases.go b/internals/proxy/middlewares/aliases.go index deb500b..4a76f57 100644 --- a/internals/proxy/middlewares/aliases.go +++ b/internals/proxy/middlewares/aliases.go @@ -44,6 +44,8 @@ func (data AliasMiddleware) Use() http.Handler { if !body.Empty { bodyData = body.Data + log.Dev(jsonutils.ToJson(dataAliases)) + aliasData := processDataAliases(dataAliases, bodyData) for key, value := range aliasData { From 5b112f704f2758a66f6ab3eb13ab3e61a6e7763b Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 23:00:23 +0200 Subject: [PATCH 33/42] updated defaults --- config/defaults.yml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/config/defaults.yml b/config/defaults.yml index db8b3cf..32f2e60 100644 --- a/config/defaults.yml +++ b/config/defaults.yml @@ -4,6 +4,27 @@ server: logLevel: info settings: + dataAliases: + "@message": + [ + { alias: msg, score: 100 }, + { alias: content, score: 99 }, + { alias: description, score: 98 }, + { alias: text, score: 20 }, + { alias: summary, score: 15 }, + { alias: details, score: 14 }, + + { alias: data.message, score: 10 }, + { alias: data.content, score: 9 }, + { alias: data.description, score: 8 }, + { alias: data.text, score: 7 }, + { alias: data.summary, score: 6 }, + { alias: data.details, score: 5 }, + + { alias: body, score: 2 }, + { alias: data, score: 1 }, + ] + variables: recipients: ${RECIPIENTS} number: ${NUMBER} From d72320ec247d3e4c225374a31eba08211e938ab0 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 23:05:11 +0200 Subject: [PATCH 34/42] finished testing --- internals/proxy/middlewares/aliases.go | 4 ---- utils/config/loader.go | 2 -- 2 files changed, 6 deletions(-) diff --git a/internals/proxy/middlewares/aliases.go b/internals/proxy/middlewares/aliases.go index 4a76f57..d38e81a 100644 --- a/internals/proxy/middlewares/aliases.go +++ b/internals/proxy/middlewares/aliases.go @@ -44,8 +44,6 @@ func (data AliasMiddleware) Use() http.Handler { if !body.Empty { bodyData = body.Data - log.Dev(jsonutils.ToJson(dataAliases)) - aliasData := processDataAliases(dataAliases, bodyData) for key, value := range aliasData { @@ -53,8 +51,6 @@ func (data AliasMiddleware) Use() http.Handler { keyWithoutPrefix := key[1:] - log.Dev("Key: ", keyWithoutPrefix) - switch prefix { case "@": bodyData[keyWithoutPrefix] = value diff --git a/utils/config/loader.go b/utils/config/loader.go index d6d8b3b..d2567f2 100644 --- a/utils/config/loader.go +++ b/utils/config/loader.go @@ -82,8 +82,6 @@ func InitEnv() { config.Unmarshal("settings", &settings) - log.Dev(jsonutils.ToJson(settings)) - ENV.SETTINGS["*"] = &settings } From c6f400bfae0ab112b742d58a56a44bfe066a393b Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 23:08:34 +0200 Subject: [PATCH 35/42] reworked readme for dataAliases --- .github/templates/README.template.md | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/.github/templates/README.template.md b/.github/templates/README.template.md index 0ec58f5..54511c4 100644 --- a/.github/templates/README.template.md +++ b/.github/templates/README.template.md @@ -35,6 +35,8 @@ endpoint restrictions, placeholders, and flexible configuration. - [Configuration](#configuration) - [Endpoints](#endpoints) - [Variables](#variables) + - [Data Aliases](#data-aliases) + - [Message Templates](#message-templates) - [Contributing](#contributing) - [Support](#support) - [License](#license) @@ -293,12 +295,12 @@ settings: recipients: ["+123400002", "group.id", "user.id"] ``` -### Message Aliases +### Data Aliases -To improve compatibility with other services Secured Signal API provides **Message Aliases** for the `message` attribute. +To improve compatibility with other services Secured Signal API provides **Data Aliases** and a built-in `message` Alias.
-Default Message Aliases +Default `message` Aliases | Alias | Score | Alias | Score | | ------------ | ----- | ---------------- | ----- | @@ -312,20 +314,23 @@ To improve compatibility with other services Secured Signal API provides **Messa
-Secured Signal API will pick the best scoring Message Alias (if available) to extract the correct message from the Request Body. +Secured Signal API will pick the best scoring Data Alias (if available) to extract the correct message from the Request Body. -Message Aliases can be added by setting `messageAliases` in your config: +Data Aliases can be added by setting `dataAliases` in your config: ```yaml settings: - messageAliases: - [ - { alias: "msg", score: 80 }, - { alias: "data.message", score: 79 }, - { alias: "array[0].message", score: 78 }, - ] + dataAliases: + "@message": + [ + { alias: "msg", score: 80 }, + { alias: "data.message", score: 79 }, + { alias: "array[0].message", score: 78 }, + ] ``` +Use `@` for aliasing Body Keys and `.` for aliasing Variables. + ### Port To change the Port which Secured Signal API uses, you need to set `server.port` in your config. (default: `8880`) From e9e74bfca03d2e3cb4586e6c3dfdd6d1d30e4336 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 23:39:17 +0200 Subject: [PATCH 36/42] Update config.yml --- examples/config.yml | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/examples/config.yml b/examples/config.yml index 9be5a31..fc0679c 100644 --- a/examples/config.yml +++ b/examples/config.yml @@ -5,14 +5,21 @@ api: url: http://signal-api:8080 tokens: [token1, token2] -logLevel: INFO +logLevel: info settings: + messageTemplate: | + You've got a Notification: + {{@message}} + At {{@data.timestamp}} on {{@data.date}}. + Send using {{.NUMBER}}. + variables: number: "+123400001" recipients: ["+123400002", "group.id", "user.id"] - messageAliases: [{ alias: "msg", score: 100 }] + dataAliases: + "@message": [{ alias: "msg", score: 100 }] blockedEndpoints: - /v1/about From 7118d06fdd33bdfe71b3feb76f8adc7b3b6add77 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 23:43:01 +0200 Subject: [PATCH 37/42] Update README.template.md --- .github/templates/README.template.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/templates/README.template.md b/.github/templates/README.template.md index 54511c4..102daeb 100644 --- a/.github/templates/README.template.md +++ b/.github/templates/README.template.md @@ -314,7 +314,7 @@ To improve compatibility with other services Secured Signal API provides **Data -Secured Signal API will pick the best scoring Data Alias (if available) to extract the correct message from the Request Body. +Secured Signal API will pick the best scoring Data Alias (if available) to extract set the Key to the correct Value from the Request Body. Data Aliases can be added by setting `dataAliases` in your config: @@ -327,13 +327,17 @@ settings: { alias: "data.message", score: 79 }, { alias: "array[0].message", score: 78 }, ] + ".NUMBER": + [ + { alias: "phone_number", score: 100 }, + ] ``` Use `@` for aliasing Body Keys and `.` for aliasing Variables. ### Port -To change the Port which Secured Signal API uses, you need to set `server.port` in your config. (default: `8880`) +To change the Port which Secured Signal API uses, you need to set `service.port` in your config. (default: `8880`) ### Log Level From f9ac0d1651fe71236d6698437ebf097e3b00d46f Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 23:44:23 +0200 Subject: [PATCH 38/42] Update defaults.yml --- config/defaults.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/defaults.yml b/config/defaults.yml index 32f2e60..229832b 100644 --- a/config/defaults.yml +++ b/config/defaults.yml @@ -1,4 +1,4 @@ -server: +service: port: 8880 logLevel: info From 3c09f73dfef700cd4a98f9e7c436fabf75ca851f Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Fri, 19 Sep 2025 23:45:45 +0200 Subject: [PATCH 39/42] Update config.yml --- examples/config.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/config.yml b/examples/config.yml index fc0679c..2005892 100644 --- a/examples/config.yml +++ b/examples/config.yml @@ -1,7 +1,8 @@ # Example Config (all configurations shown) +service: + port: 8880 api: - port: 8880 url: http://signal-api:8080 tokens: [token1, token2] From 1a4ae0e4d5f237de1e26c848438fdb1e3383b5a1 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Sat, 20 Sep 2025 00:00:15 +0200 Subject: [PATCH 40/42] Update README.template.md --- .github/templates/README.template.md | 32 ++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/.github/templates/README.template.md b/.github/templates/README.template.md index 102daeb..90ec9c1 100644 --- a/.github/templates/README.template.md +++ b/.github/templates/README.template.md @@ -31,7 +31,7 @@ endpoint restrictions, placeholders, and flexible configuration. - [Getting Started](#getting-started) - [Setup](#setup) - [Usage](#usage) -- [Best Practices](#security-best-practices) +- [Best Practices](#best-practices) - [Configuration](#configuration) - [Endpoints](#endpoints) - [Variables](#variables) @@ -121,8 +121,9 @@ curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer API_T #### Placeholders -If you are not comfortable / don't want to hardcode your Number for example and/or Recipients in you, may use **Placeholders** in your Request. See [Custom Variables](#variables). +If you are not comfortable / don't want to hardcode your Number for example and/or Recipients in you, may use **Placeholders** in your Request. +You can use [**Variable**](#variables) (`{{.NUMBER}}`) Placeholders and **Body** Placeholders (`{{@data.key}}`) These Placeholders can be used in the Request Query or the Body of a Request like so: **Body** @@ -146,6 +147,14 @@ http://sec-signal-api:8880/v1/receive/?@number={{.NUMBER}} http://sec-signal-api:8880/v1/receive/{{.NUMBER}} ``` +You can also combine them: + +```json +{ + "content": "{{.NUMBER}} -> {{.RECIPIENTS}}" +} +``` + #### KeyValue Pair Injection In some cases you may not be able to access / modify the Request Body, in that case specify needed values in the Request Query: @@ -295,6 +304,21 @@ settings: recipients: ["+123400002", "group.id", "user.id"] ``` +### Message Templates + +To customize the `message` attribute you can use **Message Templates** to build your message by using other Body Keys and Variables. +Use `messageTemplate` to configure: + +```yaml +settings: + messageTemplate: | + Your Message: + {{@message}}. + Sent with Secured Signal API. +``` + +Use `{{@data.key}}` to reference Body Keys and `{{.KEY}}` for Variables. + ### Data Aliases To improve compatibility with other services Secured Signal API provides **Data Aliases** and a built-in `message` Alias. @@ -327,9 +351,9 @@ settings: { alias: "data.message", score: 79 }, { alias: "array[0].message", score: 78 }, ] - ".NUMBER": + ".NUMBER": [ - { alias: "phone_number", score: 100 }, + { alias: "phone_number", score: 100 }, ] ``` From 6e954977a0fd30861d7132976aafb6be1b275445 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Sat, 20 Sep 2025 08:27:52 +0200 Subject: [PATCH 41/42] Update README.template.md --- .github/templates/README.template.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/templates/README.template.md b/.github/templates/README.template.md index 90ec9c1..3689970 100644 --- a/.github/templates/README.template.md +++ b/.github/templates/README.template.md @@ -3,7 +3,7 @@

Secure Proxy for Signal Messenger REST API

-token-based authentication, +Adding token-based authentication, endpoint restrictions, placeholders, and flexible configuration.

From 18fa18fc3dce7a2399119f32315bc700e99a2e47 Mon Sep 17 00:00:00 2001 From: CodeShell <122738806+CodeShellDev@users.noreply.github.com> Date: Sat, 20 Sep 2025 08:40:41 +0200 Subject: [PATCH 42/42] Update README.template.md --- .github/templates/README.template.md | 30 +++++++--------------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/.github/templates/README.template.md b/.github/templates/README.template.md index 3689970..7f697b4 100644 --- a/.github/templates/README.template.md +++ b/.github/templates/README.template.md @@ -123,29 +123,13 @@ curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer API_T If you are not comfortable / don't want to hardcode your Number for example and/or Recipients in you, may use **Placeholders** in your Request. -You can use [**Variable**](#variables) (`{{.NUMBER}}`) Placeholders and **Body** Placeholders (`{{@data.key}}`) -These Placeholders can be used in the Request Query or the Body of a Request like so: +You can use [**Variable**](#variables) `{{.NUMBER}}` Placeholders and **Body** Placeholders `{{@data.key}}`. -**Body** - -```json -{ - "number": "{{ .NUMBER }}", - "recipients": "{{ .RECIPIENTS }}" -} -``` - -**Query** - -``` -http://sec-signal-api:8880/v1/receive/?@number={{.NUMBER}} -``` - -**Path** - -``` -http://sec-signal-api:8880/v1/receive/{{.NUMBER}} -``` +| Type | Example | +| :--- | :------ | +| Body | `{"number": "{{ .NUMBER }}", "recipients": "{{ .RECIPIENTS }}"}` | +| Query| `http://sec-signal-api:8880/v1/receive/?@number={{.NUMBER}}` | +| Path | `http://sec-signal-api:8880/v1/receive/{{.NUMBER}}` | You can also combine them: @@ -164,7 +148,7 @@ In some cases you may not be able to access / modify the Request Body, in that c In order to differentiate Injection Queries and _regular_ Queries you have to add `@` in front of any KeyValue Pair assignment. -Supported types include **strings**, **ints** and **arrays**. See [Formatting](#string-to-type). +Supported types include **strings**, **ints**, **arrays** and **json dictionaries**. See [Formatting](#string-to-type). ## Best Practices