Skip to content

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

Draft
angelcaamal wants to merge 2 commits into
mainfrom
fix/remove-deprecated-slack-api
Draft

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

Conversation

@angelcaamal
Copy link
Copy Markdown
Contributor

@angelcaamal angelcaamal commented May 11, 2026

Description

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
Comment on lines +96 to +125
const signingSecret = process.env.SLACK_SECRET;
const requestSignature = req.headers['x-slack-signature'];
const requestTimestamp = req.headers['x-slack-request-timestamp'];
const requestBody = req.rawBody;

if (!requestSignature || !requestTimestamp) {
const err = new Error('Missing Slack validation headers.');
err.code = 400;
throw err;
}

const baseString = `v0:${requestTimestamp}:${requestBody}`;
const expectedSignature =
'v0=' +
crypto
.createHmac('sha256', signingSecret)
.update(baseString, 'utf8')
.digest('hex');

const sigBuffer = Buffer.from(requestSignature, 'utf8');
const expBuffer = Buffer.from(expectedSignature, 'utf8');

// This method throws an exception if an incoming request is invalid.
verifyRequestSignature(signature);
if (
sigBuffer.length !== expBuffer.length ||
!crypto.timingSafeEqual(sigBuffer, expBuffer)
) {
const err = new Error('Invalid Slack signature.');
err.code = 401;
throw err;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

security-high high

To improve security and robustness, several enhancements should be made to the webhook verification logic:

  1. Replay Attack Protection: Verify that the request timestamp is recent (e.g., within 5 minutes) as recommended by Slack documentation. This is a critical security step when manually validating signatures.
  2. Configuration Check: Explicitly check for the presence of SLACK_SECRET to provide a clearer error message if the environment variable is missing.
  3. Efficient HMAC Calculation: Use hmac.update() multiple times instead of string concatenation. This is more efficient for large payloads and avoids potential encoding issues by handling the raw body Buffer directly.
  const signingSecret = process.env.SLACK_SECRET;
  const requestSignature = req.headers['x-slack-signature'];
  const requestTimestamp = req.headers['x-slack-request-timestamp'];
  const requestBody = req.rawBody;

  if (!signingSecret) {
    const err = new Error('Slack signing secret not found.');
    err.code = 500;
    throw err;
  }

  if (!requestSignature || !requestTimestamp) {
    const err = new Error('Missing Slack validation headers.');
    err.code = 400;
    throw err;
  }

  // Prevent replay attacks by verifying the timestamp is recent
  const now = Math.floor(Date.now() / 1000);
  if (Math.abs(now - Number(requestTimestamp)) > 60 * 5) {
    const err = new Error('Slack request timestamp is too old.');
    err.code = 401;
    throw err;
  }

  const hmac = crypto.createHmac('sha256', signingSecret);
  hmac.update('v0:' + requestTimestamp + ':', 'utf8');
  hmac.update(requestBody || '');
  const expectedSignature = 'v0=' + hmac.digest('hex');

  const sigBuffer = Buffer.from(requestSignature, 'utf8');
  const expBuffer = Buffer.from(expectedSignature, 'utf8');

  if (
    sigBuffer.length !== expBuffer.length ||
    !crypto.timingSafeEqual(sigBuffer, expBuffer)
  ) {
    const err = new Error('Invalid Slack signature.');
    err.code = 401;
    throw err;
  }

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.

1 participant