mirror of
https://github.com/lukaszraczylo/traefikoidc.git
synced 2026-10-17 04:37:48 +00:00
feat(templates): add toJson helper for object claims (#165)
Header templates can now emit an object or array claim as one JSON
header. Example: {{get .Claims "tenant" | toJson}}. The backend parses
the claim, so the middleware does not need to know its schema.
The helper adds no new access path. The validator treats toJson like
default and checks its arguments. A claim must be on the whitelist, and
a dump of the whole claims map is rejected. An absent or empty claim
skips the header. The JSON encoder escapes control characters.
A rendered header value over 8192 bytes is still dropped. The drop is
now logged at error level, with the header name and without the value.
Other sanitizer drops stay at debug level.
Tests cover value types, edge cases, whitelist bypass attempts, token
leaks and header injection. A new yaegi check runs the helper under the
interpreter. README, configuration docs and the plugin manifest show the
new helper.
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@@ -85,8 +86,10 @@ var defaultExcludedURLs = map[string]struct{}{
|
||||
|
||||
// headerTemplateFuncMap returns the function map available to custom header
|
||||
// value templates, gated by the effective claims whitelist (built-in +
|
||||
// config.AllowedClaims). It exposes exactly two helpers:
|
||||
// config.AllowedClaims). It exposes exactly three helpers:
|
||||
// - default: substitute a fallback when a value is nil/empty.
|
||||
// - toJson: serialize a value (e.g. an object claim) to compact JSON; nil and
|
||||
// "" yield "" so an absent claim skips the header instead of sending `""`.
|
||||
// - get: safe map access RESTRICTED to whitelisted claim keys, so it cannot be
|
||||
// used to read a non-whitelisted claim, a raw token, or the whole data map
|
||||
// (issue #149 review). This is the sole runtime enforcement of the claims
|
||||
@@ -102,6 +105,16 @@ func headerTemplateFuncMap(allowedClaims map[string]bool) template.FuncMap {
|
||||
}
|
||||
return val
|
||||
},
|
||||
"toJson": func(val interface{}) (string, error) {
|
||||
if val == nil || val == "" {
|
||||
return "", nil
|
||||
}
|
||||
b, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(b), nil
|
||||
},
|
||||
"get": func(m interface{}, key string) interface{} {
|
||||
if !allowedClaims[key] {
|
||||
return ""
|
||||
|
||||
Reference in New Issue
Block a user