Repository navigation
Expand file tree
/
Copy pathmessage.go
More file actions
276 lines (222 loc) · 7.68 KB
/
Copy pathmessage.go
File metadata and controls
276 lines (222 loc) · 7.68 KB
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
package httpsig
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/url"
"sync"
"github.com/dunglas/httpsfv"
)
// MessageFromRequest creates a Message for signing or verifying an HTTP
// request. Request headers are cloned. If the body must be read and req.GetBody
// is unavailable, the original body is buffered and replaced so it remains
// readable. Callers must enforce size limits for untrusted request bodies.
func MessageFromRequest(req *http.Request) *Message {
var (
getBody func() (io.ReadCloser, error)
bodySnapshot []byte
)
// for client requests req.Body can be nil, but not for server requests
switch {
case req.GetBody == nil && req.Body == nil:
getBody = func() (io.ReadCloser, error) { return http.NoBody, nil }
case req.GetBody != nil:
getBody = req.GetBody
default:
getBody = func() (io.ReadCloser, error) {
if len(bodySnapshot) != 0 {
return io.NopCloser(bytes.NewReader(bodySnapshot)), nil
}
save, newBody, err := drainBody(req.Body)
if err != nil {
return nil, err
}
req.Body = newBody
bodySnapshot = save
return io.NopCloser(bytes.NewReader(bodySnapshot)), nil
}
}
return &Message{
Method: req.Method,
Authority: req.Host,
URL: req.URL,
Header: req.Header.Clone(),
Body: getBody,
IsRequest: true,
Context: req.Context(),
}
}
// MessageFromResponse creates a Message from a received HTTP response and
// its associated request. Response and request headers are cloned. The
// response body may be buffered and restored when a covered component needs
// it. The request body is available for ;req components only when the
// associated request has a GetBody function.
func MessageFromResponse(rw *http.Response) *Message {
var bodySnapshot []byte
getResponseBody := func() (io.ReadCloser, error) {
if len(bodySnapshot) != 0 {
return io.NopCloser(bytes.NewReader(bodySnapshot)), nil
}
save, newBody, err := drainBody(rw.Body)
if err != nil {
return nil, err
}
rw.Body = newBody
bodySnapshot = save
return io.NopCloser(bytes.NewReader(bodySnapshot)), nil
}
var getRequestBody func() (io.ReadCloser, error)
if rw.Request.GetBody != nil {
getRequestBody = rw.Request.GetBody
} else {
getRequestBody = func() (io.ReadCloser, error) { return http.NoBody, nil }
}
return &Message{
Method: rw.Request.Method,
Authority: rw.Request.Host,
URL: rw.Request.URL,
Header: rw.Header.Clone(),
Body: getResponseBody,
StatusCode: rw.StatusCode,
RequestHeader: rw.Request.Header.Clone(),
RequestBody: getRequestBody,
IsRequest: false,
Context: rw.Request.Context(),
}
}
// MessageForResponse creates a Message for signing a response to req.
// It clones the response and request headers and makes the supplied body
// available for Content-Digest and other covered components. The request
// body may be buffered and restored when a ;req component requires it.
func MessageForResponse(req *http.Request, respHeader http.Header, body []byte, respCode int) *Message {
var bodySnapshot []byte
getBody := func() (io.ReadCloser, error) {
if len(bodySnapshot) != 0 {
return io.NopCloser(bytes.NewReader(bodySnapshot)), nil
}
save, newBody, err := drainBody(req.Body)
if err != nil {
return nil, err
}
req.Body = newBody
bodySnapshot = save
return io.NopCloser(bytes.NewReader(bodySnapshot)), nil
}
return &Message{
Method: req.Method,
Authority: req.Host,
URL: req.URL,
Header: respHeader.Clone(),
Body: func() (io.ReadCloser, error) { return io.NopCloser(bytes.NewReader(body)), nil },
StatusCode: respCode,
RequestHeader: req.Header.Clone(),
RequestBody: getBody,
IsRequest: false,
Context: req.Context(),
}
}
// Message represents the HTTP request or response data needed to construct
// and verify an HTTP message signature. For a response, request-related
// fields supply data used by component identifiers with the ;req parameter.
// Not every field is necessarily covered by the signature.
// Message data must remain stable after it is prepared for signing or
// verification, apart from headers added by signing. A Message must not be
// copied after its first use.
type Message struct {
// Context is passed to key resolution and Accept-Signature nonce generation.
Context context.Context //nolint: containedctx
// Method is the associated HTTP request method.
Method string
// Authority is the authority (host and optional port) of the request.
Authority string
// URL is the associated HTTP request URL.
URL *url.URL
// Header contains the headers of the request or response being processed.
// The signer may update this map with signatures and a Content-Digest.
Header http.Header
// Body opens the body of the request or response being processed.
Body func() (io.ReadCloser, error)
// RequestHeader contains the associated request headers for a response.
RequestHeader http.Header
// RequestBody opens the associated request body for response ;req components.
RequestBody func() (io.ReadCloser, error)
// StatusCode is the HTTP response status code (unused for requests).
StatusCode int
// IsRequest distinguishes requests from responses for canonicalization.
IsRequest bool
queryOnce sync.Once
queryValues url.Values
}
// queryParams parses the associated request query at most once per Message.
func (m *Message) queryParams() url.Values {
m.queryOnce.Do(func() {
m.queryValues = m.URL.Query()
})
return m.queryValues
}
func (m *Message) addSignature(label string, signature []byte, signatureInput httpsfv.InnerList) (http.Header, error) {
// check to see if there are already signature/signature-input headers
// if there are we want to store the current (case-sensitive) name of the header,
// and we want to parse out the current values, so we can append our new signature
var (
signatureHeaderDict *httpsfv.Dictionary
inputHeaderDict *httpsfv.Dictionary
err error
)
if signatureHeader := m.Header.Values(headerSignature); len(signatureHeader) != 0 {
signatureHeaderDict, err = httpsfv.UnmarshalDictionary(signatureHeader)
} else {
signatureHeaderDict = httpsfv.NewDictionary()
}
if err != nil {
return nil, err
}
if inputHeader := m.Header.Values(headerSignatureInput); len(inputHeader) != 0 {
inputHeaderDict, err = httpsfv.UnmarshalDictionary(inputHeader)
} else {
inputHeaderDict = httpsfv.NewDictionary()
}
if err != nil {
return nil, err
}
label = createUniqueLabel(label, inputHeaderDict)
signatureHeaderDict.Add(label, httpsfv.NewItem(signature))
inputHeaderDict.Add(label, signatureInput)
marshalledSignatureHeader, err := httpsfv.Marshal(signatureHeaderDict)
if err != nil {
return nil, err
}
marshalledInputHeader, err := httpsfv.Marshal(inputHeaderDict)
if err != nil {
return nil, err
}
m.Header.Set(headerSignature, marshalledSignatureHeader)
m.Header.Set(headerSignatureInput, marshalledInputHeader)
return m.Header, nil
}
func createUniqueLabel(label string, dict *httpsfv.Dictionary) string {
uniqueLabel := label
_, labelPresent := dict.Get(uniqueLabel)
for count := 1; labelPresent; count++ {
uniqueLabel = fmt.Sprintf("%s%d", label, count)
_, labelPresent = dict.Get(uniqueLabel)
}
return uniqueLabel
}
func drainBody(body io.ReadCloser) ([]byte, io.ReadCloser, error) {
if body == nil || body == http.NoBody {
// No copying needed. Preserve the magic sentinel meaning of NoBody.
return nil, http.NoBody, nil
}
var buf bytes.Buffer
if _, err := buf.ReadFrom(body); err != nil {
return nil, body, err
}
if err := body.Close(); err != nil {
return nil, body, err
}
data := buf.Bytes()
return data, io.NopCloser(bytes.NewReader(data)), nil
}