5
5
"fmt"
6
6
"net/http"
7
7
"net/url"
8
+ "strconv"
8
9
"strings"
9
10
"sync"
10
11
"time"
@@ -17,7 +18,8 @@ import (
17
18
"github.com/coder/coder/v2/coderd/tracing"
18
19
)
19
20
20
- var sensitivePatterns = []string {"code" ,"token" ,"key" ,"secret" ,"password" ,"auth" ,"credential" ,"api_key" }
21
+ var safeParams = []string {"page" ,"limit" ,"offset" }
22
+ var countParams = []string {"ids" ,"template_ids" }
21
23
22
24
func safeQueryParams (params url.Values ) []slog.Field {
23
25
if len (params )== 0 {
@@ -26,25 +28,42 @@ func safeQueryParams(params url.Values) []slog.Field {
26
28
27
29
fields := make ([]slog.Field ,0 ,len (params ))
28
30
for key ,values := range params {
29
- sensitive := false
30
-
31
- // Check if this parameter should be redacted
32
- for _ ,pattern := range sensitivePatterns {
33
- if strings .Contains (strings .ToLower (key ),pattern ) {
34
- sensitive = true
31
+ // Check if this parameter should be included
32
+ for _ ,pattern := range safeParams {
33
+ if strings .EqualFold (key ,pattern ) {
34
+ // Prepend query parameters in the log line to ensure we don't have issues with collisions
35
+ // in case any other internal logging fields already log fields with similar names
36
+ fieldName := "query_" + key
37
+
38
+ // Log the actual values for non-sensitive parameters
39
+ if len (values )== 1 {
40
+ fields = append (fields ,slog .F (fieldName ,values [0 ]))
41
+ continue
42
+ }
43
+ fields = append (fields ,slog .F (fieldName ,values ))
35
44
}
36
45
}
37
- if ! sensitive {
46
+ // Some query params we just want to log the count of the params length
47
+ for _ ,pattern := range countParams {
48
+ if ! strings .EqualFold (key ,pattern ) {
49
+ continue
50
+ }
51
+ count := 0
52
+
38
53
// Prepend query parameters in the log line to ensure we don't have issues with collisions
39
54
// in case any other internal logging fields already log fields with similar names
40
55
fieldName := "query_" + key
41
56
42
- // Log the actual values for non-sensitive parameters
43
- if len (values )== 1 {
44
- fields = append (fields ,slog .F (fieldName ,values [0 ]))
45
- continue
57
+ // Count comma-separated values for CSV format
58
+ for _ ,v := range values {
59
+ if strings .Contains (v ,"," ) {
60
+ count += len (strings .Split (v ,"," ))
61
+ continue
62
+ }
63
+ count ++
46
64
}
47
- fields = append (fields ,slog .F (fieldName ,values ))
65
+ // For logging we always want strings
66
+ fields = append (fields ,slog .F (fieldName + "_count" ,strconv .Itoa (count )))
48
67
}
49
68
}
50
69
return fields