Skip to content

Security hardening and auth/form fixes - #1077

Merged
mdipierro merged 18 commits into
masterfrom
security-fixes
Nov 7, 2026
Merged

mdipierro merged 18 commits into
masterfrom
security-fixes

Conversation

@mdipierro

@mdipierro mdipierro commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Security hardening for auth, sessions, Grid, Form, downloads, Publisher and the dashboard, plus three bug fixes reported by users (SSO duplicate accounts, list:reference multi-select, hidden fields in form.vars). Each commit message explains its issue and fix in detail.

Upgrade notes (behavior changes)

  • Publisher with policy=None now denies everything and requires login (auth.user). Apps that relied on open access must pass an explicit policy, e.g. ALLOW_ALL_POLICY.
  • Session token is no longer read from the POST body (_session_token). Clients without cookies must send the X-Session-Token header.
  • Emailed auth links (verify email, reset password): set base_url on Auth or the PY4WEB_BASE_URL env var. Without it the old Origin/Host behavior is kept and a warning is logged. _scaffold exposes BASE_URL in settings.py.
  • JSON login API (auth/api/login) enforces two factor auth: it returns 401 with two_factor_url instead of a session when 2FA is needed.
  • Login throttling: 10 failures per account / 100 per IP in 10 minutes block the password check (login_max_attempts, login_max_attempts_per_ip, login_attempts_window; 0 disables). The dashboard allows 5 failures per IP per 5 minutes.
  • New auth_attempt table defined by Auth.define_tables (2FA tries and login throttling).
  • Password hashing: new hashes use pbkdf2 with 210000 iterations of sha512. Existing hashes keep working and are upgraded at the next login.
  • Downloads: only safe types (images, audio, video, txt) are served inline; everything else is an attachment. Use inline_extensions= to change the list.
  • Grid: ?orderby= only accepts the grid's own columns; delete requires the form key; the referrer must be a local or same-host URL.
  • Token auth plugins no longer write the user into the session cookie.
  • Server-side sessions: old session ids stop working when the id rotates (login, logout).

Fixes

Auth and sessions

  • Emailed link host pinning, sanitized X-Script-Name (host header poisoning of reset links).
  • Password reset no longer bypasses email verification or approval.
  • 2FA enforced on the JSON API, SSO, PAM and LDAP logins; 2FA tries counted server side; the code expires (default 300s).
  • OAuth2 state is stored and checked (login CSRF); SSO next validated (open redirect); blocked/pending checks on every login path; WebAuthn stores only the user id and requires user verification.
  • CSPRNG (secrets) instead of uuid1() for form keys, URLSigner keys and session ids.
  • Single-use reset tokens, constant-time URLSigner comparison, stricter prevent_open_redirect.

Grid, Form, downloads

  • Details, edit and delete only load records matching the grid query (IDOR).
  • Delete CSRF, referrer XSS and open redirect, and sorting on hidden columns are fixed.
  • Uploaded HTML/SVG is no longer served inline (stored XSS); nosniff and a sandbox CSP on every download.
  • Form honours deletable=False.

Dashboard

  • Demo and readonly modes are truly read-only; tickets no longer exposed in demo mode; safe_join for translations; validated script name.

Bug fixes from user reports

  • SSO duplicate accounts: the first Google login of a user with a password account created a second auth_user (or failed on the unique email). It now links to the existing account when the provider marks the email verified (Google email_verified, Discord verified). An unverified email never links (account takeover risk) and gets a clear 401.
  • list:reference fields render a multiple select again instead of a text box showing [1, 2].
  • Hidden fields are back in form.vars (regression from 2e44f6e). Only validated fields, plus fields set by a validation callback, are written to the database.

Tests

python -m pytest tests/ on a clean checkout of this branch: 177 passed (master: 136). New tests are in tests/test_auth_security.py, tests/test_grid_security.py and tests/test_form.py.

🤖 Generated with Claude Code

mdipierro and others added 18 commits November 7, 2026 11:01
- dashboard: make demo/readonly modes read-only (delete_app, new_file,
  dbadmin grid, translation folder creation), fix "DEMO" typo that
  exposed tickets in demo mode, use safe_join for translation endpoints
