@celljs/ta-relay 4.24.1 → 4.25.0
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/README.en.md +6 -6
- package/README.md +6 -6
- package/esm/common/index.d.ts +5 -0
- package/esm/common/index.js +5 -0
- package/esm/common/relay-accounts.d.ts +2 -0
- package/esm/common/relay-accounts.js +21 -0
- package/esm/common/relay-blobs.d.ts +2 -0
- package/esm/common/relay-blobs.js +1 -0
- package/esm/common/relay-content-origin.d.ts +44 -0
- package/esm/common/relay-content-origin.js +67 -0
- package/esm/common/relay-content.d.ts +2 -0
- package/esm/common/relay-content.js +12 -2
- package/esm/common/relay-friends.js +1 -1
- package/esm/common/relay-hub-util.js +1 -1
- package/esm/common/relay-hub.js +4 -4
- package/esm/common/relay-hubs-admins.js +3 -3
- package/esm/common/relay-hubs.js +5 -5
- package/esm/common/relay-info.d.ts +11 -1
- package/esm/common/relay-info.js +20 -2
- package/esm/common/relay-moderation-client.d.ts +32 -0
- package/esm/common/relay-moderation-client.js +51 -0
- package/esm/common/relay-moderation-door.d.ts +24 -0
- package/esm/common/relay-moderation-door.js +48 -0
- package/esm/common/relay-moderation-policy.d.ts +78 -0
- package/esm/common/relay-moderation-policy.js +59 -0
- package/esm/common/relay-moderation.d.ts +165 -0
- package/esm/common/relay-moderation.js +464 -0
- package/esm/common/relay-protocol.d.ts +5 -1
- package/esm/common/relay-protocol.js +4 -0
- package/esm/common/relay-room-app.d.ts +5 -0
- package/esm/common/relay-room-app.js +8 -0
- package/esm/node/relay-chat-files.js +1 -1
- package/esm/node/relay-server-accounts.d.ts +2 -0
- package/esm/node/relay-server-accounts.js +4 -0
- package/esm/node/relay-server-gate.d.ts +39 -0
- package/esm/node/relay-server-gate.js +124 -0
- package/esm/node/relay-server-web.d.ts +5 -0
- package/esm/node/relay-server-web.js +28 -0
- package/esm/node/relay-server.d.ts +9 -2
- package/esm/node/relay-server.js +40 -47
- package/esm/node/relay-store.d.ts +4 -0
- package/esm/node/relay-store.js +2 -0
- package/esm/viewer/viewer-i18n.d.ts +25 -0
- package/esm/viewer/viewer-i18n.js +132 -32
- package/esm/viewer/viewer-style.d.ts +1 -1
- package/esm/viewer/viewer-style.js +13 -0
- package/esm/viewer/viewer.js +127 -6
- package/lib/common/index.d.ts +5 -0
- package/lib/common/index.js +5 -0
- package/lib/common/relay-accounts.d.ts +2 -0
- package/lib/common/relay-accounts.js +21 -0
- package/lib/common/relay-blobs.d.ts +2 -0
- package/lib/common/relay-blobs.js +1 -0
- package/lib/common/relay-content-origin.d.ts +45 -0
- package/lib/common/relay-content-origin.js +71 -0
- package/lib/common/relay-content.d.ts +2 -0
- package/lib/common/relay-content.js +12 -2
- package/lib/common/relay-friends.js +1 -1
- package/lib/common/relay-hub-util.js +1 -1
- package/lib/common/relay-hub.js +4 -4
- package/lib/common/relay-hubs-admins.js +3 -3
- package/lib/common/relay-hubs.js +5 -5
- package/lib/common/relay-info.d.ts +11 -1
- package/lib/common/relay-info.js +20 -2
- package/lib/common/relay-moderation-client.d.ts +33 -0
- package/lib/common/relay-moderation-client.js +54 -0
- package/lib/common/relay-moderation-door.d.ts +25 -0
- package/lib/common/relay-moderation-door.js +51 -0
- package/lib/common/relay-moderation-policy.d.ts +79 -0
- package/lib/common/relay-moderation-policy.js +62 -0
- package/lib/common/relay-moderation.d.ts +166 -0
- package/lib/common/relay-moderation.js +468 -0
- package/lib/common/relay-protocol.d.ts +5 -1
- package/lib/common/relay-protocol.js +4 -0
- package/lib/common/relay-room-app.d.ts +5 -0
- package/lib/common/relay-room-app.js +8 -0
- package/lib/node/relay-chat-files.js +1 -1
- package/lib/node/relay-server-accounts.d.ts +2 -0
- package/lib/node/relay-server-accounts.js +4 -0
- package/lib/node/relay-server-gate.d.ts +40 -0
- package/lib/node/relay-server-gate.js +128 -0
- package/lib/node/relay-server-web.d.ts +6 -0
- package/lib/node/relay-server-web.js +31 -0
- package/lib/node/relay-server.d.ts +9 -2
- package/lib/node/relay-server.js +39 -46
- package/lib/node/relay-store.d.ts +4 -0
- package/lib/node/relay-store.js +2 -0
- package/lib/viewer/viewer-i18n.d.ts +25 -0
- package/lib/viewer/viewer-i18n.js +132 -32
- package/lib/viewer/viewer-style.d.ts +1 -1
- package/lib/viewer/viewer-style.js +13 -0
- package/lib/viewer/viewer.js +127 -6
- package/package.json +3 -3
- package/viewer/join.html +5 -5
- package/viewer/page.html +25 -12
- package/viewer/room.html +4 -4
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
import { RelayBytes } from './relay-bytes.js';
|
|
2
|
+
import { RelayContentUtil } from './relay-content.js';
|
|
3
|
+
import { RELAY_APPEAL_PATH, RELAY_STANDING_PATH, RelayModerationPolicyUtil, } from './relay-moderation-policy.js';
|
|
4
|
+
import { RELAY_ID, RelayCloseCode } from './relay-protocol.js';
|
|
5
|
+
/** Where anyone reports a link of content (the viewer's "Report"): POST, JSON of RelayReportInput. */
|
|
6
|
+
export const RELAY_REPORT_PATH = '/v1/report';
|
|
7
|
+
/** Where the operator of the relay reads the reports and blocks: `authorization: Bearer <TA_ADMIN_TOKEN>`. */
|
|
8
|
+
export const RELAY_MODERATION_PATH = '/v1/moderation';
|
|
9
|
+
/** Why a link is reported. */
|
|
10
|
+
export const RELAY_REPORT_REASONS = ['phishing', 'malware', 'sexual', 'illegal', 'harassment', 'other'];
|
|
11
|
+
/** Reasons a few reports are enough for (RelayModerationPolicy.severe). */
|
|
12
|
+
export const RELAY_SEVERE_REASONS = ['phishing', 'malware'];
|
|
13
|
+
/** Internal paths, reached only from the Worker or `ta relay` once they checked the caller (RelayModerationDoor). */
|
|
14
|
+
export const RELAY_MODERATION_INTERNAL = { bind: '/bind', blocks: '/blocks', viewer: '/viewer', copy: '/copy', policy: '/policy' };
|
|
15
|
+
const HOUR_MS = 60 * 60 * 1000;
|
|
16
|
+
const DAY_MS = 24 * HOUR_MS;
|
|
17
|
+
const MAX_REPORTS = 500;
|
|
18
|
+
const REPORT_DAYS = 90;
|
|
19
|
+
const MAX_TRIES = 10;
|
|
20
|
+
const MAX_NOTE = 500;
|
|
21
|
+
const MAX_BLOCKS = 5000;
|
|
22
|
+
/** The addresses kept of the people who reported a link, hashed: enough to count them for the rules. */
|
|
23
|
+
const MAX_FROM = 32;
|
|
24
|
+
const MAX_LOG = 500;
|
|
25
|
+
const MAX_APPEALS = 200;
|
|
26
|
+
const MAX_HOSTS = 50;
|
|
27
|
+
const JSON_HEADERS = { 'content-type': 'application/json', 'cache-control': 'no-store' };
|
|
28
|
+
/**
|
|
29
|
+
* Reports of links of content and what the operator blocked (docs/design/ta-link.md 2.10). The relay cannot read what is
|
|
30
|
+
* shared, so it can only take reports, and stop handing on what its operator blocked: the copy of a share on the relay,
|
|
31
|
+
* and every link of a host through the domain of content. Runs the same in a Durable Object and in `ta relay`.
|
|
32
|
+
*/
|
|
33
|
+
export class RelayModeration {
|
|
34
|
+
constructor(storage, now = Date.now) {
|
|
35
|
+
this.storage = storage;
|
|
36
|
+
this.now = now;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* POST RELAY_REPORT_PATH from anyone, `address` counted for its limit; GET and POST RELAY_MODERATION_PATH with
|
|
40
|
+
* `adminToken` (none: the operator set none, and they are not there).
|
|
41
|
+
*/
|
|
42
|
+
async handle(request, address, adminToken, who) {
|
|
43
|
+
var _a, _b;
|
|
44
|
+
const url = new URL(request.url);
|
|
45
|
+
const { pathname } = url;
|
|
46
|
+
const internal = await this.internal(request, url, who);
|
|
47
|
+
if (internal) {
|
|
48
|
+
return internal;
|
|
49
|
+
}
|
|
50
|
+
if (pathname === RELAY_STANDING_PATH || pathname === RELAY_APPEAL_PATH) {
|
|
51
|
+
if (!who) {
|
|
52
|
+
return RelayModeration.answer(401, { error: 'Sign in first.' });
|
|
53
|
+
}
|
|
54
|
+
if (pathname === RELAY_STANDING_PATH) {
|
|
55
|
+
return RelayModeration.answer(200, await this.standing(who.id));
|
|
56
|
+
}
|
|
57
|
+
const text = String((_a = (await request.json().catch(() => ({}))).text) !== null && _a !== void 0 ? _a : '')
|
|
58
|
+
.trim()
|
|
59
|
+
.slice(0, MAX_NOTE);
|
|
60
|
+
return text ? RelayModeration.answer(200, await this.appeal(who, text)) : RelayModeration.answer(400, { error: 'Say why, in a few words.' });
|
|
61
|
+
}
|
|
62
|
+
if (pathname === RELAY_REPORT_PATH) {
|
|
63
|
+
return request.method === 'POST' ? this.report(request, address) : RelayModeration.answer(405, { error: 'Not allowed.' });
|
|
64
|
+
}
|
|
65
|
+
if (pathname !== RELAY_MODERATION_PATH || !adminToken) {
|
|
66
|
+
return RelayModeration.answer(404, { error: 'Not found.' });
|
|
67
|
+
}
|
|
68
|
+
if (!(await RelayModeration.same((_b = request.headers.get('authorization')) !== null && _b !== void 0 ? _b : '', `Bearer ${adminToken}`))) {
|
|
69
|
+
return RelayModeration.answer(401, { error: 'Not the operator.' });
|
|
70
|
+
}
|
|
71
|
+
if (request.method === 'GET') {
|
|
72
|
+
return RelayModeration.answer(200, await this.state());
|
|
73
|
+
}
|
|
74
|
+
if (request.method !== 'POST') {
|
|
75
|
+
return RelayModeration.answer(405, { error: 'Not allowed.' });
|
|
76
|
+
}
|
|
77
|
+
const action = (await request.json().catch(() => undefined));
|
|
78
|
+
try {
|
|
79
|
+
await this.apply(action);
|
|
80
|
+
return RelayModeration.answer(200, await this.state());
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
return RelayModeration.answer(400, { error: error.message });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** Whether the operator blocked this host or this share. */
|
|
87
|
+
async blocked(host, share) {
|
|
88
|
+
const blocks = await this.blocks();
|
|
89
|
+
return (!!host && blocks.hosts.includes(host)) || (!!share && blocks.shares.includes(share));
|
|
90
|
+
}
|
|
91
|
+
async reports() {
|
|
92
|
+
return (await this.kept()).map(({ from: _from, fromSevere: _severe, ...report }) => report);
|
|
93
|
+
}
|
|
94
|
+
async kept() {
|
|
95
|
+
var _a;
|
|
96
|
+
const since = this.now() - REPORT_DAYS * DAY_MS;
|
|
97
|
+
return ((_a = (await this.storage.get('reports'))) !== null && _a !== void 0 ? _a : []).filter(report => report.last >= since);
|
|
98
|
+
}
|
|
99
|
+
async blocks() {
|
|
100
|
+
var _a, _b, _c, _d;
|
|
101
|
+
const blocks = await this.storage.get('blocks');
|
|
102
|
+
return { hosts: (_a = blocks === null || blocks === void 0 ? void 0 : blocks.hosts) !== null && _a !== void 0 ? _a : [], shares: (_b = blocks === null || blocks === void 0 ? void 0 : blocks.shares) !== null && _b !== void 0 ? _b : [], auto: (_c = blocks === null || blocks === void 0 ? void 0 : blocks.auto) !== null && _c !== void 0 ? _c : {}, pardoned: (_d = blocks === null || blocks === void 0 ? void 0 : blocks.pardoned) !== null && _d !== void 0 ? _d : [] };
|
|
103
|
+
}
|
|
104
|
+
async policy() {
|
|
105
|
+
return RelayModerationPolicyUtil.of(await this.storage.get('policy'));
|
|
106
|
+
}
|
|
107
|
+
async sanctions() {
|
|
108
|
+
var _a;
|
|
109
|
+
const now = this.now();
|
|
110
|
+
const kept = (_a = (await this.storage.get('sanctions'))) !== null && _a !== void 0 ? _a : {};
|
|
111
|
+
return Object.fromEntries(Object.entries(kept).filter(([, sanction]) => RelayModerationPolicyUtil.active(sanction, now)));
|
|
112
|
+
}
|
|
113
|
+
async appeals() {
|
|
114
|
+
var _a;
|
|
115
|
+
return (_a = (await this.storage.get('appeals'))) !== null && _a !== void 0 ? _a : [];
|
|
116
|
+
}
|
|
117
|
+
async log() {
|
|
118
|
+
var _a;
|
|
119
|
+
return (_a = (await this.storage.get('log'))) !== null && _a !== void 0 ? _a : [];
|
|
120
|
+
}
|
|
121
|
+
async record(entry) {
|
|
122
|
+
await this.storage.put('log', [{ ...entry, at: this.now() }, ...(await this.log())].slice(0, MAX_LOG));
|
|
123
|
+
}
|
|
124
|
+
async owner(host) {
|
|
125
|
+
return this.storage.get(`owner:${host}`);
|
|
126
|
+
}
|
|
127
|
+
/** What the operator reads: the reports, blocks, sanctions, appeals, the record, and who shares through each computer reported. */
|
|
128
|
+
async state() {
|
|
129
|
+
const reports = await this.reports();
|
|
130
|
+
const owners = {};
|
|
131
|
+
for (const host of new Set(reports.map(report => report.host))) {
|
|
132
|
+
const owner = await this.owner(host);
|
|
133
|
+
if (owner) {
|
|
134
|
+
owners[host] = owner;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
reports,
|
|
139
|
+
blocks: await this.blocks(),
|
|
140
|
+
policy: await this.policy(),
|
|
141
|
+
sanctions: await this.sanctions(),
|
|
142
|
+
appeals: await this.appeals(),
|
|
143
|
+
log: (await this.log()).slice(0, 100),
|
|
144
|
+
owners,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/** How an account stands, for itself. */
|
|
148
|
+
async standing(account) {
|
|
149
|
+
var _a;
|
|
150
|
+
const sanction = (await this.sanctions())[account];
|
|
151
|
+
const hosts = (_a = (await this.storage.get(`hosts:${account}`))) !== null && _a !== void 0 ? _a : [];
|
|
152
|
+
const blocks = await this.blocks();
|
|
153
|
+
const blocked = (await this.kept()).filter(report => hosts.includes(report.host) && blocks.shares.includes(report.share)).map(report => report.share);
|
|
154
|
+
const appeal = (await this.appeals()).find(each => each.account === account);
|
|
155
|
+
return { ...(sanction ? { sanction } : {}), blocked, ...(appeal ? { appeal } : {}) };
|
|
156
|
+
}
|
|
157
|
+
async appeal(who, text) {
|
|
158
|
+
const others = (await this.appeals()).filter(each => each.account !== who.id);
|
|
159
|
+
await this.storage.put('appeals', [{ account: who.id, ...(who.name ? { name: who.name } : {}), at: this.now(), text }, ...others].slice(0, MAX_APPEALS));
|
|
160
|
+
await this.record({ by: 'user', action: 'appeal', target: `account:${who.id}`, reason: text.slice(0, 120) });
|
|
161
|
+
return this.standing(who.id);
|
|
162
|
+
}
|
|
163
|
+
/** The paths only the Worker or `ta relay` reach, the caller checked. */
|
|
164
|
+
async internal(request, url, who) {
|
|
165
|
+
var _a, _b, _c;
|
|
166
|
+
const paths = RELAY_MODERATION_INTERNAL;
|
|
167
|
+
switch (url.pathname) {
|
|
168
|
+
case paths.blocks:
|
|
169
|
+
return RelayModeration.answer(200, await this.blocks());
|
|
170
|
+
case paths.policy:
|
|
171
|
+
return RelayModeration.answer(200, await this.policy());
|
|
172
|
+
case paths.viewer:
|
|
173
|
+
return RelayModeration.answer(200, { code: await this.viewer((_a = url.searchParams.get('host')) !== null && _a !== void 0 ? _a : '', url.searchParams.get('browser') === '1') });
|
|
174
|
+
case paths.bind: {
|
|
175
|
+
const host = String((_b = (await request.json().catch(() => ({}))).host) !== null && _b !== void 0 ? _b : '');
|
|
176
|
+
if (!who || !RELAY_ID.test(host)) {
|
|
177
|
+
return RelayModeration.answer(400, { error: 'Bind a computer to an account.' });
|
|
178
|
+
}
|
|
179
|
+
await this.bind(host, who);
|
|
180
|
+
return RelayModeration.answer(200, await this.standing(who.id));
|
|
181
|
+
}
|
|
182
|
+
case paths.copy: {
|
|
183
|
+
const code = await this.viewer((_c = url.searchParams.get('host')) !== null && _c !== void 0 ? _c : '', true, who);
|
|
184
|
+
return code ? RelayModeration.answer(403, { code }) : RelayModeration.answer(200, {});
|
|
185
|
+
}
|
|
186
|
+
default:
|
|
187
|
+
return undefined;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
/** A computer shares through this account from now on: its reports count for it. */
|
|
191
|
+
async bind(host, who) {
|
|
192
|
+
var _a, _b;
|
|
193
|
+
const before = await this.owner(host);
|
|
194
|
+
if (before && before.account !== who.id) {
|
|
195
|
+
const rest = ((_a = (await this.storage.get(`hosts:${before.account}`))) !== null && _a !== void 0 ? _a : []).filter(each => each !== host);
|
|
196
|
+
await this.storage.put(`hosts:${before.account}`, rest);
|
|
197
|
+
}
|
|
198
|
+
await this.storage.put(`owner:${host}`, { account: who.id, ...(who.name ? { name: who.name } : {}), at: this.now() });
|
|
199
|
+
const hosts = (_b = (await this.storage.get(`hosts:${who.id}`))) !== null && _b !== void 0 ? _b : [];
|
|
200
|
+
if (!hosts.includes(host)) {
|
|
201
|
+
await this.storage.put(`hosts:${who.id}`, [...hosts, host].slice(-MAX_HOSTS));
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Why a link of this computer does not open: blocked by the operator or the rules; and in a browser (the pages of the
|
|
206
|
+
* relay, `browser`), shared by no account where the policy needs one, or by one limited or banned. `who`: the account
|
|
207
|
+
* a copy is put by, in place of the owner of the computer.
|
|
208
|
+
*/
|
|
209
|
+
async viewer(host, browser, who) {
|
|
210
|
+
var _a;
|
|
211
|
+
var _b;
|
|
212
|
+
if ((await this.blocks()).hosts.includes(host)) {
|
|
213
|
+
return RelayCloseCode.BLOCKED;
|
|
214
|
+
}
|
|
215
|
+
if (!browser) {
|
|
216
|
+
return undefined;
|
|
217
|
+
}
|
|
218
|
+
const policy = await this.policy();
|
|
219
|
+
const account = (_b = who === null || who === void 0 ? void 0 : who.id) !== null && _b !== void 0 ? _b : (_a = (await this.owner(host))) === null || _a === void 0 ? void 0 : _a.account;
|
|
220
|
+
if (!account) {
|
|
221
|
+
return policy.needsAccount.includes('share') ? RelayCloseCode.SIGN_IN : undefined;
|
|
222
|
+
}
|
|
223
|
+
return RelayModerationPolicyUtil.takes((await this.sanctions())[account], 'share', policy, this.now()) ? RelayCloseCode.BLOCKED : undefined;
|
|
224
|
+
}
|
|
225
|
+
async apply(action) {
|
|
226
|
+
var _a, _b;
|
|
227
|
+
const ids = (value) => (typeof value === 'string' && RELAY_ID.test(value) ? value : undefined);
|
|
228
|
+
const host = ids(action === null || action === void 0 ? void 0 : action.host);
|
|
229
|
+
const share = ids(action === null || action === void 0 ? void 0 : action.share);
|
|
230
|
+
if ((action === null || action === void 0 ? void 0 : action.action) === 'policy') {
|
|
231
|
+
await this.storage.put('policy', RelayModerationPolicyUtil.of({ ...(await this.policy()), ...((_a = action.policy) !== null && _a !== void 0 ? _a : {}) }));
|
|
232
|
+
await this.record({ by: 'operator', action: 'policy', target: 'policy' });
|
|
233
|
+
return this.blocks();
|
|
234
|
+
}
|
|
235
|
+
if ((action === null || action === void 0 ? void 0 : action.action) === 'limit' || (action === null || action === void 0 ? void 0 : action.action) === 'ban' || (action === null || action === void 0 ? void 0 : action.action) === 'lift' || (action === null || action === void 0 ? void 0 : action.action) === 'answer') {
|
|
236
|
+
await this.sanction(action);
|
|
237
|
+
return this.blocks();
|
|
238
|
+
}
|
|
239
|
+
if ((action === null || action === void 0 ? void 0 : action.action) === 'dismiss') {
|
|
240
|
+
if (!share) {
|
|
241
|
+
throw new Error('Dismiss the report of a share.');
|
|
242
|
+
}
|
|
243
|
+
await this.storage.put('reports', (await this.kept()).filter(report => report.share !== share));
|
|
244
|
+
await this.record({ by: 'operator', action: 'dismiss', target: `share:${share}` });
|
|
245
|
+
return this.blocks();
|
|
246
|
+
}
|
|
247
|
+
if (((action === null || action === void 0 ? void 0 : action.action) !== 'block' && (action === null || action === void 0 ? void 0 : action.action) !== 'unblock') || (!host && !share)) {
|
|
248
|
+
throw new Error('Block or unblock a host or a share: { "action": "block", "host": "…" }.');
|
|
249
|
+
}
|
|
250
|
+
const blocks = await this.blocks();
|
|
251
|
+
const set = (list, id) => {
|
|
252
|
+
const rest = list.filter(each => each !== id);
|
|
253
|
+
return id && action.action === 'block' ? [...rest, id].slice(-MAX_BLOCKS) : rest;
|
|
254
|
+
};
|
|
255
|
+
const keys = [...(host ? [`host:${host}`] : []), ...(share ? [`share:${share}`] : [])];
|
|
256
|
+
const auto = { ...blocks.auto };
|
|
257
|
+
// What the operator unblocks of what the rules blocked, they leave be from then on; what the operator blocks is theirs.
|
|
258
|
+
const pardon = action.action === 'unblock' ? keys.filter(key => auto[key]) : [];
|
|
259
|
+
for (const key of keys) {
|
|
260
|
+
delete auto[key];
|
|
261
|
+
}
|
|
262
|
+
const pardoned = [...((_b = blocks.pardoned) !== null && _b !== void 0 ? _b : []).filter(key => action.action === 'unblock' || !keys.includes(key)), ...pardon].slice(-MAX_BLOCKS);
|
|
263
|
+
const next = { hosts: set(blocks.hosts, host), shares: set(blocks.shares, share), auto, pardoned: [...new Set(pardoned)] };
|
|
264
|
+
await this.storage.put('blocks', next);
|
|
265
|
+
for (const key of keys) {
|
|
266
|
+
await this.record({ by: 'operator', action: action.action, target: key });
|
|
267
|
+
}
|
|
268
|
+
return next;
|
|
269
|
+
}
|
|
270
|
+
/** Limits, bans, or lifts what an account may do; answers its appeal. What the operator lifts of the rules' they leave be. */
|
|
271
|
+
async sanction(action) {
|
|
272
|
+
var _a;
|
|
273
|
+
var _b, _c, _d, _e;
|
|
274
|
+
const account = typeof action.account === 'string' && action.account && action.account.length <= 200 ? action.account : '';
|
|
275
|
+
if (!account) {
|
|
276
|
+
throw new Error('Name the account.');
|
|
277
|
+
}
|
|
278
|
+
const kept = (_b = (await this.storage.get('sanctions'))) !== null && _b !== void 0 ? _b : {};
|
|
279
|
+
const before = kept[account];
|
|
280
|
+
const appeals = await this.appeals();
|
|
281
|
+
const name = (_c = before === null || before === void 0 ? void 0 : before.name) !== null && _c !== void 0 ? _c : (_a = appeals.find(each => each.account === account)) === null || _a === void 0 ? void 0 : _a.name;
|
|
282
|
+
if (action.action === 'answer') {
|
|
283
|
+
await this.storage.put('appeals', appeals.filter(each => each.account !== account));
|
|
284
|
+
if (action.restore) {
|
|
285
|
+
return this.sanction({ action: 'lift', account, reason: 'appeal' });
|
|
286
|
+
}
|
|
287
|
+
await this.record({ by: 'operator', action: 'keep', target: `account:${account}` });
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
if (action.action === 'lift') {
|
|
291
|
+
delete kept[account];
|
|
292
|
+
await this.storage.put('sanctions', kept);
|
|
293
|
+
if ((before === null || before === void 0 ? void 0 : before.by) === 'rule') {
|
|
294
|
+
const blocks = await this.blocks();
|
|
295
|
+
await this.storage.put('blocks', { ...blocks, pardoned: [...new Set([...((_d = blocks.pardoned) !== null && _d !== void 0 ? _d : []), `account:${account}`])] });
|
|
296
|
+
}
|
|
297
|
+
await this.record({ by: 'operator', action: 'lift', target: `account:${account}`, ...(action.reason ? { reason: action.reason } : {}) });
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
const days = Number(action.days);
|
|
301
|
+
const now = this.now();
|
|
302
|
+
kept[account] = {
|
|
303
|
+
account,
|
|
304
|
+
...(name ? { name } : {}),
|
|
305
|
+
level: action.action,
|
|
306
|
+
at: now,
|
|
307
|
+
...(days > 0 ? { until: now + Math.min(days, 3650) * DAY_MS } : {}),
|
|
308
|
+
by: 'operator',
|
|
309
|
+
reason: String((_e = action.reason) !== null && _e !== void 0 ? _e : '').slice(0, 200),
|
|
310
|
+
};
|
|
311
|
+
await this.storage.put('sanctions', kept);
|
|
312
|
+
await this.record({
|
|
313
|
+
by: 'operator',
|
|
314
|
+
action: action.action,
|
|
315
|
+
target: `account:${account}`,
|
|
316
|
+
...(action.reason ? { reason: String(action.reason).slice(0, 120) } : {}),
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
/** What the rules block after a report of this link: the link, then perhaps its computer. */
|
|
320
|
+
async enforce(report, reports) {
|
|
321
|
+
var _a, _b, _c, _d;
|
|
322
|
+
const rules = await this.policy();
|
|
323
|
+
if (!rules.auto) {
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
const blocks = await this.blocks();
|
|
327
|
+
const auto = { ...blocks.auto };
|
|
328
|
+
const pardoned = new Set(blocks.pardoned);
|
|
329
|
+
const now = this.now();
|
|
330
|
+
let changed = false;
|
|
331
|
+
const shareKey = `share:${report.share}`;
|
|
332
|
+
if (!blocks.shares.includes(report.share) && !blocks.hosts.includes(report.host) && !pardoned.has(shareKey)) {
|
|
333
|
+
const severe = (_a = report.severe) !== null && _a !== void 0 ? _a : 0;
|
|
334
|
+
const people = (_b = report.people) !== null && _b !== void 0 ? _b : 0;
|
|
335
|
+
const rule = severe >= rules.severe ? 'severe' : people >= rules.people ? 'people' : undefined;
|
|
336
|
+
if (rule) {
|
|
337
|
+
blocks.shares = [...blocks.shares, report.share].slice(-MAX_BLOCKS);
|
|
338
|
+
auto[shareKey] = { at: now, rule, count: rule === 'severe' ? severe : people };
|
|
339
|
+
changed = true;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
const owner = await this.owner(report.host);
|
|
343
|
+
if (owner) {
|
|
344
|
+
// Of an account: the rules limit the person, for policy.limitDays; the operator alone bans.
|
|
345
|
+
const hosts = (_c = (await this.storage.get(`hosts:${owner.account}`))) !== null && _c !== void 0 ? _c : [report.host];
|
|
346
|
+
const shares = new Set(reports.filter(each => hosts.includes(each.host)).map(each => each.share));
|
|
347
|
+
const stopped = blocks.shares.filter(id => shares.has(id)).length;
|
|
348
|
+
const sanctions = (_d = (await this.storage.get('sanctions'))) !== null && _d !== void 0 ? _d : {};
|
|
349
|
+
if (rules.hostAfter > 0 &&
|
|
350
|
+
stopped >= rules.hostAfter &&
|
|
351
|
+
!pardoned.has(`account:${owner.account}`) &&
|
|
352
|
+
!RelayModerationPolicyUtil.active(sanctions[owner.account], now)) {
|
|
353
|
+
sanctions[owner.account] = {
|
|
354
|
+
account: owner.account,
|
|
355
|
+
...(owner.name ? { name: owner.name } : {}),
|
|
356
|
+
level: 'limit',
|
|
357
|
+
at: now,
|
|
358
|
+
until: now + rules.limitDays * DAY_MS,
|
|
359
|
+
by: 'rule',
|
|
360
|
+
reason: `${stopped} links stopped`,
|
|
361
|
+
};
|
|
362
|
+
await this.storage.put('sanctions', sanctions);
|
|
363
|
+
await this.record({ by: 'rule', action: 'limit', target: `account:${owner.account}`, reason: `${stopped} links stopped` });
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
const hostKey = `host:${report.host}`;
|
|
367
|
+
if (!owner && rules.hostAfter > 0 && !blocks.hosts.includes(report.host) && !pardoned.has(hostKey)) {
|
|
368
|
+
const shares = new Set(reports.filter(each => each.host === report.host).map(each => each.share));
|
|
369
|
+
const blocked = blocks.shares.filter(id => shares.has(id)).length;
|
|
370
|
+
if (blocked >= rules.hostAfter) {
|
|
371
|
+
blocks.hosts = [...blocks.hosts, report.host].slice(-MAX_BLOCKS);
|
|
372
|
+
auto[hostKey] = { at: now, rule: 'host', count: blocked };
|
|
373
|
+
changed = true;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
if (changed) {
|
|
377
|
+
await this.storage.put('blocks', { ...blocks, auto });
|
|
378
|
+
for (const [key, made] of Object.entries(auto)) {
|
|
379
|
+
if (made.at === now) {
|
|
380
|
+
await this.record({ by: 'rule', action: 'block', target: key, reason: made.rule });
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
async report(request, address) {
|
|
386
|
+
const input = RelayModeration.parse(await request.json().catch(() => undefined));
|
|
387
|
+
if (!input) {
|
|
388
|
+
return RelayModeration.answer(400, { error: 'Report a link of content: its host, its share and why.' });
|
|
389
|
+
}
|
|
390
|
+
const now = this.now();
|
|
391
|
+
const key = `tries:${address}`;
|
|
392
|
+
const before = await this.storage.get(key);
|
|
393
|
+
const tries = before && now - before.since < HOUR_MS ? before : { count: 0, since: now };
|
|
394
|
+
if (tries.count >= MAX_TRIES) {
|
|
395
|
+
return RelayModeration.answer(429, { error: 'Too many reports: try again later.' });
|
|
396
|
+
}
|
|
397
|
+
await this.storage.put(key, { ...tries, count: tries.count + 1 });
|
|
398
|
+
const reports = await this.kept();
|
|
399
|
+
const who = (await RelayModeration.digest(`ta-report:${address}`)).slice(0, 16);
|
|
400
|
+
const severe = RELAY_SEVERE_REASONS.includes(input.reason);
|
|
401
|
+
const same = reports.find(report => report.share === input.share && report.host === input.host);
|
|
402
|
+
const counted = (list, add) => (add && !(list === null || list === void 0 ? void 0 : list.includes(who)) ? [...(list !== null && list !== void 0 ? list : []), who].slice(-MAX_FROM) : (list !== null && list !== void 0 ? list : []));
|
|
403
|
+
if (same) {
|
|
404
|
+
same.count++;
|
|
405
|
+
same.last = now;
|
|
406
|
+
same.reason = input.reason;
|
|
407
|
+
if (input.note) {
|
|
408
|
+
same.note = input.note;
|
|
409
|
+
}
|
|
410
|
+
if (input.link) {
|
|
411
|
+
same.link = input.link;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
const report = same !== null && same !== void 0 ? same : { ...input, at: now, last: now, count: 1 };
|
|
415
|
+
if (!same) {
|
|
416
|
+
reports.push(report);
|
|
417
|
+
}
|
|
418
|
+
report.from = counted(report.from, true);
|
|
419
|
+
report.fromSevere = counted(report.fromSevere, severe);
|
|
420
|
+
report.people = report.from.length;
|
|
421
|
+
report.severe = report.fromSevere.length;
|
|
422
|
+
await this.storage.put('reports', reports.sort((a, b) => b.last - a.last).slice(0, MAX_REPORTS));
|
|
423
|
+
await this.enforce(report, reports);
|
|
424
|
+
return new Response(null, { status: 204 });
|
|
425
|
+
}
|
|
426
|
+
static parse(value) {
|
|
427
|
+
const given = value;
|
|
428
|
+
if (!given ||
|
|
429
|
+
typeof given.host !== 'string' ||
|
|
430
|
+
typeof given.share !== 'string' ||
|
|
431
|
+
!RELAY_ID.test(given.host) ||
|
|
432
|
+
!RELAY_ID.test(given.share) ||
|
|
433
|
+
!RELAY_REPORT_REASONS.includes(given.reason)) {
|
|
434
|
+
return undefined;
|
|
435
|
+
}
|
|
436
|
+
const note = typeof given.note === 'string' ? given.note.trim().slice(0, MAX_NOTE) : '';
|
|
437
|
+
const link = RelayModeration.linkOf(given.link, given.host, given.share);
|
|
438
|
+
return { host: given.host, share: given.share, reason: given.reason, ...(note ? { note } : {}), ...(link ? { link } : {}) };
|
|
439
|
+
}
|
|
440
|
+
/** A link of content of this host and share, as sent with a report; undefined for anything else. */
|
|
441
|
+
static linkOf(value, host, share) {
|
|
442
|
+
if (typeof value !== 'string' || value.length > 2048) {
|
|
443
|
+
return undefined;
|
|
444
|
+
}
|
|
445
|
+
try {
|
|
446
|
+
const link = RelayContentUtil.parse(value);
|
|
447
|
+
return link.host === host && link.share === share && link.access !== 'self' && link.access !== 'people' ? value.trim() : undefined;
|
|
448
|
+
}
|
|
449
|
+
catch (_a) {
|
|
450
|
+
return undefined;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
static answer(status, body) {
|
|
454
|
+
return new Response(JSON.stringify(body), { status, headers: JSON_HEADERS });
|
|
455
|
+
}
|
|
456
|
+
/** Compares by their SHA-256, so that the time it takes says nothing of the token. */
|
|
457
|
+
static async same(given, expected) {
|
|
458
|
+
return (await RelayModeration.digest(given)) === (await RelayModeration.digest(expected));
|
|
459
|
+
}
|
|
460
|
+
static async digest(text) {
|
|
461
|
+
return RelayBytes.toBase64(new Uint8Array(await globalThis.crypto.subtle.digest('SHA-256', new TextEncoder().encode(text))));
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
//# sourceMappingURL=relay-moderation.js.map
|
|
@@ -116,7 +116,11 @@ export declare enum RelayCloseCode {
|
|
|
116
116
|
/** Too many devices are connected to this host. */
|
|
117
117
|
FULL = 4429,
|
|
118
118
|
/** No answer in time: to the hello, or to the pings of the heartbeat; the connection is gone without a word. */
|
|
119
|
-
TIMEOUT = 4408
|
|
119
|
+
TIMEOUT = 4408,
|
|
120
|
+
/** The operator of the relay blocked what is shared by this host or link (RelayModeration). */
|
|
121
|
+
BLOCKED = 4451,
|
|
122
|
+
/** Where the operator needs it, links open in a browser only of one who signed in to an account (RelayModerationPolicy.needsAccount). */
|
|
123
|
+
SIGN_IN = 4452
|
|
120
124
|
}
|
|
121
125
|
/**
|
|
122
126
|
* The heartbeat: hosts and devices send `ping` to the relay every so often, outside the channels, and it
|
|
@@ -89,6 +89,10 @@ export var RelayCloseCode;
|
|
|
89
89
|
RelayCloseCode[RelayCloseCode["FULL"] = 4429] = "FULL";
|
|
90
90
|
/** No answer in time: to the hello, or to the pings of the heartbeat; the connection is gone without a word. */
|
|
91
91
|
RelayCloseCode[RelayCloseCode["TIMEOUT"] = 4408] = "TIMEOUT";
|
|
92
|
+
/** The operator of the relay blocked what is shared by this host or link (RelayModeration). */
|
|
93
|
+
RelayCloseCode[RelayCloseCode["BLOCKED"] = 4451] = "BLOCKED";
|
|
94
|
+
/** Where the operator needs it, links open in a browser only of one who signed in to an account (RelayModerationPolicy.needsAccount). */
|
|
95
|
+
RelayCloseCode[RelayCloseCode["SIGN_IN"] = 4452] = "SIGN_IN";
|
|
92
96
|
})(RelayCloseCode || (RelayCloseCode = {}));
|
|
93
97
|
/**
|
|
94
98
|
* The heartbeat: hosts and devices send `ping` to the relay every so often, outside the channels, and it
|
|
@@ -12,6 +12,11 @@ export declare const ROOM_APP_STALE_MS: number;
|
|
|
12
12
|
export declare namespace RoomAppUtil {
|
|
13
13
|
/** What others wrote, which a model reads inside tags: no tags of its own. */
|
|
14
14
|
function quote(text: string): string;
|
|
15
|
+
/**
|
|
16
|
+
* What others wrote, as the words of a message that the user reads too, drawn as Markdown: quoted, and a picture it
|
|
17
|
+
* names (``) shown as written, never loaded from where they chose.
|
|
18
|
+
*/
|
|
19
|
+
function shown(text: string): string;
|
|
15
20
|
/**
|
|
16
21
|
* What others said to the room since `since`: not by the user, not too long ago, not to someone in particular (@ a
|
|
17
22
|
* person, a double or an agent: theirs to answer), not said by a double or an agent.
|
|
@@ -16,6 +16,14 @@ export var RoomAppUtil;
|
|
|
16
16
|
return text.replace(/</g, '<').replace(/>/g, '>');
|
|
17
17
|
}
|
|
18
18
|
RoomAppUtil.quote = quote;
|
|
19
|
+
/**
|
|
20
|
+
* What others wrote, as the words of a message that the user reads too, drawn as Markdown: quoted, and a picture it
|
|
21
|
+
* names (``) shown as written, never loaded from where they chose.
|
|
22
|
+
*/
|
|
23
|
+
function shown(text) {
|
|
24
|
+
return quote(text).replace(/!\[/g, '!\\[');
|
|
25
|
+
}
|
|
26
|
+
RoomAppUtil.shown = shown;
|
|
19
27
|
/**
|
|
20
28
|
* What others said to the room since `since`: not by the user, not too long ago, not to someone in particular (@ a
|
|
21
29
|
* person, a double or an agent: theirs to answer), not said by a double or an agent.
|
|
@@ -10,6 +10,8 @@ export declare class RelayServerAccounts {
|
|
|
10
10
|
constructor(data: string | undefined, options?: Pick<RelayAccountsOptions, 'authenticate' | 'issue'> & {
|
|
11
11
|
providers?: RelayAccountProvider[];
|
|
12
12
|
});
|
|
13
|
+
/** The account of a bearer token (RelayAccounts.whoIs). */
|
|
14
|
+
whoIs(authorization: string): Promise<import("./index.js").AccountName | undefined>;
|
|
13
15
|
/** Answers a request of RELAY_ACCOUNT_PATH; never throws, an unhandled rejection would end the relay. */
|
|
14
16
|
serve(request: IncomingMessage, response: ServerResponse): void;
|
|
15
17
|
protected answer(request: IncomingMessage, response: ServerResponse): Promise<void>;
|
|
@@ -17,6 +17,10 @@ export class RelayServerAccounts {
|
|
|
17
17
|
const providers = (_a = options.providers) !== null && _a !== void 0 ? _a : (options.authenticate ? [] : [new RelayGitHubAccounts()]);
|
|
18
18
|
this.accounts = new RelayAccounts({ book: account => books.book(account), providers, authenticate: options.authenticate, issue: options.issue });
|
|
19
19
|
}
|
|
20
|
+
/** The account of a bearer token (RelayAccounts.whoIs). */
|
|
21
|
+
whoIs(authorization) {
|
|
22
|
+
return this.accounts.whoIs(authorization);
|
|
23
|
+
}
|
|
20
24
|
/** Answers a request of RELAY_ACCOUNT_PATH; never throws, an unhandled rejection would end the relay. */
|
|
21
25
|
serve(request, response) {
|
|
22
26
|
this.answer(request, response).catch(() => {
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { type RelayAccountWho, RelayModeration, type RelayModerationStorage } from '../common/relay-moderation.js';
|
|
3
|
+
export interface RelayServerGateOptions {
|
|
4
|
+
content?: string;
|
|
5
|
+
adminToken?: string;
|
|
6
|
+
/** The account of a bearer token (the account service of the relay). */
|
|
7
|
+
whoIs(authorization: string): Promise<RelayAccountWho | undefined>;
|
|
8
|
+
/** Whether this secret is the one a computer holds its id with. */
|
|
9
|
+
holds(host: string, secret: string): boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* What `ta relay` answers before anything else (docs/design/ta-link.md 2.10): by the domain a request came to, its own
|
|
13
|
+
* or that of content; the reports of links and the operator's blocks.
|
|
14
|
+
*/
|
|
15
|
+
export declare class RelayServerGate {
|
|
16
|
+
protected readonly options: RelayServerGateOptions;
|
|
17
|
+
protected readonly moderation: RelayModeration;
|
|
18
|
+
constructor(storage: RelayModerationStorage, options: RelayServerGateOptions);
|
|
19
|
+
/** The address a request reached the relay at, by its Host. */
|
|
20
|
+
static urlOf(request: IncomingMessage): URL;
|
|
21
|
+
/** Whether it answered the request: not on this domain, sent to the domain of content, a report, the operator's. */
|
|
22
|
+
answer(request: IncomingMessage, response: ServerResponse): boolean;
|
|
23
|
+
/** What the policy needs an account for, as /v1/info says it. */
|
|
24
|
+
needsAccount(): Promise<{
|
|
25
|
+
needsAccount?: string[];
|
|
26
|
+
}>;
|
|
27
|
+
/** Whether a WebSocket to this path has no place on the domain it came to. */
|
|
28
|
+
refuses(request: IncomingMessage): boolean;
|
|
29
|
+
static headersOf(request: IncomingMessage): Headers;
|
|
30
|
+
/** Why a viewer of a link of this host is refused (RelayModeration.viewer); the pages of the relay in a browser by their Origin. */
|
|
31
|
+
refusesViewer(request: IncomingMessage, host: string): Promise<number | undefined>;
|
|
32
|
+
/** Why a copy is not kept: where links need an account, the one who puts it names theirs (`x-ta-account`). */
|
|
33
|
+
refusesCopy(request: IncomingMessage): Promise<number | undefined>;
|
|
34
|
+
/** The status a copy is refused with: read, when its share is blocked (410); put, by none or a sanctioned account where links need one (451). */
|
|
35
|
+
refusesBlob(request: IncomingMessage, share: string): Promise<number | undefined>;
|
|
36
|
+
protected moderate(request: IncomingMessage, response: ServerResponse): Promise<void>;
|
|
37
|
+
/** The body of a request, or undefined past `max` bytes: the rest is read and dropped. */
|
|
38
|
+
static body(request: IncomingMessage, max: number): Promise<Buffer<ArrayBuffer> | undefined>;
|
|
39
|
+
}
|