This repository has been archived by the owner on Oct 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
signature_test.go
315 lines (280 loc) · 10.3 KB
/
signature_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package httpsignatures
import (
"github.com/stretchr/testify/assert"
"net/http"
"net/url"
"testing"
)
const (
testDate = "Thu, 05 Jan 2012 21:31:40 GMT"
)
// Signing
// Test Signature String Config Parser
func TestConfigParserMissingAlgorithmShouldFail(t *testing.T) {
var s SignatureParameters
err := s.FromConfig("Test", "", nil)
assert.EqualError(t, err, ErrorNoAlgorithmConfigured)
httpErr, _ := ErrorToHTTPCode(err.Error())
assert.Equal(t, http.StatusInternalServerError, httpErr)
}
func TestConfigParserMissingKeyIdShouldFail(t *testing.T) {
var s SignatureParameters
err := s.FromConfig("", "hmac-sha256", nil)
assert.EqualError(t, err, ErrorNoKeyIDConfigured)
httpErr, _ := ErrorToHTTPCode(err.Error())
assert.Equal(t, http.StatusInternalServerError, httpErr)
}
func TestConfigParserNotRequiredDateHeader(t *testing.T) {
var s SignatureParameters
err := s.FromConfig("Test", "hmac-sha256", []string{"(request-target)", "host"})
assert.Nil(t, err) // It's okay to not require the date header for the signature
sigParam := SignatureParameters{KeyID: "Test", Algorithm: algorithmHmacSha256, Headers: HeaderValues{},
HeaderList: []string{"(request-target)", "host"}}
assert.Equal(t, sigParam, s)
}
func TestConfigParserMissingDateHeader(t *testing.T) {
var s SignatureParameters
err := s.FromConfig("Test", "hmac-sha256", nil) // the date header will be implicitly required
assert.Nil(t, err)
sigParam := SignatureParameters{KeyID: "Test", Algorithm: algorithmHmacSha256, Headers: HeaderValues{},
HeaderList: []string{"date"}}
assert.Equal(t, sigParam, s)
u, err := url.Parse("https://www.example.com/foo?param=value&pet=dog")
assert.Nil(t, err)
r := &http.Request{
Header: http.Header{
"Authorization": []string{DefaultTestAuthHeader},
},
Method: http.MethodPost,
URL: u,
}
err = s.ParseRequest(r) // it is not okay to have no date header when required
assert.EqualError(t, err, ErrorMissingRequiredHeader+" 'date'")
httpErr, _ := ErrorToHTTPCode(err.Error())
assert.Equal(t, http.StatusBadRequest, httpErr)
}
// Verification
// Test Signature String From Request Parser
func TestRequestParserMissingSignatureShouldFail(t *testing.T) {
const authHeader string = `keyId="Test",algorithm="hmac-sha256"`
u, err := url.Parse("https://www.example.com/foo?param=value&pet=dog")
assert.Nil(t, err)
r := &http.Request{
Header: http.Header{
"Date": []string{testDate},
"Authorization": []string{authHeader},
},
Method: http.MethodPost,
URL: u,
}
var s SignatureParameters
err = s.FromRequest(r)
assert.EqualError(t, err, ErrorMissingSignatureParameterSignature)
httpErr, _ := ErrorToHTTPCode(err.Error())
assert.Equal(t, http.StatusBadRequest, httpErr)
}
func TestRequestParserMissingAlgorithmShouldFail(t *testing.T) {
const authHeader string = `keyId="Test",signature="fffff"`
u, err := url.Parse("https://www.example.com/foo?param=value&pet=dog")
assert.Nil(t, err)
r := &http.Request{
Header: http.Header{
"Date": []string{testDate},
"Authorization": []string{authHeader},
},
Method: http.MethodPost,
URL: u,
}
var s SignatureParameters
err = s.FromRequest(r)
assert.EqualError(t, err, ErrorMissingSignatureParameterAlgorithm)
httpErr, _ := ErrorToHTTPCode(err.Error())
assert.Equal(t, http.StatusBadRequest, httpErr)
}
func TestRequestParserMissingKeyIdShouldFail(t *testing.T) {
const authHeader string = `algorithm="hmac-sha256",signature="fffff"`
u, err := url.Parse("https://www.example.com/foo?param=value&pet=dog")
assert.Nil(t, err)
r := &http.Request{
Header: http.Header{
"Date": []string{testDate},
"Authorization": []string{authHeader},
},
Method: http.MethodPost,
URL: u,
}
var s SignatureParameters
err = s.FromRequest(r)
assert.EqualError(t, err, ErrorMissingSignatureParameterKeyId)
httpErr, _ := ErrorToHTTPCode(err.Error())
assert.Equal(t, http.StatusBadRequest, httpErr)
}
func TestRequestParserDualHeaderShouldPickLastOne(t *testing.T) {
const authHeader string = `keyId="Test",algorithm="hmac-sha256",signature="fffff",signature="abcde"`
u, err := url.Parse("https://www.example.com/foo?param=value&pet=dog")
assert.Nil(t, err)
r := &http.Request{
Header: http.Header{
"Date": []string{testDate},
"Authorization": []string{authHeader},
},
Method: http.MethodPost,
URL: u,
}
var s SignatureParameters
err = s.FromRequest(r)
assert.Nil(t, err)
sigParam := SignatureParameters{KeyID: "Test", Algorithm: algorithmHmacSha256, HeaderList: []string{"date"},
Headers: HeaderValues{"date": testDate}, Signature: "abcde"}
assert.Equal(t, sigParam, s)
}
func TestRequestParserMissingDateHeader(t *testing.T) {
const authHeader string = `keyId="Test",algorithm="hmac-sha256",signature="fffff",headers="(request-target) host"`
u, err := url.Parse("https://www.example.com/foo?param=value&pet=dog#bar")
assert.Nil(t, err)
r := &http.Request{
Header: http.Header{
"Date": []string{testDate},
"Authorization": []string{authHeader},
},
Method: http.MethodPost,
Host: "example.com",
URL: u,
}
var s SignatureParameters
err = s.FromRequest(r)
assert.Nil(t, err)
sigParam := SignatureParameters{KeyID: "Test", Algorithm: algorithmHmacSha256,
Headers: HeaderValues{"(request-target)": "post /foo?param=value&pet=dog#bar", "host": "example.com"},
Signature: "fffff", HeaderList: []string{"(request-target)", "host"}}
assert.Equal(t, sigParam, s)
}
func TestRequestParserTestURLVersions(t *testing.T) {
const authHeader string = `keyId="Test",algorithm="hmac-sha256",signature="fffff",headers="(request-target) host"`
u, err := url.Parse("https://www.example.com/foo?param=value&pet=dog#bar")
assert.Nil(t, err)
r := &http.Request{
Header: http.Header{
"Date": []string{testDate},
"Authorization": []string{authHeader},
},
Method: http.MethodPost,
Host: "example.com",
URL: u,
}
var s SignatureParameters
err = s.FromRequest(r)
assert.Nil(t, err)
sigParam := SignatureParameters{KeyID: "Test", Algorithm: algorithmHmacSha256,
Headers: HeaderValues{"(request-target)": "post /foo?param=value&pet=dog#bar", "host": "example.com"},
Signature: "fffff", HeaderList: []string{"(request-target)", "host"}}
assert.Equal(t, sigParam, s)
r.URL, err = url.Parse("https://www.example.com/foo?param=value&pet=dog")
assert.Nil(t, err)
err = s.FromRequest(r)
assert.Nil(t, err)
sigParam = SignatureParameters{KeyID: "Test", Algorithm: algorithmHmacSha256,
Headers: HeaderValues{"(request-target)": "post /foo?param=value&pet=dog", "host": "example.com"},
Signature: "fffff", HeaderList: []string{"(request-target)", "host"}}
assert.Equal(t, sigParam, s)
r.URL, err = url.Parse("https://www.example.com/foo?param=value#bar")
assert.Nil(t, err)
err = s.FromRequest(r)
assert.Nil(t, err)
sigParam = SignatureParameters{KeyID: "Test", Algorithm: algorithmHmacSha256,
Headers: HeaderValues{"(request-target)": "post /foo?param=value#bar", "host": "example.com"},
Signature: "fffff", HeaderList: []string{"(request-target)", "host"}}
assert.Equal(t, sigParam, s)
r.URL, err = url.Parse("https://www.example.com/foo")
assert.Nil(t, err)
err = s.FromRequest(r)
assert.Nil(t, err)
sigParam = SignatureParameters{KeyID: "Test", Algorithm: algorithmHmacSha256,
Headers: HeaderValues{"(request-target)": "post /foo", "host": "example.com"},
Signature: "fffff", HeaderList: []string{"(request-target)", "host"}}
assert.Equal(t, sigParam, s)
}
func TestRequestParserInvalidKeyShouldBeIgnored(t *testing.T) {
const authHeader string = `Signature keyId="Test",algorithm="hmac-sha256",
garbage="bob",signature="fffff"`
u, err := url.Parse("https://www.example.com/foo?param=value&pet=dog")
assert.Nil(t, err)
r := &http.Request{
Header: http.Header{
"Date": []string{testDate},
"Authorization": []string{authHeader},
},
Method: http.MethodPost,
Host: "example.com",
URL: u,
}
var s SignatureParameters
err = s.FromRequest(r)
assert.Nil(t, err)
sigParam := SignatureParameters{KeyID: "Test", Algorithm: algorithmHmacSha256, HeaderList: []string{"date"},
Headers: HeaderValues{"date": testDate}, Signature: "fffff"}
assert.Equal(t, sigParam, s)
}
// todo , change hmac back to RSA from example in http-signatures-draft-05
const DefaultTestAuthHeader string = `Signature keyId="Test",algorithm="hmac-sha256",
signature="ATp0r26dbMIxOopqw0OfABDT7CKMIoENumuruOtarj8n/97Q3htHFYpH8yOSQk3Z5zh8UxUym6FYTb5+
A0Nz3NRsXJibnYi7brE/4tx5But9kkFGzG+xpUmimN4c3TMN7OFH//+r8hBf7BT9/GmHDUVZT2JzWGLZES2xDOUuMtA="`
func TestRequestParserLoadHeaderMissingDateHeader(t *testing.T) {
u, err := url.Parse("https://www.example.com/foo?param=value&pet=dog")
assert.Nil(t, err)
r := &http.Request{
Header: http.Header{
"Authorization": []string{DefaultTestAuthHeader},
},
Method: http.MethodPost,
Host: "example.com",
URL: u,
}
var s SignatureParameters
err = s.FromRequest(r) // the date header will be implicitly required
assert.EqualError(t, err, ErrorMissingRequiredHeader+" 'date'")
httpErr, _ := ErrorToHTTPCode(err.Error())
assert.Equal(t, http.StatusBadRequest, httpErr)
}
// Test Parse SignatureParameters from Request
func TestParseRequestWithNoSignatureShouldFail(t *testing.T) {
r := &http.Request{
Header: http.Header{
"Date": []string{testDate},
},
}
var s SignatureParameters
err := s.FromRequest(r)
assert.EqualError(t, err, ErrorNoSignatureHeaderFoundInRequest)
httpErr, _ := ErrorToHTTPCode(err.Error())
assert.Equal(t, http.StatusBadRequest, httpErr)
}
func TestParseRequestWithNoHostShouldFail(t *testing.T) {
r := &http.Request{
Header: http.Header{
"Date": []string{testDate},
"Authorization": []string{DefaultTestAuthHeader},
},
Method: http.MethodPost,
}
_, err := requestTargetLine(r)
assert.EqualError(t, err, ErrorURLNotInRequest)
httpErr, _ := ErrorToHTTPCode(err.Error())
assert.Equal(t, http.StatusBadRequest, httpErr)
}
func TestParseRequestWithNoMethodShouldFail(t *testing.T) {
u, err := url.Parse("https://www.example.com/foo?param=value&pet=dog")
assert.Nil(t, err)
r := &http.Request{
Header: http.Header{
"Date": []string{testDate},
"Authorization": []string{DefaultTestAuthHeader},
},
Host: "example.com",
URL: u,
}
_, err = requestTargetLine(r)
assert.EqualError(t, err, ErrorMethodNotInRequest)
httpErr, _ := ErrorToHTTPCode(err.Error())
assert.Equal(t, http.StatusBadRequest, httpErr)
}