Skip to content

Fix Feishu webhook signature verification #34788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 20, 2025
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 68 additions & 4 deletions services/webhook/feishu.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@
package webhook

import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"net/http"
"strconv"
"strings"
"time"

webhook_model "code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json"
api "code.gitea.io/gitea/modules/structs"
webhook_module "code.gitea.io/gitea/modules/webhook"
)

type (
// FeishuPayload represents
// FeishuPayload represents the payload for Feishu webhook
FeishuPayload struct {
MsgType string `json:"msg_type"` // text / post / image / share_chat / interactive / file /audio / media
Content struct {
Timestamp int64 `json:"timestamp,omitempty"` // Unix timestamp for signature verification
Sign string `json:"sign,omitempty"` // Signature for verification
MsgType string `json:"msg_type"` // text / post / image / share_chat / interactive / file /audio / media
Content struct {
Text string `json:"text"`
} `json:"content"`
}
Expand Down Expand Up @@ -184,9 +193,64 @@ func (feishuConvertor) WorkflowJob(p *api.WorkflowJobPayload) (FeishuPayload, er
return newFeishuTextPayload(text), nil
}

// GenSign generates a signature for Feishu webhook
// https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot
func GenSign(secret string, timestamp int64) (string, error) {
// timestamp + key do sha256, then base64 encode
stringToSign := strconv.FormatInt(timestamp, 10) + "\n" + secret

h := hmac.New(sha256.New, []byte(stringToSign))
_, err := h.Write([]byte{})
if err != nil {
return "", err
}

signature := base64.StdEncoding.EncodeToString(h.Sum(nil))
return signature, nil
}

func newFeishuRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
var pc payloadConvertor[FeishuPayload] = feishuConvertor{}
return newJSONRequest(pc, w, t, true)

// Get the payload first
payload, err := newPayload(pc, []byte(t.PayloadContent), t.EventType)
if err != nil {
return nil, nil, err
}

// Add timestamp and signature if secret is provided
if w.Secret != "" {
timestamp := time.Now().Unix()
payload.Timestamp = timestamp

// Generate signature
sign, err := GenSign(w.Secret, timestamp)
if err != nil {
return nil, nil, err
}
payload.Sign = sign
}

// Marshal the payload
body, err := json.MarshalIndent(payload, "", " ")
if err != nil {
return nil, nil, err
}

// Create the request
method := w.HTTPMethod
if method == "" {
method = http.MethodPost
}

req, err := http.NewRequest(method, w.URL, bytes.NewReader(body))
if err != nil {
return nil, nil, err
}
req.Header.Set("Content-Type", "application/json")

// Add default headers
return req, body, addDefaultHeaders(req, []byte(w.Secret), w, t, body)
}

func init() {
Expand Down
Loading