mailery 0.16.0 → 0.16.1

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/testing.js CHANGED
@@ -299,8 +299,36 @@ var init_webhook_tolerance = __esm({
299
299
  // src/server/providers/sendgrid.ts
300
300
  var sendgrid_exports = {};
301
301
  __export(sendgrid_exports, {
302
- SendGridProvider: () => SendGridProvider
302
+ SendGridProvider: () => SendGridProvider,
303
+ normalizeWebhookVerificationKey: () => normalizeWebhookVerificationKey
303
304
  });
305
+ function normalizeWebhookVerificationKey(input) {
306
+ let key = String(input ?? "").trim().replace(/^["']|["']$/g, "");
307
+ if (key.includes("\\n")) key = key.replace(/\\n/g, "\n");
308
+ let pem;
309
+ if (key.includes(PEM_HEADER)) {
310
+ pem = key;
311
+ } else {
312
+ const b64 = key.replace(/\s+/g, "");
313
+ if (!b64 || !/^[A-Za-z0-9+/=]+$/.test(b64)) {
314
+ throw new Error(
315
+ "SendGridProvider: webhookVerificationKey is neither a PEM public key nor a base64 SPKI key. Copy the Verification Key from SendGrid \u2192 Settings \u2192 Mail Settings \u2192 Signed Event Webhook."
316
+ );
317
+ }
318
+ pem = `${PEM_HEADER}
319
+ ${b64.match(/.{1,64}/g).join("\n")}
320
+ ${PEM_FOOTER}
321
+ `;
322
+ }
323
+ try {
324
+ crypto2.createPublicKey(pem);
325
+ } catch (err) {
326
+ throw new Error(
327
+ `SendGridProvider: webhookVerificationKey did not parse as a public key (${String(err?.message ?? err)}). Copy the Verification Key from SendGrid \u2192 Settings \u2192 Mail Settings \u2192 Signed Event Webhook.`
328
+ );
329
+ }
330
+ return pem;
331
+ }
304
332
  function normalizeSendGridEvent(e) {
305
333
  const type = mapEventType(e.event);
306
334
  if (!type) return null;
@@ -339,24 +367,29 @@ function mapEventType(sgEvent) {
339
367
  return null;
340
368
  }
341
369
  }
342
- var SG_SIG_HEADER, SG_TS_HEADER, SendGridProvider;
370
+ var SG_SIG_HEADER, SG_TS_HEADER, PEM_HEADER, PEM_FOOTER, SendGridProvider;
343
371
  var init_sendgrid = __esm({
344
372
  "src/server/providers/sendgrid.ts"() {
345
373
  init_webhook_tolerance();
346
374
  SG_SIG_HEADER = "x-twilio-email-event-webhook-signature";
347
375
  SG_TS_HEADER = "x-twilio-email-event-webhook-timestamp";
376
+ PEM_HEADER = "-----BEGIN PUBLIC KEY-----";
377
+ PEM_FOOTER = "-----END PUBLIC KEY-----";
348
378
  SendGridProvider = class {
349
379
  constructor(opts) {
350
380
  this.opts = opts;
351
381
  sgMail.setApiKey(opts.apiKey);
352
382
  this.sendRatePerSecond = opts.sendRatePerSecond ?? 10;
353
383
  this.webhookToleranceSeconds = resolveWebhookToleranceSeconds(opts.webhookToleranceSeconds);
384
+ this.verificationKeyPem = opts.webhookVerificationKey ? normalizeWebhookVerificationKey(opts.webhookVerificationKey) : null;
354
385
  }
355
386
  opts;
356
387
  name = "sendgrid";
357
388
  sendRatePerSecond;
358
389
  /** Resolved replay window in seconds; `0` means the check is disabled. */
359
390
  webhookToleranceSeconds;
391
+ /** The verification key as PEM, whatever shape it was configured in. */
392
+ verificationKeyPem;
360
393
  async send(args) {
361
394
  const msg = {
362
395
  to: args.to,
@@ -388,7 +421,7 @@ var init_sendgrid = __esm({
388
421
  };
389
422
  }
390
423
  async verifyWebhook(rawBody, headers) {
391
- if (!this.opts.webhookVerificationKey) return false;
424
+ if (!this.verificationKeyPem) return false;
392
425
  const sig = headers[SG_SIG_HEADER];
393
426
  const ts = headers[SG_TS_HEADER];
394
427
  if (!sig || !ts) return false;
@@ -396,7 +429,7 @@ var init_sendgrid = __esm({
396
429
  try {
397
430
  const verifier = crypto2.createVerify("sha256");
398
431
  verifier.update(payload);
399
- if (!verifier.verify(this.opts.webhookVerificationKey, sig, "base64")) return false;
432
+ if (!verifier.verify(this.verificationKeyPem, sig, "base64")) return false;
400
433
  } catch {
401
434
  return false;
402
435
  }