Skip to content

Commit

Permalink
Fix HMAC to avoid canonicalization issues
Browse files Browse the repository at this point in the history
  • Loading branch information
turt2live committed Dec 27, 2024
1 parent 2cf5a65 commit afa8e96
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions api/_responses/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"net/url"
"strconv"
"time"
Expand All @@ -31,19 +30,14 @@ func Redirect(ctx rcontext.RequestContext, toUrl string, auth _apimeta.AuthConte
toUrl = appendQueryParam(toUrl, "matrix_exp", strconv.FormatInt(expirationTime.UnixMilli(), 10))

// Prepare our HMAC message contents as a JSON object
hmacInput := make(map[string]string)
hmacInput["url"] = toUrl
hmacMessage := toUrl + "||"
if auth.User.UserId != "" {
hmacInput["access_token"] = auth.User.AccessToken
}
hmacMessage, err := json.Marshal(hmacInput)
if err != nil {
panic(err) // "should never happen"
hmacMessage += auth.User.AccessToken
}

// Actually do the HMAC
mac := hmac.New(sha256.New, []byte("THIS_IS_A_SECRET_KEY")) // TODO: @@ Actual secret key
mac.Write(hmacMessage)
mac.Write([]byte(hmacMessage))
verifyHmac := mac.Sum(nil)

// Append the HMAC to the URL
Expand Down

0 comments on commit afa8e96

Please sign in to comment.