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

Conversation

SnowballXueQiu
Copy link
Contributor

Fix Feishu Webhook Signature Verification

This PR implements proper signature verification for Feishu (Lark) webhooks according to the official documentation.

Changes

  • Implemented the GenSign function based on Feishu's official Go sample code
  • Modified the webhook request creation to include timestamp and signature in the payload when a secret is configured
  • Fixed the signature generation algorithm to properly use HMAC-SHA256 with the correct string format

Implementation Details

The signature verification works as follows:

  1. When a webhook secret is provided, a timestamp is generated
  2. The signature string is created using timestamp + "\n" + secret
  3. The HMAC-SHA256 algorithm is applied to an empty string using the signature string as the key
  4. The result is Base64 encoded to produce the final signature
  5. Both timestamp and signature are added to the payload

According to Feishu's documentation, the timestamp must be within 1 hour (3600 seconds) of the current time to be considered valid.

Security Note

Feishu emphasizes the importance of keeping webhook URLs secure. Do not disclose them on GitHub, blogs, or any public sites to prevent unauthorized use.

References

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jun 20, 2025
@github-actions github-actions bot added the modifies/go Pull requests that update Go code label Jun 20, 2025
@techknowlogick techknowlogick added backport/v1.24 This PR should be backported to Gitea 1.24 type/bug labels Jun 20, 2025
@SnowballXueQiu
Copy link
Contributor Author

@techknowlogick All errors fixed

**Performance Overhead**
   `fmt.Sprintf` is **slower** than simple string concatenation or `strconv.FormatInt` because:

   * It uses reflection and parsing logic internally.
   * It's designed for formatting many types, not just basic strings or numbers.

Co-Authored-By: hiifong <[email protected]>
@wxiaoguang
Copy link
Contributor

Added a test (the test data is generated by Feishu python demo), and simplified the code a little

@SnowballXueQiu
Copy link
Contributor Author

@wxiaoguang Sorry, due to a funny problem, one of your commit(probably called 'change a better name') was reverted. Please push your code again.

@wxiaoguang
Copy link
Contributor

Done. Hopefully the updated code looks good to you.

Thank you very much for the PR

@wxiaoguang wxiaoguang added this to the 1.25.0 milestone Jun 20, 2025
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Jun 20, 2025
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Jun 20, 2025
@lunny lunny added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Jun 20, 2025
@lunny lunny merged commit 40dec17 into go-gitea:main Jun 20, 2025
26 checks passed
GiteaBot pushed a commit to GiteaBot/gitea that referenced this pull request Jun 20, 2025
# Fix Feishu Webhook Signature Verification

This PR implements proper signature verification for Feishu (Lark)
webhooks according to the [official
documentation](https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot).

## Changes

- Implemented the `GenSign` function based on Feishu's official Go
sample code
- Modified the webhook request creation to include timestamp and
signature in the payload when a secret is configured
- Fixed the signature generation algorithm to properly use HMAC-SHA256
with the correct string format

## Implementation Details

The signature verification works as follows:
1. When a webhook secret is provided, a timestamp is generated
2. The signature string is created using `timestamp + "\n" + secret`
3. The HMAC-SHA256 algorithm is applied to an empty string using the
signature string as the key
4. The result is Base64 encoded to produce the final signature
5. Both timestamp and signature are added to the payload

According to Feishu's documentation, the timestamp must be within 1 hour
(3600 seconds) of the current time to be considered valid.

## Security Note

Feishu emphasizes the importance of keeping webhook URLs secure. Do not
disclose them on GitHub, blogs, or any public sites to prevent
unauthorized use.

## References

- [Feishu Custom Bot
Documentation](https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot)

---------

Co-authored-by: hiifong <[email protected]>
Co-authored-by: wxiaoguang <[email protected]>
@GiteaBot GiteaBot added backport/done All backports for this PR have been created and removed reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. labels Jun 20, 2025
@wxiaoguang
Copy link
Contributor

Found that the fix is incomplete, there is no "Secret" input on the UI

zjjhot added a commit to zjjhot/gitea that referenced this pull request Jun 23, 2025
* giteaofficial/main: (22 commits)
  [skip ci] Updated translations via Crowdin
  Fix required contexts and commit status matching bug (go-gitea#34815)
  Add "Cancel workflow run" button to Actions list page (go-gitea#34817)
  Use `shallowRef` instead of `ref` in `.vue` files where possible (go-gitea#34813)
  Edit file workflow for creating a fork and proposing changes (go-gitea#34240)
  Refactor packages (go-gitea#34777)
  Refactor wiki (go-gitea#34805)
  [skip ci] Updated translations via Crowdin
  Upgrade htmx to 2.0.5 (go-gitea#34809)
  Forks repository list page follow other repositories page (go-gitea#34784)
  Add post-installation redirect based on admin account status (go-gitea#34493)
  Rework delete org and rename org UI (go-gitea#34762)
  Refactor editor (go-gitea#34780)
  Improve img lazy loading (go-gitea#34804)
  [skip ci] Updated translations via Crowdin
  Fix Feishu webhook signature verification (go-gitea#34788)
  Upgrade chi to v5.2.2 (go-gitea#34798)
  Fix the issue of abnormal interface when there is no issue-item on the project page (go-gitea#34791)
  Fix container range bug (go-gitea#34795)
  Fix OCI manifest parser (go-gitea#34797)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/done All backports for this PR have been created backport/v1.24 This PR should be backported to Gitea 1.24 lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/go Pull requests that update Go code type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants