Skip to content

Commit afa8e96

Browse files
committed
Fix HMAC to avoid canonicalization issues
1 parent 2cf5a65 commit afa8e96

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

api/_responses/redirect.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"crypto/hmac"
55
"crypto/sha256"
66
"encoding/hex"
7-
"encoding/json"
87
"net/url"
98
"strconv"
109
"time"
@@ -31,19 +30,14 @@ func Redirect(ctx rcontext.RequestContext, toUrl string, auth _apimeta.AuthConte
3130
toUrl = appendQueryParam(toUrl, "matrix_exp", strconv.FormatInt(expirationTime.UnixMilli(), 10))
3231

3332
// Prepare our HMAC message contents as a JSON object
34-
hmacInput := make(map[string]string)
35-
hmacInput["url"] = toUrl
33+
hmacMessage := toUrl + "||"
3634
if auth.User.UserId != "" {
37-
hmacInput["access_token"] = auth.User.AccessToken
38-
}
39-
hmacMessage, err := json.Marshal(hmacInput)
40-
if err != nil {
41-
panic(err) // "should never happen"
35+
hmacMessage += auth.User.AccessToken
4236
}
4337

4438
// Actually do the HMAC
4539
mac := hmac.New(sha256.New, []byte("THIS_IS_A_SECRET_KEY")) // TODO: @@ Actual secret key
46-
mac.Write(hmacMessage)
40+
mac.Write([]byte(hmacMessage))
4741
verifyHmac := mac.Sum(nil)
4842

4943
// Append the HMAC to the URL

0 commit comments

Comments
 (0)