Security and access
This page explains how Scholar Lens signs people in, decides who may see what, and contains the risky parts: fetching from the web, running code and handling sign-ins to other sites. It is for developers and for whoever runs the server.
Signing in
- Passwords are hashed with argon2id. The minimum length is 6 characters. Five failed sign-ins lock the account for 15 minutes.
- Two cookies carry the session. Both are
HttpOnlyandSameSite=Strict, andSecurewhenCOOKIE_SECURE=true.- The access cookie holds a JWT signed with
JWT_SECRET, valid for 15 minutes, sent to/api. - The refresh cookie holds an opaque token, valid for 30 days, sent only to
/api/auth. The server stores its hash inauth_sessions. Each refresh replaces it with a new one (rotation).
- The access cookie holds a JWT signed with
- Sessions are listed on the Account page with their browser and address. Signing one out revokes its refresh token.
POST /api/auth/logout-allrevokes them all. - A temporary password set by an admin blocks every endpoint except "who am I", change password and sign out, until the user chooses a new one.
- Self sign-up, when on, creates a pending account that can't sign in until an admin approves it.
SameSite=Strict cookies are not sent on requests started by other sites, which protects the API from cross-site request forgery. CORS allows only the origins in CORS_ORIGINS.
Personal access tokens
The browser extension uses a bearer token instead of cookies:
- The secret is
slp_followed by 32 random bytes. It is shown once. The server keeps only its SHA-256 hash and a short prefix for display. - A token can call only
GET /api/me,GET /api/spacesand/api/capture*. Anything else returns 403token_scope. - It follows its account: a disabled account or a temporary password stops it too.
- Revoking it returns 401
token_revokedfrom then on. A user can have at most 20 live tokens.
Roles
| Role | Can |
|---|---|
admin | Everything a user can, plus: manage accounts, change server settings, publish to and manage the shared Library, view a user's content read-only. No storage or answer limits. |
user | Own Library, Spaces, chats, notes, study sets. Ask for an item to be published. |
Who sees what
The rules live in app/access.py.
- Library items belong to their owner. Only the owner changes or deletes them.
- Spaces have one owner. Sharing adds
editors (sources, instructions, defaults) andviewers (read and chat). - Sharing a Space shares read access to its Library items, nothing else. Chats stay private to whoever started them. So do notes, memory, study sets and sign-ins.
- The shared Library is readable by everyone and usable in any Space. Only admins change, re-index, unpublish or delete its items, the uploader included.
- No access looks like no item. A Space, chat or document someone can't see returns 404, never 403, so its existence doesn't leak.
- Admin oversight is read-only and logged (
admin.viewed, with an optional reason). The user sees every access on their Account page.
Retrieval enforces the same rules: the search scope is built from what the asker can read, so a passage from someone else's private item can never reach an answer.
Limits
app/limits.py enforces, for users but not admins:
- a storage quota (
STORAGE_QUOTA_MB, 2 GB) over their Library files and chat attachments, - a daily answer limit (
DAILY_RUN_LIMIT, 200), counted from midnight UTC.
Upload size (MAX_UPLOAD_MB) applies to everyone. An upload's content must match its extension.
Fetching from the web
Imports, web pages and reference lookups fetch URLs that come from users or search results. Every fetch, and every redirect hop, goes through an SSRF guard: http and https only, and the host must resolve to public addresses. Private, loopback and link-local addresses are refused, so a link can't make the server read its own network. FETCH_ALLOW_PRIVATE=true turns this off for development only.
Downloads are capped in size (FETCH_MAX_MB). Fetching happens in the worker, not in the request.
Running code
Code runs only in the sandbox: gVisor, no network, read-only root, no capabilities, resource limits and a process watchdog. See The code sandbox. The API reaches it with a shared token over an internal network.
Sign-ins to other sites
The sign-in browser keeps only one site's cookies, sealed (encrypted and authenticated) with a server key, per student and Space, for 14 days. Cookies never reach the model, prompts, events or logs. See Web research and sign-in.
Secrets
JWT_SECRETmust be at least 32 characters and not a default. The API refuses to start otherwise.- Secrets saved in the app (API keys, the sandbox token) are encrypted with Fernet, using
SETTINGS_KEYor a key derived fromJWT_SECRET. They are never sent back to the browser. .envfiles are not in git or in backups.
Exposure
Every port in the stack binds to 127.0.0.1 by default. The sign-in browser's DevTools and VNC ports have no authentication and must never be exposed. For access from other machines, put the UI and API behind HTTPS and set COOKIE_SECURE=true.
Audit log
audit_log records security-relevant actions with the actor and target: sign-ins, account changes, token creation and revocation, admin access to content, publishing, captures.