@bymax-one/nest-notification 1.1.2 → 1.2.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/CHANGELOG.md +133 -1
- package/README.md +9 -3
- package/dist/server/index.cjs +460 -61
- package/dist/server/index.d.cts +97 -5
- package/dist/server/index.d.ts +97 -5
- package/dist/server/index.mjs +460 -61
- package/package.json +1 -1
package/dist/server/index.mjs
CHANGED
|
@@ -270,24 +270,55 @@ var FALLBACK_DEFINITION = {
|
|
|
270
270
|
status: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
271
271
|
message: "Unknown notification error"
|
|
272
272
|
};
|
|
273
|
+
var MAX_CAUSE_DEPTH = 5;
|
|
274
|
+
var UNREADABLE_CAUSE_MESSAGE = "Cause unavailable: inspecting it threw";
|
|
275
|
+
function toLogSafeCause(cause, depth = 0) {
|
|
276
|
+
try {
|
|
277
|
+
return copyLogSafeCause(cause, depth);
|
|
278
|
+
} catch {
|
|
279
|
+
return new Error(UNREADABLE_CAUSE_MESSAGE);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
function copyLogSafeCause(cause, depth) {
|
|
283
|
+
if (cause instanceof Error) {
|
|
284
|
+
const safe = new Error(cause.message);
|
|
285
|
+
Object.defineProperty(safe, "name", { value: cause.name, writable: true });
|
|
286
|
+
if (cause.stack !== void 0) {
|
|
287
|
+
safe.stack = cause.stack;
|
|
288
|
+
}
|
|
289
|
+
if (depth < MAX_CAUSE_DEPTH && "cause" in cause) {
|
|
290
|
+
Object.defineProperty(safe, "cause", {
|
|
291
|
+
value: toLogSafeCause(cause.cause, depth + 1),
|
|
292
|
+
writable: true
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
return safe;
|
|
296
|
+
}
|
|
297
|
+
return typeof cause === "object" && cause !== null ? String(cause) : cause;
|
|
298
|
+
}
|
|
273
299
|
var NotificationException = class extends HttpException {
|
|
274
300
|
/**
|
|
275
301
|
* @param key - Catalog key selecting the code, default HTTP status, and message.
|
|
276
302
|
* @param details - Optional structured context placed under `error.details`.
|
|
277
|
-
* @param
|
|
278
|
-
*
|
|
303
|
+
* @param optionsOrStatus - An {@link NotificationExceptionOptions} object, or — kept
|
|
304
|
+
* for backward compatibility — a bare `HttpStatus` acting as `options.status`.
|
|
305
|
+
* @param overrideMessage - Replaces the catalog's default message when supplied;
|
|
306
|
+
* kept for backward compatibility with the positional form. `options.message` wins
|
|
307
|
+
* when both are given.
|
|
279
308
|
*/
|
|
280
|
-
constructor(key, details,
|
|
309
|
+
constructor(key, details, optionsOrStatus, overrideMessage) {
|
|
281
310
|
const definition = NOTIFICATION_ERROR_DEFINITION_MAP.get(key) ?? FALLBACK_DEFINITION;
|
|
311
|
+
const options = typeof optionsOrStatus === "number" ? { status: optionsOrStatus } : optionsOrStatus ?? {};
|
|
282
312
|
const body = {
|
|
283
313
|
error: {
|
|
284
314
|
code: definition.code,
|
|
285
|
-
message: overrideMessage ?? definition.message,
|
|
315
|
+
message: options.message ?? overrideMessage ?? definition.message,
|
|
286
316
|
details: details ?? null
|
|
287
317
|
}
|
|
288
318
|
};
|
|
289
|
-
super(body,
|
|
319
|
+
super(body, options.status ?? definition.status, { cause: toLogSafeCause(options.cause) });
|
|
290
320
|
this.code = definition.code;
|
|
321
|
+
Object.defineProperty(this, "options", { enumerable: false });
|
|
291
322
|
}
|
|
292
323
|
};
|
|
293
324
|
|
|
@@ -508,6 +539,252 @@ var NoOpNotificationLogRepository = class {
|
|
|
508
539
|
NoOpNotificationLogRepository = __decorateClass([
|
|
509
540
|
Injectable()
|
|
510
541
|
], NoOpNotificationLogRepository);
|
|
542
|
+
|
|
543
|
+
// src/server/utils/redact.ts
|
|
544
|
+
var REDACTED_VALUE = "[redacted]";
|
|
545
|
+
function redactValues(text, values) {
|
|
546
|
+
const spans = [];
|
|
547
|
+
for (const value of values) {
|
|
548
|
+
if (value === "") {
|
|
549
|
+
continue;
|
|
550
|
+
}
|
|
551
|
+
for (let start = text.indexOf(value); start !== -1; start = text.indexOf(value, start + 1)) {
|
|
552
|
+
spans.push({ start, end: start + value.length });
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
spans.sort((a, b) => a.start - b.start);
|
|
556
|
+
let cleaned = "";
|
|
557
|
+
let cursor = 0;
|
|
558
|
+
for (const { start, end } of spans) {
|
|
559
|
+
if (start < cursor) {
|
|
560
|
+
cursor = Math.max(cursor, end);
|
|
561
|
+
continue;
|
|
562
|
+
}
|
|
563
|
+
cleaned += text.slice(cursor, start) + REDACTED_VALUE;
|
|
564
|
+
cursor = end;
|
|
565
|
+
}
|
|
566
|
+
return cleaned + text.slice(cursor);
|
|
567
|
+
}
|
|
568
|
+
function coerceRedacted(value, values) {
|
|
569
|
+
try {
|
|
570
|
+
return redactValues(String(value), values);
|
|
571
|
+
} catch {
|
|
572
|
+
return REDACTED_VALUE;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
function readRedactedMessage(error, values) {
|
|
576
|
+
try {
|
|
577
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
578
|
+
return values ? redactValues(message, values) : message;
|
|
579
|
+
} catch {
|
|
580
|
+
return REDACTED_VALUE;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
function collectErrorChainText(error) {
|
|
584
|
+
const parts = [];
|
|
585
|
+
const seen = /* @__PURE__ */ new Set();
|
|
586
|
+
let current = error;
|
|
587
|
+
while (current !== null && current !== void 0 && !seen.has(current)) {
|
|
588
|
+
seen.add(current);
|
|
589
|
+
parts.push(readRedactedMessage(current));
|
|
590
|
+
if (!isSafeError(current)) {
|
|
591
|
+
break;
|
|
592
|
+
}
|
|
593
|
+
try {
|
|
594
|
+
if (typeof current.stack === "string") {
|
|
595
|
+
parts.push(current.stack);
|
|
596
|
+
}
|
|
597
|
+
} catch {
|
|
598
|
+
}
|
|
599
|
+
try {
|
|
600
|
+
current = "cause" in current ? current.cause : void 0;
|
|
601
|
+
} catch {
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
return parts.join("\n");
|
|
605
|
+
}
|
|
606
|
+
var MIN_ECHO_LENGTH = 16;
|
|
607
|
+
function collectEchoedExcerpts(text, reference) {
|
|
608
|
+
const excerpts = [];
|
|
609
|
+
let index = 0;
|
|
610
|
+
while (index + MIN_ECHO_LENGTH <= text.length) {
|
|
611
|
+
const window = text.slice(index, index + MIN_ECHO_LENGTH);
|
|
612
|
+
let longest = 0;
|
|
613
|
+
for (let at = reference.indexOf(window); at !== -1; at = reference.indexOf(window, at + 1)) {
|
|
614
|
+
let length = MIN_ECHO_LENGTH;
|
|
615
|
+
while (text[index + length] !== void 0 && text[index + length] === reference[at + length]) {
|
|
616
|
+
length += 1;
|
|
617
|
+
}
|
|
618
|
+
longest = Math.max(longest, length);
|
|
619
|
+
}
|
|
620
|
+
if (longest === 0) {
|
|
621
|
+
index += 1;
|
|
622
|
+
continue;
|
|
623
|
+
}
|
|
624
|
+
excerpts.push(text.slice(index, index + longest));
|
|
625
|
+
index += longest;
|
|
626
|
+
}
|
|
627
|
+
return excerpts;
|
|
628
|
+
}
|
|
629
|
+
function isSafeError(value) {
|
|
630
|
+
try {
|
|
631
|
+
return value instanceof Error;
|
|
632
|
+
} catch {
|
|
633
|
+
return false;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
function attachCauseIfAbsent(target, cause) {
|
|
637
|
+
try {
|
|
638
|
+
if (target instanceof Error && !("cause" in target)) {
|
|
639
|
+
return Reflect.set(target, "cause", cause) && target.cause === cause;
|
|
640
|
+
}
|
|
641
|
+
return false;
|
|
642
|
+
} catch {
|
|
643
|
+
return false;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
function scrubValuesFromErrorChain(error, values) {
|
|
647
|
+
if (!isSafeError(error)) {
|
|
648
|
+
if (error === void 0 || error === null) {
|
|
649
|
+
return error;
|
|
650
|
+
}
|
|
651
|
+
return coerceRedacted(error, values);
|
|
652
|
+
}
|
|
653
|
+
const seen = /* @__PURE__ */ new WeakMap();
|
|
654
|
+
const chain = [];
|
|
655
|
+
let headRepresentative = error;
|
|
656
|
+
let cursor = error;
|
|
657
|
+
while (!seen.has(cursor)) {
|
|
658
|
+
const link = registerLink(cursor, values, seen);
|
|
659
|
+
chain.push(link);
|
|
660
|
+
if (cursor === error) {
|
|
661
|
+
headRepresentative = link.representative;
|
|
662
|
+
}
|
|
663
|
+
if (!link.hasCause || !isSafeError(link.rawCause)) {
|
|
664
|
+
break;
|
|
665
|
+
}
|
|
666
|
+
cursor = link.rawCause;
|
|
667
|
+
}
|
|
668
|
+
wireCauses(chain, values, seen);
|
|
669
|
+
return headRepresentative;
|
|
670
|
+
}
|
|
671
|
+
function wireCauses(chain, values, seen) {
|
|
672
|
+
for (const link of chain) {
|
|
673
|
+
if (!link.hasCause) {
|
|
674
|
+
continue;
|
|
675
|
+
}
|
|
676
|
+
const raw = link.rawCause;
|
|
677
|
+
let childRepresentative;
|
|
678
|
+
if (isSafeError(raw)) {
|
|
679
|
+
childRepresentative = seen.get(raw);
|
|
680
|
+
} else if (raw === void 0 || raw === null) {
|
|
681
|
+
childRepresentative = raw;
|
|
682
|
+
} else {
|
|
683
|
+
childRepresentative = coerceRedacted(raw, values);
|
|
684
|
+
}
|
|
685
|
+
if (link.representative === link.node) {
|
|
686
|
+
Reflect.set(link.node, "cause", childRepresentative);
|
|
687
|
+
} else {
|
|
688
|
+
Object.defineProperty(link.representative, "cause", {
|
|
689
|
+
value: childRepresentative,
|
|
690
|
+
writable: true
|
|
691
|
+
});
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
function registerLink(node, values, seen) {
|
|
696
|
+
try {
|
|
697
|
+
const name = redactValues(node.name, values);
|
|
698
|
+
const message = redactValues(node.message, values);
|
|
699
|
+
const stack = node.stack === void 0 ? void 0 : redactValues(node.stack, values);
|
|
700
|
+
const hasCause = "cause" in node;
|
|
701
|
+
const rawCause = hasCause ? node.cause : void 0;
|
|
702
|
+
const applied = stripExtraProperties(node, values) && (node.name === name || Reflect.set(node, "name", name)) && Reflect.set(node, "message", message) && (stack === void 0 || Reflect.set(node, "stack", stack)) && (!hasCause || Reflect.set(node, "cause", void 0));
|
|
703
|
+
const inPlace = applied && node.name === name && node.message === message && // When `stack` is undefined the node had none and none was written, so
|
|
704
|
+
// the single read-back comparison covers both shapes.
|
|
705
|
+
node.stack === stack && (!hasCause || node.cause === void 0);
|
|
706
|
+
const representative = inPlace ? node : buildRedactedCopy(name, message, stack);
|
|
707
|
+
seen.set(node, representative);
|
|
708
|
+
return { node, representative, hasCause, rawCause };
|
|
709
|
+
} catch {
|
|
710
|
+
const representative = buildRedactedCopy("Error", REDACTED_VALUE, void 0);
|
|
711
|
+
seen.set(node, representative);
|
|
712
|
+
return { node, representative, hasCause: false, rawCause: void 0 };
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
function cloneRedacted(root, values) {
|
|
716
|
+
const seen = /* @__PURE__ */ new WeakMap();
|
|
717
|
+
const pending = [];
|
|
718
|
+
const cloneValue = (value) => {
|
|
719
|
+
if (typeof value === "string") {
|
|
720
|
+
return redactValues(value, values);
|
|
721
|
+
}
|
|
722
|
+
if (typeof value === "number" || typeof value === "bigint") {
|
|
723
|
+
return redactValues(String(value), values) === String(value) ? value : REDACTED_VALUE;
|
|
724
|
+
}
|
|
725
|
+
if (typeof value !== "object" || value === null) {
|
|
726
|
+
return value;
|
|
727
|
+
}
|
|
728
|
+
const existing = seen.get(value);
|
|
729
|
+
if (existing !== void 0) {
|
|
730
|
+
return existing;
|
|
731
|
+
}
|
|
732
|
+
const target = Array.isArray(value) ? [] : {};
|
|
733
|
+
seen.set(value, target);
|
|
734
|
+
pending.push({ source: value, target });
|
|
735
|
+
return target;
|
|
736
|
+
};
|
|
737
|
+
const result = cloneValue(root);
|
|
738
|
+
for (let entry = pending.pop(); entry !== void 0; entry = pending.pop()) {
|
|
739
|
+
for (const [key, descriptor] of collectOwnEntries(entry.source)) {
|
|
740
|
+
Object.defineProperty(entry.target, redactValues(key, values), {
|
|
741
|
+
value: "value" in descriptor ? cloneValue(descriptor.value) : REDACTED_VALUE,
|
|
742
|
+
enumerable: true,
|
|
743
|
+
configurable: true
|
|
744
|
+
});
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
return result;
|
|
748
|
+
}
|
|
749
|
+
function collectOwnEntries(source) {
|
|
750
|
+
try {
|
|
751
|
+
return Object.entries(Object.getOwnPropertyDescriptors(source)).filter(
|
|
752
|
+
([, descriptor]) => descriptor.enumerable === true
|
|
753
|
+
);
|
|
754
|
+
} catch {
|
|
755
|
+
return [];
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
var STANDARD_ERROR_KEYS = /* @__PURE__ */ new Set(["name", "message", "stack", "cause"]);
|
|
759
|
+
var EXCEPTION_CONTRACT_KEYS = /* @__PURE__ */ new Set(["response", "status", "options", "code"]);
|
|
760
|
+
function deleteExtraOwnKeys(node, contractKeys) {
|
|
761
|
+
return Reflect.ownKeys(node).every(
|
|
762
|
+
(key) => STANDARD_ERROR_KEYS.has(key) || contractKeys.has(key) || Reflect.deleteProperty(node, key)
|
|
763
|
+
);
|
|
764
|
+
}
|
|
765
|
+
var NO_CONTRACT_KEYS = /* @__PURE__ */ new Set();
|
|
766
|
+
function stripExtraProperties(node, values) {
|
|
767
|
+
try {
|
|
768
|
+
if (node instanceof NotificationException) {
|
|
769
|
+
const redactedResponse = cloneRedacted(node.getResponse(), values);
|
|
770
|
+
const rebuiltOptions = {};
|
|
771
|
+
return Reflect.set(node, "response", redactedResponse) && node.getResponse() === redactedResponse && Reflect.set(node, "options", rebuiltOptions) && Reflect.get(node, "options") === rebuiltOptions && deleteExtraOwnKeys(node, EXCEPTION_CONTRACT_KEYS);
|
|
772
|
+
}
|
|
773
|
+
return deleteExtraOwnKeys(node, NO_CONTRACT_KEYS);
|
|
774
|
+
} catch {
|
|
775
|
+
return false;
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
function buildRedactedCopy(name, message, stack) {
|
|
779
|
+
const copy = new Error(message);
|
|
780
|
+
Object.defineProperty(copy, "name", { value: name, writable: true });
|
|
781
|
+
if (stack !== void 0) {
|
|
782
|
+
copy.stack = stack;
|
|
783
|
+
}
|
|
784
|
+
return copy;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
// src/server/services/email.service.ts
|
|
511
788
|
var FALLBACK_LOCALE = "en";
|
|
512
789
|
var EmailService = class {
|
|
513
790
|
/**
|
|
@@ -555,12 +832,17 @@ var EmailService = class {
|
|
|
555
832
|
if (error instanceof NotificationException) {
|
|
556
833
|
throw error;
|
|
557
834
|
}
|
|
835
|
+
const secretValues = this.gatherRedactionValues(error, input.auditRedactValues, sendOptions);
|
|
558
836
|
await this.audit(
|
|
559
837
|
this.auditEntry("failed", input.tenantId, recipient, input.userId, {
|
|
560
|
-
errorMessage: error
|
|
838
|
+
errorMessage: readRedactedMessage(error, secretValues)
|
|
561
839
|
})
|
|
562
840
|
);
|
|
563
|
-
throw new NotificationException(
|
|
841
|
+
throw new NotificationException(
|
|
842
|
+
"EMAIL_SEND_FAILED",
|
|
843
|
+
{ providerName: this.provider.name },
|
|
844
|
+
{ cause: secretValues.length > 0 ? scrubValuesFromErrorChain(error, secretValues) : error }
|
|
845
|
+
);
|
|
564
846
|
}
|
|
565
847
|
}
|
|
566
848
|
/**
|
|
@@ -574,7 +856,12 @@ var EmailService = class {
|
|
|
574
856
|
async sendTemplate(input) {
|
|
575
857
|
const requestedLocale = input.locale ?? this.options.global.defaultLocale;
|
|
576
858
|
const locale = await this.resolveTemplateLocale(input.template, requestedLocale);
|
|
577
|
-
const rendered = await this.renderTemplate(
|
|
859
|
+
const rendered = await this.renderTemplate(
|
|
860
|
+
input.template,
|
|
861
|
+
input.data,
|
|
862
|
+
locale,
|
|
863
|
+
input.auditRedactValues
|
|
864
|
+
);
|
|
578
865
|
const tags = [...input.tags ?? [], { name: "template", value: input.template }];
|
|
579
866
|
return this.send({
|
|
580
867
|
tenantId: input.tenantId,
|
|
@@ -586,7 +873,8 @@ var EmailService = class {
|
|
|
586
873
|
...input.from !== void 0 ? { from: input.from } : {},
|
|
587
874
|
...input.fromName !== void 0 ? { fromName: input.fromName } : {},
|
|
588
875
|
...input.replyTo !== void 0 ? { replyTo: input.replyTo } : {},
|
|
589
|
-
...input.userId !== void 0 ? { userId: input.userId } : {}
|
|
876
|
+
...input.userId !== void 0 ? { userId: input.userId } : {},
|
|
877
|
+
...input.auditRedactValues !== void 0 ? { auditRedactValues: input.auditRedactValues } : {}
|
|
590
878
|
});
|
|
591
879
|
}
|
|
592
880
|
/** Returns the resolved email options or throws when the channel is not configured. */
|
|
@@ -630,7 +918,8 @@ var EmailService = class {
|
|
|
630
918
|
...input.text !== void 0 ? { text: input.text } : {},
|
|
631
919
|
...input.cc !== void 0 ? { cc: input.cc } : {},
|
|
632
920
|
...input.bcc !== void 0 ? { bcc: input.bcc } : {},
|
|
633
|
-
...input.attachments !== void 0 ? { attachments: input.attachments } : {}
|
|
921
|
+
...input.attachments !== void 0 ? { attachments: input.attachments } : {},
|
|
922
|
+
...input.auditRedactValues !== void 0 ? { redactValues: input.auditRedactValues } : {}
|
|
634
923
|
};
|
|
635
924
|
}
|
|
636
925
|
/** Picks the locale that has a template, falling back to `en`, else throws `TEMPLATE_NOT_FOUND`. */
|
|
@@ -644,11 +933,17 @@ var EmailService = class {
|
|
|
644
933
|
throw new NotificationException("TEMPLATE_NOT_FOUND", { template, locale });
|
|
645
934
|
}
|
|
646
935
|
/** Renders the template, mapping a renderer failure to `TEMPLATE_RENDER_FAILED`. */
|
|
647
|
-
async renderTemplate(template, data, locale) {
|
|
936
|
+
async renderTemplate(template, data, locale, redactValues2) {
|
|
648
937
|
try {
|
|
649
938
|
return await this.renderer.render(template, data, locale);
|
|
650
|
-
} catch {
|
|
651
|
-
throw new NotificationException(
|
|
939
|
+
} catch (error) {
|
|
940
|
+
throw new NotificationException(
|
|
941
|
+
"TEMPLATE_RENDER_FAILED",
|
|
942
|
+
{ template },
|
|
943
|
+
{
|
|
944
|
+
cause: redactValues2 ? scrubValuesFromErrorChain(error, redactValues2) : error
|
|
945
|
+
}
|
|
946
|
+
);
|
|
652
947
|
}
|
|
653
948
|
}
|
|
654
949
|
/** Builds an audit entry with the recipient already masked. */
|
|
@@ -671,9 +966,7 @@ var EmailService = class {
|
|
|
671
966
|
await this.auditLog.create(entry);
|
|
672
967
|
} catch (error) {
|
|
673
968
|
if (!this.options.audit.swallowErrors) {
|
|
674
|
-
throw new NotificationException("AUDIT_LOG_FAILED", {
|
|
675
|
-
cause: error instanceof Error ? error.message : String(error)
|
|
676
|
-
});
|
|
969
|
+
throw new NotificationException("AUDIT_LOG_FAILED", void 0, { cause: error });
|
|
677
970
|
}
|
|
678
971
|
}
|
|
679
972
|
}
|
|
@@ -682,6 +975,26 @@ var EmailService = class {
|
|
|
682
975
|
const mask = this.options.audit.maskRecipient;
|
|
683
976
|
return Array.isArray(to) ? to.map((recipient) => mask(recipient)).join(", ") : mask(to);
|
|
684
977
|
}
|
|
978
|
+
/**
|
|
979
|
+
* Assembles every value to scrub from a provider failure: the caller's
|
|
980
|
+
* declared secrets plus any excerpt of the rendered body the error is
|
|
981
|
+
* ECHOING — a policy/DLP relay that quotes the rejected content puts the
|
|
982
|
+
* body (and any secret inside it) into the error without the caller having
|
|
983
|
+
* declared anything. Echo detection starts from the collected excerpts so
|
|
984
|
+
* that with nothing declared and nothing echoed the list is verifiably
|
|
985
|
+
* empty and the raw error passes through untouched.
|
|
986
|
+
*/
|
|
987
|
+
gatherRedactionValues(error, declared, sendOptions) {
|
|
988
|
+
const errorText = collectErrorChainText(error);
|
|
989
|
+
const values = collectEchoedExcerpts(errorText, sendOptions.html);
|
|
990
|
+
if (sendOptions.text !== void 0) {
|
|
991
|
+
values.push(...collectEchoedExcerpts(errorText, sendOptions.text));
|
|
992
|
+
}
|
|
993
|
+
if (declared) {
|
|
994
|
+
values.push(...declared);
|
|
995
|
+
}
|
|
996
|
+
return values;
|
|
997
|
+
}
|
|
685
998
|
};
|
|
686
999
|
EmailService = __decorateClass([
|
|
687
1000
|
Injectable(),
|
|
@@ -920,13 +1233,22 @@ var OtpService = class {
|
|
|
920
1233
|
});
|
|
921
1234
|
await this.deliverOtp(input, code, cfg);
|
|
922
1235
|
} catch (error) {
|
|
923
|
-
|
|
1236
|
+
let failure = error;
|
|
1237
|
+
try {
|
|
1238
|
+
await this.releaseOtp(input);
|
|
1239
|
+
} catch (cleanupError) {
|
|
1240
|
+
attachCauseIfAbsent(cleanupError, error);
|
|
1241
|
+
failure = cleanupError;
|
|
1242
|
+
}
|
|
1243
|
+
const outgoing = isSafeError(failure) ? scrubValuesFromErrorChain(failure, [code]) : coerceRedacted(failure, [code]);
|
|
924
1244
|
await this.audit(
|
|
925
1245
|
this.otpAuditEntry("failed", input, {
|
|
926
|
-
|
|
1246
|
+
// `outgoing` is already fully redacted by the scrub above; this read
|
|
1247
|
+
// only needs the fail-closed guard, not a second redaction pass.
|
|
1248
|
+
errorMessage: readRedactedMessage(outgoing)
|
|
927
1249
|
})
|
|
928
1250
|
);
|
|
929
|
-
throw
|
|
1251
|
+
throw outgoing;
|
|
930
1252
|
}
|
|
931
1253
|
}
|
|
932
1254
|
/** Delivers the code by email when requested; throws on failure (release is handled by the caller). */
|
|
@@ -953,6 +1275,9 @@ var OtpService = class {
|
|
|
953
1275
|
to: input.recipient,
|
|
954
1276
|
template: input.emailTemplate ?? DEFAULT_OTP_TEMPLATE,
|
|
955
1277
|
data: { ...input.emailData, code, expiresInMinutes, purpose: input.purpose },
|
|
1278
|
+
// The provider receives the code inside the rendered body and may echo it
|
|
1279
|
+
// in an error; EmailService must redact it from ITS OWN failed-audit entry.
|
|
1280
|
+
auditRedactValues: [code],
|
|
956
1281
|
...input.locale !== void 0 ? { locale: input.locale } : {},
|
|
957
1282
|
...input.userId !== void 0 ? { userId: input.userId } : {}
|
|
958
1283
|
};
|
|
@@ -997,9 +1322,7 @@ var OtpService = class {
|
|
|
997
1322
|
await this.auditLog.create(entry);
|
|
998
1323
|
} catch (error) {
|
|
999
1324
|
if (!this.options.audit.swallowErrors) {
|
|
1000
|
-
throw new NotificationException("AUDIT_LOG_FAILED", {
|
|
1001
|
-
cause: error instanceof Error ? error.message : String(error)
|
|
1002
|
-
});
|
|
1325
|
+
throw new NotificationException("AUDIT_LOG_FAILED", void 0, { cause: error });
|
|
1003
1326
|
}
|
|
1004
1327
|
}
|
|
1005
1328
|
}
|
|
@@ -1435,7 +1758,7 @@ var NotificationAuditInterceptor = class {
|
|
|
1435
1758
|
await this.auditLog.create(this.buildEntry(input, tenantId, verb, errorMessage));
|
|
1436
1759
|
} catch (error) {
|
|
1437
1760
|
if (!this.options.audit.swallowErrors) {
|
|
1438
|
-
throw new NotificationException("AUDIT_LOG_FAILED", { cause:
|
|
1761
|
+
throw new NotificationException("AUDIT_LOG_FAILED", void 0, { cause: error });
|
|
1439
1762
|
}
|
|
1440
1763
|
}
|
|
1441
1764
|
}
|
|
@@ -1661,28 +1984,66 @@ var ResendEmailProvider = class {
|
|
|
1661
1984
|
async send(options) {
|
|
1662
1985
|
const client = await this.getClient();
|
|
1663
1986
|
const from2 = this.formatFrom(options.from, options.fromName);
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1987
|
+
let result;
|
|
1988
|
+
try {
|
|
1989
|
+
result = await client.emails.send({
|
|
1990
|
+
from: from2,
|
|
1991
|
+
to: options.to,
|
|
1992
|
+
subject: options.subject,
|
|
1993
|
+
html: options.html,
|
|
1994
|
+
text: options.text,
|
|
1995
|
+
replyTo: options.replyTo,
|
|
1996
|
+
cc: options.cc,
|
|
1997
|
+
bcc: options.bcc,
|
|
1998
|
+
tags: options.tags,
|
|
1999
|
+
headers: options.headers,
|
|
2000
|
+
attachments: options.attachments
|
|
2001
|
+
});
|
|
2002
|
+
} catch (error) {
|
|
2003
|
+
throw this.sendFailure(error instanceof Error ? error.message : String(error), options);
|
|
2004
|
+
}
|
|
1677
2005
|
if (result.error) {
|
|
1678
|
-
this.
|
|
1679
|
-
throw new Error(`Resend send failed: ${result.error.message}`);
|
|
2006
|
+
throw this.sendFailure(result.error.message, options);
|
|
1680
2007
|
}
|
|
1681
2008
|
if (!result.data?.id) {
|
|
1682
2009
|
throw new Error("Resend returned no message ID");
|
|
1683
2010
|
}
|
|
1684
2011
|
return { messageId: result.data.id };
|
|
1685
2012
|
}
|
|
2013
|
+
/**
|
|
2014
|
+
* Builds the failure for a rejected send, logging the scrubbed reason on the
|
|
2015
|
+
* way out — the single exit shared by the rejected-promise and `{ error }`
|
|
2016
|
+
* result shapes, so neither can surface an unscrubbed message.
|
|
2017
|
+
*
|
|
2018
|
+
* @param rawMessage - The provider's own error text, unscrubbed.
|
|
2019
|
+
* @param options - The send input whose body and declared values to scrub.
|
|
2020
|
+
* @returns The `Error` for the caller to throw.
|
|
2021
|
+
*/
|
|
2022
|
+
sendFailure(rawMessage, options) {
|
|
2023
|
+
const reason = this.scrubSendError(rawMessage, options);
|
|
2024
|
+
this.logger.warn(`[RESEND_SEND_FAILED] ${reason}`);
|
|
2025
|
+
return new Error(`Resend send failed: ${reason}`);
|
|
2026
|
+
}
|
|
2027
|
+
/**
|
|
2028
|
+
* Removes the API key, the caller's declared secrets, and any echoed body
|
|
2029
|
+
* excerpt from an SDK error message — the provider cannot know which part of
|
|
2030
|
+
* the body is secret, so a detected echo is scrubbed wholesale.
|
|
2031
|
+
*
|
|
2032
|
+
* @param message - The raw SDK error message.
|
|
2033
|
+
* @param options - The send input whose body and declared values to scrub.
|
|
2034
|
+
* @returns The message with every known-secret value replaced.
|
|
2035
|
+
*/
|
|
2036
|
+
scrubSendError(message, options) {
|
|
2037
|
+
const values = collectEchoedExcerpts(message, options.html);
|
|
2038
|
+
if (options.text !== void 0) {
|
|
2039
|
+
values.push(...collectEchoedExcerpts(message, options.text));
|
|
2040
|
+
}
|
|
2041
|
+
if (options.redactValues) {
|
|
2042
|
+
values.push(...options.redactValues);
|
|
2043
|
+
}
|
|
2044
|
+
values.push(...[__privateGet(this, _options).apiKey].filter((secret) => Boolean(secret)));
|
|
2045
|
+
return redactValues(message, values);
|
|
2046
|
+
}
|
|
1686
2047
|
/**
|
|
1687
2048
|
* Formats the RFC-5322 `from` header. The `Name <address>` display-name form is
|
|
1688
2049
|
* only used when a non-empty address exists — otherwise it would emit the literal
|
|
@@ -1754,7 +2115,6 @@ var IMPLICIT_TLS_PORT = 465;
|
|
|
1754
2115
|
var DEFAULT_CONNECTION_TIMEOUT_MS = 1e4;
|
|
1755
2116
|
var DEFAULT_GREETING_TIMEOUT_MS = 1e4;
|
|
1756
2117
|
var DEFAULT_SOCKET_TIMEOUT_MS = 2e4;
|
|
1757
|
-
var REDACTED = "[redacted]";
|
|
1758
2118
|
var LOOPBACK_HOSTS = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "::1", "[::1]"]);
|
|
1759
2119
|
function areCredentialsComplete(credentials) {
|
|
1760
2120
|
return Boolean(credentials.user) && Boolean(credentials.pass);
|
|
@@ -1827,18 +2187,22 @@ var SmtpEmailProvider = class {
|
|
|
1827
2187
|
this.guardHeaderInjection(options);
|
|
1828
2188
|
const from2 = this.buildSender(options.from, options.fromName);
|
|
1829
2189
|
const transport = await this.getTransport();
|
|
1830
|
-
const info = await this.dispatch(
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
2190
|
+
const info = await this.dispatch(
|
|
2191
|
+
transport,
|
|
2192
|
+
{
|
|
2193
|
+
from: from2,
|
|
2194
|
+
to: options.to,
|
|
2195
|
+
subject: options.subject,
|
|
2196
|
+
html: options.html,
|
|
2197
|
+
text: options.text,
|
|
2198
|
+
replyTo: options.replyTo,
|
|
2199
|
+
cc: options.cc,
|
|
2200
|
+
bcc: options.bcc,
|
|
2201
|
+
headers: options.headers,
|
|
2202
|
+
attachments: this.mapAttachments(options.attachments)
|
|
2203
|
+
},
|
|
2204
|
+
options.redactValues
|
|
2205
|
+
);
|
|
1842
2206
|
if (!info.messageId) {
|
|
1843
2207
|
throw new Error("SMTP transport returned no message ID");
|
|
1844
2208
|
}
|
|
@@ -1854,15 +2218,53 @@ var SmtpEmailProvider = class {
|
|
|
1854
2218
|
* @returns The transport's send info.
|
|
1855
2219
|
* @throws Error When the transport rejects the message.
|
|
1856
2220
|
*/
|
|
1857
|
-
async dispatch(transport, payload) {
|
|
2221
|
+
async dispatch(transport, payload, redactValues2) {
|
|
1858
2222
|
try {
|
|
1859
2223
|
return await transport.sendMail(payload);
|
|
1860
2224
|
} catch (error) {
|
|
1861
|
-
const reason = this.
|
|
2225
|
+
const reason = this.scrubTransportError(
|
|
2226
|
+
error instanceof Error ? error.message : String(error),
|
|
2227
|
+
payload,
|
|
2228
|
+
redactValues2
|
|
2229
|
+
);
|
|
1862
2230
|
this.logger.warn(`[SMTP_SEND_FAILED] ${reason}`);
|
|
1863
2231
|
throw new Error(`SMTP send failed: ${reason}`);
|
|
1864
2232
|
}
|
|
1865
2233
|
}
|
|
2234
|
+
/**
|
|
2235
|
+
* Removes the credentials, the caller's declared secrets and any echoed body
|
|
2236
|
+
* excerpt from a transport error's text — the credential scrub alone cannot
|
|
2237
|
+
* cover content the provider does not know is secret.
|
|
2238
|
+
*
|
|
2239
|
+
* Echo detection runs against the RAW message and every value is replaced in
|
|
2240
|
+
* a single pass. Redacting credentials first would break a contiguous body
|
|
2241
|
+
* run that happens to quote one, dropping the surviving halves below the
|
|
2242
|
+
* detection window and leaving the rest of that body — and any secret inside
|
|
2243
|
+
* it — in the log line.
|
|
2244
|
+
*
|
|
2245
|
+
* @param message - The raw transport error message.
|
|
2246
|
+
* @param payload - The message that was sent, whose body is the echo reference.
|
|
2247
|
+
* @param declaredValues - The caller's declared secrets, if any.
|
|
2248
|
+
* @returns The message with every known-secret value replaced.
|
|
2249
|
+
*/
|
|
2250
|
+
scrubTransportError(message, payload, declaredValues) {
|
|
2251
|
+
const values = collectEchoedExcerpts(message, payload.html);
|
|
2252
|
+
if (payload.text !== void 0) {
|
|
2253
|
+
values.push(...collectEchoedExcerpts(message, payload.text));
|
|
2254
|
+
}
|
|
2255
|
+
if (declaredValues) {
|
|
2256
|
+
values.push(...declaredValues);
|
|
2257
|
+
}
|
|
2258
|
+
values.push(...this.credentialValues());
|
|
2259
|
+
return redactValues(message, values);
|
|
2260
|
+
}
|
|
2261
|
+
/** The configured credentials that qualify as secret material, if any. */
|
|
2262
|
+
credentialValues() {
|
|
2263
|
+
const credentials = __privateGet(this, _options2).credentials;
|
|
2264
|
+
return [credentials?.user, credentials?.pass].filter(
|
|
2265
|
+
(secret) => Boolean(secret)
|
|
2266
|
+
);
|
|
2267
|
+
}
|
|
1866
2268
|
/**
|
|
1867
2269
|
* Replaces every occurrence of either configured credential with a placeholder.
|
|
1868
2270
|
*
|
|
@@ -1875,19 +2277,16 @@ var SmtpEmailProvider = class {
|
|
|
1875
2277
|
* public login: an SES SMTP username, for one, is itself generated secret
|
|
1876
2278
|
* material. Matching is literal and unconditional, so a short credential
|
|
1877
2279
|
* over-redacts a message rather than risking a miss — the safe direction for a
|
|
1878
|
-
* control whose other failure mode is persisting a secret.
|
|
2280
|
+
* control whose other failure mode is persisting a secret. The two credentials
|
|
2281
|
+
* can overlap — a password built from the username, say `relay` /
|
|
2282
|
+
* `relay-secret` — which {@link redactValues} handles by merging overlapping
|
|
2283
|
+
* matches into a single marker.
|
|
1879
2284
|
*
|
|
1880
2285
|
* @param message - The raw error message.
|
|
1881
2286
|
* @returns The message with both credentials removed, unchanged when none is set.
|
|
1882
2287
|
*/
|
|
1883
2288
|
redact(message) {
|
|
1884
|
-
|
|
1885
|
-
const secrets = [credentials?.user, credentials?.pass].filter((secret) => Boolean(secret)).sort((a, b) => b.length - a.length);
|
|
1886
|
-
let redacted = message;
|
|
1887
|
-
for (const secret of secrets) {
|
|
1888
|
-
redacted = redacted.split(secret).join(REDACTED);
|
|
1889
|
-
}
|
|
1890
|
-
return redacted;
|
|
2289
|
+
return redactValues(message, this.credentialValues());
|
|
1891
2290
|
}
|
|
1892
2291
|
/**
|
|
1893
2292
|
* Rejects a line break in any caller-supplied value that becomes a message header.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bymax-one/nest-notification",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "Multi-channel notification library for NestJS — email, OTP, SMS, push — with pluggable providers and storage, multi-tenant by design, zero runtime dependencies.",
|
|
5
5
|
"author": "Bymax One <support@bymax.one>",
|
|
6
6
|
"license": "MIT",
|