Best-effort same-origin check using Origin / Referer headers. Notes: - We compare only the host:port (netloc) to avoid false failures behind TLS terminators (e.g., Cloudflared) where the app server may see HTTP. - If headers are missing, we allow the request. - If headers are
()
| 1466 | app.secret_key = load_or_create_session_key() |
| 1467 | app.config.update( |
| 1468 | SESSION_COOKIE_HTTPONLY=True, |
| 1469 | SESSION_COOKIE_SAMESITE="Lax", |
| 1470 | SESSION_REFRESH_EACH_REQUEST=False, |
| 1471 | PERMANENT_SESSION_LIFETIME=timedelta(hours=12), |
| 1472 | # Global hard cap (needed for Flask/Werkzeug to accept large multipart bodies). |
| 1473 | # We enforce tighter per-feature limits below. |
| 1474 | MAX_CONTENT_LENGTH=(333 * 1024 * 1024) + (8 * 1024 * 1024), # Per-request cap; 10GB vault files use 16MB chunks. |
| 1475 | ) |
| 1476 | |
| 1477 | # --------------------------- |
| 1478 | # Upload size limits (requested) |
| 1479 | # --------------------------- |
| 1480 | # Chat attachments (DM + Groups): 33MB max per attached file/voice. |
| 1481 | CHAT_MAX_BYTES = 33 * 1024 * 1024 |
| 1482 | |
| 1483 | # Calls (audio/video) are disabled (Discuss is text/voice/file only) |
| 1484 | ENABLE_CALLS = True |
| 1485 | # Profile pictures: keep small to reduce risk/CPU (10MB). |
| 1486 | PFP_MAX_BYTES = 10 * 1024 * 1024 |
| 1487 | # Vault Files uploads: 10GB max (applies to both classic and chunked uploads). |
| 1488 | FILES_MAX_BYTES = 10 * 1024 * 1024 * 1024 |
| 1489 | |
| 1490 | # --------------------------- |
| 1491 | # Security helpers (CSRF + per-request nonce + security headers) |
| 1492 | # --------------------------- |
| 1493 | # Dependency-free CSRF protection: |
| 1494 | # - A random token stored in the signed session cookie |
| 1495 | # - Automatically injected into POST forms (client-side) |
| 1496 | # - Automatically attached to same-origin POST/PUT/PATCH/DELETE fetch() calls |
| 1497 | # |
| 1498 | # This keeps the app Termux-friendly (no extra dependencies) while stopping |
| 1499 | # cross-site form posts and most CSRF attacks. |
| 1500 | |
| 1501 | # --------------------------- |
| 1502 | # Trusted device auto-login (cookie-based) |
| 1503 | # --------------------------- |
| 1504 | |
| 1505 | DEVICE_FP_COOKIE = "bs_device_fp" |
| 1506 | DEVICE_TOKEN_COOKIE = "bs_device_token" |
| 1507 | |
| 1508 | def _sha256_hex(s: str) -> str: |
| 1509 | try: |
| 1510 | import hashlib |
| 1511 | return hashlib.sha256(s.encode("utf-8")).hexdigest() |
| 1512 | except Exception: |
| 1513 | return "" |
| 1514 | |
| 1515 | def _ensure_device_fp_cookie(resp=None) -> Optional[str]: |
no test coverage detected