-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensitive.go
63 lines (53 loc) · 1.44 KB
/
sensitive.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package sensitive
import (
"encoding/json"
"fmt"
"strings"
"github.com/gofiber/fiber/v2"
)
func New(config ...Config) fiber.Handler {
// set default config
cfg := configDefault(config...)
logger := NewLogger(cfg.DebugMode)
logger.Printf("[Sensitive] Config: %+v\n", cfg)
return func(c *fiber.Ctx) error {
// Don't execute middleware if Next returns true
if cfg.Next != nil && cfg.Next(c) {
return c.Next()
}
// Let request to continue execute
c.Next()
// Intercept the response body
interceptBody := c.Response().Body()
logger.Print("[Sensitive] Original Body:", string(interceptBody))
var mapResponseBody fiber.Map
err := json.Unmarshal(interceptBody, &mapResponseBody)
if err != nil {
return c.SendString(string(interceptBody))
}
for _, key := range cfg.Keys {
switch mapResponseBody[key].(type) {
case string:
break
default:
continue
}
target := mapResponseBody[key].(string)
length := len(target)
if length > 2 {
firstChar := target[:1]
middle := strings.Repeat(cfg.Mark, len(target[1:length-1]))
lastChar := target[length-1:]
logger.Print("[Sensitive] > before", key, "=", mapResponseBody[key])
mapResponseBody[key] = fmt.Sprintf("%s%s%s",
firstChar,
middle,
lastChar,
)
logger.Print("[Sensitive] > after ", key, "=", mapResponseBody[key])
}
}
logger.Print("[Sensitive] Blinded Body:", mapResponseBody)
return c.JSON(mapResponseBody)
}
}