-
Notifications
You must be signed in to change notification settings - Fork 0
/
digest.go
94 lines (72 loc) · 2.71 KB
/
digest.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
package sigs
import (
"crypto"
"net/http"
"strings"
"github.com/benpate/derp"
"github.com/benpate/re"
"github.com/benpate/rosetta/list"
"github.com/benpate/rosetta/slice"
"github.com/rs/zerolog/log"
)
// ApplyDigest calculates the digest of the body from a given
// http.Request, then adds the digest to the Request's header.
func ApplyDigest(request *http.Request, fn DigestFunc) error {
// Retrieve the request body (in a replayable manner)
body, err := re.ReadRequestBody(request)
if err != nil {
return derp.Wrap(err, "sigs.ApplyDigest", "Error reading request body")
}
// Try to calculate the digest with the DigestFunc
result := fn(body)
// Apply the digest to the Request
request.Header.Set(FieldDigest, result)
return nil
}
// VerifyDigest verifies that the digest in the http.Request header
// matches the contents of the http.Request body.
func VerifyDigest(request *http.Request, allowedHashes ...crypto.Hash) error {
// Retrieve the request body (in a replayable manner)
body, err := re.ReadRequestBody(request)
if err != nil {
return derp.Wrap(err, "sigs.VerifyDigest", "Error reading request body")
}
// Retrieve the digest(s) included in the HTTP Request
digestHeader := request.Header.Get(FieldDigest)
// If there is no digest header, then there is nothing to verify
if digestHeader == "" {
return nil
}
// Process the digest header into separate values
headerValues := strings.Split(digestHeader, ",")
atLeastOneAlgorithmMatches := false
for _, headerValue := range headerValues {
headerValue = strings.Trim(headerValue, " ")
digestAlgorithm, digestValue := list.Split(headerValue, '=')
// If we recognize the digest algorithm, then use it to verify the body/digest
fn, err := getDigestFuncByName(digestAlgorithm)
if err != nil {
log.Trace().Msg("sigs.VerifyDigest: Unknown digest algorithm: " + digestAlgorithm)
continue
}
// If the values match, then success!
if headerValue == fn(body) {
log.Trace().Msg("sigs.VerifyDigest: Valid Digest Found. Algorithm: " + digestAlgorithm)
// Verify that this algorithm is in the list of allowed hashes
hash := getHashByName(digestAlgorithm)
if slice.Contains(allowedHashes, hash) {
atLeastOneAlgorithmMatches = true
}
continue
}
// If the values DON'T MATCH, then fail immediately.
// We don't want bad actors "digest shopping"
return derp.NewForbiddenError("sigs.VerifyDigest", "Digest verification failed", digestValue)
}
// If we have found at least one digest that matches, then success!
if atLeastOneAlgorithmMatches {
return nil
}
// Otherwise, the digest hash does not meet our minimum requirements. Fail.
return derp.NewForbiddenError("sigs.VerifyDigest", "No matching digest found")
}