forked from elastic/apm-agent-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sanitizer_test.go
134 lines (116 loc) · 4.04 KB
/
sanitizer_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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package apm_test
import (
"context"
"net/http"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.elastic.co/apm/v2"
"go.elastic.co/apm/v2/apmtest"
"go.elastic.co/apm/v2/model"
"go.elastic.co/apm/v2/transport/transporttest"
)
func TestSanitizeRequestResponse(t *testing.T) {
req, _ := http.NewRequest("GET", "http://server.testing/", nil)
req.SetBasicAuth("foo", "bar")
req.Header.Set("X-Custom-Preauth-Header", "fubar")
for _, c := range []*http.Cookie{
{Name: "secret", Value: "top"},
{Name: "Custom-Credit-Card-Number", Value: "top"},
{Name: "sessionid", Value: "123"},
{Name: "user_id", Value: "456"},
} {
req.AddCookie(c)
}
tx, _, errors := apmtest.WithTransaction(func(ctx context.Context) {
e := apm.CaptureError(ctx, errors.New("boom!"))
defer e.Send()
tx := apm.TransactionFromContext(ctx)
tx.Context.SetHTTPRequest(req)
e.Context.SetHTTPRequest(req)
h := make(http.Header)
h.Add("Set-Cookie", (&http.Cookie{Name: "foo", Value: "bar"}).String())
h.Add("Set-Cookie", (&http.Cookie{Name: "baz", Value: "qux"}).String())
h.Set("X-Custom-Authly-Header", "bazquux")
tx.Context.SetHTTPResponseHeaders(h)
tx.Context.SetHTTPStatusCode(http.StatusTeapot)
e.Context.SetHTTPResponseHeaders(h)
e.Context.SetHTTPStatusCode(http.StatusTeapot)
})
checkContext := func(context *model.Context) {
assert.Equal(t, context.Request.Cookies, model.Cookies{
{Name: "Custom-Credit-Card-Number", Value: "[REDACTED]"},
{Name: "secret", Value: "[REDACTED]"},
{Name: "sessionid", Value: "[REDACTED]"},
{Name: "user_id", Value: "456"},
})
assert.Equal(t, model.Headers{
{
Key: "Authorization",
Values: []string{"[REDACTED]"},
},
{
Key: "X-Custom-Preauth-Header",
Values: []string{"[REDACTED]"},
},
}, context.Request.Headers)
// NOTE: the response includes multiple Set-Cookie headers,
// but we only report a single "[REDACTED]" value.
assert.Equal(t, model.Headers{
{
Key: "Set-Cookie",
Values: []string{"[REDACTED]"},
},
{
Key: "X-Custom-Authly-Header",
Values: []string{"[REDACTED]"},
},
}, context.Response.Headers)
}
checkContext(tx.Context)
for _, e := range errors {
checkContext(e.Context)
}
}
func TestSetSanitizedFieldNamesNone(t *testing.T) {
testSetSanitizedFieldNames(t, "top")
}
func TestSetSanitizedFieldNamesCaseSensitivity(t *testing.T) {
// patterns are matched case-insensitively by default
testSetSanitizedFieldNames(t, "[REDACTED]", "Secret")
// patterns can be made case-sensitive by clearing the "i" flag.
testSetSanitizedFieldNames(t, "top", "(?-i:Secret)")
}
func testSetSanitizedFieldNames(t *testing.T, expect string, sanitized ...string) {
tracer, transport := transporttest.NewRecorderTracer()
defer tracer.Close()
tracer.SetSanitizedFieldNames(sanitized...)
req, _ := http.NewRequest("GET", "http://server.testing/", nil)
req.AddCookie(&http.Cookie{Name: "secret", Value: "top"})
tx := tracer.StartTransaction("name", "type")
tx.Context.SetHTTPRequest(req)
tx.End()
tracer.Flush(nil)
payloads := transport.Payloads()
require.Len(t, payloads.Transactions, 1)
assert.Equal(t, payloads.Transactions[0].Context.Request.Cookies, model.Cookies{
{Name: "secret", Value: expect},
})
}