@cmssy/core 11.5.1 → 11.6.0

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.
package/dist/index.cjs CHANGED
@@ -764,21 +764,24 @@ var CmssyWebhookError = class extends Error {
764
764
  }
765
765
  };
766
766
  var DEFAULT_TOLERANCE_SECONDS = 300;
767
+ var MAX_SIGNATURES = 8;
767
768
  function parseSignatureHeader(header) {
768
769
  let timestamp = null;
769
- let signature = null;
770
+ const signatures = [];
770
771
  for (const part of header.split(",")) {
771
772
  const idx = part.indexOf("=");
772
773
  if (idx === -1) continue;
773
774
  const key = part.slice(0, idx).trim();
774
775
  const value = part.slice(idx + 1).trim();
775
776
  if (key === "t") timestamp = Number(value);
776
- else if (key === "v1") signature = value;
777
+ else if (key === "v1" && value && signatures.length < MAX_SIGNATURES) {
778
+ signatures.push(value);
779
+ }
777
780
  }
778
- if (timestamp === null || !Number.isFinite(timestamp) || !signature) {
781
+ if (timestamp === null || !Number.isFinite(timestamp) || !signatures.length) {
779
782
  throw new CmssyWebhookError("Malformed X-Cmssy-Signature header");
780
783
  }
781
- return { timestamp, signature };
784
+ return { timestamp, signatures };
782
785
  }
783
786
  function hexToBytes(hex) {
784
787
  if (hex.length % 2 !== 0 || /[^0-9a-fA-F]/.test(hex)) return null;
@@ -792,9 +795,10 @@ function bytesToHex(bytes) {
792
795
  return [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");
793
796
  }
794
797
  function timingSafeHexEqual(expectedHex, providedHex) {
798
+ if (expectedHex.length !== providedHex.length) return false;
795
799
  const expected = hexToBytes(expectedHex);
796
800
  const provided = hexToBytes(providedHex);
797
- if (!expected || !provided || expected.length !== provided.length) {
801
+ if (!expected || !provided) {
798
802
  return false;
799
803
  }
800
804
  let diff = 0;
@@ -824,17 +828,28 @@ async function verifyCmssyWebhook(options) {
824
828
  if (!signatureHeader) {
825
829
  throw new CmssyWebhookError("Missing X-Cmssy-Signature header");
826
830
  }
827
- if (!secret) {
831
+ const candidates = typeof secret === "string" ? [secret] : Array.isArray(secret) ? secret : [];
832
+ if (candidates.some((value) => typeof value !== "string")) {
833
+ throw new CmssyWebhookError("Webhook secret must be a string");
834
+ }
835
+ const secrets = candidates.filter(Boolean);
836
+ if (!secrets.length) {
828
837
  throw new CmssyWebhookError("Missing webhook secret");
829
838
  }
830
- const { timestamp, signature } = parseSignatureHeader(signatureHeader);
839
+ const { timestamp, signatures } = parseSignatureHeader(signatureHeader);
831
840
  const toleranceMs = (options.toleranceSeconds ?? DEFAULT_TOLERANCE_SECONDS) * 1e3;
832
841
  const now = options.now ?? Date.now();
833
842
  if (Math.abs(now - timestamp) > toleranceMs) {
834
843
  throw new CmssyWebhookError("Webhook timestamp outside tolerance");
835
844
  }
836
- const expected = await hmacSha256Hex(secret, `${timestamp}.${body}`);
837
- if (!timingSafeHexEqual(expected, signature)) {
845
+ let matched = false;
846
+ for (const candidate of secrets) {
847
+ const expected = await hmacSha256Hex(candidate, `${timestamp}.${body}`);
848
+ for (const signature of signatures) {
849
+ if (timingSafeHexEqual(expected, signature)) matched = true;
850
+ }
851
+ }
852
+ if (!matched) {
838
853
  throw new CmssyWebhookError("Webhook signature mismatch");
839
854
  }
840
855
  let parsed;
package/dist/index.js CHANGED
@@ -762,21 +762,24 @@ var CmssyWebhookError = class extends Error {
762
762
  }
763
763
  };
764
764
  var DEFAULT_TOLERANCE_SECONDS = 300;
765
+ var MAX_SIGNATURES = 8;
765
766
  function parseSignatureHeader(header) {
766
767
  let timestamp = null;
767
- let signature = null;
768
+ const signatures = [];
768
769
  for (const part of header.split(",")) {
769
770
  const idx = part.indexOf("=");
770
771
  if (idx === -1) continue;
771
772
  const key = part.slice(0, idx).trim();
772
773
  const value = part.slice(idx + 1).trim();
773
774
  if (key === "t") timestamp = Number(value);
774
- else if (key === "v1") signature = value;
775
+ else if (key === "v1" && value && signatures.length < MAX_SIGNATURES) {
776
+ signatures.push(value);
777
+ }
775
778
  }
776
- if (timestamp === null || !Number.isFinite(timestamp) || !signature) {
779
+ if (timestamp === null || !Number.isFinite(timestamp) || !signatures.length) {
777
780
  throw new CmssyWebhookError("Malformed X-Cmssy-Signature header");
778
781
  }
779
- return { timestamp, signature };
782
+ return { timestamp, signatures };
780
783
  }
781
784
  function hexToBytes(hex) {
782
785
  if (hex.length % 2 !== 0 || /[^0-9a-fA-F]/.test(hex)) return null;
@@ -790,9 +793,10 @@ function bytesToHex(bytes) {
790
793
  return [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");
791
794
  }
792
795
  function timingSafeHexEqual(expectedHex, providedHex) {
796
+ if (expectedHex.length !== providedHex.length) return false;
793
797
  const expected = hexToBytes(expectedHex);
794
798
  const provided = hexToBytes(providedHex);
795
- if (!expected || !provided || expected.length !== provided.length) {
799
+ if (!expected || !provided) {
796
800
  return false;
797
801
  }
798
802
  let diff = 0;
@@ -822,17 +826,28 @@ async function verifyCmssyWebhook(options) {
822
826
  if (!signatureHeader) {
823
827
  throw new CmssyWebhookError("Missing X-Cmssy-Signature header");
824
828
  }
825
- if (!secret) {
829
+ const candidates = typeof secret === "string" ? [secret] : Array.isArray(secret) ? secret : [];
830
+ if (candidates.some((value) => typeof value !== "string")) {
831
+ throw new CmssyWebhookError("Webhook secret must be a string");
832
+ }
833
+ const secrets = candidates.filter(Boolean);
834
+ if (!secrets.length) {
826
835
  throw new CmssyWebhookError("Missing webhook secret");
827
836
  }
828
- const { timestamp, signature } = parseSignatureHeader(signatureHeader);
837
+ const { timestamp, signatures } = parseSignatureHeader(signatureHeader);
829
838
  const toleranceMs = (options.toleranceSeconds ?? DEFAULT_TOLERANCE_SECONDS) * 1e3;
830
839
  const now = options.now ?? Date.now();
831
840
  if (Math.abs(now - timestamp) > toleranceMs) {
832
841
  throw new CmssyWebhookError("Webhook timestamp outside tolerance");
833
842
  }
834
- const expected = await hmacSha256Hex(secret, `${timestamp}.${body}`);
835
- if (!timingSafeHexEqual(expected, signature)) {
843
+ let matched = false;
844
+ for (const candidate of secrets) {
845
+ const expected = await hmacSha256Hex(candidate, `${timestamp}.${body}`);
846
+ for (const signature of signatures) {
847
+ if (timingSafeHexEqual(expected, signature)) matched = true;
848
+ }
849
+ }
850
+ if (!matched) {
836
851
  throw new CmssyWebhookError("Webhook signature mismatch");
837
852
  }
838
853
  let parsed;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmssy/core",
3
- "version": "11.5.1",
3
+ "version": "11.6.0",
4
4
  "description": "Framework-agnostic cmssy gateway: GraphQL client, block/field system, editor protocol. No React, no Next.",
5
5
  "keywords": [
6
6
  "cmssy",
@@ -62,7 +62,7 @@
62
62
  "vitest": "^2.1.0"
63
63
  },
64
64
  "dependencies": {
65
- "@cmssy/types": "0.32.0",
65
+ "@cmssy/types": "0.33.0",
66
66
  "jose": "^6.2.3"
67
67
  },
68
68
  "scripts": {