@aglyn/tenant-data-admin 1.0.0-beta.220 → 1.0.0-beta.221
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 +7 -7
- package/src/lib/server/hosted-page-review.d.ts +7 -0
- package/src/lib/server/hosted-page-review.js +131 -20
- package/src/lib/server/hosted-page-review.js.map +1 -1
- package/src/lib/server/leaving-notice.d.ts +98 -0
- package/src/lib/server/leaving-notice.js +250 -0
- package/src/lib/server/leaving-notice.js.map +1 -0
- package/src/lib/server/link-reputation-review.d.ts +54 -0
- package/src/lib/server/link-reputation-review.js +144 -0
- package/src/lib/server/link-reputation-review.js.map +1 -0
- package/src/lib/server/org-age.d.ts +15 -6
- package/src/lib/server/org-age.js +20 -8
- package/src/lib/server/org-age.js.map +1 -1
- package/src/lib/server/outbound-send-review.js +23 -6
- package/src/lib/server/outbound-send-review.js.map +1 -1
- package/src/lib/server/page-security-hold.d.ts +93 -0
- package/src/lib/server/page-security-hold.js +180 -0
- package/src/lib/server/page-security-hold.js.map +1 -0
- package/src/lib/server/web-risk.d.ts +100 -0
- package/src/lib/server/web-risk.js +383 -0
- package/src/lib/server/web-risk.js.map +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../../../libs/tenant/data/admin/src/lib/server/page-security-hold.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 * THE AUTOMATIC SECURITY HOLD (AGL-3450).\n *\n * The page screen (`hosted-page-review.ts`) holds a phishing page: the\n * version is not served and an urgent `phishing` row lands in the abuse\n * queue. When the workspace is in its first fortnight\n * ({@link isYoungWorkspaceAge}), a hold is also the moment to stop the\n * ACCOUNT: the two document-share harvesters found on 2026-10-01 were each\n * one page in a days-old workspace, and nothing stopped the same account\n * publishing the next one. So a held page from a young workspace places a\n * `security` lock on the workspace, on the site (`takedown`, so it stays down\n * through an outage) and on the account that published it.\n *\n * `security`, never `abuse`: `abuse` is a permanent ban, and a screen is not\n * a person. The hold is reversible and precautionary; staff confirm it and\n * re-place it as `abuse` by hand, or lift it on Staff → Lockdown, so a false\n * positive costs a support email rather than an account.\n *\n * ## Split by where each half can run\n *\n * The review runs wherever a page is composed — the published-site runtime\n * on every render, and the console's link re-check. The lock does not: it is\n * the staff lockdown path (`apps/console/utils/server/org-lockdown.ts` and\n * its neighbours), which revokes sessions, evicts the tenant cache, emails\n * the owners and writes the audit trail, and which only the console holds.\n * So this module is the DURABLE half: a `securityHolds/{orgId}` document,\n * created once, that says \"this workspace is to be held, and why\". The\n * console applies every pending one on its fifteen-minute tick and at the\n * end of any console sweep that held a page.\n *\n * ## Idempotent by construction\n *\n * One document per WORKSPACE, created in a transaction only when absent. A\n * second held page from the same workspace finds it and does nothing: no\n * second lock, notice or alert. Once staff have looked at a workspace and\n * lifted its hold, its next held page is held and queued for them as usual,\n * and is not locked again automatically — staff are the ones who know it now.\n *\n * ## Never the house\n *\n * A workspace that owns the platform's own marketing site\n * (`PLATFORM_MARKETING_HOST_ID`) is never held here; the console re-checks\n * that and refuses a workspace owned by a staff account before it locks\n * anything.\n *=========================================*/\n\nimport {\n describePhishingScreenSignals,\n isYoungWorkspaceAge,\n type PhishingScreenSignal,\n} from '@aglyn/shared-util-email/outbound-phishing-screen'\nimport firebaseAdmin from './firebase-admin'\nimport { platformMarketingHostId } from './platform-marketing-consent'\n\n/** Where a workspace's automatic hold is recorded: one document per workspace. */\nexport const SECURITY_HOLD_COLLECTION = 'securityHolds'\n\n/** The actor every automatic hold's audit rows name. */\nexport const SECURITY_HOLD_ACTOR_UID = 'system:page-screen'\n\n/** How long one console run may hold a claim before another may take it over. */\nexport const SECURITY_HOLD_CLAIM_LEASE_MS = 10 * 60_000\n\n/** The lock reason a hold places. Never `abuse`: see the module header. */\nexport const SECURITY_HOLD_LOCK_REASON = 'security'\n\n/**\n * - `pending` — requested; the console has not applied it yet.\n * - `applying` — a console run claimed it.\n * - `applied` — the locks were placed (see `outcome` for each).\n * - `skipped` — nothing was locked, and `outcome.skipped` says why: the house,\n * a staff-owned workspace, a workspace already gone.\n */\nexport type SecurityHoldState = 'pending' | 'applying' | 'applied' | 'skipped'\n\n/** What a held page asks the console to hold. */\nexport interface SecurityHoldRequest {\n orgId: string\n hostId: string\n screenId: string\n versionId: string\n /** The abuse row the page hold filed, and its `HS-…` reference. */\n reviewId: string\n reference: string\n signals: readonly PhishingScreenSignal[]\n ageDays: number | null\n /** The page, in the words the review row names it. */\n pageLabel: string\n pageUrl: string | null\n siteName: string | null\n}\n\n/** The stored document. */\nexport interface SecurityHoldRecord extends SecurityHoldRequest {\n state: SecurityHoldState\n /** The signals, in the words staff read on the abuse row. */\n evidence: string\n requestedAtMs: number\n claimedAtMs?: number | null\n settledAtMs?: number | null\n /** What the console did, step by step. */\n outcome?: Record<string, unknown> | null\n}\n\nexport type SecurityHoldRequestOutcome =\n | 'requested'\n | 'exists'\n | 'not-young'\n | 'no-workspace'\n | 'house'\n | 'failed'\n\n/**\n * Does this workspace own the platform's own marketing site? Read from the\n * org document the caller already holds, so asking costs nothing. `false` on\n * an install that names no marketing host.\n */\nexport function isHouseWorkspace(input: {\n hostId?: string | null\n org?: Record<string, unknown> | null\n}): boolean {\n const house = platformMarketingHostId()\n if (!house) return false\n if (input.hostId === house) return true\n const hosts = input.org?.['hosts']\n return Boolean(hosts && typeof hosts === 'object' && house in (hosts as Record<string, unknown>))\n}\n\n/** Workspaces this process already asked about, so a busy held page asks once. */\nconst requestedMemo = new Set<string>()\n\n/** Test seam: forget what this process asked. */\nexport function resetPageSecurityHoldMemoForTests(): void {\n requestedMemo.clear()\n}\n\nfunction holdsCollection(firestore?: FirebaseFirestore.Firestore) {\n return (firestore ?? firebaseAdmin.app().firestore()).collection(SECURITY_HOLD_COLLECTION)\n}\n\n/**\n * Ask for a held page's workspace to be held. Creates the workspace's hold\n * document when there is none; answers `exists` when there is. A workspace\n * that is not young, has no id or is the house is never held.\n *\n * Never throws: the page hold it rides on has already happened.\n */\nexport async function requestPageSecurityHold(\n request: SecurityHoldRequest & {\n org?: Record<string, unknown> | null\n nowMs?: number\n },\n): Promise<SecurityHoldRequestOutcome> {\n if (!isYoungWorkspaceAge(request.ageDays)) return 'not-young'\n if (!request.orgId) return 'no-workspace'\n if (isHouseWorkspace({ hostId: request.hostId, org: request.org })) return 'house'\n if (requestedMemo.has(request.orgId)) return 'exists'\n try {\n const firestore = firebaseAdmin.app().firestore()\n const ref = holdsCollection(firestore).doc(request.orgId)\n const record: SecurityHoldRecord = {\n orgId: request.orgId,\n hostId: request.hostId,\n screenId: request.screenId,\n versionId: request.versionId,\n reviewId: request.reviewId,\n reference: request.reference,\n signals: [...request.signals],\n ageDays: request.ageDays,\n pageLabel: String(request.pageLabel ?? '').slice(0, 300),\n pageUrl: request.pageUrl ? String(request.pageUrl).slice(0, 500) : null,\n siteName: request.siteName ? String(request.siteName).slice(0, 200) : null,\n evidence: describePhishingScreenSignals(request.signals).join(' ').slice(0, 2000),\n state: 'pending',\n requestedAtMs: request.nowMs ?? Date.now(),\n claimedAtMs: null,\n settledAtMs: null,\n outcome: null,\n }\n const created = await firestore.runTransaction(async (transaction) => {\n const existing = await transaction.get(ref)\n if (existing.exists) return false\n transaction.set(ref, record)\n return true\n })\n requestedMemo.add(request.orgId)\n return created ? 'requested' : 'exists'\n } catch (error) {\n console.error('[security-hold] the hold could not be requested', request.orgId, error)\n return 'failed'\n }\n}\n\n/** The pending holds, oldest first by nothing more than the store's order. */\nexport async function listPendingSecurityHolds(\n firestore: FirebaseFirestore.Firestore,\n limit = 20,\n): Promise<SecurityHoldRecord[]> {\n const snapshot = await holdsCollection(firestore)\n .where('state', 'in', ['pending', 'applying'])\n .limit(limit)\n .get()\n return snapshot.docs.map((doc) => doc.data() as SecurityHoldRecord)\n}\n\n/**\n * Claim one hold for this run. A hold another run claimed inside the lease\n * is left to it; one whose claim lapsed (that run died) is taken over.\n * Answers the claimed record, or null.\n */\nexport async function claimSecurityHold(\n firestore: FirebaseFirestore.Firestore,\n orgId: string,\n nowMs: number = Date.now(),\n): Promise<SecurityHoldRecord | null> {\n const ref = holdsCollection(firestore).doc(orgId)\n return firestore.runTransaction(async (transaction) => {\n const snapshot = await transaction.get(ref)\n if (!snapshot.exists) return null\n const record = snapshot.data() as SecurityHoldRecord\n const claimLive =\n record.state === 'applying' &&\n typeof record.claimedAtMs === 'number' &&\n nowMs - record.claimedAtMs < SECURITY_HOLD_CLAIM_LEASE_MS\n if (record.state !== 'pending' && !(record.state === 'applying' && !claimLive)) return null\n transaction.set(ref, { state: 'applying', claimedAtMs: nowMs }, { merge: true })\n return { ...record, state: 'applying', claimedAtMs: nowMs }\n })\n}\n\n/** Record what the console did with a claimed hold. */\nexport async function settleSecurityHold(\n firestore: FirebaseFirestore.Firestore,\n orgId: string,\n state: 'applied' | 'skipped',\n outcome: Record<string, unknown>,\n nowMs: number = Date.now(),\n): Promise<void> {\n await holdsCollection(firestore)\n .doc(orgId)\n .set({ state, outcome, settledAtMs: nowMs }, { merge: true })\n}\n"],"names":["describePhishingScreenSignals","isYoungWorkspaceAge","firebaseAdmin","platformMarketingHostId","SECURITY_HOLD_COLLECTION","SECURITY_HOLD_ACTOR_UID","SECURITY_HOLD_CLAIM_LEASE_MS","SECURITY_HOLD_LOCK_REASON","isHouseWorkspace","input","house","hostId","hosts","org","Boolean","requestedMemo","Set","resetPageSecurityHoldMemoForTests","clear","holdsCollection","firestore","app","collection","requestPageSecurityHold","request","ageDays","orgId","has","ref","doc","record","screenId","versionId","reviewId","reference","signals","pageLabel","String","slice","pageUrl","siteName","evidence","join","state","requestedAtMs","nowMs","Date","now","claimedAtMs","settledAtMs","outcome","created","runTransaction","transaction","existing","get","exists","set","add","error","console","listPendingSecurityHolds","limit","snapshot","where","docs","map","data","claimSecurityHold","claimLive","merge","settleSecurityHold"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;2CA4C2C,GAE3C,SACEA,6BAA6B,EAC7BC,mBAAmB,QAEd,oDAAmD;AAC1D,OAAOC,mBAAmB,sBAAkB;AAC5C,SAASC,uBAAuB,QAAQ,kCAA8B;AAEtE,gFAAgF,GAChF,OAAO,MAAMC,2BAA2B,gBAAe;AAEvD,sDAAsD,GACtD,OAAO,MAAMC,0BAA0B,qBAAoB;AAE3D,+EAA+E,GAC/E,OAAO,MAAMC,+BAA+B,KAAK,MAAM;AAEvD,yEAAyE,GACzE,OAAO,MAAMC,4BAA4B,WAAU;AAgDnD;;;;CAIC,GACD,OAAO,SAASC,iBAAiBC,KAGhC;QAIeA;IAHd,MAAMC,QAAQP;IACd,IAAI,CAACO,OAAO,OAAO;IACnB,IAAID,MAAME,MAAM,KAAKD,OAAO,OAAO;IACnC,MAAME,SAAQH,aAAAA,MAAMI,GAAG,qBAATJ,UAAW,CAAC,QAAQ;IAClC,OAAOK,QAAQF,SAAS,OAAOA,UAAU,YAAYF,SAAUE;AACjE;AAEA,gFAAgF,GAChF,MAAMG,gBAAgB,IAAIC;AAE1B,+CAA+C,GAC/C,OAAO,SAASC;IACdF,cAAcG,KAAK;AACrB;AAEA,SAASC,gBAAgBC,SAAuC;IAC9D,OAAO,CAACA,oBAAAA,YAAalB,cAAcmB,GAAG,GAAGD,SAAS,IAAIE,UAAU,CAAClB;AACnE;AAEA;;;;;;CAMC,GACD,OAAO,eAAemB,wBACpBC,OAGC;IAED,IAAI,CAACvB,oBAAoBuB,QAAQC,OAAO,GAAG,OAAO;IAClD,IAAI,CAACD,QAAQE,K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,KAAa,EACbiB,KAA4B,EAC5BO,OAAgC,EAChCL,QAAgBC,KAAKC,GAAG,EAAE;IAE1B,MAAM5B,gBAAgBC,WACnBS,GAAG,CAACH,OACJ+B,GAAG,CAAC;QAAEd;QAAOO;QAASD,aAAaJ;IAAM,GAAG;QAAEyB,OAAO;IAAK;AAC/D"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Aglyn LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { type LinkReputationAnswer, type LinkThreatType } from '@aglyn/shared-util-email/link-reputation';
|
|
18
|
+
export declare const WEB_RISK_SEARCH_ENDPOINT = "https://webrisk.googleapis.com/v1/uris:search";
|
|
19
|
+
export declare const WEB_RISK_CACHE_COLLECTION = "webRiskVerdicts";
|
|
20
|
+
/** The kill switch: `{ enabled: false }` turns every lookup off. */
|
|
21
|
+
export declare const WEB_RISK_SETTINGS_COLLECTION = "platformSettings";
|
|
22
|
+
export declare const WEB_RISK_SETTINGS_DOC = "webRisk";
|
|
23
|
+
/** One call's budget. */
|
|
24
|
+
export declare const WEB_RISK_TIMEOUT_MS = 1000;
|
|
25
|
+
/** One lookup's budget, however many hosts it asks about. */
|
|
26
|
+
export declare const WEB_RISK_DEADLINE_MS = 1200;
|
|
27
|
+
/** How long a clean answer is kept in the store. */
|
|
28
|
+
export declare const WEB_RISK_CLEAN_TTL_MS: number;
|
|
29
|
+
/** How long a clean answer is trusted from this process's memory. */
|
|
30
|
+
export declare const WEB_RISK_MEMORY_CLEAN_TTL_MS: number;
|
|
31
|
+
/** A listing whose answer named no `expireTime` is kept this long. */
|
|
32
|
+
export declare const WEB_RISK_LISTED_FALLBACK_TTL_MS: number;
|
|
33
|
+
/** What `uris.search` answered for one URI. */
|
|
34
|
+
export interface WebRiskSearchResult {
|
|
35
|
+
/** The lists it is on; empty when it is on none. */
|
|
36
|
+
threats: LinkThreatType[];
|
|
37
|
+
/** Until when the answer may be kept, when the API said. */
|
|
38
|
+
expireTimeMs: number | null;
|
|
39
|
+
}
|
|
40
|
+
/** The contract: one URI, its listing. Throws on anything but an answer. */
|
|
41
|
+
export interface WebRiskClient {
|
|
42
|
+
searchUri(uri: string): Promise<WebRiskSearchResult>;
|
|
43
|
+
}
|
|
44
|
+
/** The API answered with a status that is not an answer. */
|
|
45
|
+
export declare class WebRiskHttpError extends Error {
|
|
46
|
+
readonly status: number;
|
|
47
|
+
constructor(status: number, detail: string);
|
|
48
|
+
}
|
|
49
|
+
/** There is no credential to call with. */
|
|
50
|
+
export declare class WebRiskUnavailableError extends Error {
|
|
51
|
+
constructor(reason: string);
|
|
52
|
+
}
|
|
53
|
+
/** `uris.search`'s body, read defensively: an empty object is "not listed". */
|
|
54
|
+
export declare function parseWebRiskSearch(body: unknown): WebRiskSearchResult;
|
|
55
|
+
/** The URI a host is looked up as. Only the host: never a path or a query. */
|
|
56
|
+
export declare function webRiskUriForHost(host: string): string;
|
|
57
|
+
/** The default client: `uris.search` over HTTPS with a bearer token. */
|
|
58
|
+
export declare function createWebRiskHttpClient(options: {
|
|
59
|
+
accessToken: () => Promise<string | null>;
|
|
60
|
+
fetch?: typeof fetch;
|
|
61
|
+
timeoutMs?: number;
|
|
62
|
+
endpoint?: string;
|
|
63
|
+
}): WebRiskClient;
|
|
64
|
+
/** Test seam: forget the caches, the switch, the back-off; optionally stand in a client. */
|
|
65
|
+
export declare function resetWebRiskForTests(client?: WebRiskClient | null): void;
|
|
66
|
+
/**
|
|
67
|
+
* Hosts that are the platform's own: every site's subdomain on the tenant
|
|
68
|
+
* apex. Never looked up — a neighbour's site is screened on its own page.
|
|
69
|
+
*/
|
|
70
|
+
export declare function platformReputationExclusions(): string[];
|
|
71
|
+
export interface HostReputationLookupOptions {
|
|
72
|
+
/** How long the caller will wait, overall. */
|
|
73
|
+
deadlineMs?: number;
|
|
74
|
+
/** The most hosts asked about; the rest read unknown. */
|
|
75
|
+
maxHosts?: number;
|
|
76
|
+
nowMs?: number;
|
|
77
|
+
}
|
|
78
|
+
/** What a lookup answered, and how many hosts it had to ask the API about. */
|
|
79
|
+
export interface HostReputationLookupResult extends LinkReputationAnswer {
|
|
80
|
+
looked: number;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The reputation of each host: listed, clean, or unknown. Platform hosts are
|
|
84
|
+
* clean without asking. Never throws, and never waits longer than the
|
|
85
|
+
* deadline: a call still running then finishes in the background and fills
|
|
86
|
+
* the caches for the next asker.
|
|
87
|
+
*
|
|
88
|
+
* "Never throws" covers the store as well as the API: a missing app, a
|
|
89
|
+
* settings read that fails before it is a promise, or a cache reference
|
|
90
|
+
* that cannot be built all read every host as unknown. The page review
|
|
91
|
+
* calls this before its own fail-closed `try`, so a throw here would take
|
|
92
|
+
* down every page with a link off the site.
|
|
93
|
+
*/
|
|
94
|
+
export declare function lookupHostReputation(hosts: readonly string[], options?: HostReputationLookupOptions): Promise<HostReputationLookupResult>;
|
|
95
|
+
/**
|
|
96
|
+
* Puts this lookup behind the shared screen's reputation seam, so the send
|
|
97
|
+
* seam (`screenTenantMessage`) asks it. Called by
|
|
98
|
+
* `installOutboundScreenGate`, at that module's load.
|
|
99
|
+
*/
|
|
100
|
+
export declare function installLinkReputationLookup(): void;
|
|
@@ -0,0 +1,383 @@
|
|
|
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
|
+
* GOOGLE WEB RISK (AGL-3451): the lookup behind the link reputation screen.
|
|
19
|
+
*
|
|
20
|
+
* `@aglyn/shared-util-email/link-reputation` decides which hosts are foreign
|
|
21
|
+
* and turns a listing into a strong `web-risk-link` signal. This is the store
|
|
22
|
+
* half: the `uris.search` client, the cache, the kill switch.
|
|
23
|
+
*
|
|
24
|
+
* ## The client
|
|
25
|
+
*
|
|
26
|
+
* {@link WebRiskClient} is the contract: one URI in, the threat types it is
|
|
27
|
+
* listed for and how long that answer may be kept. The default
|
|
28
|
+
* implementation calls `webrisk.googleapis.com/v1/uris:search` for
|
|
29
|
+
* SOCIAL_ENGINEERING, MALWARE and UNWANTED_SOFTWARE as the platform's own
|
|
30
|
+
* service account — the firebase-admin credential every other server-side
|
|
31
|
+
* Google call here already uses (`client-error-report.ts`), so there is no
|
|
32
|
+
* key of its own. Only `https://<host>/` is ever sent: see the module header
|
|
33
|
+
* of `link-reputation.ts`.
|
|
34
|
+
*
|
|
35
|
+
* ## Not evidence when it fails
|
|
36
|
+
*
|
|
37
|
+
* A call is given {@link WEB_RISK_TIMEOUT_MS}, and a whole lookup
|
|
38
|
+
* {@link WEB_RISK_DEADLINE_MS}. A host whose answer did not arrive, or whose
|
|
39
|
+
* call failed, is UNKNOWN: it holds nothing and logs a warning. A credential
|
|
40
|
+
* the API refuses (the API is not enabled on a self-hosted project, say) backs
|
|
41
|
+
* the lookup off for a quarter of an hour, so a misconfiguration is one log
|
|
42
|
+
* line rather than one per page.
|
|
43
|
+
*
|
|
44
|
+
* ## The cache
|
|
45
|
+
*
|
|
46
|
+
* Per host, in two layers:
|
|
47
|
+
*
|
|
48
|
+
* - this process's memory — a listed host until the answer's `expireTime`,
|
|
49
|
+
* which Web Risk sets and which may not be exceeded; a clean one for
|
|
50
|
+
* {@link WEB_RISK_MEMORY_CLEAN_TTL_MS}, short so a listing the daily
|
|
51
|
+
* re-check writes reaches every warm render within minutes;
|
|
52
|
+
* - `webRiskVerdicts/{host}` — server-only, one document per host, a clean
|
|
53
|
+
* answer kept {@link WEB_RISK_CLEAN_TTL_MS}. Bounded by the number of
|
|
54
|
+
* distinct foreign hosts and rewritten in place, so it needs no TTL policy.
|
|
55
|
+
*
|
|
56
|
+
* Read in one `getAll` per lookup, so a page's twenty hosts cost one round
|
|
57
|
+
* trip, and nothing at all once they are in memory.
|
|
58
|
+
*
|
|
59
|
+
* ## The kill switch
|
|
60
|
+
*
|
|
61
|
+
* `platformSettings/webRisk` `{ enabled: false }` stops every lookup within a
|
|
62
|
+
* minute — every host reads unknown, so nothing holds on it — without a
|
|
63
|
+
* deploy. Absent or anything else means on.
|
|
64
|
+
*=========================================*/ import { TENANT_APEX } from "@aglyn/aglyn/app-utils/tenant-apex";
|
|
65
|
+
import { LINK_THREAT_TYPES, MAX_REPUTATION_HOSTS_PER_LOOKUP, normalizeReputationHost, setLinkReputationLookup } from "@aglyn/shared-util-email/link-reputation";
|
|
66
|
+
import { getApp } from "firebase-admin/app";
|
|
67
|
+
import firebaseAdmin from "./firebase-admin.js";
|
|
68
|
+
export const WEB_RISK_SEARCH_ENDPOINT = 'https://webrisk.googleapis.com/v1/uris:search';
|
|
69
|
+
export const WEB_RISK_CACHE_COLLECTION = 'webRiskVerdicts';
|
|
70
|
+
/** The kill switch: `{ enabled: false }` turns every lookup off. */ export const WEB_RISK_SETTINGS_COLLECTION = 'platformSettings';
|
|
71
|
+
export const WEB_RISK_SETTINGS_DOC = 'webRisk';
|
|
72
|
+
/** One call's budget. */ export const WEB_RISK_TIMEOUT_MS = 1000;
|
|
73
|
+
/** One lookup's budget, however many hosts it asks about. */ export const WEB_RISK_DEADLINE_MS = 1200;
|
|
74
|
+
/** How long a clean answer is kept in the store. */ export const WEB_RISK_CLEAN_TTL_MS = 12 * 60 * 60000;
|
|
75
|
+
/** How long a clean answer is trusted from this process's memory. */ export const WEB_RISK_MEMORY_CLEAN_TTL_MS = 10 * 60000;
|
|
76
|
+
/** A listing whose answer named no `expireTime` is kept this long. */ export const WEB_RISK_LISTED_FALLBACK_TTL_MS = 5 * 60000;
|
|
77
|
+
/** How long the switch's answer is trusted. */ const SWITCH_TTL_MS = 60000;
|
|
78
|
+
/** How long a refused credential stops the lookup. */ const REFUSED_BACKOFF_MS = 15 * 60000;
|
|
79
|
+
/** How long a rate limit stops the lookup. */ const THROTTLED_BACKOFF_MS = 60000;
|
|
80
|
+
/** At most one warning per this long, so an outage is not a log flood. */ const WARN_EVERY_MS = 60000;
|
|
81
|
+
/** The API answered with a status that is not an answer. */ export class WebRiskHttpError extends Error {
|
|
82
|
+
constructor(status, detail){
|
|
83
|
+
super(`Web Risk answered ${status}: ${detail.slice(0, 200)}`), this.status = status;
|
|
84
|
+
this.name = 'WebRiskHttpError';
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/** There is no credential to call with. */ export class WebRiskUnavailableError extends Error {
|
|
88
|
+
constructor(reason){
|
|
89
|
+
super(reason);
|
|
90
|
+
this.name = 'WebRiskUnavailableError';
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/** `uris.search`'s body, read defensively: an empty object is "not listed". */ export function parseWebRiskSearch(body) {
|
|
94
|
+
const threat = body == null ? void 0 : body.threat;
|
|
95
|
+
if (!threat || typeof threat !== 'object') return {
|
|
96
|
+
threats: [],
|
|
97
|
+
expireTimeMs: null
|
|
98
|
+
};
|
|
99
|
+
const listed = Array.isArray(threat.threatTypes) ? threat.threatTypes : [];
|
|
100
|
+
const threats = LINK_THREAT_TYPES.filter((type)=>listed.includes(type));
|
|
101
|
+
const expireTimeMs = typeof threat.expireTime === 'string' ? Date.parse(threat.expireTime) : Number.NaN;
|
|
102
|
+
return {
|
|
103
|
+
threats,
|
|
104
|
+
expireTimeMs: Number.isFinite(expireTimeMs) ? expireTimeMs : null
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/** The URI a host is looked up as. Only the host: never a path or a query. */ export function webRiskUriForHost(host) {
|
|
108
|
+
return `https://${host}/`;
|
|
109
|
+
}
|
|
110
|
+
/** The default client: `uris.search` over HTTPS with a bearer token. */ export function createWebRiskHttpClient(options) {
|
|
111
|
+
var _options_fetch, _options_timeoutMs;
|
|
112
|
+
const request = (_options_fetch = options.fetch) != null ? _options_fetch : fetch;
|
|
113
|
+
const timeoutMs = (_options_timeoutMs = options.timeoutMs) != null ? _options_timeoutMs : WEB_RISK_TIMEOUT_MS;
|
|
114
|
+
return {
|
|
115
|
+
async searchUri (uri) {
|
|
116
|
+
var _options_endpoint;
|
|
117
|
+
const token = await options.accessToken();
|
|
118
|
+
if (!token) throw new WebRiskUnavailableError('no service-account credential');
|
|
119
|
+
const url = new URL((_options_endpoint = options.endpoint) != null ? _options_endpoint : WEB_RISK_SEARCH_ENDPOINT);
|
|
120
|
+
for (const type of LINK_THREAT_TYPES)url.searchParams.append('threatTypes', type);
|
|
121
|
+
url.searchParams.set('uri', uri);
|
|
122
|
+
const controller = new AbortController();
|
|
123
|
+
const timer = setTimeout(()=>controller.abort(), timeoutMs);
|
|
124
|
+
try {
|
|
125
|
+
const response = await request(url.toString(), {
|
|
126
|
+
method: 'GET',
|
|
127
|
+
headers: {
|
|
128
|
+
Authorization: `Bearer ${token}`,
|
|
129
|
+
Accept: 'application/json'
|
|
130
|
+
},
|
|
131
|
+
signal: controller.signal
|
|
132
|
+
});
|
|
133
|
+
if (!response.ok) {
|
|
134
|
+
throw new WebRiskHttpError(response.status, await response.text().catch(()=>''));
|
|
135
|
+
}
|
|
136
|
+
return parseWebRiskSearch(await response.json());
|
|
137
|
+
} finally{
|
|
138
|
+
clearTimeout(timer);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* The platform service account's access token, or null when this deployment
|
|
145
|
+
* has none — the same credential `client-error-report.ts` writes logs with.
|
|
146
|
+
*/ async function serviceAccountAccessToken() {
|
|
147
|
+
try {
|
|
148
|
+
var _ref;
|
|
149
|
+
var _this;
|
|
150
|
+
const credential = getApp().options.credential;
|
|
151
|
+
if (!credential) return null;
|
|
152
|
+
return (_ref = (_this = await credential.getAccessToken()) == null ? void 0 : _this.access_token) != null ? _ref : null;
|
|
153
|
+
} catch (unused) {
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
let defaultClient = null;
|
|
158
|
+
let clientOverride;
|
|
159
|
+
function activeClient() {
|
|
160
|
+
if (clientOverride !== undefined) return clientOverride;
|
|
161
|
+
defaultClient != null ? defaultClient : defaultClient = createWebRiskHttpClient({
|
|
162
|
+
accessToken: serviceAccountAccessToken
|
|
163
|
+
});
|
|
164
|
+
return defaultClient;
|
|
165
|
+
}
|
|
166
|
+
const memory = new Map();
|
|
167
|
+
let switchMemo = null;
|
|
168
|
+
let backoffUntilMs = 0;
|
|
169
|
+
let lastWarnAtMs = 0;
|
|
170
|
+
/** Test seam: forget the caches, the switch, the back-off; optionally stand in a client. */ export function resetWebRiskForTests(client) {
|
|
171
|
+
memory.clear();
|
|
172
|
+
switchMemo = null;
|
|
173
|
+
backoffUntilMs = 0;
|
|
174
|
+
lastWarnAtMs = 0;
|
|
175
|
+
clientOverride = client;
|
|
176
|
+
defaultClient = null;
|
|
177
|
+
}
|
|
178
|
+
function warn(nowMs, message, error) {
|
|
179
|
+
if (nowMs - lastWarnAtMs < WARN_EVERY_MS) return;
|
|
180
|
+
lastWarnAtMs = nowMs;
|
|
181
|
+
console.warn(`[web-risk] ${message} — the host reads unknown, which is not evidence`, error != null ? error : '');
|
|
182
|
+
}
|
|
183
|
+
/** Is the lookup switched off? Read at most once a minute; an unreadable switch is on. */ async function switchedOff(nowMs) {
|
|
184
|
+
if (switchMemo && nowMs - switchMemo.atMs < SWITCH_TTL_MS) return switchMemo.off;
|
|
185
|
+
const off = await firebaseAdmin.app().firestore().collection(WEB_RISK_SETTINGS_COLLECTION).doc(WEB_RISK_SETTINGS_DOC).get().then((snapshot)=>snapshot.get('enabled') === false, ()=>false);
|
|
186
|
+
switchMemo = {
|
|
187
|
+
off,
|
|
188
|
+
atMs: nowMs
|
|
189
|
+
};
|
|
190
|
+
return off;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Hosts that are the platform's own: every site's subdomain on the tenant
|
|
194
|
+
* apex. Never looked up — a neighbour's site is screened on its own page.
|
|
195
|
+
*/ export function platformReputationExclusions() {
|
|
196
|
+
return [
|
|
197
|
+
TENANT_APEX
|
|
198
|
+
].filter(Boolean);
|
|
199
|
+
}
|
|
200
|
+
function isPlatformHost(host) {
|
|
201
|
+
return platformReputationExclusions().some((domain)=>host === domain || host.endsWith(`.${domain}`));
|
|
202
|
+
}
|
|
203
|
+
function verdictFrom(data) {
|
|
204
|
+
if (!data) return null;
|
|
205
|
+
const threats = Array.isArray(data['threats']) ? LINK_THREAT_TYPES.filter((type)=>data['threats'].includes(type)) : [];
|
|
206
|
+
const checkedAtMs = Number(data['checkedAtMs']);
|
|
207
|
+
const expiresAtMs = Number(data['expiresAtMs']);
|
|
208
|
+
if (!Number.isFinite(checkedAtMs) || !Number.isFinite(expiresAtMs)) return null;
|
|
209
|
+
return {
|
|
210
|
+
threats,
|
|
211
|
+
checkedAtMs,
|
|
212
|
+
expiresAtMs
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function remember(host, verdict, nowMs) {
|
|
216
|
+
memory.set(host, _extends({}, verdict, {
|
|
217
|
+
memoryUntilMs: verdict.threats.length ? verdict.expiresAtMs : Math.min(verdict.expiresAtMs, nowMs + WEB_RISK_MEMORY_CLEAN_TTL_MS)
|
|
218
|
+
}));
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* The reputation of each host: listed, clean, or unknown. Platform hosts are
|
|
222
|
+
* clean without asking. Never throws, and never waits longer than the
|
|
223
|
+
* deadline: a call still running then finishes in the background and fills
|
|
224
|
+
* the caches for the next asker.
|
|
225
|
+
*
|
|
226
|
+
* "Never throws" covers the store as well as the API: a missing app, a
|
|
227
|
+
* settings read that fails before it is a promise, or a cache reference
|
|
228
|
+
* that cannot be built all read every host as unknown. The page review
|
|
229
|
+
* calls this before its own fail-closed `try`, so a throw here would take
|
|
230
|
+
* down every page with a link off the site.
|
|
231
|
+
*/ export async function lookupHostReputation(hosts, options = {}) {
|
|
232
|
+
try {
|
|
233
|
+
return await lookupHostReputationOrThrow(hosts, options);
|
|
234
|
+
} catch (error) {
|
|
235
|
+
var _options_nowMs;
|
|
236
|
+
warn((_options_nowMs = options.nowMs) != null ? _options_nowMs : Date.now(), 'a lookup failed before it could answer', error);
|
|
237
|
+
const unknown = [
|
|
238
|
+
...new Set(hosts.map((raw)=>normalizeReputationHost(raw)).filter((host)=>Boolean(host)))
|
|
239
|
+
];
|
|
240
|
+
return {
|
|
241
|
+
hits: [],
|
|
242
|
+
clean: [],
|
|
243
|
+
unknown,
|
|
244
|
+
looked: 0
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
async function lookupHostReputationOrThrow(hosts, options) {
|
|
249
|
+
var _options_nowMs;
|
|
250
|
+
const nowMs = (_options_nowMs = options.nowMs) != null ? _options_nowMs : Date.now();
|
|
251
|
+
const result = {
|
|
252
|
+
hits: [],
|
|
253
|
+
clean: [],
|
|
254
|
+
unknown: [],
|
|
255
|
+
looked: 0
|
|
256
|
+
};
|
|
257
|
+
const wanted = [];
|
|
258
|
+
for (const raw of hosts){
|
|
259
|
+
var _options_maxHosts;
|
|
260
|
+
const host = normalizeReputationHost(raw);
|
|
261
|
+
if (!host || wanted.includes(host) || result.clean.includes(host)) continue;
|
|
262
|
+
if (isPlatformHost(host)) result.clean.push(host);
|
|
263
|
+
else if (wanted.length < ((_options_maxHosts = options.maxHosts) != null ? _options_maxHosts : MAX_REPUTATION_HOSTS_PER_LOOKUP)) wanted.push(host);
|
|
264
|
+
else result.unknown.push(host);
|
|
265
|
+
}
|
|
266
|
+
if (!wanted.length) return result;
|
|
267
|
+
const settle = (host, verdict)=>{
|
|
268
|
+
if (verdict.threats.length) result.hits.push({
|
|
269
|
+
host,
|
|
270
|
+
threats: verdict.threats
|
|
271
|
+
});
|
|
272
|
+
else result.clean.push(host);
|
|
273
|
+
};
|
|
274
|
+
// 1. This process's memory.
|
|
275
|
+
let missing = wanted.filter((host)=>{
|
|
276
|
+
const known = memory.get(host);
|
|
277
|
+
if (known && nowMs < known.memoryUntilMs) {
|
|
278
|
+
settle(host, known);
|
|
279
|
+
return false;
|
|
280
|
+
}
|
|
281
|
+
return true;
|
|
282
|
+
});
|
|
283
|
+
if (!missing.length) return result;
|
|
284
|
+
// 2. The switch. Off reads every remaining host as unknown.
|
|
285
|
+
if (await switchedOff(nowMs)) {
|
|
286
|
+
result.unknown.push(...missing);
|
|
287
|
+
return result;
|
|
288
|
+
}
|
|
289
|
+
// 3. The store, in one round trip.
|
|
290
|
+
const firestore = firebaseAdmin.app().firestore();
|
|
291
|
+
const collection = firestore.collection(WEB_RISK_CACHE_COLLECTION);
|
|
292
|
+
try {
|
|
293
|
+
const snapshots = await firestore.getAll(...missing.map((host)=>collection.doc(host)));
|
|
294
|
+
const stored = new Map();
|
|
295
|
+
for (const snapshot of snapshots){
|
|
296
|
+
const verdict = verdictFrom(snapshot.exists ? snapshot.data() : undefined);
|
|
297
|
+
if (verdict && nowMs < verdict.expiresAtMs) stored.set(snapshot.id, verdict);
|
|
298
|
+
}
|
|
299
|
+
missing = missing.filter((host)=>{
|
|
300
|
+
const verdict = stored.get(host);
|
|
301
|
+
if (!verdict) return true;
|
|
302
|
+
remember(host, verdict, nowMs);
|
|
303
|
+
settle(host, verdict);
|
|
304
|
+
return false;
|
|
305
|
+
});
|
|
306
|
+
} catch (error) {
|
|
307
|
+
warn(nowMs, 'the verdict cache could not be read', error);
|
|
308
|
+
}
|
|
309
|
+
if (!missing.length) return result;
|
|
310
|
+
// 4. The API, every remaining host at once, within the deadline.
|
|
311
|
+
const client = activeClient();
|
|
312
|
+
if (!client || nowMs < backoffUntilMs) {
|
|
313
|
+
result.unknown.push(...missing);
|
|
314
|
+
return result;
|
|
315
|
+
}
|
|
316
|
+
result.looked = missing.length;
|
|
317
|
+
const answers = new Map();
|
|
318
|
+
const calls = missing.map(async (host)=>{
|
|
319
|
+
try {
|
|
320
|
+
var _answer_expireTimeMs;
|
|
321
|
+
const answer = await client.searchUri(webRiskUriForHost(host));
|
|
322
|
+
const checkedAtMs = Date.now();
|
|
323
|
+
const verdict = {
|
|
324
|
+
threats: answer.threats,
|
|
325
|
+
checkedAtMs,
|
|
326
|
+
expiresAtMs: answer.threats.length ? (_answer_expireTimeMs = answer.expireTimeMs) != null ? _answer_expireTimeMs : checkedAtMs + WEB_RISK_LISTED_FALLBACK_TTL_MS : checkedAtMs + WEB_RISK_CLEAN_TTL_MS
|
|
327
|
+
};
|
|
328
|
+
answers.set(host, verdict);
|
|
329
|
+
remember(host, verdict, checkedAtMs);
|
|
330
|
+
await collection.doc(host).set(_extends({
|
|
331
|
+
host
|
|
332
|
+
}, verdict, {
|
|
333
|
+
listed: verdict.threats.length > 0
|
|
334
|
+
})).catch((error)=>warn(checkedAtMs, 'a verdict could not be cached', error));
|
|
335
|
+
} catch (error) {
|
|
336
|
+
if (error instanceof WebRiskHttpError && [
|
|
337
|
+
401,
|
|
338
|
+
403,
|
|
339
|
+
404
|
|
340
|
+
].includes(error.status)) {
|
|
341
|
+
backoffUntilMs = Date.now() + REFUSED_BACKOFF_MS;
|
|
342
|
+
console.error('[web-risk] the API refused the credential — lookups paused for 15 minutes', error);
|
|
343
|
+
} else if (error instanceof WebRiskHttpError && error.status === 429) {
|
|
344
|
+
backoffUntilMs = Date.now() + THROTTLED_BACKOFF_MS;
|
|
345
|
+
warn(Date.now(), 'the API is rate limiting', error);
|
|
346
|
+
} else if (error instanceof WebRiskUnavailableError) {
|
|
347
|
+
backoffUntilMs = Date.now() + REFUSED_BACKOFF_MS;
|
|
348
|
+
warn(Date.now(), 'no credential to call the API with', error);
|
|
349
|
+
} else {
|
|
350
|
+
warn(Date.now(), `the lookup of ${host} failed or timed out`, error);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
let timer;
|
|
355
|
+
await Promise.race([
|
|
356
|
+
Promise.allSettled(calls),
|
|
357
|
+
new Promise((resolve)=>{
|
|
358
|
+
var _options_deadlineMs;
|
|
359
|
+
timer = setTimeout(resolve, (_options_deadlineMs = options.deadlineMs) != null ? _options_deadlineMs : WEB_RISK_DEADLINE_MS);
|
|
360
|
+
})
|
|
361
|
+
]);
|
|
362
|
+
if (timer) clearTimeout(timer);
|
|
363
|
+
const late = missing.filter((host)=>!answers.has(host));
|
|
364
|
+
for (const host of missing){
|
|
365
|
+
const verdict = answers.get(host);
|
|
366
|
+
if (verdict) settle(host, verdict);
|
|
367
|
+
}
|
|
368
|
+
result.unknown.push(...late);
|
|
369
|
+
if (late.length) warn(Date.now(), `${late.length} host(s) had no answer within the deadline`);
|
|
370
|
+
return result;
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Puts this lookup behind the shared screen's reputation seam, so the send
|
|
374
|
+
* seam (`screenTenantMessage`) asks it. Called by
|
|
375
|
+
* `installOutboundScreenGate`, at that module's load.
|
|
376
|
+
*/ export function installLinkReputationLookup() {
|
|
377
|
+
if (typeof setLinkReputationLookup !== 'function') return;
|
|
378
|
+
setLinkReputationLookup((hosts, options)=>lookupHostReputation(hosts, {
|
|
379
|
+
deadlineMs: options == null ? void 0 : options.deadlineMs
|
|
380
|
+
}));
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
//# sourceMappingURL=web-risk.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../../../../libs/tenant/data/admin/src/lib/server/web-risk.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 * GOOGLE WEB RISK (AGL-3451): the lookup behind the link reputation screen.\n *\n * `@aglyn/shared-util-email/link-reputation` decides which hosts are foreign\n * and turns a listing into a strong `web-risk-link` signal. This is the store\n * half: the `uris.search` client, the cache, the kill switch.\n *\n * ## The client\n *\n * {@link WebRiskClient} is the contract: one URI in, the threat types it is\n * listed for and how long that answer may be kept. The default\n * implementation calls `webrisk.googleapis.com/v1/uris:search` for\n * SOCIAL_ENGINEERING, MALWARE and UNWANTED_SOFTWARE as the platform's own\n * service account — the firebase-admin credential every other server-side\n * Google call here already uses (`client-error-report.ts`), so there is no\n * key of its own. Only `https://<host>/` is ever sent: see the module header\n * of `link-reputation.ts`.\n *\n * ## Not evidence when it fails\n *\n * A call is given {@link WEB_RISK_TIMEOUT_MS}, and a whole lookup\n * {@link WEB_RISK_DEADLINE_MS}. A host whose answer did not arrive, or whose\n * call failed, is UNKNOWN: it holds nothing and logs a warning. A credential\n * the API refuses (the API is not enabled on a self-hosted project, say) backs\n * the lookup off for a quarter of an hour, so a misconfiguration is one log\n * line rather than one per page.\n *\n * ## The cache\n *\n * Per host, in two layers:\n *\n * - this process's memory — a listed host until the answer's `expireTime`,\n * which Web Risk sets and which may not be exceeded; a clean one for\n * {@link WEB_RISK_MEMORY_CLEAN_TTL_MS}, short so a listing the daily\n * re-check writes reaches every warm render within minutes;\n * - `webRiskVerdicts/{host}` — server-only, one document per host, a clean\n * answer kept {@link WEB_RISK_CLEAN_TTL_MS}. Bounded by the number of\n * distinct foreign hosts and rewritten in place, so it needs no TTL policy.\n *\n * Read in one `getAll` per lookup, so a page's twenty hosts cost one round\n * trip, and nothing at all once they are in memory.\n *\n * ## The kill switch\n *\n * `platformSettings/webRisk` `{ enabled: false }` stops every lookup within a\n * minute — every host reads unknown, so nothing holds on it — without a\n * deploy. Absent or anything else means on.\n *=========================================*/\n\nimport { TENANT_APEX } from '@aglyn/aglyn/app-utils/tenant-apex'\nimport {\n LINK_THREAT_TYPES,\n type LinkReputationAnswer,\n type LinkThreatType,\n MAX_REPUTATION_HOSTS_PER_LOOKUP,\n normalizeReputationHost,\n setLinkReputationLookup,\n} from '@aglyn/shared-util-email/link-reputation'\nimport { getApp } from 'firebase-admin/app'\nimport firebaseAdmin from './firebase-admin'\n\nexport const WEB_RISK_SEARCH_ENDPOINT = 'https://webrisk.googleapis.com/v1/uris:search'\nexport const WEB_RISK_CACHE_COLLECTION = 'webRiskVerdicts'\n/** The kill switch: `{ enabled: false }` turns every lookup off. */\nexport const WEB_RISK_SETTINGS_COLLECTION = 'platformSettings'\nexport const WEB_RISK_SETTINGS_DOC = 'webRisk'\n\n/** One call's budget. */\nexport const WEB_RISK_TIMEOUT_MS = 1_000\n/** One lookup's budget, however many hosts it asks about. */\nexport const WEB_RISK_DEADLINE_MS = 1_200\n/** How long a clean answer is kept in the store. */\nexport const WEB_RISK_CLEAN_TTL_MS = 12 * 60 * 60_000\n/** How long a clean answer is trusted from this process's memory. */\nexport const WEB_RISK_MEMORY_CLEAN_TTL_MS = 10 * 60_000\n/** A listing whose answer named no `expireTime` is kept this long. */\nexport const WEB_RISK_LISTED_FALLBACK_TTL_MS = 5 * 60_000\n/** How long the switch's answer is trusted. */\nconst SWITCH_TTL_MS = 60_000\n/** How long a refused credential stops the lookup. */\nconst REFUSED_BACKOFF_MS = 15 * 60_000\n/** How long a rate limit stops the lookup. */\nconst THROTTLED_BACKOFF_MS = 60_000\n/** At most one warning per this long, so an outage is not a log flood. */\nconst WARN_EVERY_MS = 60_000\n\n/** What `uris.search` answered for one URI. */\nexport interface WebRiskSearchResult {\n /** The lists it is on; empty when it is on none. */\n threats: LinkThreatType[]\n /** Until when the answer may be kept, when the API said. */\n expireTimeMs: number | null\n}\n\n/** The contract: one URI, its listing. Throws on anything but an answer. */\nexport interface WebRiskClient {\n searchUri(uri: string): Promise<WebRiskSearchResult>\n}\n\n/** The API answered with a status that is not an answer. */\nexport class WebRiskHttpError extends Error {\n constructor(\n readonly status: number,\n detail: string,\n ) {\n super(`Web Risk answered ${status}: ${detail.slice(0, 200)}`)\n this.name = 'WebRiskHttpError'\n }\n}\n\n/** There is no credential to call with. */\nexport class WebRiskUnavailableError extends Error {\n constructor(reason: string) {\n super(reason)\n this.name = 'WebRiskUnavailableError'\n }\n}\n\n/** `uris.search`'s body, read defensively: an empty object is \"not listed\". */\nexport function parseWebRiskSearch(body: unknown): WebRiskSearchResult {\n const threat = (body as { threat?: { threatTypes?: unknown; expireTime?: unknown } } | null)\n ?.threat\n if (!threat || typeof threat !== 'object') return { threats: [], expireTimeMs: null }\n const listed = Array.isArray(threat.threatTypes) ? threat.threatTypes : []\n const threats = LINK_THREAT_TYPES.filter((type) => listed.includes(type))\n const expireTimeMs =\n typeof threat.expireTime === 'string' ? Date.parse(threat.expireTime) : Number.NaN\n return { threats, expireTimeMs: Number.isFinite(expireTimeMs) ? expireTimeMs : null }\n}\n\n/** The URI a host is looked up as. Only the host: never a path or a query. */\nexport function webRiskUriForHost(host: string): string {\n return `https://${host}/`\n}\n\n/** The default client: `uris.search` over HTTPS with a bearer token. */\nexport function createWebRiskHttpClient(options: {\n accessToken: () => Promise<string | null>\n fetch?: typeof fetch\n timeoutMs?: number\n endpoint?: string\n}): WebRiskClient {\n const request = options.fetch ?? fetch\n const timeoutMs = options.timeoutMs ?? WEB_RISK_TIMEOUT_MS\n return {\n async searchUri(uri: string): Promise<WebRiskSearchResult> {\n const token = await options.accessToken()\n if (!token) throw new WebRiskUnavailableError('no service-account credential')\n const url = new URL(options.endpoint ?? WEB_RISK_SEARCH_ENDPOINT)\n for (const type of LINK_THREAT_TYPES) url.searchParams.append('threatTypes', type)\n url.searchParams.set('uri', uri)\n const controller = new AbortController()\n const timer = setTimeout(() => controller.abort(), timeoutMs)\n try {\n const response = await request(url.toString(), {\n method: 'GET',\n headers: { Authorization: `Bearer ${token}`, Accept: 'application/json' },\n signal: controller.signal,\n })\n if (!response.ok) {\n throw new WebRiskHttpError(response.status, await response.text().catch(() => ''))\n }\n return parseWebRiskSearch(await response.json())\n } finally {\n clearTimeout(timer)\n }\n },\n }\n}\n\n/**\n * The platform service account's access token, or null when this deployment\n * has none — the same credential `client-error-report.ts` writes logs with.\n */\nasync function serviceAccountAccessToken(): Promise<string | null> {\n try {\n const credential = getApp().options.credential\n if (!credential) return null\n return (await credential.getAccessToken())?.access_token ?? null\n } catch {\n return null\n }\n}\n\nlet defaultClient: WebRiskClient | null = null\nlet clientOverride: WebRiskClient | null | undefined\n\nfunction activeClient(): WebRiskClient | null {\n if (clientOverride !== undefined) return clientOverride\n defaultClient ??= createWebRiskHttpClient({ accessToken: serviceAccountAccessToken })\n return defaultClient\n}\n\ninterface Verdict {\n threats: LinkThreatType[]\n checkedAtMs: number\n /** When the answer stops being good, in the store. */\n expiresAtMs: number\n}\n\nconst memory = new Map<string, Verdict & { memoryUntilMs: number }>()\nlet switchMemo: { off: boolean; atMs: number } | null = null\nlet backoffUntilMs = 0\nlet lastWarnAtMs = 0\n\n/** Test seam: forget the caches, the switch, the back-off; optionally stand in a client. */\nexport function resetWebRiskForTests(client?: WebRiskClient | null): void {\n memory.clear()\n switchMemo = null\n backoffUntilMs = 0\n lastWarnAtMs = 0\n clientOverride = client\n defaultClient = null\n}\n\nfunction warn(nowMs: number, message: string, error?: unknown): void {\n if (nowMs - lastWarnAtMs < WARN_EVERY_MS) return\n lastWarnAtMs = nowMs\n console.warn(`[web-risk] ${message} — the host reads unknown, which is not evidence`, error ?? '')\n}\n\n/** Is the lookup switched off? Read at most once a minute; an unreadable switch is on. */\nasync function switchedOff(nowMs: number): Promise<boolean> {\n if (switchMemo && nowMs - switchMemo.atMs < SWITCH_TTL_MS) return switchMemo.off\n const off = await firebaseAdmin\n .app()\n .firestore()\n .collection(WEB_RISK_SETTINGS_COLLECTION)\n .doc(WEB_RISK_SETTINGS_DOC)\n .get()\n .then(\n (snapshot) => snapshot.get('enabled') === false,\n () => false,\n )\n switchMemo = { off, atMs: nowMs }\n return off\n}\n\n/**\n * Hosts that are the platform's own: every site's subdomain on the tenant\n * apex. Never looked up — a neighbour's site is screened on its own page.\n */\nexport function platformReputationExclusions(): string[] {\n return [TENANT_APEX].filter(Boolean)\n}\n\nfunction isPlatformHost(host: string): boolean {\n return platformReputationExclusions().some(\n (domain) => host === domain || host.endsWith(`.${domain}`),\n )\n}\n\nfunction verdictFrom(data: Record<string, unknown> | undefined): Verdict | null {\n if (!data) return null\n const threats = Array.isArray(data['threats'])\n ? LINK_THREAT_TYPES.filter((type) => (data['threats'] as unknown[]).includes(type))\n : []\n const checkedAtMs = Number(data['checkedAtMs'])\n const expiresAtMs = Number(data['expiresAtMs'])\n if (!Number.isFinite(checkedAtMs) || !Number.isFinite(expiresAtMs)) return null\n return { threats, checkedAtMs, expiresAtMs }\n}\n\nfunction remember(host: string, verdict: Verdict, nowMs: number): void {\n memory.set(host, {\n ...verdict,\n memoryUntilMs: verdict.threats.length\n ? verdict.expiresAtMs\n : Math.min(verdict.expiresAtMs, nowMs + WEB_RISK_MEMORY_CLEAN_TTL_MS),\n })\n}\n\nexport interface HostReputationLookupOptions {\n /** How long the caller will wait, overall. */\n deadlineMs?: number\n /** The most hosts asked about; the rest read unknown. */\n maxHosts?: number\n nowMs?: number\n}\n\n/** What a lookup answered, and how many hosts it had to ask the API about. */\nexport interface HostReputationLookupResult extends LinkReputationAnswer {\n looked: number\n}\n\n/**\n * The reputation of each host: listed, clean, or unknown. Platform hosts are\n * clean without asking. Never throws, and never waits longer than the\n * deadline: a call still running then finishes in the background and fills\n * the caches for the next asker.\n *\n * \"Never throws\" covers the store as well as the API: a missing app, a\n * settings read that fails before it is a promise, or a cache reference\n * that cannot be built all read every host as unknown. The page review\n * calls this before its own fail-closed `try`, so a throw here would take\n * down every page with a link off the site.\n */\nexport async function lookupHostReputation(\n hosts: readonly string[],\n options: HostReputationLookupOptions = {},\n): Promise<HostReputationLookupResult> {\n try {\n return await lookupHostReputationOrThrow(hosts, options)\n } catch (error) {\n warn(options.nowMs ?? Date.now(), 'a lookup failed before it could answer', error)\n const unknown = [\n ...new Set(\n hosts\n .map((raw) => normalizeReputationHost(raw))\n .filter((host): host is string => Boolean(host)),\n ),\n ]\n return { hits: [], clean: [], unknown, looked: 0 }\n }\n}\n\nasync function lookupHostReputationOrThrow(\n hosts: readonly string[],\n options: HostReputationLookupOptions,\n): Promise<HostReputationLookupResult> {\n const nowMs = options.nowMs ?? Date.now()\n const result: HostReputationLookupResult = { hits: [], clean: [], unknown: [], looked: 0 }\n const wanted: string[] = []\n for (const raw of hosts) {\n const host = normalizeReputationHost(raw)\n if (!host || wanted.includes(host) || result.clean.includes(host)) continue\n if (isPlatformHost(host)) result.clean.push(host)\n else if (wanted.length < (options.maxHosts ?? MAX_REPUTATION_HOSTS_PER_LOOKUP)) wanted.push(host)\n else result.unknown.push(host)\n }\n if (!wanted.length) return result\n\n const settle = (host: string, verdict: Verdict) => {\n if (verdict.threats.length) result.hits.push({ host, threats: verdict.threats })\n else result.clean.push(host)\n }\n\n // 1. This process's memory.\n let missing = wanted.filter((host) => {\n const known = memory.get(host)\n if (known && nowMs < known.memoryUntilMs) {\n settle(host, known)\n return false\n }\n return true\n })\n if (!missing.length) return result\n\n // 2. The switch. Off reads every remaining host as unknown.\n if (await switchedOff(nowMs)) {\n result.unknown.push(...missing)\n return result\n }\n\n // 3. The store, in one round trip.\n const firestore = firebaseAdmin.app().firestore()\n const collection = firestore.collection(WEB_RISK_CACHE_COLLECTION)\n try {\n const snapshots = await firestore.getAll(...missing.map((host) => collection.doc(host)))\n const stored = new Map<string, Verdict>()\n for (const snapshot of snapshots) {\n const verdict = verdictFrom(snapshot.exists ? (snapshot.data() as Record<string, unknown>) : undefined)\n if (verdict && nowMs < verdict.expiresAtMs) stored.set(snapshot.id, verdict)\n }\n missing = missing.filter((host) => {\n const verdict = stored.get(host)\n if (!verdict) return true\n remember(host, verdict, nowMs)\n settle(host, verdict)\n return false\n })\n } catch (error) {\n warn(nowMs, 'the verdict cache could not be read', error)\n }\n if (!missing.length) return result\n\n // 4. The API, every remaining host at once, within the deadline.\n const client = activeClient()\n if (!client || nowMs < backoffUntilMs) {\n result.unknown.push(...missing)\n return result\n }\n result.looked = missing.length\n const answers = new Map<string, Verdict>()\n const calls = missing.map(async (host) => {\n try {\n const answer = await client.searchUri(webRiskUriForHost(host))\n const checkedAtMs = Date.now()\n const verdict: Verdict = {\n threats: answer.threats,\n checkedAtMs,\n expiresAtMs: answer.threats.length\n ? (answer.expireTimeMs ?? checkedAtMs + WEB_RISK_LISTED_FALLBACK_TTL_MS)\n : checkedAtMs + WEB_RISK_CLEAN_TTL_MS,\n }\n answers.set(host, verdict)\n remember(host, verdict, checkedAtMs)\n await collection\n .doc(host)\n .set({ host, ...verdict, listed: verdict.threats.length > 0 })\n .catch((error: unknown) => warn(checkedAtMs, 'a verdict could not be cached', error))\n } catch (error) {\n if (error instanceof WebRiskHttpError && [401, 403, 404].includes(error.status)) {\n backoffUntilMs = Date.now() + REFUSED_BACKOFF_MS\n console.error('[web-risk] the API refused the credential — lookups paused for 15 minutes', error)\n } else if (error instanceof WebRiskHttpError && error.status === 429) {\n backoffUntilMs = Date.now() + THROTTLED_BACKOFF_MS\n warn(Date.now(), 'the API is rate limiting', error)\n } else if (error instanceof WebRiskUnavailableError) {\n backoffUntilMs = Date.now() + REFUSED_BACKOFF_MS\n warn(Date.now(), 'no credential to call the API with', error)\n } else {\n warn(Date.now(), `the lookup of ${host} failed or timed out`, error)\n }\n }\n })\n let timer: ReturnType<typeof setTimeout> | undefined\n await Promise.race([\n Promise.allSettled(calls),\n new Promise<void>((resolve) => {\n timer = setTimeout(resolve, options.deadlineMs ?? WEB_RISK_DEADLINE_MS)\n }),\n ])\n if (timer) clearTimeout(timer)\n const late = missing.filter((host) => !answers.has(host))\n for (const host of missing) {\n const verdict = answers.get(host)\n if (verdict) settle(host, verdict)\n }\n result.unknown.push(...late)\n if (late.length) warn(Date.now(), `${late.length} host(s) had no answer within the deadline`)\n return result\n}\n\n/**\n * Puts this lookup behind the shared screen's reputation seam, so the send\n * seam (`screenTenantMessage`) asks it. Called by\n * `installOutboundScreenGate`, at that module's load.\n */\nexport function installLinkReputationLookup(): void {\n if (typeof setLinkReputationLookup !== 'function') return\n setLinkReputationLookup((hosts, options) =>\n lookupHostReputation(hosts, { deadlineMs: options?.deadlineMs }),\n )\n}\n"],"names":["TENANT_APEX","LINK_THREAT_TYPES","MAX_REPUTATION_HOSTS_PER_LOOKUP","normalizeReputationHost","setLinkReputationLookup","getApp","firebaseAdmin","WEB_RISK_SEARCH_ENDPOINT","WEB_RISK_CACHE_COLLECTION","WEB_RISK_SETTINGS_COLLECTION","WEB_RISK_SETTINGS_DOC","WEB_RISK_TIMEOUT_MS","WEB_RISK_DEADLINE_MS","WEB_RISK_CLEAN_TTL_MS","WEB_RISK_MEMORY_CLEAN_TTL_MS","WEB_RISK_LISTED_FALLBACK_TTL_MS","SWITCH_TTL_MS","REFUSED_BACKOFF_MS","THROTTLED_BACKOFF_MS","WARN_EVERY_MS","WebRiskHttpError","Error","status","detail","slice","name","WebRiskUnavailableError","reason","parseWebRiskSearch","body","threat","threats","expireTimeMs","listed","Array","isArray","threatTypes","filter","type","includes","expireTime","Date","parse","Number","NaN","isFinite","webRiskUriForHost","host","createWebRiskHttpClient","options","request","fetch","timeoutMs","searchUri","uri","token","accessToken","url","URL","endpoint","searchParams","append","set","controller","AbortController","timer","setTimeout","abort","response","toString","method","headers","Authorization","Accept","signal","ok","text","catch","json","clearTimeout","serviceAccountAccessToken","credential","getAccessToken","access_token","defaultClient","clientOverride","activeClient","undefined","memory","Map","switchMemo","backoffUntilMs","lastWarnAtMs","resetWebRiskForTests","client","clear","warn","nowMs","message","error","console","switchedOff","atMs","off","app","firestore","collection","doc","get","then","snapshot","platformReputationExclusions","Boolean","isPlatformHost","some","domain","endsWith","verdictFrom","data","checkedAtMs","expiresAtMs","remember","verdict","memoryUntilMs","length","Math","min","lookupHostReputation","hosts","lookupHostReputationOrThrow","now","unknown","Set","map","raw","hits","clean","looked","result","wanted","push","maxHosts","settle","missing","known","snapshots","getAll","stored","exists","id","answers","calls","answer","Promise","race","allSettled","resolve","deadlineMs","late","has","installLinkReputationLookup"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;2CA+C2C,GAE3C,SAASA,WAAW,QAAQ,qCAAoC;AAChE,SACEC,iBAAiB,EAGjBC,+BAA+B,EAC/BC,uBAAuB,EACvBC,uBAAuB,QAClB,2CAA0C;AACjD,SAASC,MAAM,QAAQ,qBAAoB;AAC3C,OAAOC,mBAAmB,sBAAkB;AAE5C,OAAO,MAAMC,2BAA2B,gDAA+C;AACvF,OAAO,MAAMC,4BAA4B,kBAAiB;AAC1D,kEAAkE,GAClE,OAAO,MAAMC,+BAA+B,mBAAkB;AAC9D,OAAO,MAAMC,wBAAwB,UAAS;AAE9C,uBAAuB,GACvB,OAAO,MAAMC,sBAAsB,KAAK;AACxC,2DAA2D,GAC3D,OAAO,MAAMC,uBAAuB,K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