levix-bot 4.8.0 → 4.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -5
- package/SETUP.md +21 -2
- package/app.cjs +69 -23
- package/package.json +3 -2
- package/public/dashboard/assets/index-B1sOW2VX.js +561 -0
- package/public/dashboard/assets/index-CK8osB4o.css +2 -0
- package/public/dashboard/index.html +2 -2
- package/public/dashboard.css +21 -0
- package/public/dashboard.js +25 -2
- package/public/gateway-i18n.js +26 -0
- package/src/bootstrap/session-startup-policy.js +6 -6
- package/src/commands/group/antilink.cjs +82 -59
- package/src/commands/group/antispam.cjs +112 -0
- package/src/commands/group/clearwarns.cjs +6 -6
- package/src/commands/group/demote.cjs +11 -13
- package/src/commands/group/flagged.cjs +52 -0
- package/src/commands/group/kick.cjs +25 -15
- package/src/commands/group/media.cjs +65 -29
- package/src/commands/group/policy.cjs +215 -0
- package/src/commands/group/promote.cjs +11 -12
- package/src/commands/group/rules.cjs +9 -6
- package/src/commands/group/setwarn.cjs +17 -27
- package/src/commands/group/warn.cjs +13 -63
- package/src/commands/group.cjs +5 -2
- package/src/commands/mod.cjs +101 -26
- package/src/commands/translate.cjs +585 -0
- package/src/config/ai-identity.cjs +53 -1
- package/src/config/ai-persona.md +32 -73
- package/src/config/defaults.cjs +2 -0
- package/src/config/secrets.cjs +71 -3
- package/src/config/settings.cjs +22 -1
- package/src/core/socket.js +4 -5
- package/src/db/db.cjs +66 -0
- package/src/db/store.cjs +264 -16
- package/src/diagnostics/crashReports.cjs +35 -9
- package/src/diagnostics/debugLog.cjs +405 -0
- package/src/handlers/command.handler.js +1 -0
- package/src/handlers/group.handler.js +32 -1
- package/src/handlers/message.handler.js +53 -17
- package/src/middleware/antispam.middleware.js +95 -48
- package/src/panel/feedback.cjs +32 -5
- package/src/panel/one-time-code.cjs +156 -0
- package/src/panel/password-reset.cjs +187 -91
- package/src/panel/recovery-email.cjs +333 -0
- package/src/routes/dashboard.api.esm.js +267 -20
- package/src/services/aiAgent.cjs +25 -3
- package/src/services/moderationPolicy.cjs +895 -0
- package/src/services/settingsTransfer.cjs +4 -2
- package/src/utils/linkedCreds.cjs +22 -0
- package/src/utils/logger.cjs +5 -0
- package/src/utils/moderationAction.cjs +637 -0
- package/src/utils/moderationSummary.cjs +225 -0
- package/src/utils/permissions.cjs +70 -0
- package/src/utils/permissions.esm.js +48 -8
- package/src/utils/storage.cjs +8 -0
- package/src/utils/storage.esm.js +4 -0
- package/views/login.ejs +100 -28
- package/public/dashboard/assets/index-B8E5IYlB.js +0 -540
- package/public/dashboard/assets/index-C5D4wzm4.css +0 -2
- package/src/commands/group/antiSpam.cjs +0 -97
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
// The panel's recovery email.
|
|
2
|
+
//
|
|
3
|
+
// Account security, stored like the password hash: one `bot_settings` row,
|
|
4
|
+
// sealed with the install key (src/config/vault.cjs), never a `setting:*`
|
|
5
|
+
// value, so settings export/import cannot see it. The row exists only after
|
|
6
|
+
// the operator verifies a code. Until then the candidate address lives in
|
|
7
|
+
// this process next to the relay's verification id, and a restart drops it.
|
|
8
|
+
//
|
|
9
|
+
// Delivery is a Levix-operated relay (levix-ui, src/lib/recovery-relay.ts).
|
|
10
|
+
// Contract v2, so the relay cannot be used to mail strangers:
|
|
11
|
+
//
|
|
12
|
+
// verify { kind, to, lang } the relay makes the code and
|
|
13
|
+
// emails it; the answer is an id
|
|
14
|
+
// confirm { kind, verification, code } the operator's guess; a match
|
|
15
|
+
// answers a token for the address
|
|
16
|
+
// reset { kind, to, code, token, lang } refused without that token
|
|
17
|
+
//
|
|
18
|
+
// This process never sees a verify code, so the relay checks it: five
|
|
19
|
+
// guesses per code, single use. The token is sealed with the address and is
|
|
20
|
+
// the only thing that lets this install ask for a reset email. No free text
|
|
21
|
+
// goes to the relay. `LEVIX_RECOVERY_URL` points the requests at a local
|
|
22
|
+
// server in tests; operators do not set it.
|
|
23
|
+
|
|
24
|
+
const logger = require("../utils/logger.cjs");
|
|
25
|
+
const secrets = require("../config/secrets.cjs");
|
|
26
|
+
const { CODE_TTL_MS, createThrottle } = require("./one-time-code.cjs");
|
|
27
|
+
|
|
28
|
+
const DEFAULT_ENDPOINT = "https://levix.leviro.net/api/recovery-email";
|
|
29
|
+
const RELAY_TIMEOUT_MS = 15_000;
|
|
30
|
+
|
|
31
|
+
// Verification sends have their own throttle. Reset sends share one throttle
|
|
32
|
+
// inside password-reset.cjs, across WhatsApp and email.
|
|
33
|
+
const verifyThrottle = createThrottle();
|
|
34
|
+
|
|
35
|
+
/** @type {{ verification: string, expiresAt: number, email: string, inFlight?: boolean } | null} */
|
|
36
|
+
let pendingVerify = null;
|
|
37
|
+
// Same rule as the reset slot: the commit after `await` runs only for the
|
|
38
|
+
// latest request. `remove` bumps this so a send still in flight cannot land.
|
|
39
|
+
let verifyGeneration = 0;
|
|
40
|
+
|
|
41
|
+
const EMAIL_RE =
|
|
42
|
+
/^[a-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+$/;
|
|
43
|
+
const VERIFICATION_RE = /^[0-9a-f]{32}$/;
|
|
44
|
+
const TOKEN_RE = /^v1\.[A-Za-z0-9_-]{43}$/;
|
|
45
|
+
|
|
46
|
+
// Relay error codes passed through unchanged. Anything else is `unavailable`.
|
|
47
|
+
const RELAY_REASONS = new Set([
|
|
48
|
+
"invalid",
|
|
49
|
+
"rate_limited",
|
|
50
|
+
"unverified",
|
|
51
|
+
"wrong_code",
|
|
52
|
+
"expired",
|
|
53
|
+
"too_many_attempts",
|
|
54
|
+
"no_pending",
|
|
55
|
+
]);
|
|
56
|
+
// Relay answers that end a verification: the next guess cannot succeed.
|
|
57
|
+
const VERIFY_DEAD = new Set(["expired", "too_many_attempts", "no_pending"]);
|
|
58
|
+
|
|
59
|
+
function normalizeLang(lang) {
|
|
60
|
+
return lang === "ar" ? "ar" : "en";
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Trim, lowercase, and refuse anything the relay should not be asked to accept.
|
|
65
|
+
* ASCII only: one @, a dotted domain, a TLD of at least two characters.
|
|
66
|
+
* A newline or other control is rejected before trim. trim() would otherwise
|
|
67
|
+
* turn "a@example.com\n" into a deliverable address (header injection).
|
|
68
|
+
*
|
|
69
|
+
* @param {unknown} value
|
|
70
|
+
* @returns {{ ok: true, email: string } | { ok: false }}
|
|
71
|
+
*/
|
|
72
|
+
function normalizeEmail(value) {
|
|
73
|
+
const raw = String(value ?? "");
|
|
74
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: reject header controls before trimming an address.
|
|
75
|
+
if (/[\u0000-\u001F\u007F\u2028\u2029]/.test(raw)) return { ok: false };
|
|
76
|
+
const email = raw.trim().toLowerCase();
|
|
77
|
+
if (email.length < 3 || email.length > 254) return { ok: false };
|
|
78
|
+
if (email.includes("..")) return { ok: false };
|
|
79
|
+
const at = email.indexOf("@");
|
|
80
|
+
if (at <= 0 || at !== email.lastIndexOf("@")) return { ok: false };
|
|
81
|
+
const local = email.slice(0, at);
|
|
82
|
+
const domain = email.slice(at + 1);
|
|
83
|
+
if (local.length > 64 || local.startsWith(".") || local.endsWith(".")) return { ok: false };
|
|
84
|
+
const tld = domain.slice(domain.lastIndexOf(".") + 1);
|
|
85
|
+
if (!/^[a-z]{2,}$/.test(tld)) return { ok: false };
|
|
86
|
+
if (!EMAIL_RE.test(email)) return { ok: false };
|
|
87
|
+
return { ok: true, email };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* `abdelrhman@gmail.com` → `ab***@g***.com`.
|
|
92
|
+
* Two characters of the local part (one, when that is all there is), one
|
|
93
|
+
* character of the domain, and the last label. The stars are fixed, so the
|
|
94
|
+
* mask does not reveal how long the address is.
|
|
95
|
+
*
|
|
96
|
+
* @param {string} email a normalized address
|
|
97
|
+
*/
|
|
98
|
+
function maskEmail(email) {
|
|
99
|
+
const at = String(email ?? "").lastIndexOf("@");
|
|
100
|
+
if (at <= 0) return "***";
|
|
101
|
+
const local = email.slice(0, at);
|
|
102
|
+
const domain = email.slice(at + 1);
|
|
103
|
+
const localShown = local.slice(0, Math.min(2, local.length));
|
|
104
|
+
const dot = domain.lastIndexOf(".");
|
|
105
|
+
const domainShown = domain.slice(0, 1);
|
|
106
|
+
const tld = dot > 0 ? domain.slice(dot) : "";
|
|
107
|
+
return `${localShown}***@${domainShown}***${tld}`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The verified address and its relay token, or null. For delivery only. */
|
|
111
|
+
function readRecord() {
|
|
112
|
+
try {
|
|
113
|
+
return secrets.getRecoveryRecord();
|
|
114
|
+
} catch (error) {
|
|
115
|
+
logger.warn({ err: error }, "[recovery-email] Could not read the recovery address");
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Only an authorized record may deliver reset mail. Legacy rows need verification. */
|
|
121
|
+
function getRecord() {
|
|
122
|
+
const record = readRecord();
|
|
123
|
+
return record && !record.needsVerification ? record : null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** The verified address, or null. For delivery — never for an API response. */
|
|
127
|
+
function getAddress() {
|
|
128
|
+
return getRecord()?.email ?? null;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** What an API may return. Never the address itself. */
|
|
132
|
+
function publicStatus() {
|
|
133
|
+
const record = readRecord();
|
|
134
|
+
if (!record) return { verified: false };
|
|
135
|
+
return {
|
|
136
|
+
verified: !record.needsVerification,
|
|
137
|
+
needsVerification: record.needsVerification,
|
|
138
|
+
masked: maskEmail(record.email),
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* POST one relay request. Network failures, timeouts, 5xx and unknown answers
|
|
144
|
+
* are `unavailable`. The address, the code and the token are not logged.
|
|
145
|
+
*
|
|
146
|
+
* @param {object} body exactly one v2 contract body
|
|
147
|
+
* @returns {Promise<{ ok: true, data: object } | { ok: false, reason: string, attemptsLeft?: number }>}
|
|
148
|
+
*/
|
|
149
|
+
async function callRelay(body) {
|
|
150
|
+
const endpoint = process.env.LEVIX_RECOVERY_URL || DEFAULT_ENDPOINT;
|
|
151
|
+
let response;
|
|
152
|
+
let data = null;
|
|
153
|
+
try {
|
|
154
|
+
response = await fetch(endpoint, {
|
|
155
|
+
method: "POST",
|
|
156
|
+
headers: { "Content-Type": "application/json" },
|
|
157
|
+
body: JSON.stringify(body),
|
|
158
|
+
signal: AbortSignal.timeout(RELAY_TIMEOUT_MS),
|
|
159
|
+
redirect: "error",
|
|
160
|
+
});
|
|
161
|
+
data = await response.json().catch(() => null);
|
|
162
|
+
} catch {
|
|
163
|
+
logger.warn({ kind: body.kind }, "[recovery-email] Could not reach the relay");
|
|
164
|
+
return { ok: false, reason: "unavailable" };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (response.ok && data?.ok === true) return { ok: true, data };
|
|
168
|
+
|
|
169
|
+
let reason = "unavailable";
|
|
170
|
+
if (response.status >= 500) reason = "unavailable";
|
|
171
|
+
else if (data?.error === "unverified") {
|
|
172
|
+
if (response.status === 403) reason = "unverified";
|
|
173
|
+
} else if (RELAY_REASONS.has(data?.error)) reason = data.error;
|
|
174
|
+
else if (response.status === 400) reason = "invalid";
|
|
175
|
+
else if (response.status === 429) reason = "rate_limited";
|
|
176
|
+
const failure = { ok: false, reason };
|
|
177
|
+
if (reason === "rate_limited") {
|
|
178
|
+
const retry = response.headers.get("retry-after");
|
|
179
|
+
if (retry && /^\d+$/.test(retry) && Number.isSafeInteger(Number(retry)) && Number(retry) > 0) {
|
|
180
|
+
failure.retryAfter = Number(retry);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
if (reason === "wrong_code" && Number.isInteger(data?.attemptsLeft)) {
|
|
184
|
+
failure.attemptsLeft = data.attemptsLeft;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
logger.warn(
|
|
188
|
+
{ kind: body.kind, status: response.status, reason },
|
|
189
|
+
"[recovery-email] The relay refused the request",
|
|
190
|
+
);
|
|
191
|
+
return failure;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Ask the relay to email a reset code to the verified address.
|
|
196
|
+
*
|
|
197
|
+
* @param {{ to: string, token: string, code: string, lang?: string }} message
|
|
198
|
+
* @returns {Promise<{ ok: true } | { ok: false, reason: string }>}
|
|
199
|
+
*/
|
|
200
|
+
async function sendResetCode({ to, token, code, lang }) {
|
|
201
|
+
if (!to || !token || !code) return { ok: false, reason: "invalid" };
|
|
202
|
+
const sent = await callRelay({
|
|
203
|
+
kind: "reset",
|
|
204
|
+
to,
|
|
205
|
+
code: String(code),
|
|
206
|
+
token,
|
|
207
|
+
lang: normalizeLang(lang),
|
|
208
|
+
});
|
|
209
|
+
if (!sent.ok && sent.reason === "unverified") {
|
|
210
|
+
if (secrets.markRecoveryUnverified(to, token)) {
|
|
211
|
+
require("./password-reset.cjs").noteRecoveryAddress(null);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return sent.ok ? { ok: true } : sent;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Start verification of a new address. The previously verified address stays
|
|
219
|
+
* in the database until `confirmVerification` succeeds. The route has already
|
|
220
|
+
* checked the panel password.
|
|
221
|
+
*
|
|
222
|
+
* @returns {Promise<{ ok: true } | { ok: false, reason: string, retryAfter?: number }>}
|
|
223
|
+
*/
|
|
224
|
+
async function startVerification(email, lang, peer) {
|
|
225
|
+
const normalized = normalizeEmail(email);
|
|
226
|
+
if (!normalized.ok) return { ok: false, reason: "invalid_email" };
|
|
227
|
+
|
|
228
|
+
const now = Date.now();
|
|
229
|
+
const key = String(peer ?? "");
|
|
230
|
+
const gate = verifyThrottle.reserve(key, now);
|
|
231
|
+
if (!gate.ok) return { ok: false, reason: gate.reason, retryAfter: gate.retryAfter };
|
|
232
|
+
|
|
233
|
+
const generation = ++verifyGeneration;
|
|
234
|
+
const sent = await callRelay({ kind: "verify", to: normalized.email, lang: normalizeLang(lang) });
|
|
235
|
+
const verification = sent.ok ? sent.data.verification : null;
|
|
236
|
+
if (!sent.ok || typeof verification !== "string" || !VERIFICATION_RE.test(verification)) {
|
|
237
|
+
verifyThrottle.release(gate.reservation);
|
|
238
|
+
return sent.ok ? { ok: false, reason: "unavailable" } : sent;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Kept only after the relay accepts it, and only if this request is still
|
|
242
|
+
// the latest one. A failed send does not discard a live verification, and
|
|
243
|
+
// two overlapping sends cannot both become the pending one.
|
|
244
|
+
if (generation !== verifyGeneration) return { ok: false, reason: "throttled" };
|
|
245
|
+
pendingVerify = { verification, expiresAt: now + CODE_TTL_MS, email: normalized.email };
|
|
246
|
+
logger.info("[recovery-email] Verification code sent");
|
|
247
|
+
return { ok: true, expiresInMinutes: Math.round(CODE_TTL_MS / 60000) };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Send the operator's guess to the relay. A match stores the address with the
|
|
252
|
+
* relay's token.
|
|
253
|
+
*
|
|
254
|
+
* @param {unknown} code
|
|
255
|
+
* @returns {Promise<{ ok: true, verified: true, masked: string } | { ok: false, reason: string, attemptsLeft?: number }>}
|
|
256
|
+
*/
|
|
257
|
+
async function confirmVerification(code) {
|
|
258
|
+
const pending = pendingVerify;
|
|
259
|
+
if (!pending) return { ok: false, reason: "no_pending" };
|
|
260
|
+
if (Date.now() > pending.expiresAt) {
|
|
261
|
+
pendingVerify = null;
|
|
262
|
+
return { ok: false, reason: "expired" };
|
|
263
|
+
}
|
|
264
|
+
// Not a guess the relay would accept: nothing sent, no attempt spent.
|
|
265
|
+
const guess = String(code ?? "").trim();
|
|
266
|
+
if (!/^\d{6}$/.test(guess)) return { ok: false, reason: "wrong_code" };
|
|
267
|
+
// One guess at a time. Otherwise a second guess answered "no_pending" after
|
|
268
|
+
// the first one matched could drop the token the first one earned.
|
|
269
|
+
if (pending.inFlight) return { ok: false, reason: "throttled", retryAfter: 1 };
|
|
270
|
+
|
|
271
|
+
pending.inFlight = true;
|
|
272
|
+
let result;
|
|
273
|
+
try {
|
|
274
|
+
result = await callRelay({ kind: "confirm", verification: pending.verification, code: guess });
|
|
275
|
+
} finally {
|
|
276
|
+
pending.inFlight = false;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// Removed, replaced or already confirmed while this guess was in flight.
|
|
280
|
+
if (pendingVerify !== pending) return { ok: false, reason: "no_pending" };
|
|
281
|
+
|
|
282
|
+
if (!result.ok) {
|
|
283
|
+
if (VERIFY_DEAD.has(result.reason)) pendingVerify = null;
|
|
284
|
+
return result;
|
|
285
|
+
}
|
|
286
|
+
const token = result.data.token;
|
|
287
|
+
if (typeof token !== "string" || !TOKEN_RE.test(token)) return { ok: false, reason: "unavailable" };
|
|
288
|
+
|
|
289
|
+
try {
|
|
290
|
+
secrets.setRecoveryEmail(pending.email, token);
|
|
291
|
+
} catch (error) {
|
|
292
|
+
logger.error({ err: error }, "[recovery-email] Could not store the verified address");
|
|
293
|
+
return { ok: false, reason: "unavailable" };
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
pendingVerify = null;
|
|
297
|
+
// The saved address just changed. An email reset bound to the previous one
|
|
298
|
+
// must not still redeem.
|
|
299
|
+
require("./password-reset.cjs").noteRecoveryAddress(pending.email);
|
|
300
|
+
return { ok: true, verified: true, masked: maskEmail(pending.email) };
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function remove() {
|
|
304
|
+
pendingVerify = null;
|
|
305
|
+
verifyGeneration += 1;
|
|
306
|
+
secrets.clearRecoveryEmail();
|
|
307
|
+
require("./password-reset.cjs").noteRecoveryAddress(null);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function _resetForTests() {
|
|
311
|
+
pendingVerify = null;
|
|
312
|
+
verifyThrottle.reset();
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function _expireForTests() {
|
|
316
|
+
if (pendingVerify) pendingVerify.expiresAt = Date.now() - 1;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
module.exports = {
|
|
320
|
+
DEFAULT_ENDPOINT,
|
|
321
|
+
RELAY_TIMEOUT_MS,
|
|
322
|
+
normalizeEmail,
|
|
323
|
+
maskEmail,
|
|
324
|
+
getRecord,
|
|
325
|
+
getAddress,
|
|
326
|
+
publicStatus,
|
|
327
|
+
sendResetCode,
|
|
328
|
+
startVerification,
|
|
329
|
+
confirmVerification,
|
|
330
|
+
remove,
|
|
331
|
+
_resetForTests,
|
|
332
|
+
_expireForTests,
|
|
333
|
+
};
|