- Form: honour deletable=False when _delete is posted
- Grid: return 403 instead of infinite recursion on non-deletable delete
- auth: reject backslash/control chars in prevent_open_redirect,
  make reset tokens single use, use secrets for the 2FA code
- URLSigner: constant-time signature comparison

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Verify-email and reset-password links were built with URL(scheme=True),
whose host comes from the client-controlled Origin/Host headers, so an
attacker could request a reset for a victim and have the emailed token
point to a host they control.

- Auth gains base_url (param, or PY4WEB_BASE_URL env var); when set,
  emailed links are built from it. Otherwise the previous behaviour
  (PY4WEB_DOMAINS, then Origin/Host) is kept for compatibility and a
  warning is logged.
- X-Script-Name is only honoured when it is a plain path prefix, which
  blocks "@evil.com" style values that changed the host of absolute
  URLs even when PY4WEB_DOMAINS was set.
- _scaffold exposes BASE_URL in settings.py.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
request_reset_password overwrote any action_token other than
account-blocked, including pending-registration and pending-approval.
Since login only blocks those states, registering with someone else's
email (or while approval was required) and then requesting a reset let
the user log in with their original password without ever verifying
the email or being approved.

A reset token is now only issued for active accounts (empty token or a
previous reset request).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AuthAPI.login wrote the user into the session directly, so with 2FA
configured a stolen password was enough to log in via auth/api/login;
only the HTML form enforced the second factor.

Both API login branches (database and PAM/LDAP) now check
requires_two_factor(). When 2FA is needed the pending login is stored
in session as the form does, and the API returns a 401 with
two_factor_url pointing at the two_factor page instead of a session.
The form login uses the same helpers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The record for details/edit/delete was fetched with table(id), ignoring
the grid query, so with Grid(db.thing.owner == auth.user_id, ...) a user
could read, edit or delete any row of the table by changing ?id=.

The record is now selected with query & (table._id == id) (honouring
left=), a non integer id is rejected, and anything outside the query
redirects back to the grid like a missing record does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
downloader() served uploads inline from the app origin with a mime type
guessed from the user-supplied extension (or text/html for files stored
in the database) and without nosniff, so uploading an .html or .svg file
with a script was stored XSS.

Only extensions in SAFE_INLINE_EXTENSIONS (raster images, audio, video,
txt) are displayed inline; everything else is sent as an attachment.
Every download also gets X-Content-Type-Options: nosniff and a
"default-src 'none'; sandbox" CSP. Apps can pass inline_extensions= to
change the list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With policy=None pydal's RestAPI performs no checks, so a Publisher
created without a policy exposed full CRUD on every table, including
GET on auth_user (password hashes), to anyone who saw the service URL
printed in the page. auth was also added as an optional fixture, so no
login was required.

policy=None now means DENY_ALL_POLICY and auth is enforced as
auth.user. Apps that relied on the old behaviour must pass an explicit
policy (as the showcase example already does with ALLOW_ALL_POLICY).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When a POST had no session cookie, Session.load read _session_token
from the form or JSON body. Cross-site POSTs never carry the SameSite=Lax
cookie, so a page on another site could submit the attacker's own valid
token and the response would set the attacker's session in the victim's
browser (login CSRF / session forcing). The comment above the code
already claimed this fallback had been removed.

Clients that do not keep cookies can send the token in the
X-Session-Token header instead, which browsers cannot send cross-site
without a CORS preflight.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- delete mode deleted the record on any POST without checking the
  _formkey the confirmation form already carries, so a forged or
  same-site cross-origin POST could delete records. The signed form key
  is now verified (403 if missing or invalid).
- the base16 "referrer" parameter was used unchecked as the Back button
  href and as the redirect target, allowing javascript: XSS and open
  redirects. It must now be a local path or an http(s) URL on the
  current host; anything else, or invalid hex (previously a 500), is
  ignored.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
?orderby= accepted any table.field in the database, including fields
that are not readable or not shown (e.g. auth_user.password or a hidden
"secret" column). Sorting leaks the relative order of those values,
which can be turned into an oracle, and naming another table added it
to the select (cartesian product).

