Files
Enterprise-Knowledge-Base/internal/middleware/logger_test.go
T
wizardchen a6c85dad28 feat(middleware): enhance logger to sanitize new and old password fields
Updated the logger to include regex patterns for sanitizing "new_password" and "old_password" fields in JSON logs. Added a corresponding test case to verify the sanitization functionality.
2026-07-13 20:11:00 +08:00

67 lines
1.7 KiB
Go

package middleware
import "testing"
func TestSanitizeBody(t *testing.T) {
cases := []struct {
name string
in string
want string
}{
{
name: "camelCase apiKey",
in: `{"modelName":"gpt-5.2","apiKey":"sk-secret-123","provider":"azure_openai"}`,
want: `{"modelName":"gpt-5.2","apiKey":"***","provider":"azure_openai"}`,
},
{
name: "snake_case api_key",
in: `{"api_key":"sk-secret-123"}`,
want: `{"api_key":"***"}`,
},
{
name: "PascalCase APIKey",
in: `{"APIKey":"sk-secret-123"}`,
want: `{"APIKey":"***"}`,
},
{
name: "secretKey camelCase",
in: `{"secretKey":"abc","accessKeyId":"id"}`,
want: `{"secretKey":"***","accessKeyId":"id"}`,
},
{
name: "refreshToken / accessToken camelCase",
in: `{"refreshToken":"rt","accessToken":"at"}`,
want: `{"refreshToken":"***","accessToken":"***"}`,
},
{
name: "password and token preserved as masked",
in: `{"password":"p","token":"t"}`,
want: `{"password":"***","token":"***"}`,
},
{
name: "snake_case new_password and old_password",
in: `{"email":"alice@example.com","new_password":"FreshPass9","old_password":"OldPass9"}`,
want: `{"email":"alice@example.com","new_password":"***","old_password":"***"}`,
},
{
name: "extra whitespace around colon",
in: `{"apiKey" : "leak"}`,
want: `{"apiKey":"***"}`,
},
{
name: "non sensitive fields untouched",
in: `{"baseUrl":"https://example.com","modelName":"gpt"}`,
want: `{"baseUrl":"https://example.com","modelName":"gpt"}`,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := sanitizeBody(tc.in)
if got != tc.want {
t.Errorf("sanitizeBody(%q)\n got: %s\nwant: %s", tc.in, got, tc.want)
}
})
}
}