CORS & Auth Hardening + Dev Proxy Improvements (Backend & Frontend) #40
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR: CORS & Auth Hardening + Dev Proxy Improvements (Backend & Frontend)
Summary
This PR addresses persistent CORS inconsistency and sticky/invalid refresh-token issues observed across environments (Mac/Windows/Ubuntu, multiple hostnames/ports). It introduces an environment-aware CORS configuration, robust refresh-token validation & clearing, safer JWT timestamp handling, and frontend proxy + client-side reset logic to eliminate polluted sessions and cache artifacts during development.
Problem / Context
localhost,127.0.0.1, IPv6, and LAN IPs — especially with varying ports and browsers.What’s Changed (High-Level)
Backend
.env-dev: Added temporary config for Chat-with-Docs integration (LLMs/Embeddings via Ollama), BM25, and Document Processor.Auth (
auth.py):Module-level imports for
base64/json(avoid shadowing).validate_token_logic(...): sanity-checkiat/exp(future-issued, expired-long-ago, iat>exp).clear_refresh_token_systematically(...): attempt all domain/path/cookie-attribute combos to clear persistent cookies.log_token_validation(...): structured logging (source, token length, timestamps).“Nuclear” paths:
Enhanced
/refreshflow: decode payload (without verify) only for logging/logic checks; clear and instruct reset if invalid.Added endpoints:
POST /nuclear-clear-cookies&POST /force-logout-clearto force-clear cookie variants + set cache-busting headers.POST /test-cookie-settingto validate cookie set/clear behavior.Security (
security.py):expas a Unix timestamp and auto-injectiatif missing for consistency across validators.Config (
config.py):field_validatorparsers for list-like env vars and host lists.CORS Utilities (
cors_utils.py):build_dev_origin_regex(...): allowhttp(s)://(localhost|127.0.0.1|[::1]|LAN-IP)(:port)?patterns.validate_production_origins(...): parse & validate origins, warn on insecure patterns.log_cors_config(...),get_cors_debug_info(...)helpers.main.py:Environment-aware CORS:
allow_origin_regexgenerated fromCORS_DEV_HOSTS.Centralized logging of applied CORS mode.
New utility
auth_fix.py:Frontend
Config (
src/config/index.ts):VITE_USE_PROXYtoggle; dev defaults to proxy via relative URLs.Vite (
vite.config.ts):VITE_API_TARGET(defaulthttp://127.0.0.1:8005).ApiService:
On token errors (
STALE_TOKEN_RESET_REQUIRED,INVALID_TOKEN_RESET_REQUIRED*,BLOCKED_TOKEN_DETECTED):/loginwith reason param.Plugin Studio / UDR minor additions: Components/utilities created for ongoing integration; unrelated to CORS logic but included in this patch set.
Configuration / Env Notes
New/Used backend envs (see
backend/.env-dev):LLM_PROVIDER,EMBEDDING_PROVIDER,ENABLE_CONTEXTUAL_RETRIEVAL,OLLAMA_*endpoints & models,BM25_*vars,DOCUMENT_PROCESSOR_*.CORS (backend):
APP_ENV=dev|prodcontrols regex vs explicit origins.CORS_DEV_HOSTS(list) defaults:["localhost","127.0.0.1","[::1]","10.0.2.149"]CORS_ORIGINSused in prod only (explicit list).Frontend:
VITE_USE_PROXY=true|false(defaulttruein dev).VITE_API_TARGETfor Vite proxy target if needed.Security Considerations
JWT:
expandiatnormalized to timestamps → consistent validation.Cookies:
Logging:
Backward Compatibility
CORS_ORIGINSlist (same as before but now validated).VITE_USE_PROXY=falseto direct-call API.