Skip to content

Commit

Permalink
msg validation error
Browse files Browse the repository at this point in the history
  • Loading branch information
makiuchi-d committed Jul 11, 2024
1 parent 411bf02 commit fff7bbe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 7 additions & 3 deletions server/auth/mackey.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,17 @@ func EncryptMACKey(appKey, macKey string) (string, error) {
}

// ValidateMsgHMAC validates the hmac of a websocket message.
func ValidateMsgHMAC(mac hash.Hash, data []byte) ([]byte, bool) {
func ValidateMsgHMAC(mac hash.Hash, data []byte) ([]byte, error) {
dlen := len(data) - mac.Size()
if dlen < 0 {
return nil, false
return nil, xerrors.Errorf("data=%v", data)
}
data, h := data[:dlen], data[dlen:]
return data, hmac.Equal(h, CalculateMsgHMAC(mac, data))
hash := CalculateMsgHMAC(mac, data)
if !hmac.Equal(h, hash) {
return nil, xerrors.Errorf("hash=(%v, %v)", h, hash)
}
return data, nil
}

func CalculateMsgHMAC(mac hash.Hash, data []byte) []byte {
Expand Down
8 changes: 4 additions & 4 deletions server/binary/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ func BuildRegularMsgFrame(t MsgType, seq int, payload []byte, hmac hash.Hash) []
}

// ParseMsg parse binary data to Msg struct
func UnmarshalMsg(hmac hash.Hash, alldata []byte) (Msg, error) {
data, ok := auth.ValidateMsgHMAC(hmac, alldata)
if !ok {
return nil, xerrors.Errorf("invalid msg hmac (len=%v)", len(alldata))
func UnmarshalMsg(hmac hash.Hash, data []byte) (Msg, error) {
data, err := auth.ValidateMsgHMAC(hmac, data)
if err != nil {
return nil, xerrors.Errorf("invalid msg hmac: %w", err)
}

if len(data) < 1 {
Expand Down

0 comments on commit fff7bbe

Please sign in to comment.