Aller au contenu principal

CORS Strategy for OCapistaine

Overview

OCapistaine uses two CORS strategies depending on the environment:

  • Production (Render): Static allowed origins configured in render.yaml
  • Development (ngrok): Dynamic origins generated by scripts/set_streamlit_env.py

Architecture

Production:
render.yaml → STREAMLIT_SERVER_ALLOWED_ORIGINS (static)
Vaettir Nginx → Render (ocapistaine.onrender.com)

Development:
.env → set_streamlit_env.py → STREAMLIT_SERVER_ALLOWED_ORIGINS (dynamic)
Vaettir Nginx → ngrok → localhost:8502

Production CORS

On Render, origins are configured statically in render.yaml:

- key: STREAMLIT_SERVER_ALLOWED_ORIGINS
value: "https://ocapistaine.onrender.com,https://ocapistaine.vaettir.locki.io,https://vaettir.locki.io"

No scripts or dynamic configuration needed. The Vaettir nginx proxy passes requests through with the correct headers, and Streamlit validates the origin against this list.

Development CORS

For local development via ngrok, the set_streamlit_env.py script auto-generates allowed origins.

Configuration

Edit .env:

NGROK_DOMAIN=your-dev-tunnel.ngrok-free.app
STREAMLIT_PORT=8502

# Optional: additional origins
# STREAMLIT_CUSTOM_ORIGINS=https://custom.com

Usage

# Option A: Convenience script
./scripts/run_streamlit.sh

# Option B: Manual
eval $(python scripts/set_streamlit_env.py)
streamlit run app/front.py

Generated Origins

The script builds the origin list from:

SourceOriginWhen
Alwayshttp://localhost:{STREAMLIT_PORT}Always
Alwayshttp://127.0.0.1:{STREAMLIT_PORT}Always
.envhttps://{NGROK_DOMAIN}If NGROK_DOMAIN is set
Hardcodedhttps://ocapistaine-dev.vaettir.locki.ioAlways (dev proxy)
Hardcodedhttps://ocapistaine.vaettir.locki.ioAlways (production proxy)
Hardcodedhttps://vaettir.locki.ioAlways
.envCustom originsIf STREAMLIT_CUSTOM_ORIGINS is set

Verification

python scripts/set_streamlit_env.py
# Output shows all configured origins

echo $STREAMLIT_SERVER_ALLOWED_ORIGINS
# Comma-separated list

Environment Variables

Streamlit CORS Variables

VariableDescriptionSet By
STREAMLIT_SERVER_ENABLE_CORSEnable CORS checkingrender.yaml / script
STREAMLIT_SERVER_ENABLE_XSRF_PROTECTIONXSRF protection (disabled behind proxy)render.yaml / script
STREAMLIT_SERVER_ALLOWED_ORIGINSComma-separated allowed originsrender.yaml / script
STREAMLIT_BROWSER_SERVER_ADDRESSPublic domain for browserOptional
STREAMLIT_BROWSER_SERVER_PORTPublic port (443 for HTTPS)Optional

Input Variables

VariableDescriptionExample
NGROK_DOMAINngrok tunnel domain (no protocol)my-tunnel.ngrok-free.app
STREAMLIT_PORTLocal Streamlit port8502
STREAMLIT_CUSTOM_ORIGINSAdditional origins (comma-separated)https://custom.com

Troubleshooting

CORS Errors in Production

Check that render.yaml includes the requesting domain in STREAMLIT_SERVER_ALLOWED_ORIGINS. Redeploy after changes.

CORS Errors in Development

# Verify origins are set
echo $STREAMLIT_SERVER_ALLOWED_ORIGINS

# If empty, run the script
eval $(python scripts/set_streamlit_env.py)

# Quick test with wildcard
export STREAMLIT_SERVER_ALLOWED_ORIGINS="*"
streamlit run app/front.py

Origin Not in Allowed List

If the browser shows origin X not in allowed origins:

  1. Check the exact origin (including protocol and port)
  2. Add it to STREAMLIT_CUSTOM_ORIGINS in .env (dev) or render.yaml (prod)