-
Notifications
You must be signed in to change notification settings - Fork 2
/
trailer_test.go
179 lines (157 loc) · 5.13 KB
/
trailer_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
package httpsign
import (
"bytes"
"encoding/base64"
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
var rawPost1 = `POST /foo HTTP/1.1
Content-Type: text/plain
Transfer-Encoding: chunked
Trailer: Expires, Hdr
4
HTTP
7
Message
a
Signatures
0
Expires: Wed, 9 Nov 2022 07:28:00 GMT
Hdr: zoom
`
var rawPost2 = `POST /foo HTTP/1.1
Content-Type: text/plain
Transfer-Encoding: chunked
Trailer: Expires, Hdr
4
HTTP
7
Message
a
Signatures
0
Expires: Wed, 9 Nov 2022 07:28:00 GMT
Hdr: zoom
`
var rawHeaders1 = `POST /foo HTTP/1.1
Content-Type: text/plain
Transfer-Encoding: chunked
Trailer: Expires, Hdr
`
var longReq1 = rawHeaders1 + "5000\r\n" + strings.Repeat("x", 0x5000) + "\r\n\r\n" + "Hdr: zoomba\r\n\r\n"
func TestTrailer_Get(t *testing.T) {
fetchVerifier := func(r *http.Request) (string, *Verifier) {
sigName := "sig1"
verifier, _ := NewHMACSHA256Verifier(bytes.Repeat([]byte{1}, 64), NewVerifyConfig().SetKeyID("key1"),
*NewFields().AddHeader("@method").
AddHeaderExt("hdr", false, false, false, true))
return sigName, verifier
}
fetchSigner := func(res http.Response, r *http.Request) (string, *Signer) {
sigName := "sig1"
signer, _ := NewHMACSHA256Signer(bytes.Repeat([]byte{0}, 64), NewSignConfig().SetKeyID("key"),
Headers("@status", "bar", "date", "Content-Digest"))
return sigName, signer
}
simpleHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Header().Set("bar", "baz and baz again")
_, _ = fmt.Fprintln(w, "Hello, client")
_, _ = fmt.Fprintln(w, "Hello again")
}
config := NewHandlerConfig().SetFetchVerifier(fetchVerifier).
SetFetchSigner(fetchSigner).SetDigestSchemesSend([]string{DigestSha256}).SetDigestSchemesRecv([]string{DigestSha256})
ts := httptest.NewServer(WrapHandler(http.HandlerFunc(simpleHandler), *config))
defer ts.Close()
c := &Client{config: ClientConfig{
signatureName: "sig1",
signer: func() *Signer {
signer, _ := NewHMACSHA256Signer(bytes.Repeat([]byte{1}, 64), NewSignConfig().SetKeyID("key1"),
*NewFields().AddHeader("@method").
AddHeaderExt("hdr", false, false, false, true))
return signer
}(),
verifier: nil,
fetchVerifier: nil,
},
client: *http.DefaultClient,
}
req := readRequestChunked(rawPost1)
req.RequestURI = "" // otherwise Do will complain
u, err := url.Parse(ts.URL + "/")
if err != nil {
panic(err)
}
req.URL = u
res, err := c.Do(req)
var gotRes string
if res != nil {
gotRes = res.Status
}
if err != nil {
t.Errorf("Get() error = %v", err)
return
}
if gotRes != "200 OK" {
t.Errorf("Get() gotRes = %v", gotRes)
}
req2 := readRequest(longReq1)
if err != nil {
t.Errorf("createBody: %s", err)
}
req2.RequestURI = "" // otherwise Do will complain
req2.URL = u
res, err = c.Do(req2)
if res != nil {
gotRes = res.Status
}
if err != nil {
t.Errorf("Get() error = %v", err)
return
}
if gotRes != "200 OK" {
t.Errorf("Get() gotRes = %v", gotRes)
}
}
func TestTrailer_SigFields(t *testing.T) {
config := NewSignConfig().SignAlg(false).setFakeCreated(1618884475).SetKeyID("test-shared-secret")
fields := Headers("@authority", "@method", "content-type")
signatureName := "sig1"
key, _ := base64.StdEncoding.DecodeString("uzvJfB4u3N0Jy4T7NZ75MDVcr8zSTInedJtkgcu46YW4XByzNJjxBdtjUkdJPBtbmHhIDi6pcl8jsasjlTMtDQ==")
signer, _ := NewHMACSHA256Signer(key, config, fields)
req := readRequest(rawPost2)
sigInput, sig, err := SignRequest(signatureName, *signer, req)
assert.NoError(t, err, "signature failed")
// Add signature correctly
signedMessage := rawPost2 + "Signature: " + sig + "\n" + "Signature-Input: " + sigInput + "\n\n"
signedMessage = strings.Replace(signedMessage, "Trailer: Expires, Hdr", "Trailer: Expires, Hdr, Signature, Signature-Input",
1)
req2 := readRequestChunked(signedMessage)
verifier, err := NewHMACSHA256Verifier(key, NewVerifyConfig().SetVerifyCreated(false).SetKeyID("test-shared-secret"), fields)
assert.NoError(t, err, "could not generate Verifier")
err = VerifyRequest(signatureName, *verifier, req2)
assert.NoError(t, err, "verification error")
// Missing Signature-Input
signedMessage = rawPost2 + "Signature: " + sig + "\n\n"
signedMessage = strings.Replace(signedMessage, "Trailer: Expires, Hdr", "Trailer: Expires, Hdr, Signature, Signature-Input",
1)
req2 = readRequestChunked(signedMessage)
verifier, err = NewHMACSHA256Verifier(key, NewVerifyConfig().SetVerifyCreated(false).SetKeyID("test-shared-secret"), fields)
assert.NoError(t, err, "could not generate Verifier")
err = VerifyRequest(signatureName, *verifier, req2)
assert.Error(t, err, "verification error")
// Missing Signature
signedMessage = rawPost2 + "Signature-Input: " + sigInput + "\n\n"
signedMessage = strings.Replace(signedMessage, "Trailer: Expires, Hdr", "Trailer: Expires, Hdr, Signature, Signature-Input",
1)
req2 = readRequestChunked(signedMessage)
verifier, err = NewHMACSHA256Verifier(key, NewVerifyConfig().SetVerifyCreated(false).SetKeyID("test-shared-secret"), fields)
assert.NoError(t, err, "could not generate Verifier")
err = VerifyRequest(signatureName, *verifier, req2)
assert.Error(t, err, "verification error")
}