Skip to content

fix(slack): replace deprecated @slack/events-api with native crypto validation#4313

Open
angelcaamal wants to merge 4 commits into
mainfrom
fix/remove-deprecated-slack-api
Open

fix(slack): replace deprecated @slack/events-api with native crypto validation#4313
angelcaamal wants to merge 4 commits into
mainfrom
fix/remove-deprecated-slack-api

Conversation

@angelcaamal
Copy link
Copy Markdown
Contributor

@angelcaamal angelcaamal commented May 11, 2026

Description

This PR refactors the Slack function sample to remove the deprecated @slack/events-api library. The request validation has been rewritten to use manual signature verification with the native Node.js crypto module.

Additionally, the test suite has been updated to reflect these changes and ensure stability in the CI pipeline.

Fixes Internal: b/414440396

Note: Before submitting a pull request, please open an issue for discussion if you are not associated with Google.

Checklist

  • I have followed guidelines from CONTRIBUTING.MD and Samples Style Guide
  • Tests pass: npm test (see Testing)
  • Lint pass: npm run lint (see Style)
  • Required CI tests pass (see CI testing)
  • These samples need a new API enabled in testing projects to pass (let us know which ones)
  • These samples need a new/updated env vars in testing projects set to pass (let us know which ones)
  • This pull request is from a branch created directly off of GoogleCloudPlatform/nodejs-docs-samples. Not a fork.
  • This sample adds a new sample directory, and I updated the CODEOWNERS file with the codeowners for this sample
  • This sample adds a new sample directory, and I created GitHub Actions workflow for this sample
  • This sample adds a new Product API, and I updated the Blunderbuss issue/PR auto-assigner with the codeowners for this sample
  • Please merge this PR for me once it is approved

Note: Any check with (dev), (experimental), or (legacy) can be ignored and should not block your PR from merging (see CI testing).

@angelcaamal angelcaamal added api: cloudfunctions Issues related to the Cloud Run functions API. samples Issues that are directly related to samples. labels May 11, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request replaces the @slack/events-api dependency with a manual implementation of Slack webhook signature verification using the Node.js crypto module. The changes include a new verifyWebhook function and updated integration and unit tests to support the manual signing process. Feedback was provided to enhance security by implementing replay attack protection via timestamp verification, adding explicit checks for the signing secret, and optimizing the HMAC calculation process.

Comment thread functions/slack/index.js
@angelcaamal angelcaamal marked this pull request as ready for review May 12, 2026 18:54
@angelcaamal angelcaamal requested review from a team as code owners May 12, 2026 18:54
const {SLACK_SECRET} = process.env;
const SLACK_TIMESTAMP = Date.now();
process.env.SLACK_SECRET = process.env.SLACK_SECRET || 'test-slack-secret';
const SLACK_SECRET = process.env.SLACK_SECRET;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not change the values of global states like env variables, since it's not a generally a good habit in practice. Just pull the value directly in your code's const where you can use the ternary operator. However, also is good practice to raise an exception when an environment variable is not set, as it is a good practice to avoid obfuscating errors in real life.

Suggested change
const SLACK_SECRET = process.env.SLACK_SECRET;
if (process.env.SLACK_SECRET is not undefined){
const SLACK_SECRET = process.env.SLACK_SECRET;
}
else:
raise("Error, SLACK_SECRET env var is not set.")

p.d: pardon my node.js syntax, might not be accurate.

const SLACK_TOKEN = 'slack-token';
const KG_API_KEY = 'kg-api-key';
process.env.SLACK_SECRET = process.env.SLACK_SECRET || 'slack-token';
process.env.KG_API_KEY = process.env.KG_API_KEY || 'test-kg-api-key';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above: do not change var envs, raise exception on being unset.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: cloudfunctions Issues related to the Cloud Run functions API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants