-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensitive_test.go
164 lines (155 loc) · 4.15 KB
/
sensitive_test.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package sensitive
import (
"encoding/json"
"io"
"net/http/httptest"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/stretchr/testify/assert"
)
func TestSensitive(t *testing.T) {
tests := []struct {
name string
config Config
originalResponse fiber.Map
blindedResponse fiber.Map
}{
{
name: "empty string",
config: Config{Keys: []string{"citizen_id"}},
originalResponse: fiber.Map{
"key": "value",
"citizen_id": "", // <<< sensitive value
},
blindedResponse: fiber.Map{
"key": "value",
"citizen_id": "", // <<< sensitive value
},
},
{
name: "no blind when have only 2 characters value",
config: Config{Keys: []string{"onlytwo"}},
originalResponse: fiber.Map{
"k": "v",
"onlytwo": "az", // <<< sensitive value
},
blindedResponse: fiber.Map{
"k": "v",
"onlytwo": "az", // <<< sensitive value
},
},
{
name: "blinded x with 1 of 3 characters value",
config: Config{Keys: []string{"somekey"}},
originalResponse: fiber.Map{
"k": "v",
"somekey": "123", // <<< sensitive value
},
blindedResponse: fiber.Map{
"k": "v",
"somekey": "1x3", // <<< sensitive value
},
},
{
name: "multi keys",
config: Config{Keys: []string{"k1", "k2", "k3"}},
originalResponse: fiber.Map{
"static": "value",
"k1": "abcdefg", // <<< sensitive value
"k2": "0987654321", // <<< sensitive value
"k3": "A0000000Z", // <<< sensitive value
},
blindedResponse: fiber.Map{
"static": "value",
"k1": "axxxxxg", // <<< sensitive value
"k2": "0xxxxxxxx1", // <<< sensitive value
"k3": "AxxxxxxxZ", // <<< sensitive value
},
},
{
name: "blinded with 'T'",
config: Config{Keys: []string{"key"}, Mark: "T"},
originalResponse: fiber.Map{
"key": "1234567890", // <<< sensitive value
},
blindedResponse: fiber.Map{
"key": "1TTTTTTTT0", // <<< sensitive value
},
},
{
name: "empty body",
config: Config{},
originalResponse: fiber.Map{},
blindedResponse: fiber.Map{},
},
{
name: "not blind when target key is not type string (float)",
config: Config{Keys: []string{"not_string"}},
originalResponse: fiber.Map{
"string": "1234567890",
"not_string": 1234.12, // <<< sensitive value
},
blindedResponse: fiber.Map{
"string": "1234567890",
"not_string": 1234.12, // <<< sensitive value
},
},
{
name: "not blind when target key is not type string (map[string]interface{})",
config: Config{Keys: []string{"not_string"}},
originalResponse: fiber.Map{
"string": "1234567890",
"not_string": map[string]interface{}{"k": "v"}, // <<< sensitive value
},
blindedResponse: fiber.Map{
"string": "1234567890",
"not_string": map[string]interface{}{"k": "v"}, // <<< sensitive value
},
},
{
name: "empty body",
config: Config{},
originalResponse: fiber.Map{},
blindedResponse: fiber.Map{},
},
{
name: "call Next() once",
config: Config{Next: func(c *fiber.Ctx) bool {
return true
}},
originalResponse: nil,
blindedResponse: nil,
},
}
route := "/_test"
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
app := fiber.New()
app.Use(New(tt.config))
app.Get(route, func(c *fiber.Ctx) error {
return c.JSON(tt.originalResponse)
})
req := httptest.NewRequest("GET", route, nil)
resp, _ := app.Test(req, -1)
if resp.StatusCode == fiber.StatusOK {
body, _ := io.ReadAll(resp.Body)
var actualResponseBody fiber.Map
_ = json.Unmarshal(body, &actualResponseBody)
assert.Equal(t, tt.blindedResponse, actualResponseBody)
}
})
}
t.Run("Test error intercept body Unmarshal", func(t *testing.T) {
originalResponse := "Some string"
expected := "Some string"
app := fiber.New()
app.Use(New())
app.Get(route, func(c *fiber.Ctx) error {
return c.SendString(originalResponse)
})
req := httptest.NewRequest("GET", route, nil)
resp, _ := app.Test(req, -1)
body, _ := io.ReadAll(resp.Body)
assert.Equal(t, string(body), expected)
})
}