Skip to content

OIDC security hardening#1516

Open
davmlaw wants to merge 1 commit intomasterfrom
oidc-security-hardening
Open

OIDC security hardening#1516
davmlaw wants to merge 1 commit intomasterfrom
oidc-security-hardening

Conversation

@davmlaw
Copy link
Copy Markdown
Contributor

@davmlaw davmlaw commented Apr 2, 2026

Addresses SACGF/variantgrid_private#3823.

Changes

oidc_auth/backend.py

  • Implement provider_logout function so OIDC_OP_LOGOUT_URL_METHOD correctly terminates Keycloak sessions on logout
  • Validate and sanitize OIDC claim values (email, username, given_name, family_name) before writing to user fields — validates email format, strips control characters, enforces field max lengths
  • Replace claims["groups"] direct key access with .get("groups", []) plus an isinstance type check to handle absent or malformed claims gracefully
  • Validate group name components against a safe-character pattern before creating or assigning groups from provider claims
  • Fix maintenance-mode boolean expression to use explicit parentheses with explanatory comment

oidc_auth/oidc_error_handler.py

  • Log exceptions via logger.exception() before redirecting, so OIDC errors produce an audit trail

oidc_auth/session_refresh.py

  • Add comment explaining why API and AJAX requests are excluded from browser-based session refresh

variantgrid/settings/env/shariantcommon.py

  • Enable PKCE (OIDC_USE_PKCE, OIDC_PKCE_CODE_CHALLENGE_METHOD = 'S256')
  • Fix OIDC_OP_LOGOUT_URL_METHOD module path (auth.backendoidc_auth.backend)
  • Replace ALLOWED_HOSTS = ['*'] with explicit Shariant hostnames

Test plan

  • Log in via Keycloak on test/demo — confirm normal login still works
  • Log out — confirm Keycloak session is terminated (re-visiting the app prompts for credentials rather than auto-logging in)
  • Confirm a user without the required group receives the "not authorised" error message
  • Confirm a user with an invalid groups claim ("groups": "not-a-list") is rejected cleanly
  • Confirm ALLOWED_HOSTS correctly rejects requests with an unrecognised Host: header

- Fix broken provider_logout (was referencing non-existent auth.backend module)
- Enable PKCE (RFC 7636) for authorization code flow
- Validate and sanitize OIDC claim values before writing to user fields
- Replace direct claims["groups"] key access with .get() + type check
- Validate group names from OIDC provider before creating/assigning
- Restrict ALLOWED_HOSTS from wildcard to explicit Shariant hostnames
- Log OIDC exceptions before swallowing them in error handler middleware
- Document API/AJAX session refresh exclusion policy
@davmlaw davmlaw force-pushed the oidc-security-hardening branch from ddc78fe to 97a35d9 Compare April 2, 2026 00:34
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