@peac/protocol 0.15.1 → 0.15.2

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.
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Internal Ed25519 signature-width guard for the verification paths.
3
+ *
4
+ * NOT re-exported from the package barrel (`index.ts` does
5
+ * `export * from './verify'` / `'./verify-local'`, so anything exported from
6
+ * those modules becomes public API). Keep this helper in `_internal` so the
7
+ * guard adds no public surface.
8
+ */
9
+ /** Ed25519 signatures are fixed-width 64 bytes (RFC 8032 / RFC 8709). */
10
+ export declare const ED25519_SIGNATURE_BYTES = 64;
11
+ /**
12
+ * Return the decoded byte length of a compact JWS signature segment, or `null`
13
+ * when the token is not a well-formed three-part compact JWS or the signature
14
+ * segment is not decodable base64url. Those `null` cases are deliberately left
15
+ * to the existing format/verification error paths (this guard does not change
16
+ * their classification); only a well-formed token whose signature decodes to a
17
+ * non-64-byte width is surfaced to the caller as a wrong-length signature.
18
+ */
19
+ export declare function ed25519SignatureByteLength(jws: string): number | null;
20
+ //# sourceMappingURL=signature.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signature.d.ts","sourceRoot":"","sources":["../../src/_internal/signature.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,yEAAyE;AACzE,eAAO,MAAM,uBAAuB,KAAK,CAAC;AAE1C;;;;;;;GAOG;AACH,wBAAgB,0BAA0B,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAUrE"}
package/dist/index.cjs CHANGED
@@ -2209,6 +2209,18 @@ async function resolveJWKS(issuerUrl, options) {
2209
2209
  revokedKeys
2210
2210
  };
2211
2211
  }
2212
+ var ED25519_SIGNATURE_BYTES = 64;
2213
+ function ed25519SignatureByteLength(jws) {
2214
+ const parts = jws.split(".");
2215
+ if (parts.length !== 3) {
2216
+ return null;
2217
+ }
2218
+ try {
2219
+ return crypto$1.base64urlDecode(parts[2]).length;
2220
+ } catch {
2221
+ return null;
2222
+ }
2223
+ }
2212
2224
 
2213
2225
  // src/verify.ts
2214
2226
  function jwkToPublicKey(jwk) {
@@ -2306,6 +2318,23 @@ async function verifyReceipt(optionsOrJws) {
2306
2318
  };
2307
2319
  }
2308
2320
  const publicKey = jwkToPublicKey(jwk);
2321
+ const signatureBytes = ed25519SignatureByteLength(receiptJws);
2322
+ if (signatureBytes !== null && signatureBytes !== ED25519_SIGNATURE_BYTES) {
2323
+ const durationMs = performance.now() - startTime;
2324
+ fireTelemetryHook(telemetry?.onReceiptVerified, {
2325
+ receiptHash: hashReceipt(receiptJws),
2326
+ valid: false,
2327
+ reasonCode: "invalid_signature",
2328
+ issuer: payload.iss,
2329
+ kid: header.kid,
2330
+ durationMs
2331
+ });
2332
+ return {
2333
+ ok: false,
2334
+ reason: "invalid_signature",
2335
+ details: "Ed25519 signature has invalid length"
2336
+ };
2337
+ }
2309
2338
  const result = await crypto$1.verify(receiptJws, publicKey);
2310
2339
  if (!result.valid) {
2311
2340
  const durationMs = performance.now() - startTime;
@@ -2341,7 +2370,7 @@ async function verifyReceipt(optionsOrJws) {
2341
2370
  ...jwksFetchTime && { jwks_fetch_ms: jwksFetchTime }
2342
2371
  }
2343
2372
  };
2344
- } catch (err) {
2373
+ } catch {
2345
2374
  const durationMs = performance.now() - startTime;
2346
2375
  fireTelemetryHook(telemetry?.onReceiptVerified, {
2347
2376
  receiptHash: hashReceipt(receiptJws),
@@ -2352,7 +2381,7 @@ async function verifyReceipt(optionsOrJws) {
2352
2381
  return {
2353
2382
  ok: false,
2354
2383
  reason: "verification_error",
2355
- details: err instanceof Error ? err.message : String(err)
2384
+ details: "Unexpected verification error."
2356
2385
  };
2357
2386
  }
2358
2387
  }
@@ -2665,11 +2694,18 @@ async function verifyLocal(jws, publicKey, options = {}) {
2665
2694
  message: `Invalid receipt payload: ${syntaxMessage}`
2666
2695
  };
2667
2696
  }
2668
- const message = err instanceof Error ? err.message : String(err);
2697
+ const signatureBytes = ed25519SignatureByteLength(jws);
2698
+ if (signatureBytes !== null && signatureBytes !== ED25519_SIGNATURE_BYTES) {
2699
+ return {
2700
+ valid: false,
2701
+ code: "E_INVALID_SIGNATURE",
2702
+ message: "Ed25519 signature has invalid length"
2703
+ };
2704
+ }
2669
2705
  return {
2670
2706
  valid: false,
2671
2707
  code: "E_INTERNAL",
2672
- message: `Unexpected verification error: ${message}`
2708
+ message: "Unexpected verification error."
2673
2709
  };
2674
2710
  }
2675
2711
  }