-
Notifications
You must be signed in to change notification settings - Fork 7
/
signer_test.go
307 lines (260 loc) · 7.23 KB
/
signer_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
package surl
import (
"crypto/rand"
"net/url"
"path"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
formatters = []struct {
name string
formatter Option
}{
{
name: "path",
formatter: WithPathFormatter(),
},
{
name: "query",
formatter: WithQueryFormatter(),
},
}
encoders = []struct {
name string
encoder Option
}{
{
name: "decimal",
encoder: WithDecimalExpiry(),
},
{
name: "base58",
encoder: WithBase58Expiry(),
},
}
opts = []struct {
name string
options []Option
}{
{
name: "no opts",
},
{
name: "prefix",
options: []Option{PrefixPath("/signed")},
},
{
name: "skip query",
options: []Option{SkipQuery()},
},
{
name: "skip scheme",
options: []Option{SkipScheme()},
},
{
name: "prefix and skip query",
options: []Option{SkipQuery(), PrefixPath("/signed")},
},
{
name: "prefix and skip scheme",
options: []Option{SkipScheme(), PrefixPath("/signed")},
},
{
name: "prefix and skip query and skip scheme",
options: []Option{SkipQuery(), SkipScheme(), PrefixPath("/signed")},
},
}
)
func TestSigner(t *testing.T) {
inputs := []struct {
name string
unsigned string
}{
{
name: "with query",
unsigned: "https://example.com/a/b/c?foo=bar",
},
{
name: "without query",
unsigned: "https://example.com/a/b/c",
},
{
name: "with only question mark",
unsigned: "https://example.com/a/b/c?",
},
{
name: "absolute path",
unsigned: "/a/b/c",
},
}
// invoke test for each combination of unsigned url, formatter, encoder, and set of
// options
for _, tt := range inputs {
for _, f := range formatters {
for _, enc := range encoders {
for _, opt := range opts {
options := append(opt.options, f.formatter, enc.encoder)
signer := New([]byte("abc123"), options...)
t.Run(path.Join(tt.name, f.name, enc.name, opt.name), func(t *testing.T) {
signed, err := signer.Sign(tt.unsigned, time.Second*10)
require.NoError(t, err)
// check valid URL
_, err = url.Parse(signed)
require.NoError(t, err)
// check valid signature
err = signer.Verify(signed)
require.NoError(t, err)
})
}
}
}
}
}
func TestSigner_SkipQuery(t *testing.T) {
signer := New([]byte("abc123"))
// Demonstrate the SkipQuery option by changing the
// query string on the signed URL and showing it still verifies.
t.Run("skip query", func(t *testing.T) {
sign := New([]byte("abc123"), SkipQuery())
u := "https://example.com/a/b/c?foo=bar"
signed, err := sign.Sign(u, time.Minute)
require.NoError(t, err)
signed = signed + "&page_num=3&page_size=20"
err = sign.Verify(signed)
require.NoError(t, err)
t.Run("check original query parameters are intact", func(t *testing.T) {
u, err := url.Parse(signed)
require.NoError(t, err)
assert.Equal(t, "bar", u.Query().Get("foo"))
})
})
// Demonstrate how changing the query string invalidates the signed URL
t.Run("do not skip query", func(t *testing.T) {
u := "https://example.com/a/b/c?foo=bar"
signed, err := signer.Sign(u, time.Minute)
require.NoError(t, err)
signed = signed + "&page_num=3&page_size=20"
err = signer.Verify(signed)
assert.Equal(t, ErrInvalidSignature, err)
})
}
func TestSigner_SkipScheme(t *testing.T) {
// Demonstrate the SkipScheme option by changing the scheme on the signed
// URL and showing it still verifies.
t.Run("skip scheme", func(t *testing.T) {
signer := New([]byte("abc123"), SkipScheme())
unsigned := "https://example.com/a/b/c?foo=bar"
signed, err := signer.Sign(unsigned, time.Minute)
require.NoError(t, err)
u, err := url.Parse(signed)
require.NoError(t, err)
u.Scheme = "http"
err = signer.Verify(u.String())
require.NoError(t, err)
})
// Demonstrate how changing the scheme invalidates the signed URL
t.Run("do not skip scheme", func(t *testing.T) {
signer := New([]byte("abc123"))
unsigned := "https://example.com/a/b/c?foo=bar"
signed, err := signer.Sign(unsigned, time.Minute)
require.NoError(t, err)
u, err := url.Parse(signed)
require.NoError(t, err)
u.Scheme = "http"
err = signer.Verify(u.String())
assert.Equal(t, ErrInvalidSignature, err)
})
}
func TestSigner_Prefix(t *testing.T) {
signer := New([]byte("abc123"), PrefixPath("/signed"))
t.Run("invalid prefix", func(t *testing.T) {
err := signer.Verify("http://abc.com/wrongprefix/foo/bar?expiry=123&signature=fJLFKJ3903")
assert.Equal(t, ErrInvalidFormat, err)
})
}
func TestSigner_Errors(t *testing.T) {
t.Run("expired", func(t *testing.T) {
signer := New([]byte("abc123"))
u := "https://example.com/a/b/c?baz=cow&foo=bar"
signed, err := signer.Sign(u, time.Duration(0))
require.NoError(t, err)
err = signer.Verify(signed)
assert.Equal(t, ErrExpired, err)
})
t.Run("relative path", func(t *testing.T) {
signer := New([]byte("abc123"))
_, err := signer.Sign("foo/bar", time.Minute)
assert.Error(t, err)
})
t.Run("empty url", func(t *testing.T) {
signer := New([]byte("abc123"))
_, err := signer.Sign("", 10*time.Second)
assert.Error(t, err)
})
t.Run("not a url", func(t *testing.T) {
signer := New([]byte("abc123"))
_, err := signer.Sign("cod", 10*time.Second)
assert.Error(t, err)
})
t.Run("scheme has changed", func(t *testing.T) {
signer := New([]byte("abc123"))
signed, err := signer.Sign("https://example.com/a/b/c?baz=cow&foo=bar", 10*time.Second)
require.NoError(t, err)
hacked, err := url.Parse(signed)
require.NoError(t, err)
hacked.Scheme = "http"
err = signer.Verify(hacked.String())
assert.Error(t, err)
})
t.Run("hostname has changed", func(t *testing.T) {
signer := New([]byte("abc123"))
signed, err := signer.Sign("https://example.com/a/b/c?baz=cow&foo=bar", 10*time.Second)
require.NoError(t, err)
hacked, err := url.Parse(signed)
require.NoError(t, err)
hacked.Host = "hacked.com:1337"
err = signer.Verify(hacked.String())
assert.Error(t, err)
})
}
var (
bu string
berr error
)
func Benchmark(b *testing.B) {
secret := make([]byte, 64)
_, err := rand.Read(secret)
require.NoError(b, err)
// invoke bench for each combination of formatter, encoder, and set of
// options
for _, f := range formatters {
for _, enc := range encoders {
for _, opt := range opts {
options := append(opt.options, f.formatter, enc.encoder)
b.Run(path.Join("sign", f.name, enc.name, opt.name), func(b *testing.B) {
signer := New(secret, options...)
var u string
for n := 0; n < b.N; n++ {
// store result to prevent compiler eliminating func call
u, _ = signer.Sign("https://example.com/a/b/c?x=1&y=2&z=3", time.Hour)
}
// store result in pkg var to to prevent compiler eliminating benchmark
bu = u
})
b.Run(path.Join("verify", f.name, enc.name, opt.name), func(b *testing.B) {
signer := New(secret, options...)
signed, _ := signer.Sign("https://example.com/a/b/c?x=1&y=2&z=3", time.Hour)
for n := 0; n < b.N; n++ {
// store result to prevent compiler eliminating func call
err = signer.Verify(signed)
}
// store result in pkg var to to prevent compiler eliminating benchmark
berr = err
})
}
}
}
}