The requested order must now match the orderby of one of the grid's
columns; otherwise the default order is used.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- OAuth2: the state was generated with random.choice and never stored,
  so the callback accepted any code (login CSRF: an attacker could log
  the victim into the attacker's account). A CSPRNG state is now stored
  in session at login and must match on callback. Google scoped
  rejected a mismatching state but skipped the check when none was in
  session; it is now required.
- next was not validated on SSO logins (open redirect); it now goes
  through prevent_open_redirect.
- SSO callbacks, the PAM/LDAP API login and WebAuthn skipped the
  blocked/pending account checks and (for SSO/PAM/LDAP) two factor.
  Auth gains check_login_blockers() and complete_login(), used by every
  redirect based login; they also rotate the session uuid through
  store_user_in_session.
- WebAuthn stored the whole auth_user row (password hash, action_token)
  in the session and did not rotate it; it now stores only the id via
  store_user_in_session, requires user verification for passwordless
  login, and makes the challenge single use even on failure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
uuid1() is a timestamp plus the machine MAC address and clock sequence,
which an attacker can read from any uuid1 they are given, leaving only
the timestamp to guess. It was used for:
- the Form CSRF signing key (_formkey)
- the URLSigner per-session key and the salt included in every signed
  URL (which leaked the node/clock_seq)
- the session uuid set at login, which is the cookie value when the
  session is stored server side

They now come from secrets.

With server side storage, changing the session uuid (login rotation,
session.clear() on logout) left the old id valid in storage. Session
now overwrites the old entry when the id changes, so a stolen or
fixated id stops working.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The two factor code and the tries counter lived only in the session,
which by default is the client side cookie. Replaying the cookie issued
right after the code was sent reset the counter, so the 6 digit code
could be brute forced, and the code never expired.

- New AttemptCounter backed by an auth_attempt table (defined by
  Auth.define_tables, keys stored as sha256) counts failed tries per
  user; once two_factor_tries is reached the 2FA step is aborted and the
  guess is not even evaluated, whatever the session says.
- The code expires after two_factor_lifespan seconds (default 300).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On a request authenticated by a token plugin (SimpleTokenPlugin,
JwtTokenPlugin) Auth.on_request wrote the user (the full auth_user row,
password hash included, for SimpleTokenPlugin) into session["user"],
and the session sent back a Set-Cookie. That cookie kept working without
the token, so revoking or expiring an API token did not cut off a client
that kept cookies.

The token user is now stored for the current request only (in the WSGI
environ). user_id, is_logged_in, get_user and auth.user/condition
enforcement read it from there, and the session is left untouched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- New passwords are hashed with pbkdf2(210000,20,sha512) (OWASP
  recommendation) instead of pydal's default 1000 iterations, for
  auth_user and the dashboard password file. Existing hashes embed their
  algorithm and keep working; auth_user hashes are upgraded at the next
  successful login.
- Auth.login (form and API, including the PAM/LDAP API branch) is
  throttled with the server side AttemptCounter: after
  login_max_attempts (10) failures for an account, or
  login_max_attempts_per_ip (100) from one address, within
  login_attempts_window (600s) the password is not checked at all. A
  successful login resets the account counter. Set the limits to 0 to
  disable.
- The dashboard login allows 5 failures per ip per 5 minutes (kept in
  memory, per process).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The dashboard index read HTTP_X_SCRIPT_NAME directly; use
get_script_name() so the same path-only validation applies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Logging in with Google for the first time created a duplicate auth_user
(or failed on the unique email) for users who already had a password
account. If no account has the sso_id yet and the provider asserts the
email is verified, link the existing account instead. Unverified emails
never link to an existing account, to prevent account takeover.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- the default validator of a list:reference field is now
  IS_EMPTY_OR(IS_LIST_OF(IS_IN_DB(...))), which has no .options, so the
  form rendered a text box with "[1, 2]". get_options now looks inside
  wrapping validators through .other.
- since 2e44f6e form.vars only held the validated fields, so hidden
  fields posted with the form were lost. Submitted vars are kept again,
  but only validated fields (plus table fields a validation callback
  sets) are written to the database, never raw posted values.
- test_form now sets request.app_name so it passes when run alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mdipierro
mdipierro merged commit 7f15df3 into master Nov 7, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant