levix-bot 4.5.0 → 4.7.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/app.cjs +32 -7
- package/package.json +2 -2
- package/public/dashboard/assets/index-RnT7DAde.js +540 -0
- package/public/dashboard/assets/index-UcqCdCvG.css +2 -0
- package/public/dashboard/index.html +4 -3
- package/public/dashboard.css +123 -53
- package/public/gateway-i18n.js +295 -0
- package/public/gateway.js +30 -1
- package/public/ui-boot.js +71 -0
- package/src/bootstrap/core.js +9 -0
- package/src/commands/autodelete.cjs +213 -0
- package/src/commands/pack.cjs +46 -25
- package/src/commands/packs.cjs +4 -1
- package/src/commands/shortlink.cjs +273 -66
- package/src/commands/stickers.cjs +26 -8
- package/src/config/defaults.cjs +1 -0
- package/src/config/settings.cjs +10 -0
- package/src/core/session.js +9 -1
- package/src/db/db.cjs +57 -0
- package/src/db/store.cjs +250 -0
- package/src/diagnostics/crashReports.cjs +491 -0
- package/src/diagnostics/device.cjs +119 -0
- package/src/diagnostics/sanitize.cjs +90 -0
- package/src/handlers/command.handler.js +1 -0
- package/src/handlers/message.handler.js +15 -0
- package/src/index.js +9 -1
- package/src/panel/feedback.cjs +13 -1
- package/src/routes/dashboard.api.esm.js +188 -4
- package/src/services/autoDelete.cjs +578 -0
- package/src/stickers/errors.cjs +7 -6
- package/src/stickers/library.cjs +118 -13
- package/src/utils/bidi.cjs +21 -0
- package/src/utils/storage.cjs +14 -0
- package/src/utils/storage.esm.js +15 -0
- package/views/dashboard.ejs +2 -1
- package/views/login.ejs +102 -71
- package/views/qr.ejs +59 -30
- package/views/setup.ejs +34 -30
- package/public/dashboard/assets/index-COI8zgqa.css +0 -2
- package/public/dashboard/assets/index-IZMBuNAZ.js +0 -505
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
// Local crash reports (contract sections 4 and 5).
|
|
2
|
+
//
|
|
3
|
+
// A report is one JSON file in `<data>/crash-reports/`. It is written and read
|
|
4
|
+
// only on this device; it leaves only when the operator presses Send on one
|
|
5
|
+
// report, and then only as the sanitized upload object below. Nothing here
|
|
6
|
+
// contacts the network by itself and nothing is ever retried automatically.
|
|
7
|
+
//
|
|
8
|
+
// The store is bounded three ways: at most 20 pending files, files older than
|
|
9
|
+
// 7 days are swept, and a send record keeps the last 20 ids. Every write is
|
|
10
|
+
// atomic — a `.tmp` file, then a rename — so a crash mid-write cannot leave a
|
|
11
|
+
// half-written report for the list to trip over.
|
|
12
|
+
|
|
13
|
+
const crypto = require("node:crypto");
|
|
14
|
+
const fs = require("node:fs");
|
|
15
|
+
const path = require("node:path");
|
|
16
|
+
const { dataPath } = require("../config/paths.cjs");
|
|
17
|
+
const { sanitizeText } = require("./sanitize.cjs");
|
|
18
|
+
const { sanitizeClientFields } = require("./device.cjs");
|
|
19
|
+
|
|
20
|
+
const CRASH_DIR = dataPath("crash-reports");
|
|
21
|
+
const SENT_FILE = path.join(CRASH_DIR, "sent.json");
|
|
22
|
+
|
|
23
|
+
const MAX_FILES = 20;
|
|
24
|
+
const MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
|
|
25
|
+
const SENT_MAX = 20;
|
|
26
|
+
|
|
27
|
+
const CATEGORIES = Object.freeze([
|
|
28
|
+
"android-crash",
|
|
29
|
+
"android-anr",
|
|
30
|
+
"android-native",
|
|
31
|
+
"web-error",
|
|
32
|
+
"web-rejection",
|
|
33
|
+
"react-render",
|
|
34
|
+
"node-uncaught",
|
|
35
|
+
"node-rejection",
|
|
36
|
+
]);
|
|
37
|
+
const CRUMB_KINDS = Object.freeze(["nav", "lifecycle", "action"]);
|
|
38
|
+
|
|
39
|
+
const TYPE_MAX = 120;
|
|
40
|
+
const MESSAGE_MAX = 300;
|
|
41
|
+
const FRAME_MAX = 200;
|
|
42
|
+
const FRAMES_MAX = 20;
|
|
43
|
+
const NAME_MAX = 60;
|
|
44
|
+
const SCREEN_MAX = 40;
|
|
45
|
+
const APP_CAPS = Object.freeze({ levix: 32, nativeApp: 40, node: 32, platform: 32 });
|
|
46
|
+
|
|
47
|
+
const ID_PATTERN = /^[0-9a-f]{16}$/;
|
|
48
|
+
const FILE_PATTERN = /^([0-9a-f]{16})\.json$/;
|
|
49
|
+
|
|
50
|
+
const DEFAULT_ENDPOINT = "https://levix.leviro.net/api/crash-reports";
|
|
51
|
+
const SEND_TIMEOUT_MS = 30000;
|
|
52
|
+
|
|
53
|
+
// POST /crash-reports: 10 per 10 minutes per process. The panel's own error
|
|
54
|
+
// hooks are the only caller; the limit is so a render loop cannot fill the disk.
|
|
55
|
+
const POST_MAX = 10;
|
|
56
|
+
const POST_WINDOW_MS = 10 * 60 * 1000;
|
|
57
|
+
let postAttempts = [];
|
|
58
|
+
|
|
59
|
+
function crashReportRetryAfter() {
|
|
60
|
+
const now = Date.now();
|
|
61
|
+
postAttempts = postAttempts.filter((at) => now - at < POST_WINDOW_MS);
|
|
62
|
+
if (postAttempts.length < POST_MAX) return 0;
|
|
63
|
+
return Math.max(1, Math.ceil((postAttempts[0] + POST_WINDOW_MS - now) / 1000));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function recordCrashReportAttempt() {
|
|
67
|
+
postAttempts.push(Date.now());
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Test hook only: forget the in-memory POST window. */
|
|
71
|
+
function resetCrashReportThrottle() {
|
|
72
|
+
postAttempts = [];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// --- files ----------------------------------------------------------------
|
|
76
|
+
|
|
77
|
+
function ensureDir() {
|
|
78
|
+
fs.mkdirSync(CRASH_DIR, { recursive: true });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function reportFile(id) {
|
|
82
|
+
return path.join(CRASH_DIR, `${id}.json`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function newId() {
|
|
86
|
+
return crypto.randomBytes(8).toString("hex");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** 16 hex from category + type + the first three frames. Grouping only. */
|
|
90
|
+
function fingerprintOf(category, type, stack) {
|
|
91
|
+
const frames = Array.isArray(stack) ? stack.slice(0, 3).join("\n") : "";
|
|
92
|
+
return crypto
|
|
93
|
+
.createHash("sha256")
|
|
94
|
+
.update(`${category}|${type}|${frames}`)
|
|
95
|
+
.digest("hex")
|
|
96
|
+
.slice(0, 16);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function clip(value, max) {
|
|
100
|
+
return sanitizeText(value, max);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function normalizeStack(stack) {
|
|
104
|
+
let frames = [];
|
|
105
|
+
if (Array.isArray(stack)) frames = stack;
|
|
106
|
+
else if (typeof stack === "string") frames = stack.split(/\r?\n/);
|
|
107
|
+
return frames
|
|
108
|
+
.filter((frame) => typeof frame === "string" && frame.trim() !== "")
|
|
109
|
+
.slice(0, FRAMES_MAX)
|
|
110
|
+
.map((frame) => sanitizeText(frame, FRAME_MAX));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function normalizeBreadcrumbs(list) {
|
|
114
|
+
if (!Array.isArray(list)) return [];
|
|
115
|
+
const out = [];
|
|
116
|
+
for (const item of list.slice(-20)) {
|
|
117
|
+
if (!item || typeof item !== "object") continue;
|
|
118
|
+
const name = sanitizeText(item.name, NAME_MAX);
|
|
119
|
+
if (!name) continue;
|
|
120
|
+
const kind = CRUMB_KINDS.includes(item.kind) ? item.kind : "action";
|
|
121
|
+
const at =
|
|
122
|
+
typeof item.at === "string" && item.at ? item.at.slice(0, 40) : new Date().toISOString();
|
|
123
|
+
out.push({ at, kind, name });
|
|
124
|
+
}
|
|
125
|
+
return out;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** A route id only: no URL, no scheme, no query, no hash. */
|
|
129
|
+
function normalizeScreen(value) {
|
|
130
|
+
if (typeof value !== "string") return null;
|
|
131
|
+
let text = value.trim().split("?")[0].split("#")[0];
|
|
132
|
+
if (text.includes("://")) {
|
|
133
|
+
const parts = text.split("/");
|
|
134
|
+
text = parts[parts.length - 1] || "";
|
|
135
|
+
}
|
|
136
|
+
text = text.replace(/^[#/]+/, "");
|
|
137
|
+
text = sanitizeText(text, SCREEN_MAX);
|
|
138
|
+
return text || null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function runtimeFacts() {
|
|
142
|
+
try {
|
|
143
|
+
const { describeRuntime } = require("../panel/feedback.cjs");
|
|
144
|
+
const runtime = describeRuntime();
|
|
145
|
+
return { version: runtime?.version || "unknown", platform: runtime?.platform || "unknown" };
|
|
146
|
+
} catch {
|
|
147
|
+
return { version: "unknown", platform: "unknown" };
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** The server's own facts, plus nativeApp only when a native capture knew it. */
|
|
152
|
+
function buildApp(extra) {
|
|
153
|
+
const runtime = runtimeFacts();
|
|
154
|
+
const app = {
|
|
155
|
+
levix: clip(runtime.version, APP_CAPS.levix) || "unknown",
|
|
156
|
+
node: clip(process.version, APP_CAPS.node) || "unknown",
|
|
157
|
+
platform: clip(runtime.platform, APP_CAPS.platform) || "unknown",
|
|
158
|
+
};
|
|
159
|
+
const nativeApp = clip(extra?.nativeApp, APP_CAPS.nativeApp);
|
|
160
|
+
if (nativeApp) app.nativeApp = nativeApp;
|
|
161
|
+
return app;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function sanitizeApp(app) {
|
|
165
|
+
const out = {};
|
|
166
|
+
if (!app || typeof app !== "object") return out;
|
|
167
|
+
for (const [key, cap] of Object.entries(APP_CAPS)) {
|
|
168
|
+
const text = clip(app[key], cap);
|
|
169
|
+
if (text) out[key] = text;
|
|
170
|
+
}
|
|
171
|
+
return out;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Keep the newest MAX_FILES, delete the oldest. */
|
|
175
|
+
function enforceCap() {
|
|
176
|
+
let names;
|
|
177
|
+
try {
|
|
178
|
+
names = fs.readdirSync(CRASH_DIR);
|
|
179
|
+
} catch {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const files = [];
|
|
183
|
+
for (const name of names) {
|
|
184
|
+
if (!FILE_PATTERN.test(name)) continue;
|
|
185
|
+
const full = path.join(CRASH_DIR, name);
|
|
186
|
+
let stat;
|
|
187
|
+
try {
|
|
188
|
+
stat = fs.statSync(full);
|
|
189
|
+
} catch {
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
files.push({ full, mtimeMs: stat.mtimeMs });
|
|
193
|
+
}
|
|
194
|
+
files.sort((a, b) => a.mtimeMs - b.mtimeMs);
|
|
195
|
+
while (files.length > MAX_FILES) {
|
|
196
|
+
const oldest = files.shift();
|
|
197
|
+
try {
|
|
198
|
+
fs.unlinkSync(oldest.full);
|
|
199
|
+
} catch {}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** Drop reports older than seven days, then enforce the count cap. */
|
|
204
|
+
function sweep() {
|
|
205
|
+
ensureDir();
|
|
206
|
+
const now = Date.now();
|
|
207
|
+
let names;
|
|
208
|
+
try {
|
|
209
|
+
names = fs.readdirSync(CRASH_DIR);
|
|
210
|
+
} catch {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
for (const name of names) {
|
|
214
|
+
if (!FILE_PATTERN.test(name)) continue;
|
|
215
|
+
const full = path.join(CRASH_DIR, name);
|
|
216
|
+
try {
|
|
217
|
+
if (now - fs.statSync(full).mtimeMs > MAX_AGE_MS) fs.unlinkSync(full);
|
|
218
|
+
} catch {}
|
|
219
|
+
}
|
|
220
|
+
enforceCap();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function writeReport(report) {
|
|
224
|
+
ensureDir();
|
|
225
|
+
const file = reportFile(report.id);
|
|
226
|
+
const tmp = `${file}.tmp`;
|
|
227
|
+
fs.writeFileSync(tmp, JSON.stringify(report));
|
|
228
|
+
fs.renameSync(tmp, file);
|
|
229
|
+
enforceCap();
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function readReport(id) {
|
|
233
|
+
if (!ID_PATTERN.test(String(id || ""))) return null;
|
|
234
|
+
try {
|
|
235
|
+
return JSON.parse(fs.readFileSync(reportFile(id), "utf8"));
|
|
236
|
+
} catch {
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function listReports() {
|
|
242
|
+
sweep();
|
|
243
|
+
let names;
|
|
244
|
+
try {
|
|
245
|
+
names = fs.readdirSync(CRASH_DIR);
|
|
246
|
+
} catch {
|
|
247
|
+
return [];
|
|
248
|
+
}
|
|
249
|
+
const reports = [];
|
|
250
|
+
for (const name of names) {
|
|
251
|
+
const match = FILE_PATTERN.exec(name);
|
|
252
|
+
if (!match) continue;
|
|
253
|
+
const report = readReport(match[1]);
|
|
254
|
+
if (report) reports.push(report);
|
|
255
|
+
}
|
|
256
|
+
reports.sort((a, b) => String(b.createdAt).localeCompare(String(a.createdAt)));
|
|
257
|
+
return reports;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function deleteReport(id) {
|
|
261
|
+
if (!ID_PATTERN.test(String(id || ""))) return false;
|
|
262
|
+
try {
|
|
263
|
+
fs.unlinkSync(reportFile(id));
|
|
264
|
+
return true;
|
|
265
|
+
} catch {
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function deleteAllReports() {
|
|
271
|
+
let names;
|
|
272
|
+
try {
|
|
273
|
+
names = fs.readdirSync(CRASH_DIR);
|
|
274
|
+
} catch {
|
|
275
|
+
return 0;
|
|
276
|
+
}
|
|
277
|
+
let removed = 0;
|
|
278
|
+
for (const name of names) {
|
|
279
|
+
if (!FILE_PATTERN.test(name)) continue;
|
|
280
|
+
try {
|
|
281
|
+
fs.unlinkSync(path.join(CRASH_DIR, name));
|
|
282
|
+
removed += 1;
|
|
283
|
+
} catch {}
|
|
284
|
+
}
|
|
285
|
+
return removed;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// --- sent record ----------------------------------------------------------
|
|
289
|
+
|
|
290
|
+
function readSent() {
|
|
291
|
+
try {
|
|
292
|
+
const data = JSON.parse(fs.readFileSync(SENT_FILE, "utf8"));
|
|
293
|
+
if (Array.isArray(data)) return data;
|
|
294
|
+
if (Array.isArray(data?.sent)) return data.sent;
|
|
295
|
+
return [];
|
|
296
|
+
} catch {
|
|
297
|
+
return [];
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function recordSent(entry) {
|
|
302
|
+
ensureDir();
|
|
303
|
+
const list = [entry, ...readSent()].slice(0, SENT_MAX);
|
|
304
|
+
const tmp = `${SENT_FILE}.tmp`;
|
|
305
|
+
try {
|
|
306
|
+
fs.writeFileSync(tmp, JSON.stringify({ sent: list }));
|
|
307
|
+
fs.renameSync(tmp, SENT_FILE);
|
|
308
|
+
} catch {}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// --- building and uploading ----------------------------------------------
|
|
312
|
+
|
|
313
|
+
function buildReport(input = {}) {
|
|
314
|
+
const category = CATEGORIES.includes(input.category) ? input.category : null;
|
|
315
|
+
if (!category) {
|
|
316
|
+
const error = new Error("Unknown crash category");
|
|
317
|
+
error.status = 400;
|
|
318
|
+
throw error;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const errorInput = input.error && typeof input.error === "object" ? input.error : {};
|
|
322
|
+
const type = sanitizeText(errorInput.type, TYPE_MAX) || "Error";
|
|
323
|
+
const stack = normalizeStack(errorInput.stack);
|
|
324
|
+
|
|
325
|
+
return {
|
|
326
|
+
schema: 1,
|
|
327
|
+
id: newId(),
|
|
328
|
+
fingerprint: fingerprintOf(category, type, stack),
|
|
329
|
+
createdAt: new Date().toISOString(),
|
|
330
|
+
category,
|
|
331
|
+
error: { type, message: sanitizeText(errorInput.message, MESSAGE_MAX), stack },
|
|
332
|
+
screen: normalizeScreen(input.screen),
|
|
333
|
+
app: buildApp(input.app),
|
|
334
|
+
device: sanitizeClientFields(input.device),
|
|
335
|
+
breadcrumbs: normalizeBreadcrumbs(input.breadcrumbs),
|
|
336
|
+
status: "pending",
|
|
337
|
+
lastError: null,
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** Persist one new report. Returns it. Throws a 400 for an unknown category. */
|
|
342
|
+
function createReport(input = {}) {
|
|
343
|
+
const report = buildReport(input);
|
|
344
|
+
writeReport(report);
|
|
345
|
+
return report;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* The exact object that would be sent: no local-only keys, re-sanitized. This
|
|
350
|
+
* is also what GET /crash-reports previews (with status/lastError added back).
|
|
351
|
+
*/
|
|
352
|
+
function uploadObject(report) {
|
|
353
|
+
const errorInput = report?.error && typeof report.error === "object" ? report.error : {};
|
|
354
|
+
const category = CATEGORIES.includes(report?.category) ? report.category : "web-error";
|
|
355
|
+
return {
|
|
356
|
+
schema: 1,
|
|
357
|
+
id: typeof report?.id === "string" ? report.id : "",
|
|
358
|
+
fingerprint: typeof report?.fingerprint === "string" ? report.fingerprint : "",
|
|
359
|
+
createdAt: typeof report?.createdAt === "string" ? report.createdAt : "",
|
|
360
|
+
category,
|
|
361
|
+
error: {
|
|
362
|
+
type: sanitizeText(errorInput.type, TYPE_MAX) || "Error",
|
|
363
|
+
message: sanitizeText(errorInput.message, MESSAGE_MAX),
|
|
364
|
+
stack: normalizeStack(errorInput.stack),
|
|
365
|
+
},
|
|
366
|
+
screen: normalizeScreen(report?.screen),
|
|
367
|
+
app: sanitizeApp(report?.app),
|
|
368
|
+
device: sanitizeClientFields(report?.device),
|
|
369
|
+
breadcrumbs: normalizeBreadcrumbs(report?.breadcrumbs),
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function endpoint() {
|
|
374
|
+
return process.env.LEVIX_CRASH_REPORT_URL || DEFAULT_ENDPOINT;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Forward one report. Never retried here; the caller decides. Resolves with
|
|
379
|
+
* `{ ok: true }` or `{ ok: false, status, error }`.
|
|
380
|
+
*/
|
|
381
|
+
async function forwardReport(report) {
|
|
382
|
+
const payload = uploadObject(report);
|
|
383
|
+
const runtime = runtimeFacts();
|
|
384
|
+
let response;
|
|
385
|
+
let data = null;
|
|
386
|
+
try {
|
|
387
|
+
response = await fetch(endpoint(), {
|
|
388
|
+
method: "POST",
|
|
389
|
+
headers: {
|
|
390
|
+
"Content-Type": "application/json",
|
|
391
|
+
"User-Agent": `Levix/${runtime.version} (${runtime.platform})`,
|
|
392
|
+
},
|
|
393
|
+
body: JSON.stringify(payload),
|
|
394
|
+
signal: AbortSignal.timeout(SEND_TIMEOUT_MS),
|
|
395
|
+
});
|
|
396
|
+
data = await response.json().catch(() => null);
|
|
397
|
+
} catch {
|
|
398
|
+
return {
|
|
399
|
+
ok: false,
|
|
400
|
+
status: 502,
|
|
401
|
+
error: "Could not reach the Levix server. Check the bot's internet connection and try again.",
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
if (response.ok && data?.ok) return { ok: true, id: data.id ?? payload.id };
|
|
406
|
+
|
|
407
|
+
const remote = typeof data?.error === "string" ? sanitizeText(data.error, 200) : "";
|
|
408
|
+
const status = response.status === 429 ? 429 : 502;
|
|
409
|
+
return {
|
|
410
|
+
ok: false,
|
|
411
|
+
status,
|
|
412
|
+
error: remote || `The crash-report service refused the report (HTTP ${response.status}).`,
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/** Send one stored report. Success deletes it and records the send. */
|
|
417
|
+
async function sendReport(id) {
|
|
418
|
+
const report = readReport(id);
|
|
419
|
+
if (!report) return { ok: false, status: 404, error: "Crash report not found" };
|
|
420
|
+
|
|
421
|
+
const result = await forwardReport(report);
|
|
422
|
+
if (result.ok) {
|
|
423
|
+
deleteReport(report.id);
|
|
424
|
+
recordSent({
|
|
425
|
+
id: report.id,
|
|
426
|
+
sentAt: new Date().toISOString(),
|
|
427
|
+
category: uploadObject(report).category,
|
|
428
|
+
type: uploadObject(report).error.type,
|
|
429
|
+
});
|
|
430
|
+
return { ok: true };
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
report.status = "failed";
|
|
434
|
+
report.lastError = result.error;
|
|
435
|
+
try {
|
|
436
|
+
writeReport(report);
|
|
437
|
+
} catch {}
|
|
438
|
+
return { ok: false, status: result.status, error: result.error };
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/** What GET /crash-reports returns: previews plus the send history. */
|
|
442
|
+
function list() {
|
|
443
|
+
const reports = listReports().map((report) => ({
|
|
444
|
+
...uploadObject(report),
|
|
445
|
+
status: report.status || "pending",
|
|
446
|
+
lastError: report.lastError ?? null,
|
|
447
|
+
}));
|
|
448
|
+
return { reports, sent: readSent() };
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* Write a report from a Node fatal handler. Synchronous and wrapped so a
|
|
453
|
+
* failure to write can never throw out of a process that is already dying.
|
|
454
|
+
*/
|
|
455
|
+
function captureNodeFatal(kind, error) {
|
|
456
|
+
try {
|
|
457
|
+
const category = kind === "node-rejection" ? "node-rejection" : "node-uncaught";
|
|
458
|
+
const type = error?.name || (typeof error === "string" ? "Error" : "UnknownError");
|
|
459
|
+
const message = error?.message || (typeof error === "string" ? error : "");
|
|
460
|
+
const stack = typeof error?.stack === "string" ? error.stack : "";
|
|
461
|
+
return createReport({ category, error: { type, message, stack } });
|
|
462
|
+
} catch {
|
|
463
|
+
return null;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
module.exports = {
|
|
468
|
+
CRASH_DIR,
|
|
469
|
+
CATEGORIES,
|
|
470
|
+
MAX_FILES,
|
|
471
|
+
MAX_AGE_MS,
|
|
472
|
+
SENT_MAX,
|
|
473
|
+
POST_MAX,
|
|
474
|
+
POST_WINDOW_MS,
|
|
475
|
+
createReport,
|
|
476
|
+
uploadObject,
|
|
477
|
+
list,
|
|
478
|
+
listReports,
|
|
479
|
+
readReport,
|
|
480
|
+
deleteReport,
|
|
481
|
+
deleteAllReports,
|
|
482
|
+
sweep,
|
|
483
|
+
forwardReport,
|
|
484
|
+
sendReport,
|
|
485
|
+
recordSent,
|
|
486
|
+
readSent,
|
|
487
|
+
captureNodeFatal,
|
|
488
|
+
crashReportRetryAfter,
|
|
489
|
+
recordCrashReportAttempt,
|
|
490
|
+
resetCrashReportThrottle,
|
|
491
|
+
};
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// The device diagnostics object (contract section 1).
|
|
2
|
+
//
|
|
3
|
+
// One function decides what an operator's panel page is allowed to tell the
|
|
4
|
+
// server about the machine it is running on. It is a strict allowlist: only the
|
|
5
|
+
// keys in the contract survive, anything else is dropped, the two server-owned
|
|
6
|
+
// triples of keys (levix/node/platform) are written by this process and never
|
|
7
|
+
// read from the client, and the whole object is kept under 2 KB.
|
|
8
|
+
//
|
|
9
|
+
// This same function builds the preview the operator sees, so the preview and
|
|
10
|
+
// the forwarded object cannot drift apart.
|
|
11
|
+
|
|
12
|
+
const { stripControl } = require("./sanitize.cjs");
|
|
13
|
+
|
|
14
|
+
/** String client fields and their caps, straight from the contract. */
|
|
15
|
+
const STRING_CAPS = Object.freeze({
|
|
16
|
+
nativeApp: 40,
|
|
17
|
+
android: 40,
|
|
18
|
+
device: 80,
|
|
19
|
+
abi: 24,
|
|
20
|
+
webview: 40,
|
|
21
|
+
screen: 24,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
/** Enumerated client fields. Anything outside the list is dropped. */
|
|
25
|
+
const ENUMS = Object.freeze({
|
|
26
|
+
language: ["ar", "en"],
|
|
27
|
+
theme: ["light", "dark"],
|
|
28
|
+
connectivity: ["wifi", "cellular", "ethernet", "vpn", "none", "unknown"],
|
|
29
|
+
memory: ["ok", "low", "unknown"],
|
|
30
|
+
storage: ["ok", "low", "unknown"],
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const DIAGNOSTICS_MAX_BYTES = 2048;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Trim, take out control characters, cap. Section 1 only promises these; the
|
|
37
|
+
* section 4 substitutions would mangle a legitimate value (`412x915@2.63`
|
|
38
|
+
* reads as an e-mail, a long hex `device` as a secret), so they are deliberately
|
|
39
|
+
* NOT applied here. Empty means "do not send".
|
|
40
|
+
*/
|
|
41
|
+
function clip(value, max) {
|
|
42
|
+
if (typeof value !== "string") return "";
|
|
43
|
+
return stripControl(value).trim().slice(0, max);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Only the client-owned keys of section 1, validated. Used by `buildDiagnostics`
|
|
48
|
+
* and by the crash-report store for its `device` block (which is section 1's
|
|
49
|
+
* Android and page keys without levix/node/platform).
|
|
50
|
+
*
|
|
51
|
+
* @param {unknown} client
|
|
52
|
+
* @returns {Record<string, string>}
|
|
53
|
+
*/
|
|
54
|
+
function sanitizeClientFields(client) {
|
|
55
|
+
const out = {};
|
|
56
|
+
if (!client || typeof client !== "object" || Array.isArray(client)) return out;
|
|
57
|
+
|
|
58
|
+
for (const [key, cap] of Object.entries(STRING_CAPS)) {
|
|
59
|
+
const text = clip(client[key], cap);
|
|
60
|
+
if (text) out[key] = text;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
for (const [key, allowed] of Object.entries(ENUMS)) {
|
|
64
|
+
const raw = client[key];
|
|
65
|
+
if (typeof raw !== "string") continue;
|
|
66
|
+
const value = raw.trim().toLowerCase();
|
|
67
|
+
if (allowed.includes(value)) out[key] = value;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** describeRuntime() may need the database; the server keys are never fatal. */
|
|
74
|
+
function runtimeFacts() {
|
|
75
|
+
try {
|
|
76
|
+
const { describeRuntime } = require("../panel/feedback.cjs");
|
|
77
|
+
const runtime = describeRuntime();
|
|
78
|
+
return { version: runtime?.version || "unknown", platform: runtime?.platform || "unknown" };
|
|
79
|
+
} catch {
|
|
80
|
+
return { version: "unknown", platform: "unknown" };
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The 2 KB ceiling is a safety net; the caps above already keep it small. */
|
|
85
|
+
function enforceSize(object) {
|
|
86
|
+
const dropOrder = ["device", "abi", "webview", "nativeApp", "android", "screen"];
|
|
87
|
+
for (const key of dropOrder) {
|
|
88
|
+
if (Buffer.byteLength(JSON.stringify(object), "utf8") <= DIAGNOSTICS_MAX_BYTES) break;
|
|
89
|
+
delete object[key];
|
|
90
|
+
}
|
|
91
|
+
return object;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Build the section 1 object from a client object plus this server's own facts.
|
|
96
|
+
* A client value for levix/node/platform is ignored by construction.
|
|
97
|
+
*
|
|
98
|
+
* @param {unknown} client
|
|
99
|
+
* @returns {Record<string, unknown>}
|
|
100
|
+
*/
|
|
101
|
+
function buildDiagnostics(client) {
|
|
102
|
+
const runtime = runtimeFacts();
|
|
103
|
+
const object = {
|
|
104
|
+
schema: 1,
|
|
105
|
+
levix: clip(runtime.version, 32) || "unknown",
|
|
106
|
+
node: clip(process.version, 32) || "unknown",
|
|
107
|
+
platform: clip(runtime.platform, 32) || "unknown",
|
|
108
|
+
...sanitizeClientFields(client),
|
|
109
|
+
};
|
|
110
|
+
return enforceSize(object);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
module.exports = {
|
|
114
|
+
DIAGNOSTICS_MAX_BYTES,
|
|
115
|
+
STRING_CAPS,
|
|
116
|
+
ENUMS,
|
|
117
|
+
buildDiagnostics,
|
|
118
|
+
sanitizeClientFields,
|
|
119
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// The one diagnostics text sanitizer (contract section 4).
|
|
2
|
+
//
|
|
3
|
+
// Anything a crash report or a feedback diagnostics object can carry — an error
|
|
4
|
+
// message, a stack frame, a breadcrumb name — is put through here before it is
|
|
5
|
+
// stored locally and again before it leaves the device. The point is narrow and
|
|
6
|
+
// absolute: a report must never carry a WhatsApp identity, an e-mail address, a
|
|
7
|
+
// phone number, a URL's query string, an API key or a token, or an absolute
|
|
8
|
+
// path that names the machine it came from.
|
|
9
|
+
//
|
|
10
|
+
// It is deliberately the same function on every side (Levix Node here, Android
|
|
11
|
+
// at capture, levix-ui on receipt), so a value cannot survive one hop and be
|
|
12
|
+
// scrubbed on the next.
|
|
13
|
+
|
|
14
|
+
const os = require("node:os");
|
|
15
|
+
const { DATA_DIR } = require("../config/paths.cjs");
|
|
16
|
+
|
|
17
|
+
// A WhatsApp JID in any of the v7 shapes: phone, group, LID, broadcast,
|
|
18
|
+
// newsletter. `[^\s@]+` rather than `\S+` so the match cannot swallow an
|
|
19
|
+
// earlier `@` and leave its tail behind.
|
|
20
|
+
const JID_PATTERN = /[^\s@]+@(?:s\.whatsapp\.net|g\.us|lid|broadcast|newsletter)\b/gi;
|
|
21
|
+
|
|
22
|
+
const EMAIL_PATTERN = /[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)+/g;
|
|
23
|
+
|
|
24
|
+
// Scheme + authority; the tail (path, query, fragment) is dropped.
|
|
25
|
+
const URL_PATTERN = /(https?):\/\/([^\s/?#]+)([^\s]*)/gi;
|
|
26
|
+
|
|
27
|
+
// 24+ chars from the URL-safe alphabet. A value that is only digits is left for
|
|
28
|
+
// the number rule, so a long phone number still reads as <number>.
|
|
29
|
+
const SECRET_PATTERN = /[A-Za-z0-9_+\-/=]{24,}/g;
|
|
30
|
+
|
|
31
|
+
const NUMBER_PATTERN = /\+?\d{7,}/g;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Control characters mangle text on the way out and cannot be rendered anyway.
|
|
35
|
+
* Tabs and line breaks are kept: a multi-line message or stack is still content
|
|
36
|
+
* the operator may need to read.
|
|
37
|
+
*/
|
|
38
|
+
function stripControl(value) {
|
|
39
|
+
let out = "";
|
|
40
|
+
for (const char of String(value ?? "")) {
|
|
41
|
+
const code = char.codePointAt(0);
|
|
42
|
+
const control = code < 0x20 || code === 0x7f || (code >= 0x80 && code <= 0x9f);
|
|
43
|
+
if (!control || char === "\n" || char === "\r" || char === "\t") out += char;
|
|
44
|
+
}
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Replace one occurrence of a literal string (no regex escaping to get wrong). */
|
|
49
|
+
function replaceLiteral(text, needle, replacement) {
|
|
50
|
+
if (!needle || !text.includes(needle)) return text;
|
|
51
|
+
return text.split(needle).join(replacement);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function collapseUrl(_match, scheme, authority, rest) {
|
|
55
|
+
const base = `${scheme}://${authority}`;
|
|
56
|
+
return rest ? `${base}/<path>` : base;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Sanitize one value. `max` (when > 0) is the hard length cap applied after the
|
|
61
|
+
* substitutions, matching the per-field caps in the contract.
|
|
62
|
+
*
|
|
63
|
+
* @param {unknown} value
|
|
64
|
+
* @param {number} [max]
|
|
65
|
+
* @returns {string}
|
|
66
|
+
*/
|
|
67
|
+
function sanitizeText(value, max = 0) {
|
|
68
|
+
let text = stripControl(value);
|
|
69
|
+
|
|
70
|
+
// Order matters. URLs first so a path is gone before the raw path rules can
|
|
71
|
+
// rewrite a URL's own path; identities before the generic token rule so a JID
|
|
72
|
+
// reads as <jid> rather than as several fragments.
|
|
73
|
+
text = text.replace(URL_PATTERN, collapseUrl);
|
|
74
|
+
text = text.replace(JID_PATTERN, "<jid>");
|
|
75
|
+
text = text.replace(EMAIL_PATTERN, "<email>");
|
|
76
|
+
|
|
77
|
+
// Absolute paths that name the install, then the user's home.
|
|
78
|
+
text = replaceLiteral(text, DATA_DIR, "<data>");
|
|
79
|
+
text = replaceLiteral(text, os.homedir(), "~");
|
|
80
|
+
|
|
81
|
+
// Secrets before numbers: a long key that happens to contain digits is fully
|
|
82
|
+
// redacted, while a pure-digit run falls through to <number>.
|
|
83
|
+
text = text.replace(SECRET_PATTERN, (match) => (/^\+?\d+$/.test(match) ? match : "<secret>"));
|
|
84
|
+
text = text.replace(NUMBER_PATTERN, "<number>");
|
|
85
|
+
|
|
86
|
+
text = text.trim();
|
|
87
|
+
return max > 0 ? text.slice(0, max) : text;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
module.exports = { sanitizeText, stripControl };
|