@aglyn/tenant-data-admin 1.0.0-beta.200 → 1.0.0-beta.202
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/package.json +8 -7
- package/src/lib/server/card-payment-velocity.d.ts +85 -0
- package/src/lib/server/card-payment-velocity.js +202 -0
- package/src/lib/server/card-payment-velocity.js.map +1 -0
- package/src/lib/server/configured-url-fetch.d.ts +42 -0
- package/src/lib/server/configured-url-fetch.js +118 -0
- package/src/lib/server/configured-url-fetch.js.map +1 -0
- package/src/lib/server/dns-probe.d.ts +7 -0
- package/src/lib/server/dns-probe.js +52 -0
- package/src/lib/server/dns-probe.js.map +1 -1
- package/src/lib/server/email-deliverability.d.ts +150 -0
- package/src/lib/server/email-deliverability.js +538 -0
- package/src/lib/server/email-deliverability.js.map +1 -0
- package/src/lib/server/email-sender-reputation.d.ts +2 -11
- package/src/lib/server/email-sender-reputation.js +8 -25
- package/src/lib/server/email-sender-reputation.js.map +1 -1
- package/src/lib/server/email-suppression.d.ts +10 -1
- package/src/lib/server/email-suppression.js +4 -3
- package/src/lib/server/email-suppression.js.map +1 -1
- package/src/lib/server/erase-person.js +31 -3
- package/src/lib/server/erase-person.js.map +1 -1
- package/src/lib/server/host-visitor-records.d.ts +16 -2
- package/src/lib/server/host-visitor-records.js +31 -6
- package/src/lib/server/host-visitor-records.js.map +1 -1
- package/src/lib/server/hosted-page-review.d.ts +112 -0
- package/src/lib/server/hosted-page-review.js +378 -0
- package/src/lib/server/hosted-page-review.js.map +1 -0
- package/src/lib/server/member-email-aliases.js +18 -7
- package/src/lib/server/member-email-aliases.js.map +1 -1
- package/src/lib/server/notifications.d.ts +8 -0
- package/src/lib/server/notifications.js +92 -17
- package/src/lib/server/notifications.js.map +1 -1
- package/src/lib/server/org-age.d.ts +27 -0
- package/src/lib/server/org-age.js +44 -0
- package/src/lib/server/org-age.js.map +1 -0
- package/src/lib/server/org-member-notice.js +20 -5
- package/src/lib/server/org-member-notice.js.map +1 -1
- package/src/lib/server/organizations.d.ts +10 -1
- package/src/lib/server/organizations.js +12 -4
- package/src/lib/server/organizations.js.map +1 -1
- package/src/lib/server/outbound-send-review.d.ts +224 -0
- package/src/lib/server/outbound-send-review.js +479 -0
- package/src/lib/server/outbound-send-review.js.map +1 -0
- package/src/lib/server/payment-fraud-signal.d.ts +156 -0
- package/src/lib/server/payment-fraud-signal.js +368 -0
- package/src/lib/server/payment-fraud-signal.js.map +1 -0
- package/src/lib/server/payment-risk-record.d.ts +59 -0
- package/src/lib/server/payment-risk-record.js +76 -0
- package/src/lib/server/payment-risk-record.js.map +1 -0
- package/src/lib/server/render-system-email.d.ts +176 -0
- package/src/lib/server/render-system-email.js +315 -0
- package/src/lib/server/render-system-email.js.map +1 -0
- package/src/lib/server/sending-domains.d.ts +6 -0
- package/src/lib/server/sending-domains.js +98 -6
- package/src/lib/server/sending-domains.js.map +1 -1
- package/src/lib/server/staff-alert-email.d.ts +2 -1
- package/src/lib/server/staff-alert-email.js +16 -5
- package/src/lib/server/staff-alert-email.js.map +1 -1
- package/src/lib/server/stripe-card-authentication.d.ts +35 -0
- package/src/lib/server/stripe-card-authentication.js +63 -0
- package/src/lib/server/stripe-card-authentication.js.map +1 -0
- package/src/lib/server/tenant-write-lockdown.d.ts +32 -2
- package/src/lib/server/tenant-write-lockdown.js +33 -3
- package/src/lib/server/tenant-write-lockdown.js.map +1 -1
- package/src/tenancy.d.ts +5 -0
- package/src/tenancy.js +10 -0
- package/src/tenancy.js.map +1 -1
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
2
|
+
/**
|
|
3
|
+
* @license
|
|
4
|
+
* Copyright 2026 Aglyn LLC
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/ /*==========================================
|
|
18
|
+
* HELD FOR REVIEW: outbound mail from a young workspace (AGL-3356).
|
|
19
|
+
*
|
|
20
|
+
* The phishing screen (`screenOutboundEmail`, shared-util-email) decides
|
|
21
|
+
* whether a message carries a phishing signal, and `signalsThatHold` beside
|
|
22
|
+
* it decides which signals hold for whom. This module is what happens next,
|
|
23
|
+
* for every surface that screens — the send seam every tenant message
|
|
24
|
+
* crosses ({@link installOutboundScreenGate}), the campaign core and the
|
|
25
|
+
* automation step before they commit, and a site publish
|
|
26
|
+
* (`hosted-page-review.ts`) — so none restates it:
|
|
27
|
+
*
|
|
28
|
+
* - WHO IS SCREENED. Every workspace, in two tiers. A lookalike link holds
|
|
29
|
+
* whatever the workspace's age. The soft rules (a brand in the sender
|
|
30
|
+
* name, the three-part lure) hold only for a workspace younger than
|
|
31
|
+
* {@link OUTBOUND_REVIEW_YOUNG_DAYS}: the incident's workspace was days
|
|
32
|
+
* old, and an established customer's clumsy copy never waits on them. An
|
|
33
|
+
* org whose creation date cannot be read is an existing customer (the same
|
|
34
|
+
* reading `orgAgeDays` gives the sending ramp) and is not young.
|
|
35
|
+
* - WHERE A HOLD GOES. Into the abuse queue at `/admin/abuse-reports` — the
|
|
36
|
+
* staff surface that already exists to answer "is this phishing", with its
|
|
37
|
+
* audit trail, its urgent count and its staff notification — as a
|
|
38
|
+
* `phishing` row whose `heldSend` names the message. Not a new queue: a
|
|
39
|
+
* second place to look is a place nobody looks.
|
|
40
|
+
* - HOW STAFF DECIDE. By the row's existing status. `dismissed` (a false
|
|
41
|
+
* positive) RELEASES the send; `actioned` REJECTS it. The admin route
|
|
42
|
+
* calls {@link decideHeldOutboundSend} in the same act as the status
|
|
43
|
+
* write, so the decision and its audit row cannot come apart.
|
|
44
|
+
* - WHAT A RELEASE COVERS. Exactly the content that was held: the review
|
|
45
|
+
* row's id is derived from the message's source and a hash of its
|
|
46
|
+
* content, so an edited message is screened afresh rather than riding a
|
|
47
|
+
* release granted to different words.
|
|
48
|
+
*
|
|
49
|
+
* A hold is never a drop. A held campaign is parked as a scheduled send that
|
|
50
|
+
* a release puts back on the clock; a held automation step fails that one
|
|
51
|
+
* run with a reason the merchant can read, or — after a wait — stays queued
|
|
52
|
+
* until the decision.
|
|
53
|
+
*=========================================*/ import { createHash } from "crypto";
|
|
54
|
+
import { ABUSE_REPORT_COLLECTION } from "@aglyn/aglyn/app-utils/abuse-report";
|
|
55
|
+
import { TENANT_APEX } from "@aglyn/aglyn/app-utils/host-naming";
|
|
56
|
+
import { describePhishingScreenSignals, isYoungWorkspaceAge, OUTBOUND_REVIEW_YOUNG_DAYS, screenOutboundEmail, signalsThatHold } from "@aglyn/shared-util-email/outbound-phishing-screen";
|
|
57
|
+
import { setOutboundScreenGate } from "@aglyn/shared-util-email/outbound-screen-gate";
|
|
58
|
+
import { FieldValue } from "firebase-admin/firestore";
|
|
59
|
+
import { orgAgeDays } from "./org-age.js";
|
|
60
|
+
import firebaseAdmin from "./firebase-admin.js";
|
|
61
|
+
import { notifyStaff } from "./notifications.js";
|
|
62
|
+
/** A workspace younger than this many days has the soft rules applied. */ export { OUTBOUND_REVIEW_YOUNG_DAYS };
|
|
63
|
+
/**
|
|
64
|
+
* Where a held campaign is parked: a send time no processor run reaches.
|
|
65
|
+
* A release replaces it with "now"; nothing else ever does, so a hold that
|
|
66
|
+
* nobody decides stays a hold rather than lapsing into a send.
|
|
67
|
+
*/ export const HELD_SEND_AT_MS = 253402300799000;
|
|
68
|
+
/**
|
|
69
|
+
* Is this workspace young enough to screen?
|
|
70
|
+
*
|
|
71
|
+
* Age alone, deliberately. Payment history was the other candidate and the
|
|
72
|
+
* incident is the argument against it: the actor PAID, on a card that never
|
|
73
|
+
* met 3-D Secure, within days of signing up. A paid invoice proves a card
|
|
74
|
+
* worked, not that its holder is who they say.
|
|
75
|
+
*/ export function isYoungWorkspace(org, nowMs = Date.now()) {
|
|
76
|
+
return isYoungWorkspaceAge(orgAgeDays(org == null ? void 0 : org.createdAt, nowMs));
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The workspace a host's mail belongs to, as the send seam's screen reads it
|
|
80
|
+
* — stamped on the sending identity by `hostSendingIdentity` from the host
|
|
81
|
+
* and org documents it has already read.
|
|
82
|
+
*/ export function sendingWorkspaceFor(input) {
|
|
83
|
+
var _input_nowMs, _identity_ownNames, _identity_ownDomains;
|
|
84
|
+
var _input_org;
|
|
85
|
+
const identity = workspaceScreenIdentity({
|
|
86
|
+
org: input.org,
|
|
87
|
+
host: input.host
|
|
88
|
+
});
|
|
89
|
+
return {
|
|
90
|
+
hostId: input.hostId,
|
|
91
|
+
orgId: input.orgId,
|
|
92
|
+
ageDays: orgAgeDays((_input_org = input.org) == null ? void 0 : _input_org.createdAt, (_input_nowMs = input.nowMs) != null ? _input_nowMs : Date.now()),
|
|
93
|
+
ownNames: ((_identity_ownNames = identity.ownNames) != null ? _identity_ownNames : []).filter((name)=>Boolean(name)),
|
|
94
|
+
ownDomains: ((_identity_ownDomains = identity.ownDomains) != null ? _identity_ownDomains : []).filter((domain)=>Boolean(domain))
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The workspace's own names and hosts, as the screen reads them: a brand in
|
|
99
|
+
* its own name is its own, and a link to its own site is not "elsewhere".
|
|
100
|
+
*/ export function workspaceScreenIdentity(input) {
|
|
101
|
+
var _input_org, _input_host;
|
|
102
|
+
const org = (_input_org = input.org) != null ? _input_org : {};
|
|
103
|
+
const host = (_input_host = input.host) != null ? _input_host : {};
|
|
104
|
+
const text = (value)=>typeof value === 'string' ? value : '';
|
|
105
|
+
const subdomain = text(host['subdomain']);
|
|
106
|
+
return {
|
|
107
|
+
ownNames: [
|
|
108
|
+
text(org['name']),
|
|
109
|
+
text(host['name']),
|
|
110
|
+
subdomain
|
|
111
|
+
],
|
|
112
|
+
ownDomains: [
|
|
113
|
+
subdomain ? `${subdomain}.${TENANT_APEX}` : '',
|
|
114
|
+
text(host['cname']),
|
|
115
|
+
text(host['sendingDomain'])
|
|
116
|
+
]
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* JSON with every object's keys sorted, so the same design serializes to the
|
|
121
|
+
* same string however it was read. Firestore hands back a map's keys in an
|
|
122
|
+
* order that differs by reader, and a release keyed on a hash of an unsorted
|
|
123
|
+
* serialization would not match the very content it released.
|
|
124
|
+
*/ export function canonicalJson(value) {
|
|
125
|
+
return JSON.stringify(value, (_key, entry)=>entry && typeof entry === 'object' && !Array.isArray(entry) ? Object.fromEntries(Object.keys(entry).sort().map((key)=>[
|
|
126
|
+
key,
|
|
127
|
+
entry[key]
|
|
128
|
+
])) : entry);
|
|
129
|
+
}
|
|
130
|
+
/** A stable hash of what a message says, so a release covers these words only. */ export function outboundContentHash(parts) {
|
|
131
|
+
return createHash('sha256').update(canonicalJson(parts)).digest('hex');
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* The review row's id: one row per (source, content). Hex, so the admin
|
|
135
|
+
* route's report-id pattern addresses it like any intake row.
|
|
136
|
+
*/ export function heldOutboundReviewId(path, contentHash) {
|
|
137
|
+
return createHash('sha256').update(`outbound-hold:${path}:${contentHash}`).digest('hex').slice(0, 40);
|
|
138
|
+
}
|
|
139
|
+
/** The staff-facing reference, in the intake's `AR-` style. */ export function heldOutboundReference(reviewId) {
|
|
140
|
+
return `HS-${reviewId.slice(0, 10).toUpperCase()}`;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Write (or count) the review row for a hold, and tell staff the first time.
|
|
144
|
+
* Returns the row's decision state — a row staff already decided answers
|
|
145
|
+
* with that decision and is not written.
|
|
146
|
+
*
|
|
147
|
+
* One writer for every surface, so a campaign, a seam message and a page
|
|
148
|
+
* land in the queue in the one shape the admin route and page already read.
|
|
149
|
+
*/ export async function fileOutboundHold(filing) {
|
|
150
|
+
const firestore = firebaseAdmin.app().firestore();
|
|
151
|
+
const ref = firestore.collection(ABUSE_REPORT_COLLECTION).doc(filing.reviewId);
|
|
152
|
+
const reference = heldOutboundReference(filing.reviewId);
|
|
153
|
+
let first = false;
|
|
154
|
+
let state = 'held';
|
|
155
|
+
await firestore.runTransaction(async (transaction)=>{
|
|
156
|
+
var _ref, _filing_extra;
|
|
157
|
+
const existing = await transaction.get(ref);
|
|
158
|
+
first = !existing.exists;
|
|
159
|
+
const stored = existing.exists ? existing.get('heldSend') : undefined;
|
|
160
|
+
state = (stored == null ? void 0 : stored.state) === 'released' || (stored == null ? void 0 : stored.state) === 'rejected' ? stored.state : 'held';
|
|
161
|
+
if (state !== 'held') return;
|
|
162
|
+
transaction.set(ref, _extends({
|
|
163
|
+
reference,
|
|
164
|
+
category: 'phishing',
|
|
165
|
+
severity: 'urgent',
|
|
166
|
+
// Who filed it: the screen, not a person. The queue reads it to
|
|
167
|
+
// say so, and a reporter-contact field stays honestly empty.
|
|
168
|
+
source: 'outbound-screen',
|
|
169
|
+
// The flagged host as TEXT — the page never renders a reported URL
|
|
170
|
+
// as a link, and this one may be a phishing kit's.
|
|
171
|
+
url: filing.url,
|
|
172
|
+
reportedHostname: filing.reportedHostname,
|
|
173
|
+
hostId: filing.heldSend.hostId,
|
|
174
|
+
orgId: filing.heldSend.orgId,
|
|
175
|
+
details: [
|
|
176
|
+
filing.headline,
|
|
177
|
+
...describePhishingScreenSignals(filing.heldSend.signals),
|
|
178
|
+
filing.heldSend.kind === 'page' ? 'Dismiss to release this publish; mark it actioned to reject it.' : filing.heldSend.kind === 'listing' ? 'Dismiss to release this submission (the publisher submits it again and it ' + 'goes through); mark it actioned to reject it.' : 'Dismiss to release this send; mark it actioned to reject it.'
|
|
179
|
+
].join('\n').slice(0, 5000),
|
|
180
|
+
reporterEmail: null,
|
|
181
|
+
reporterName: null,
|
|
182
|
+
dmca: null,
|
|
183
|
+
// Every message the same content held counts here, so a workflow
|
|
184
|
+
// firing on each new contact reads as the volume it is.
|
|
185
|
+
reportCount: FieldValue.increment(1),
|
|
186
|
+
heldSend: first ? filing.heldSend : _extends({}, filing.heldSend, {
|
|
187
|
+
heldAtMs: (_ref = stored == null ? void 0 : stored.heldAtMs) != null ? _ref : filing.heldSend.heldAtMs
|
|
188
|
+
})
|
|
189
|
+
}, (_filing_extra = filing.extra) != null ? _filing_extra : {}, {
|
|
190
|
+
updatedAt: FieldValue.serverTimestamp()
|
|
191
|
+
}, first ? {
|
|
192
|
+
status: 'open',
|
|
193
|
+
createdAt: FieldValue.serverTimestamp()
|
|
194
|
+
} : {}), {
|
|
195
|
+
merge: true
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
if (first && state === 'held') {
|
|
199
|
+
await notifyStaff({
|
|
200
|
+
type: 'system.abuseReportUrgent',
|
|
201
|
+
title: filing.alertTitle,
|
|
202
|
+
body: `${filing.alertBody} Reference ${reference}.`,
|
|
203
|
+
link: '/admin/abuse-reports'
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
return state;
|
|
207
|
+
}
|
|
208
|
+
/** The first signal that names a host, for the row's reported URL. */ export function flaggedHostOf(signals) {
|
|
209
|
+
var _ref;
|
|
210
|
+
const flagged = signals.find((signal)=>'host' in signal);
|
|
211
|
+
return (_ref = flagged == null ? void 0 : flagged.host) != null ? _ref : null;
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* The screen's answer for one message, and what to do with it.
|
|
215
|
+
*
|
|
216
|
+
* - `send` — clean under the tiers, or held earlier and RELEASED by staff
|
|
217
|
+
* for exactly this content (then `releasedReviewId` names the row, and the
|
|
218
|
+
* caller hands it to `sendEmail` so the seam's screen honors it too).
|
|
219
|
+
* - `held` — hold it. The review row is written (or its count bumped) and
|
|
220
|
+
* staff are told the first time.
|
|
221
|
+
* - `rejected` — staff rejected exactly this content. Never sent.
|
|
222
|
+
*
|
|
223
|
+
* Every workspace is screened; `signalsThatHold` applies the tiers, so an
|
|
224
|
+
* established workspace is held only by a lookalike link.
|
|
225
|
+
*
|
|
226
|
+
* Fails OPEN on a store error, the posture of every other control on the
|
|
227
|
+
* send path: an outage on the review queue must not become an outage on
|
|
228
|
+
* every workspace's mail. The screen itself cannot fail.
|
|
229
|
+
*/ export async function screenOutboundSend(request) {
|
|
230
|
+
var _request_nowMs, _request_fromName, _request_fromAddress, _request_replyTo, _request_preheader, _request_fromName1, _request_replyTo1, _request_preheader1;
|
|
231
|
+
var _request_org;
|
|
232
|
+
const nowMs = (_request_nowMs = request.nowMs) != null ? _request_nowMs : Date.now();
|
|
233
|
+
const ageDays = orgAgeDays((_request_org = request.org) == null ? void 0 : _request_org.createdAt, nowMs);
|
|
234
|
+
const verdict = screenOutboundEmail(_extends({
|
|
235
|
+
subject: request.subject,
|
|
236
|
+
fromName: (_request_fromName = request.fromName) != null ? _request_fromName : null,
|
|
237
|
+
fromAddress: (_request_fromAddress = request.fromAddress) != null ? _request_fromAddress : null,
|
|
238
|
+
replyTo: (_request_replyTo = request.replyTo) != null ? _request_replyTo : null,
|
|
239
|
+
preheader: (_request_preheader = request.preheader) != null ? _request_preheader : null,
|
|
240
|
+
bodies: request.bodies
|
|
241
|
+
}, workspaceScreenIdentity({
|
|
242
|
+
org: request.org,
|
|
243
|
+
host: request.host
|
|
244
|
+
})));
|
|
245
|
+
const signals = signalsThatHold(verdict.signals, {
|
|
246
|
+
ageDays
|
|
247
|
+
});
|
|
248
|
+
if (!signals.length) return {
|
|
249
|
+
outcome: 'send'
|
|
250
|
+
};
|
|
251
|
+
const contentHash = outboundContentHash([
|
|
252
|
+
request.subject,
|
|
253
|
+
(_request_fromName1 = request.fromName) != null ? _request_fromName1 : '',
|
|
254
|
+
(_request_replyTo1 = request.replyTo) != null ? _request_replyTo1 : '',
|
|
255
|
+
(_request_preheader1 = request.preheader) != null ? _request_preheader1 : '',
|
|
256
|
+
...request.bodies.map((body)=>body != null ? body : '')
|
|
257
|
+
]);
|
|
258
|
+
const reviewId = heldOutboundReviewId(request.path, contentHash);
|
|
259
|
+
const reference = heldOutboundReference(reviewId);
|
|
260
|
+
try {
|
|
261
|
+
const heldSend = {
|
|
262
|
+
kind: request.kind,
|
|
263
|
+
path: request.path,
|
|
264
|
+
hostId: request.hostId,
|
|
265
|
+
orgId: request.orgId,
|
|
266
|
+
contentHash,
|
|
267
|
+
subject: request.subject.slice(0, 300),
|
|
268
|
+
fromName: request.fromName ? String(request.fromName).slice(0, 120) : null,
|
|
269
|
+
signals,
|
|
270
|
+
state: 'held',
|
|
271
|
+
heldAtMs: nowMs,
|
|
272
|
+
ageDays
|
|
273
|
+
};
|
|
274
|
+
const flagged = flaggedHostOf(signals);
|
|
275
|
+
const state = await fileOutboundHold({
|
|
276
|
+
reviewId,
|
|
277
|
+
heldSend,
|
|
278
|
+
headline: `Held ${request.kind === 'campaign' ? 'campaign' : `${request.kind} email step`} ` + `"${heldSend.subject}" from a workspace ${ageDays != null ? ageDays : '?'} day(s) old.`,
|
|
279
|
+
alertTitle: 'Outbound email held for review — possible phishing',
|
|
280
|
+
alertBody: `A ${request.kind === 'campaign' ? 'campaign' : 'automated email'} ` + `was held: "${request.subject.slice(0, 120)}".`,
|
|
281
|
+
url: flagged ? `https://${flagged}/` : null,
|
|
282
|
+
reportedHostname: flagged
|
|
283
|
+
});
|
|
284
|
+
if (state === 'released') return {
|
|
285
|
+
outcome: 'send',
|
|
286
|
+
releasedReviewId: reviewId
|
|
287
|
+
};
|
|
288
|
+
return {
|
|
289
|
+
outcome: state === 'rejected' ? 'rejected' : 'held',
|
|
290
|
+
reviewId,
|
|
291
|
+
reference,
|
|
292
|
+
contentHash
|
|
293
|
+
};
|
|
294
|
+
} catch (error) {
|
|
295
|
+
console.error('[outbound-review] hold could not be recorded — allowing', error);
|
|
296
|
+
return {
|
|
297
|
+
outcome: 'send'
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* How long this process trusts what it last learned about a seam row: that
|
|
303
|
+
* it is held (so the next message carrying the same signals skips the
|
|
304
|
+
* write), or how staff decided it. Short, because a decision must reach a
|
|
305
|
+
* sender within a minute; long enough that a newsletter fanned out to a
|
|
306
|
+
* thousand people costs one transaction rather than a thousand on one
|
|
307
|
+
* document.
|
|
308
|
+
*/ const SEAM_MEMO_TTL_MS = 60000;
|
|
309
|
+
const seamMemo = new Map();
|
|
310
|
+
/** Test seam: forget what this process learned about seam rows. */ export function resetOutboundSeamMemoForTests() {
|
|
311
|
+
seamMemo.clear();
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* The seam row's id for a site and the signals that held. Keyed on the
|
|
315
|
+
* SIGNALS rather than the words: a message at the seam is already
|
|
316
|
+
* personalized per recipient, so its words differ for every person while
|
|
317
|
+
* what makes it phishing — the lookalike host, the brand and the lure — does
|
|
318
|
+
* not. A release therefore covers this site's use of exactly these signals,
|
|
319
|
+
* and a message carrying any other signal is screened afresh.
|
|
320
|
+
*/ export function seamReviewId(hostId, signals) {
|
|
321
|
+
const key = signals.map((signal)=>canonicalJson(signal)).sort().join('\n');
|
|
322
|
+
return heldOutboundReviewId(`hosts/${hostId}/outbound`, outboundContentHash([
|
|
323
|
+
key
|
|
324
|
+
]));
|
|
325
|
+
}
|
|
326
|
+
/** Is this row a staff RELEASE for this site? */ async function isReleaseForHost(reviewId, hostId) {
|
|
327
|
+
const memoKey = `release:${hostId}:${reviewId}`;
|
|
328
|
+
const memo = seamMemo.get(memoKey);
|
|
329
|
+
if (memo && Date.now() - memo.atMs < SEAM_MEMO_TTL_MS) return memo.state === 'released';
|
|
330
|
+
const snapshot = await firebaseAdmin.app().firestore().collection(ABUSE_REPORT_COLLECTION).doc(reviewId).get();
|
|
331
|
+
const held = snapshot.get('heldSend');
|
|
332
|
+
const released = (held == null ? void 0 : held.state) === 'released' && (held == null ? void 0 : held.hostId) === hostId;
|
|
333
|
+
seamMemo.set(memoKey, {
|
|
334
|
+
state: released ? 'released' : 'held',
|
|
335
|
+
atMs: Date.now()
|
|
336
|
+
});
|
|
337
|
+
return released;
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* The send seam's gate: what `sendEmail` asks when a tenant message carries
|
|
341
|
+
* a signal that holds (see `outbound-screen-gate.ts`). Installed at module
|
|
342
|
+
* load by {@link installOutboundScreenGate}.
|
|
343
|
+
*
|
|
344
|
+
* A seam hold REFUSES that one message (`held-for-review`) — the sender's own
|
|
345
|
+
* failure handling reports it, the way it reports a suppression — and files
|
|
346
|
+
* one row per site and signal set. Once staff release it, the site's next
|
|
347
|
+
* message carrying those signals sends; reject it, and none does.
|
|
348
|
+
*/ export async function screenSeamMessage(request) {
|
|
349
|
+
var _request_subject, _workspace_ageDays;
|
|
350
|
+
const { workspace } = request;
|
|
351
|
+
if (request.releasedReviewId && await isReleaseForHost(request.releasedReviewId, workspace.hostId)) {
|
|
352
|
+
return {
|
|
353
|
+
outcome: 'send'
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
const reviewId = seamReviewId(workspace.hostId, request.signals);
|
|
357
|
+
const reference = heldOutboundReference(reviewId);
|
|
358
|
+
const memo = seamMemo.get(reviewId);
|
|
359
|
+
if (memo && Date.now() - memo.atMs < SEAM_MEMO_TTL_MS) {
|
|
360
|
+
return memo.state === 'released' ? {
|
|
361
|
+
outcome: 'send'
|
|
362
|
+
} : {
|
|
363
|
+
outcome: memo.state === 'rejected' ? 'rejected' : 'held',
|
|
364
|
+
reference
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
const nowMs = Date.now();
|
|
368
|
+
const subject = String((_request_subject = request.subject) != null ? _request_subject : '').slice(0, 300);
|
|
369
|
+
const context = request.context ? String(request.context).slice(0, 80) : null;
|
|
370
|
+
const flagged = flaggedHostOf(request.signals);
|
|
371
|
+
const state = await fileOutboundHold({
|
|
372
|
+
reviewId,
|
|
373
|
+
heldSend: {
|
|
374
|
+
kind: 'message',
|
|
375
|
+
path: `hosts/${workspace.hostId}`,
|
|
376
|
+
hostId: workspace.hostId,
|
|
377
|
+
orgId: workspace.orgId,
|
|
378
|
+
contentHash: outboundContentHash([
|
|
379
|
+
request.signals
|
|
380
|
+
]),
|
|
381
|
+
subject,
|
|
382
|
+
fromName: request.fromName ? String(request.fromName).slice(0, 120) : null,
|
|
383
|
+
signals: request.signals,
|
|
384
|
+
state: 'held',
|
|
385
|
+
heldAtMs: nowMs,
|
|
386
|
+
ageDays: workspace.ageDays
|
|
387
|
+
},
|
|
388
|
+
extra: {
|
|
389
|
+
heldContext: context
|
|
390
|
+
},
|
|
391
|
+
headline: `Held ${context ? `"${context}" ` : ''}email "${subject}" from a workspace ` + `${(_workspace_ageDays = workspace.ageDays) != null ? _workspace_ageDays : '?'} day(s) old. Every later message from this site ` + 'carrying the same signals waits on this decision.',
|
|
392
|
+
alertTitle: 'Outbound email held for review — possible phishing',
|
|
393
|
+
alertBody: `An email from a site was held: "${subject.slice(0, 120)}".`,
|
|
394
|
+
url: flagged ? `https://${flagged}/` : null,
|
|
395
|
+
reportedHostname: flagged
|
|
396
|
+
});
|
|
397
|
+
seamMemo.set(reviewId, {
|
|
398
|
+
state,
|
|
399
|
+
atMs: Date.now()
|
|
400
|
+
});
|
|
401
|
+
return state === 'released' ? {
|
|
402
|
+
outcome: 'send'
|
|
403
|
+
} : {
|
|
404
|
+
outcome: state === 'rejected' ? 'rejected' : 'held',
|
|
405
|
+
reference
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Puts the review store on `sendEmail`'s phishing screen.
|
|
410
|
+
*
|
|
411
|
+
* **Called at module load**, from the bottom of this file, for the reason
|
|
412
|
+
* `installEmailSendGovernor` is — and this module is also imported by
|
|
413
|
+
* `sending-domains.ts`, so any sender that resolved its identity through
|
|
414
|
+
* `hostSendingIdentity` (the only thing that stamps a workspace on it, and
|
|
415
|
+
* so the only thing the seam screens) has installed it.
|
|
416
|
+
*/ export function installOutboundScreenGate() {
|
|
417
|
+
if (typeof setOutboundScreenGate !== 'function') return;
|
|
418
|
+
setOutboundScreenGate((request)=>screenSeamMessage(request));
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Apply a staff decision to a held send (the admin abuse route, in the same
|
|
422
|
+
* act as the row's status change).
|
|
423
|
+
*
|
|
424
|
+
* The row's `heldSend.state` is written first: it is what the screen reads,
|
|
425
|
+
* so an automation step released here sends on its next run whatever
|
|
426
|
+
* happens to the rest. A CAMPAIGN is also put back on the clock (released)
|
|
427
|
+
* or canceled (rejected) — but only while it is still the parked send this
|
|
428
|
+
* row held: a send that has since been edited, sent or canceled by the
|
|
429
|
+
* merchant is left alone, and the answer says so.
|
|
430
|
+
*/ export async function decideHeldOutboundSend(input) {
|
|
431
|
+
var _input_nowMs, _input_firestore;
|
|
432
|
+
const nowMs = (_input_nowMs = input.nowMs) != null ? _input_nowMs : Date.now();
|
|
433
|
+
const firestore = (_input_firestore = input.firestore) != null ? _input_firestore : firebaseAdmin.app().firestore();
|
|
434
|
+
const ref = firestore.collection(ABUSE_REPORT_COLLECTION).doc(input.reviewId);
|
|
435
|
+
const snapshot = await ref.get();
|
|
436
|
+
const heldSend = snapshot.get('heldSend');
|
|
437
|
+
if (!(heldSend == null ? void 0 : heldSend.path) || !heldSend.contentHash) return 'not-held';
|
|
438
|
+
const state = input.decision === 'release' ? 'released' : 'rejected';
|
|
439
|
+
const decided = {
|
|
440
|
+
state,
|
|
441
|
+
decidedAtMs: nowMs,
|
|
442
|
+
decidedByUid: input.actorUid,
|
|
443
|
+
decidedByEmail: input.actorEmail
|
|
444
|
+
};
|
|
445
|
+
await ref.set({
|
|
446
|
+
heldSend: _extends({}, heldSend, decided)
|
|
447
|
+
}, {
|
|
448
|
+
merge: true
|
|
449
|
+
});
|
|
450
|
+
if (heldSend.kind !== 'campaign') return state;
|
|
451
|
+
const sendRef = firestore.doc(heldSend.path);
|
|
452
|
+
const moved = await firestore.runTransaction(async (transaction)=>{
|
|
453
|
+
var _send_get;
|
|
454
|
+
const send = await transaction.get(sendRef);
|
|
455
|
+
const review = send.get('staffReview');
|
|
456
|
+
const status = String((_send_get = send.get('status')) != null ? _send_get : '');
|
|
457
|
+
if (!send.exists || (review == null ? void 0 : review.reviewId) !== input.reviewId || status !== 'scheduled' && status !== 'draft') {
|
|
458
|
+
return true;
|
|
459
|
+
}
|
|
460
|
+
transaction.set(sendRef, input.decision === 'release' ? {
|
|
461
|
+
status: 'scheduled',
|
|
462
|
+
// Due now: the next processor run sends it, and the screen
|
|
463
|
+
// finds this row released for exactly this content.
|
|
464
|
+
sendAtMs: nowMs,
|
|
465
|
+
staffReview: _extends({}, review, decided)
|
|
466
|
+
} : {
|
|
467
|
+
status: 'canceled',
|
|
468
|
+
sendAtMs: FieldValue.delete(),
|
|
469
|
+
staffReview: _extends({}, review, decided)
|
|
470
|
+
}, {
|
|
471
|
+
merge: true
|
|
472
|
+
});
|
|
473
|
+
return false;
|
|
474
|
+
});
|
|
475
|
+
return moved ? 'campaign-moved' : state;
|
|
476
|
+
}
|
|
477
|
+
installOutboundScreenGate();
|
|
478
|
+
|
|
479
|
+
//# sourceMappingURL=outbound-send-review.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../../../libs/tenant/data/admin/src/lib/server/outbound-send-review.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/*==========================================\n * HELD FOR REVIEW: outbound mail from a young workspace (AGL-3356).\n *\n * The phishing screen (`screenOutboundEmail`, shared-util-email) decides\n * whether a message carries a phishing signal, and `signalsThatHold` beside\n * it decides which signals hold for whom. This module is what happens next,\n * for every surface that screens — the send seam every tenant message\n * crosses ({@link installOutboundScreenGate}), the campaign core and the\n * automation step before they commit, and a site publish\n * (`hosted-page-review.ts`) — so none restates it:\n *\n * - WHO IS SCREENED. Every workspace, in two tiers. A lookalike link holds\n * whatever the workspace's age. The soft rules (a brand in the sender\n * name, the three-part lure) hold only for a workspace younger than\n * {@link OUTBOUND_REVIEW_YOUNG_DAYS}: the incident's workspace was days\n * old, and an established customer's clumsy copy never waits on them. An\n * org whose creation date cannot be read is an existing customer (the same\n * reading `orgAgeDays` gives the sending ramp) and is not young.\n * - WHERE A HOLD GOES. Into the abuse queue at `/admin/abuse-reports` — the\n * staff surface that already exists to answer \"is this phishing\", with its\n * audit trail, its urgent count and its staff notification — as a\n * `phishing` row whose `heldSend` names the message. Not a new queue: a\n * second place to look is a place nobody looks.\n * - HOW STAFF DECIDE. By the row's existing status. `dismissed` (a false\n * positive) RELEASES the send; `actioned` REJECTS it. The admin route\n * calls {@link decideHeldOutboundSend} in the same act as the status\n * write, so the decision and its audit row cannot come apart.\n * - WHAT A RELEASE COVERS. Exactly the content that was held: the review\n * row's id is derived from the message's source and a hash of its\n * content, so an edited message is screened afresh rather than riding a\n * release granted to different words.\n *\n * A hold is never a drop. A held campaign is parked as a scheduled send that\n * a release puts back on the clock; a held automation step fails that one\n * run with a reason the merchant can read, or — after a wait — stays queued\n * until the decision.\n *=========================================*/\n\nimport { createHash } from 'crypto'\nimport { ABUSE_REPORT_COLLECTION } from '@aglyn/aglyn/app-utils/abuse-report'\nimport { TENANT_APEX } from '@aglyn/aglyn/app-utils/host-naming'\nimport {\n describePhishingScreenSignals,\n isYoungWorkspaceAge,\n OUTBOUND_REVIEW_YOUNG_DAYS,\n type PhishingScreenInput,\n type PhishingScreenSignal,\n screenOutboundEmail,\n signalsThatHold,\n} from '@aglyn/shared-util-email/outbound-phishing-screen'\nimport {\n type OutboundScreenGateRequest,\n type OutboundScreenGateVerdict,\n type SendingWorkspace,\n setOutboundScreenGate,\n} from '@aglyn/shared-util-email/outbound-screen-gate'\nimport { FieldValue } from 'firebase-admin/firestore'\nimport { orgAgeDays } from './org-age'\nimport firebaseAdmin from './firebase-admin'\nimport { notifyStaff } from './notifications'\n\n/** A workspace younger than this many days has the soft rules applied. */\nexport { OUTBOUND_REVIEW_YOUNG_DAYS }\n\n/**\n * Where a held campaign is parked: a send time no processor run reaches.\n * A release replaces it with \"now\"; nothing else ever does, so a hold that\n * nobody decides stays a hold rather than lapsing into a send.\n */\nexport const HELD_SEND_AT_MS = 253402300799000\n\n/**\n * Which surface held it: a campaign, an automation step, any other tenant\n * message at the send seam (`message`), a site publish (`page`), or a\n * submission to a public catalog a plugin keeps — a marketplace listing, its\n * publisher profile (`listing`, AGL-3365).\n */\nexport type HeldOutboundSendKind =\n | 'campaign'\n | 'action'\n | 'workflow'\n | 'orgAutomation'\n | 'message'\n | 'page'\n | 'listing'\n\n/** The decision state a held send carries on its review row. */\nexport type HeldOutboundSendState = 'held' | 'released' | 'rejected'\n\n/** What the review row records about the message it holds. */\nexport interface HeldOutboundSend {\n kind: HeldOutboundSendKind\n /** The document the message came from — a send, an action, a workflow. */\n path: string\n hostId: string\n orgId: string | null\n contentHash: string\n subject: string\n fromName: string | null\n signals: PhishingScreenSignal[]\n state: HeldOutboundSendState\n heldAtMs: number\n /** Workspace age in days when it was held, for the reviewer. */\n ageDays: number | null\n}\n\n/**\n * Is this workspace young enough to screen?\n *\n * Age alone, deliberately. Payment history was the other candidate and the\n * incident is the argument against it: the actor PAID, on a card that never\n * met 3-D Secure, within days of signing up. A paid invoice proves a card\n * worked, not that its holder is who they say.\n */\nexport function isYoungWorkspace(\n org: { createdAt?: unknown } | null | undefined,\n nowMs: number = Date.now(),\n): boolean {\n return isYoungWorkspaceAge(orgAgeDays(org?.createdAt, nowMs))\n}\n\n/**\n * The workspace a host's mail belongs to, as the send seam's screen reads it\n * — stamped on the sending identity by `hostSendingIdentity` from the host\n * and org documents it has already read.\n */\nexport function sendingWorkspaceFor(input: {\n hostId: string\n orgId: string | null\n org?: Record<string, unknown> | null\n host?: Record<string, unknown> | null\n nowMs?: number\n}): SendingWorkspace {\n const identity = workspaceScreenIdentity({ org: input.org, host: input.host })\n return {\n hostId: input.hostId,\n orgId: input.orgId,\n ageDays: orgAgeDays(\n (input.org as { createdAt?: unknown } | null | undefined)?.createdAt,\n input.nowMs ?? Date.now(),\n ),\n ownNames: (identity.ownNames ?? []).filter((name): name is string => Boolean(name)),\n ownDomains: (identity.ownDomains ?? []).filter((domain): domain is string => Boolean(domain)),\n }\n}\n\n/**\n * The workspace's own names and hosts, as the screen reads them: a brand in\n * its own name is its own, and a link to its own site is not \"elsewhere\".\n */\nexport function workspaceScreenIdentity(input: {\n org?: Record<string, unknown> | null\n host?: Record<string, unknown> | null\n}): Pick<PhishingScreenInput, 'ownNames' | 'ownDomains'> {\n const org = input.org ?? {}\n const host = input.host ?? {}\n const text = (value: unknown) => (typeof value === 'string' ? value : '')\n const subdomain = text(host['subdomain'])\n return {\n ownNames: [text(org['name']), text(host['name']), subdomain],\n ownDomains: [\n subdomain ? `${subdomain}.${TENANT_APEX}` : '',\n text(host['cname']),\n text(host['sendingDomain']),\n ],\n }\n}\n\n/**\n * JSON with every object's keys sorted, so the same design serializes to the\n * same string however it was read. Firestore hands back a map's keys in an\n * order that differs by reader, and a release keyed on a hash of an unsorted\n * serialization would not match the very content it released.\n */\nexport function canonicalJson(value: unknown): string {\n return JSON.stringify(value, (_key, entry) =>\n entry && typeof entry === 'object' && !Array.isArray(entry)\n ? Object.fromEntries(\n Object.keys(entry as Record<string, unknown>)\n .sort()\n .map((key) => [key, (entry as Record<string, unknown>)[key]]),\n )\n : entry,\n )\n}\n\n/** A stable hash of what a message says, so a release covers these words only. */\nexport function outboundContentHash(parts: readonly unknown[]): string {\n return createHash('sha256').update(canonicalJson(parts)).digest('hex')\n}\n\n/**\n * The review row's id: one row per (source, content). Hex, so the admin\n * route's report-id pattern addresses it like any intake row.\n */\nexport function heldOutboundReviewId(path: string, contentHash: string): string {\n return createHash('sha256')\n .update(`outbound-hold:${path}:${contentHash}`)\n .digest('hex')\n .slice(0, 40)\n}\n\n/** The staff-facing reference, in the intake's `AR-` style. */\nexport function heldOutboundReference(reviewId: string): string {\n return `HS-${reviewId.slice(0, 10).toUpperCase()}`\n}\n\nexport interface OutboundScreenRequest {\n kind: HeldOutboundSendKind\n path: string\n hostId: string\n orgId: string | null\n org: Record<string, unknown> | null\n host: Record<string, unknown> | null\n subject: string\n fromName?: string | null\n /**\n * The address it leaves from (AGL-3362). Read by the lookalike rule only,\n * and kept out of the content hash so a release granted before this was\n * read still names the same row.\n */\n fromAddress?: string | null\n replyTo?: string | readonly string[] | null\n preheader?: string | null\n bodies: readonly (string | null | undefined)[]\n nowMs?: number\n}\n\n/** What a screen answers for one piece of content. */\nexport type OutboundScreenOutcome =\n | {\n outcome: 'send'\n /** The review row staff released this content under, when there is one. */\n releasedReviewId?: string\n }\n | { outcome: 'held' | 'rejected'; reviewId: string; reference: string; contentHash: string }\n\n/** What a surface files when its screen holds, for {@link fileOutboundHold}. */\nexport interface OutboundHoldFiling {\n reviewId: string\n heldSend: HeldOutboundSend\n /** Extra fields a surface records beside `heldSend` (a page's version). */\n extra?: Record<string, unknown>\n /** The first line of the row's details, naming what was held. */\n headline: string\n /** The staff alert's title and body, the first time. */\n alertTitle: string\n alertBody: string\n /** The URL the row reports: the flagged host, or the held page. */\n url: string | null\n reportedHostname: string | null\n}\n\n/**\n * Write (or count) the review row for a hold, and tell staff the first time.\n * Returns the row's decision state — a row staff already decided answers\n * with that decision and is not written.\n *\n * One writer for every surface, so a campaign, a seam message and a page\n * land in the queue in the one shape the admin route and page already read.\n */\nexport async function fileOutboundHold(\n filing: OutboundHoldFiling,\n): Promise<HeldOutboundSendState> {\n const firestore = firebaseAdmin.app().firestore()\n const ref = firestore.collection(ABUSE_REPORT_COLLECTION).doc(filing.reviewId)\n const reference = heldOutboundReference(filing.reviewId)\n let first = false\n let state = 'held' as HeldOutboundSendState\n await firestore.runTransaction(async (transaction) => {\n const existing = await transaction.get(ref)\n first = !existing.exists\n const stored = existing.exists\n ? (existing.get('heldSend') as Partial<HeldOutboundSend> | undefined)\n : undefined\n state = stored?.state === 'released' || stored?.state === 'rejected'\n ? stored.state\n : 'held'\n if (state !== 'held') return\n transaction.set(\n ref,\n {\n reference,\n category: 'phishing',\n severity: 'urgent',\n // Who filed it: the screen, not a person. The queue reads it to\n // say so, and a reporter-contact field stays honestly empty.\n source: 'outbound-screen',\n // The flagged host as TEXT — the page never renders a reported URL\n // as a link, and this one may be a phishing kit's.\n url: filing.url,\n reportedHostname: filing.reportedHostname,\n hostId: filing.heldSend.hostId,\n orgId: filing.heldSend.orgId,\n details: [\n filing.headline,\n ...describePhishingScreenSignals(filing.heldSend.signals),\n filing.heldSend.kind === 'page'\n ? 'Dismiss to release this publish; mark it actioned to reject it.'\n : filing.heldSend.kind === 'listing'\n ? 'Dismiss to release this submission (the publisher submits it again and it ' +\n 'goes through); mark it actioned to reject it.'\n : 'Dismiss to release this send; mark it actioned to reject it.',\n ]\n .join('\\n')\n .slice(0, 5000),\n reporterEmail: null,\n reporterName: null,\n dmca: null,\n // Every message the same content held counts here, so a workflow\n // firing on each new contact reads as the volume it is.\n reportCount: FieldValue.increment(1),\n heldSend: first\n ? filing.heldSend\n : { ...filing.heldSend, heldAtMs: stored?.heldAtMs ?? filing.heldSend.heldAtMs },\n ...(filing.extra ?? {}),\n updatedAt: FieldValue.serverTimestamp(),\n ...(first\n ? { status: 'open', createdAt: FieldValue.serverTimestamp() }\n : {}),\n },\n { merge: true },\n )\n })\n if (first && state === 'held') {\n await notifyStaff({\n type: 'system.abuseReportUrgent',\n title: filing.alertTitle,\n body: `${filing.alertBody} Reference ${reference}.`,\n link: '/admin/abuse-reports',\n })\n }\n return state\n}\n\n/** The first signal that names a host, for the row's reported URL. */\nexport function flaggedHostOf(signals: readonly PhishingScreenSignal[]): string | null {\n const flagged = signals.find(\n (signal): signal is Extract<PhishingScreenSignal, { host: string }> => 'host' in signal,\n )\n return flagged?.host ?? null\n}\n\n/**\n * The screen's answer for one message, and what to do with it.\n *\n * - `send` — clean under the tiers, or held earlier and RELEASED by staff\n * for exactly this content (then `releasedReviewId` names the row, and the\n * caller hands it to `sendEmail` so the seam's screen honors it too).\n * - `held` — hold it. The review row is written (or its count bumped) and\n * staff are told the first time.\n * - `rejected` — staff rejected exactly this content. Never sent.\n *\n * Every workspace is screened; `signalsThatHold` applies the tiers, so an\n * established workspace is held only by a lookalike link.\n *\n * Fails OPEN on a store error, the posture of every other control on the\n * send path: an outage on the review queue must not become an outage on\n * every workspace's mail. The screen itself cannot fail.\n */\nexport async function screenOutboundSend(\n request: OutboundScreenRequest,\n): Promise<OutboundScreenOutcome> {\n const nowMs = request.nowMs ?? Date.now()\n const ageDays = orgAgeDays(\n (request.org as { createdAt?: unknown } | null)?.createdAt,\n nowMs,\n )\n const verdict = screenOutboundEmail({\n subject: request.subject,\n fromName: request.fromName ?? null,\n fromAddress: request.fromAddress ?? null,\n replyTo: request.replyTo ?? null,\n preheader: request.preheader ?? null,\n bodies: request.bodies,\n ...workspaceScreenIdentity({ org: request.org, host: request.host }),\n })\n const signals = signalsThatHold(verdict.signals, { ageDays })\n if (!signals.length) return { outcome: 'send' }\n\n const contentHash = outboundContentHash([\n request.subject,\n request.fromName ?? '',\n request.replyTo ?? '',\n request.preheader ?? '',\n ...request.bodies.map((body) => body ?? ''),\n ])\n const reviewId = heldOutboundReviewId(request.path, contentHash)\n const reference = heldOutboundReference(reviewId)\n try {\n const heldSend: HeldOutboundSend = {\n kind: request.kind,\n path: request.path,\n hostId: request.hostId,\n orgId: request.orgId,\n contentHash,\n subject: request.subject.slice(0, 300),\n fromName: request.fromName ? String(request.fromName).slice(0, 120) : null,\n signals,\n state: 'held',\n heldAtMs: nowMs,\n ageDays,\n }\n const flagged = flaggedHostOf(signals)\n const state = await fileOutboundHold({\n reviewId,\n heldSend,\n headline:\n `Held ${request.kind === 'campaign' ? 'campaign' : `${request.kind} email step`} ` +\n `\"${heldSend.subject}\" from a workspace ${ageDays ?? '?'} day(s) old.`,\n alertTitle: 'Outbound email held for review — possible phishing',\n alertBody:\n `A ${request.kind === 'campaign' ? 'campaign' : 'automated email'} ` +\n `was held: \"${request.subject.slice(0, 120)}\".`,\n url: flagged ? `https://${flagged}/` : null,\n reportedHostname: flagged,\n })\n if (state === 'released') return { outcome: 'send', releasedReviewId: reviewId }\n return { outcome: state === 'rejected' ? 'rejected' : 'held', reviewId, reference, contentHash }\n } catch (error) {\n console.error('[outbound-review] hold could not be recorded — allowing', error)\n return { outcome: 'send' }\n }\n}\n\n/**\n * How long this process trusts what it last learned about a seam row: that\n * it is held (so the next message carrying the same signals skips the\n * write), or how staff decided it. Short, because a decision must reach a\n * sender within a minute; long enough that a newsletter fanned out to a\n * thousand people costs one transaction rather than a thousand on one\n * document.\n */\nconst SEAM_MEMO_TTL_MS = 60_000\nconst seamMemo = new Map<string, { state: HeldOutboundSendState; atMs: number }>()\n\n/** Test seam: forget what this process learned about seam rows. */\nexport function resetOutboundSeamMemoForTests(): void {\n seamMemo.clear()\n}\n\n/**\n * The seam row's id for a site and the signals that held. Keyed on the\n * SIGNALS rather than the words: a message at the seam is already\n * personalized per recipient, so its words differ for every person while\n * what makes it phishing — the lookalike host, the brand and the lure — does\n * not. A release therefore covers this site's use of exactly these signals,\n * and a message carrying any other signal is screened afresh.\n */\nexport function seamReviewId(hostId: string, signals: readonly PhishingScreenSignal[]): string {\n const key = signals\n .map((signal) => canonicalJson(signal))\n .sort()\n .join('\\n')\n return heldOutboundReviewId(`hosts/${hostId}/outbound`, outboundContentHash([key]))\n}\n\n/** Is this row a staff RELEASE for this site? */\nasync function isReleaseForHost(reviewId: string, hostId: string): Promise<boolean> {\n const memoKey = `release:${hostId}:${reviewId}`\n const memo = seamMemo.get(memoKey)\n if (memo && Date.now() - memo.atMs < SEAM_MEMO_TTL_MS) return memo.state === 'released'\n const snapshot = await firebaseAdmin\n .app()\n .firestore()\n .collection(ABUSE_REPORT_COLLECTION)\n .doc(reviewId)\n .get()\n const held = snapshot.get('heldSend') as Partial<HeldOutboundSend> | undefined\n const released = held?.state === 'released' && held?.hostId === hostId\n seamMemo.set(memoKey, { state: released ? 'released' : 'held', atMs: Date.now() })\n return released\n}\n\n/**\n * The send seam's gate: what `sendEmail` asks when a tenant message carries\n * a signal that holds (see `outbound-screen-gate.ts`). Installed at module\n * load by {@link installOutboundScreenGate}.\n *\n * A seam hold REFUSES that one message (`held-for-review`) — the sender's own\n * failure handling reports it, the way it reports a suppression — and files\n * one row per site and signal set. Once staff release it, the site's next\n * message carrying those signals sends; reject it, and none does.\n */\nexport async function screenSeamMessage(\n request: OutboundScreenGateRequest,\n): Promise<OutboundScreenGateVerdict> {\n const { workspace } = request\n if (\n request.releasedReviewId &&\n (await isReleaseForHost(request.releasedReviewId, workspace.hostId))\n ) {\n return { outcome: 'send' }\n }\n const reviewId = seamReviewId(workspace.hostId, request.signals)\n const reference = heldOutboundReference(reviewId)\n const memo = seamMemo.get(reviewId)\n if (memo && Date.now() - memo.atMs < SEAM_MEMO_TTL_MS) {\n return memo.state === 'released'\n ? { outcome: 'send' }\n : { outcome: memo.state === 'rejected' ? 'rejected' : 'held', reference }\n }\n const nowMs = Date.now()\n const subject = String(request.subject ?? '').slice(0, 300)\n const context = request.context ? String(request.context).slice(0, 80) : null\n const flagged = flaggedHostOf(request.signals)\n const state = await fileOutboundHold({\n reviewId,\n heldSend: {\n kind: 'message',\n path: `hosts/${workspace.hostId}`,\n hostId: workspace.hostId,\n orgId: workspace.orgId,\n contentHash: outboundContentHash([request.signals]),\n subject,\n fromName: request.fromName ? String(request.fromName).slice(0, 120) : null,\n signals: request.signals,\n state: 'held',\n heldAtMs: nowMs,\n ageDays: workspace.ageDays,\n },\n extra: { heldContext: context },\n headline:\n `Held ${context ? `\"${context}\" ` : ''}email \"${subject}\" from a workspace ` +\n `${workspace.ageDays ?? '?'} day(s) old. Every later message from this site ` +\n 'carrying the same signals waits on this decision.',\n alertTitle: 'Outbound email held for review — possible phishing',\n alertBody: `An email from a site was held: \"${subject.slice(0, 120)}\".`,\n url: flagged ? `https://${flagged}/` : null,\n reportedHostname: flagged,\n })\n seamMemo.set(reviewId, { state, atMs: Date.now() })\n return state === 'released'\n ? { outcome: 'send' }\n : { outcome: state === 'rejected' ? 'rejected' : 'held', reference }\n}\n\n/**\n * Puts the review store on `sendEmail`'s phishing screen.\n *\n * **Called at module load**, from the bottom of this file, for the reason\n * `installEmailSendGovernor` is — and this module is also imported by\n * `sending-domains.ts`, so any sender that resolved its identity through\n * `hostSendingIdentity` (the only thing that stamps a workspace on it, and\n * so the only thing the seam screens) has installed it.\n */\nexport function installOutboundScreenGate(): void {\n if (typeof setOutboundScreenGate !== 'function') return\n setOutboundScreenGate((request) => screenSeamMessage(request))\n}\n\n/**\n * Apply a staff decision to a held send (the admin abuse route, in the same\n * act as the row's status change).\n *\n * The row's `heldSend.state` is written first: it is what the screen reads,\n * so an automation step released here sends on its next run whatever\n * happens to the rest. A CAMPAIGN is also put back on the clock (released)\n * or canceled (rejected) — but only while it is still the parked send this\n * row held: a send that has since been edited, sent or canceled by the\n * merchant is left alone, and the answer says so.\n */\nexport async function decideHeldOutboundSend(input: {\n reviewId: string\n decision: 'release' | 'reject'\n actorUid: string\n actorEmail: string | null\n nowMs?: number\n firestore?: FirebaseFirestore.Firestore\n}): Promise<'released' | 'rejected' | 'not-held' | 'campaign-moved'> {\n const nowMs = input.nowMs ?? Date.now()\n const firestore = input.firestore ?? firebaseAdmin.app().firestore()\n const ref = firestore.collection(ABUSE_REPORT_COLLECTION).doc(input.reviewId)\n const snapshot = await ref.get()\n const heldSend = snapshot.get('heldSend') as HeldOutboundSend | undefined\n if (!heldSend?.path || !heldSend.contentHash) return 'not-held'\n const state: HeldOutboundSendState =\n input.decision === 'release' ? 'released' : 'rejected'\n const decided = {\n state,\n decidedAtMs: nowMs,\n decidedByUid: input.actorUid,\n decidedByEmail: input.actorEmail,\n }\n await ref.set(\n { heldSend: { ...heldSend, ...decided } },\n { merge: true },\n )\n if (heldSend.kind !== 'campaign') return state\n\n const sendRef = firestore.doc(heldSend.path)\n const moved = await firestore.runTransaction(async (transaction) => {\n const send = await transaction.get(sendRef)\n const review = send.get('staffReview') as\n | { reviewId?: string; state?: string }\n | undefined\n const status = String(send.get('status') ?? '')\n if (\n !send.exists ||\n review?.reviewId !== input.reviewId ||\n (status !== 'scheduled' && status !== 'draft')\n ) {\n return true\n }\n transaction.set(\n sendRef,\n input.decision === 'release'\n ? {\n status: 'scheduled',\n // Due now: the next processor run sends it, and the screen\n // finds this row released for exactly this content.\n sendAtMs: nowMs,\n staffReview: { ...review, ...decided },\n }\n : {\n status: 'canceled',\n sendAtMs: FieldValue.delete(),\n staffReview: { ...review, ...decided },\n },\n { merge: true },\n )\n return false\n })\n return moved ? 'campaign-moved' : state\n}\n\ninstallOutboundScreenGate()\n"],"names":["createHash","ABUSE_REPORT_COLLECTION","TENANT_APEX","describePhishingScreenSignals","isYoungWorkspaceAge","OUTBOUND_REVIEW_YOUNG_DAYS","screenOutboundEmail","signalsThatHold","setOutboundScreenGate","FieldValue","orgAgeDays","firebaseAdmin","notifyStaff","HELD_SEND_AT_MS","isYoungWorkspace","org","nowMs","Date","now","createdAt","sendingWorkspaceFor","input","identity","workspaceScreenIdentity","host","hostId","orgId","ageDays","ownNames","filter","name","Boolean","ownDomains","domain","text","value","subdomain","canonicalJson","JSON","stringify","_key","entry","Array","isArray","Object","fromEntries","keys","sort","map","key","outboundContentHash","parts","update","digest","heldOutboundReviewId","path","contentHash","slice","heldOutboundReference","reviewId","toUpperCase","fileOutboundHold","filing","firestore","app","ref","collection","doc","reference","first","state","runTransaction","transaction","existing","get","exists","stored","undefined","set","category","severity","source","url","reportedHostname","heldSend","details","headline","signals","kind","join","reporterEmail","reporterName","dmca","reportCount","increment","heldAtMs","extra","updatedAt","serverTimestamp","status","merge","type","title","alertTitle","body","alertBody","link","flaggedHostOf","flagged","find","signal","screenOutboundSend","request","verdict","subject","fromName","fromAddress","replyTo","preheader","bodies","length","outcome","String","releasedReviewId","error","console","SEAM_MEMO_TTL_MS","seamMemo","Map","resetOutboundSeamMemoForTests","clear","seamReviewId","isReleaseForHost","memoKey","memo","atMs","snapshot","held","released","screenSeamMessage","workspace","context","heldContext","installOutboundScreenGate","decideHeldOutboundSend","decision","decided","decidedAtMs","decidedByUid","actorUid","decidedByEmail","actorEmail","sendRef","moved","send","review","sendAtMs","staffReview","delete"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;2CAoC2C,GAE3C,SAASA,UAAU,QAAQ,SAAQ;AACnC,SAASC,uBAAuB,QAAQ,sCAAqC;AAC7E,SAASC,WAAW,QAAQ,qCAAoC;AAChE,SACEC,6BAA6B,EAC7BC,mBAAmB,EACnBC,0BAA0B,EAG1BC,mBAAmB,EACnBC,eAAe,QACV,oDAAmD;AAC1D,SAIEC,qBAAqB,QAChB,gDAA+C;AACtD,SAASC,UAAU,QAAQ,2BAA0B;AACrD,SAASC,UAAU,QAAQ,eAAW;AACtC,OAAOC,mBAAmB,sBAAkB;AAC5C,SAASC,WAAW,QAAQ,qBAAiB;AAE7C,wEAAwE,GACxE,SAASP,0BAA0B,GAAE;AAErC;;;;CAIC,GACD,OAAO,MAAMQ,kBAAkB,gBAAe;AAqC9C;;;;;;;CAOC,GACD,OAAO,SAASC,iBACdC,GAA+C,EAC/CC,QAAgBC,KAAKC,GAAG,EAAE;IAE1B,OAAOd,oBAAoBM,WAAWK,uBAAAA,IAAKI,SAAS,EAAEH;AACxD;AAEA;;;;CAIC,GACD,OAAO,SAASI,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