gpt4 book ai didi

security - 验证 Trello Webhook 签名

转载 作者:IT王子 更新时间:2023-10-29 00:45:11 46 4
gpt4 key购买 nike

我无法成功验证来自 Trello 的 Webhook 请求。这是我所知道的。

Trello 的 webhook 文档 here状态:

Each webhook trigger contains the HTTP header X-Trello-Webhook. The header is a base64 digest of an HMAC-SHA1 hash. The hashed content is the concatenation of the full request body and the callbackURL exactly as it was provided during webhook creation. The key used to sign this text is your application’s secret.

这是可以理解的。他们接着说

Because of certain defaults in the crypto utilities in node, the payloads that we sign are treated as binary strings, not utf-8. For example, if you take the en-dash character (U+2013 or 8211 in decimal), and create a binary buffer out of it in Node, it will show up as a buffer of [19], which are the 8 least significant bits of 8211. That is the value that is being used in the digest to compute the SHA-1.

这个我不太清楚。我的理解是有效负载(body + callbackURL)的每个字符都被放入一个 8 位整数,溢出被忽略。 (因为 8211 == 0b10000000010011,而 0b00010011 == 19)这就是我认为我的问题所在。

我用来解决 Trello 节点负载问题的函数是:

func bitShift(s string) []byte {
var byteString []byte

// For each rune in the string
for _, c := range s {

// Create a byte slice
b := []byte(string(c))

// Take the sign off the least significant byte
tmp := b[len(b)-1] << 1
tmp = tmp >> 1

// Append it to the byte string
byteString = append(byteString, tmp)
}
return byteString
}

也很有可能我在基本验证步骤上做错了。虽然我对此有些陌生,但我觉得还不错。

// VerifyNotificationHeader ...
func VerifyNotificationHeader(signedHeader, trelloAPISecret string, requestURL *url.URL, body []byte) bool {

// Put callbackURL and body into byte slice
urlBytes := bitShift(requestURL.String())
bitBody := bitShift(string(body))

// Sign, hash, and encode the payload
secret := []byte(trelloAPISecret)
keyHMAC := hmac.New(sha1.New, secret)
keyHMAC.Write(append(bitBody, urlBytes...))
signedHMAC := keyHMAC.Sum(nil)
base64signedHMAC := base64.StdEncoding.EncodeToString(signedHMAC)

if comp := strings.EqualFold(base64signedHMAC, signedHeader); !comp {
return false
}
return true
}

如果您需要更多信息,请告诉我。谢谢!

更新:已解决,查看答案。

最佳答案

你为什么要扔掉 MSB?您正在将每个 rune 转换为 byte,这是无符号的(实际上是 uint8 的别名),因此该位包含您的信息输了。

您可以考虑改用这样的函数:

func ascii(s string) []byte {
var ret []byte
for _, r := range s {
ret = append(ret, byte(r))
}
return ret
}

由于 runeint32 的别名,转换为 byte 只是删除前 24 位,这正是您想要的。

(注意:这假设小端。)

关于security - 验证 Trello Webhook 签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34209823/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com