levix-bot 4.2.1 → 4.3.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 CHANGED
@@ -26,6 +26,7 @@ const { getQrCode } = require("./src/utils/storage.cjs");
26
26
  const { clientAddress, isDirectLocalRequest } = require("./src/utils/requestOrigin.cjs");
27
27
  const { blockedFor, recordFailure, clearAttempts } = require("./src/panel/login-throttle.cjs");
28
28
  const { isPanelSessionValid, stampPanelSession } = require("./src/panel/session-auth.cjs");
29
+ const { requestResetCode, completeReset } = require("./src/panel/password-reset.cjs");
29
30
 
30
31
  const SESSION_COOKIE_NAME = "wa.sid";
31
32
  const SESSION_MAX_AGE_MS = 12 * 60 * 60 * 1000;
@@ -48,7 +49,9 @@ function isAllowedOrigin(origin, host) {
48
49
  // Sec-Fetch-Site entirely, so password-gated login gets a narrow fallback for
49
50
  // Origin:null. First-run setup is deliberately excluded: a loopback request
50
51
  // otherwise skips the setup code and could claim an unconfigured panel.
51
- const OPAQUE_ORIGIN_AUTH_PATHS = new Set(["/login"]);
52
+ // The reset endpoints are auth doors of the same shape — code-gated and
53
+ // throttled — so they ride along with login.
54
+ const OPAQUE_ORIGIN_AUTH_PATHS = new Set(["/login", "/reset/request", "/reset/confirm"]);
52
55
  function isAllowedMutationRequest(req) {
53
56
  const origin = req.get("origin");
54
57
  const host = req.get("host");
@@ -288,13 +291,13 @@ app.get("/", noStore, (req, res) => {
288
291
  }
289
292
  return res.render("dashboard");
290
293
  }
291
- return res.render("login", { error: null });
294
+ return res.render("login", { error: null, minLength: secrets.MIN_PASSWORD_LENGTH });
292
295
  });
293
296
 
294
297
  app.get("/login", noStore, (req, res) => {
295
298
  if (!secrets.hasDashboardPassword()) return res.redirect("/setup");
296
299
  if (isPanelSessionValid(req.session)) return res.redirect("/");
297
- return res.render("login", { error: null });
300
+ return res.render("login", { error: null, minLength: secrets.MIN_PASSWORD_LENGTH });
298
301
  });
299
302
 
300
303
  app.post("/login", (req, res, next) => {
@@ -306,13 +309,17 @@ app.post("/login", (req, res, next) => {
306
309
  res.set("Retry-After", String(retryAfter));
307
310
  return res.status(429).render("login", {
308
311
  error: "Too many attempts. Try again later.",
312
+ minLength: secrets.MIN_PASSWORD_LENGTH,
309
313
  });
310
314
  }
311
315
 
312
316
  if (!secrets.verifyDashboardPassword(req.body?.password)) {
313
317
  recordFailure(peer);
314
318
  logger.warn({ ip: peer }, "[dashboard] Failed login attempt");
315
- return res.status(401).render("login", { error: "Incorrect Password" });
319
+ return res.status(401).render("login", {
320
+ error: "Incorrect Password",
321
+ minLength: secrets.MIN_PASSWORD_LENGTH,
322
+ });
316
323
  }
317
324
 
318
325
  clearAttempts(peer);
@@ -328,6 +335,45 @@ app.post("/login", (req, res, next) => {
328
335
  });
329
336
  });
330
337
 
338
+ // --- Password reset over WhatsApp -----------------------------------------
339
+ //
340
+ // For the operator who forgot the panel password. The code goes to the linked
341
+ // account's own WhatsApp chat; see src/panel/password-reset.cjs for why this
342
+ // is the recovery path that fits Levix. The offline fallback is still
343
+ // `levix reset-password` on the server.
344
+
345
+ app.post("/reset/request", async (req, res) => {
346
+ if (!secrets.hasDashboardPassword()) {
347
+ return res.status(400).json({ ok: false, reason: "no_password" });
348
+ }
349
+ const result = await requestResetCode(clientAddress(req));
350
+ const status = { throttled: 429, no_session: 409, send_failed: 502 }[result.reason] ?? 200;
351
+ if (result.ok) res.set("Cache-Control", "no-store");
352
+ return res.status(status).json(result);
353
+ });
354
+
355
+ app.post("/reset/confirm", (req, res) => {
356
+ if (!secrets.hasDashboardPassword()) {
357
+ return res.status(400).json({ ok: false, reason: "no_password" });
358
+ }
359
+ const { code, password, confirm } = req.body || {};
360
+ const result = completeReset(code, password, confirm);
361
+ if (!result.ok) {
362
+ // A wrong code counts toward the login lockout, so spraying guesses at
363
+ // this door locks the same peer out of /login too.
364
+ if (result.reason === "wrong_code" || result.reason === "too_many_attempts") {
365
+ recordFailure(clientAddress(req));
366
+ }
367
+ return res.status(result.reason === "invalid_password" ? 400 : 401).json(result);
368
+ }
369
+
370
+ // The epoch moved: push every live panel session out before the browser
371
+ // that reset the password signs back in.
372
+ disconnectInvalidPanelSockets();
373
+ res.set("Cache-Control", "no-store");
374
+ return res.json({ ok: true });
375
+ });
376
+
331
377
  // POST, not GET: a link that changes state gets fired by an <img> or by the
332
378
  // browser's prefetch.
333
379
  app.post("/logout", (req, res, next) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "levix-bot",
3
- "version": "4.2.1",
3
+ "version": "4.3.0",
4
4
  "description": "A self-hosted personal WhatsApp bot with an AI agent, group moderation and a web control panel.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {