The tests from #32063 and #32062 are two to three times the size of the code they cover, and a lot of them can't fail for a reason we'd care about. This PR removes those, with no production changes. In `coderd/schedule/cron`, two tests rebuilt the schedule rule a second time to compare against, which was also the only use of `export_test.go`, so that goes too. Most of the rest, in both packages, are table rows that take the same branch as a row that stays. I also dropped a copy of the webhook scheme list and a repeated-request test that belongs at the route. What's left is a row per reject branch, the RFC cases, the regression test for the `Next` hang and the constant-time scan. I replayed each fix commit from the two PRs against the smaller suite, and the pre-fix code still fails a test for every one, other than a fix to the wording of a zone error. One row that stays is changed: the bearer wrong-scheme case used `Basic`, which was only rejected because of where its space lands, so nothing covered the scheme word. It's now `Digest`. #32162 overlaps with this in `cron`. With the shim's users gone it no longer needs to touch that package, so it's now stacked on top of this PR.
46 lines
2.2 KiB
Go
46 lines
2.2 KiB
Go
package chatwebhook_test
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatwebhook"
|
|
)
|
|
|
|
func TestGitHub(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// Official vector from
|
|
// https://docs.github.com/en/webhooks/using-webhooks/validating-webhook-deliveries
|
|
const (
|
|
vectorSecret = "It's a Secret to Everybody"
|
|
vectorBody = "Hello, World!"
|
|
vectorSig = "sha256=757107ea0eb2509fc211221cce984b8a37570b6d7586c22c46f4379c8b043e17"
|
|
)
|
|
|
|
const secret = "j9X0c2Vy-Z2l0aHViLXRlc3Qtc2VjcmV0LTAxMjM0NTY" //nolint:gosec // Test fixture, not a credential.
|
|
signHex := func(secret string, body []byte) string {
|
|
return hex.EncodeToString(hmacSHA256([]byte(secret), body))
|
|
}
|
|
sig := func(value string) http.Header { return header("X-Hub-Signature-256", value) }
|
|
body := []byte(`{"action":"opened","number":1}`)
|
|
valid := signHex(secret, body)
|
|
|
|
runVerifyCases(t, newVerifier(t, chatwebhook.SchemeGitHub), []verifyCase{
|
|
{name: "official vector", header: sig(vectorSig), body: []byte(vectorBody), secret: vectorSecret, accepted: true},
|
|
{name: "valid signature", header: sig("sha256=" + valid), body: body, secret: secret, accepted: true},
|
|
|
|
{name: "header missing", header: http.Header{}, body: body, secret: secret},
|
|
{name: "no prefix", header: sig(valid), body: body, secret: secret},
|
|
{name: "truncated digest", header: sig("sha256=" + valid[:62]), body: body, secret: secret},
|
|
{name: "digest with extra bytes", header: sig("sha256=" + valid + "00"), body: body, secret: secret},
|
|
{name: "two lines, first valid", header: header("X-Hub-Signature-256", "sha256="+valid, "X-Hub-Signature-256", "sha256="+strings.Repeat("0", 64)), body: body, secret: secret},
|
|
{name: "two lines, second valid", header: header("X-Hub-Signature-256", "sha256="+strings.Repeat("0", 64), "X-Hub-Signature-256", "sha256="+valid), body: body, secret: secret},
|
|
{name: "wrong secret", header: sig("sha256=" + valid), body: body, secret: secret + "x"},
|
|
{name: "tampered body", header: sig("sha256=" + valid), body: append([]byte(nil), append(body, ' ')...), secret: secret},
|
|
{name: "signature made with an empty key", header: sig("sha256=" + signHex("", body)), body: body, secret: ""},
|
|
})
|
|
}
|