If the user is not logged in, attempt auto-login using a trusted-device token cookie. Skips auto-login when 2FA is enabled for that account.
()
| 1574 | conn = db_connect() |
| 1575 | rows = conn.execute("SELECT username FROM users WHERE is_admin=1 ORDER BY username ASC").fetchall() |
| 1576 | conn.close() |
| 1577 | return [r["username"] for r in rows] |
| 1578 | except Exception: |
| 1579 | return [] |
| 1580 | |
| 1581 | def csrf_token() -> str: |
| 1582 | """csrf_token. |
| 1583 | |
| 1584 | Internal helper function. |
| 1585 | |
| 1586 | This docstring was added automatically to improve maintainability. |
| 1587 | |
| 1588 | Returns: |
| 1589 | Varies. |
| 1590 | """ |
| 1591 | tok = session.get("_csrf") |
| 1592 | if not tok: |
| 1593 | tok = secrets.token_urlsafe(32) |
| 1594 | session["_csrf"] = tok |
| 1595 | return tok |
| 1596 | |
| 1597 | def _same_origin_ok() -> bool: |
| 1598 | """Best-effort same-origin check using Origin / Referer headers. |
| 1599 | |
| 1600 | Notes: |
| 1601 | - We compare only the host:port (netloc) to avoid false failures behind TLS |
| 1602 | terminators (e.g., Cloudflared) where the app server may see HTTP. |
| 1603 | - If headers are missing, we allow the request. |
| 1604 | - If headers are present but malformed/unparseable, we fail closed. |
| 1605 | """ |
| 1606 | from urllib.parse import urlparse |
| 1607 | |
| 1608 | expected = (request.host or "").lower().strip() |
| 1609 | if not expected: |
| 1610 | return False |
| 1611 | |
| 1612 | origin = (request.headers.get("Origin") or "").strip() |
| 1613 | ref = (request.headers.get("Referer") or "").strip() |
| 1614 | |
| 1615 | # If neither header is present, don't block legitimate requests. |
| 1616 | if not origin and not ref: |
nothing calls this directly
no test coverage detected