opticedge-cloud-utils 1.0.36 → 1.0.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,7 +5,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.isValidWebhookSignature = isValidWebhookSignature;
7
7
  const crypto_1 = __importDefault(require("crypto"));
8
+ const generateSignature = (body, secret) => {
9
+ return crypto_1.default.createHmac('sha256', secret).update(body).digest('hex');
10
+ };
8
11
  function isValidWebhookSignature(secret, body, signature) {
9
- const computedSignature = crypto_1.default.createHmac('sha256', secret).update(body).digest('hex');
10
- return computedSignature === signature;
12
+ const expectedSignature = generateSignature(body, secret);
13
+ const expectedBuffer = Buffer.from(expectedSignature);
14
+ const signatureBuffer = Buffer.from(signature);
15
+ if (expectedBuffer.length !== signatureBuffer.length) {
16
+ return false;
17
+ }
18
+ return crypto_1.default.timingSafeEqual(expectedBuffer, signatureBuffer);
11
19
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opticedge-cloud-utils",
3
- "version": "1.0.36",
3
+ "version": "1.0.37",
4
4
  "description": "Common utilities for cloud functions",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,6 +1,18 @@
1
1
  import crypto from 'crypto'
2
2
 
3
+ const generateSignature = (body: string, secret: string): string => {
4
+ return crypto.createHmac('sha256', secret).update(body).digest('hex')
5
+ }
6
+
3
7
  export function isValidWebhookSignature(secret: string, body: string, signature: string): boolean {
4
- const computedSignature = crypto.createHmac('sha256', secret).update(body).digest('hex')
5
- return computedSignature === signature
8
+ const expectedSignature = generateSignature(body, secret)
9
+
10
+ const expectedBuffer = Buffer.from(expectedSignature)
11
+ const signatureBuffer = Buffer.from(signature)
12
+
13
+ if (expectedBuffer.length !== signatureBuffer.length) {
14
+ return false
15
+ }
16
+
17
+ return crypto.timingSafeEqual(expectedBuffer, signatureBuffer)
6
18
  }