|
| 1 | +# CORS |
| 2 | + |
| 3 | +With the version 5, we’re taking small, deliberate steps towards OWASP standards and tightening up our CORS policy |
| 4 | +was done as one of those “tiny steps”. It’s not meant to be a hurdle for developers but rather a browser‑based |
| 5 | +safeguard that helps prevent cross‑site request forgery and other attacks at end user's that could silently exfiltrate |
| 6 | +a user’s credentials or data. |
| 7 | + |
| 8 | +**Where the setting lives** |
| 9 | + |
| 10 | +* CORS is controlled by the `EXTRA_CORS_ORIGINS` environment variable in the app config. If it is **not** set, all origins are allowed by default. |
| 11 | +* `http://localhost:3000` is already whitelisted on the staging and dev API instances (`https://tasking-manager-staging-api.hotosm.org/api/docs` and `https://tasking-manager-dev-api.hotosm.org/api/docs`) so you can develop locally without CORS errors. |
| 12 | +* If a production frontend needs direct browser access, contact the system admin (support) to request the exact origin be added to the whitelist. |
| 13 | + |
| 14 | +* For any browser‑based clients calling the TM API directly are now blocked by the CORS. |
| 15 | +For any production frontends running that want direct access to the TM API in-browser, |
| 16 | +we can also allow them by adding to our whitelist for which the admins can be contacted |
| 17 | +and the origin can be whitelisted. Please feel free to contact the system admin via the support section |
| 18 | +if any whitelisting necesssary. |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## Local development options |
| 23 | + |
| 24 | +### 1) Local nginx reverse-proxy |
| 25 | + |
| 26 | +* **What:** Run a tiny nginx container that forwards requests to the TM API and injects permissive CORS headers for your browser. |
| 27 | +* **Advantages:** Easy to run (single container), works for any client, replicates production TLS/host header behaviour, can be shared among a team. |
| 28 | +* **Disadvantages / cautions:** Bypasses browser protections — **do not** expose publicly or use in production. If you need credentials (cookies) set explicit `Access-Control-Allow-Origin` and `Access-Control-Allow-Credentials` instead of `*`. |
| 29 | + |
| 30 | +**Minimal nginx.conf (development only)** |
| 31 | + |
| 32 | +```nginx |
| 33 | +http { |
| 34 | + server { listen 80; location / { |
| 35 | + proxy_pass https://tasking-manager-production-api.hotosm.org/; |
| 36 | + if ($request_method = OPTIONS) { |
| 37 | + add_header 'Access-Control-Allow-Origin' '*' always; |
| 38 | + add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,PATCH,DELETE,OPTIONS' always; |
| 39 | + add_header 'Access-Control-Allow-Headers' 'Content-Type,Authorization' always; |
| 40 | + return 204; |
| 41 | + } |
| 42 | + add_header 'Access-Control-Allow-Origin' '*' always; |
| 43 | + }} |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +**Run:** |
| 48 | + |
| 49 | +```bash |
| 50 | +docker run -d --name cors-proxy -p 8080:80 \ |
| 51 | + -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro \ |
| 52 | + nginx:alpine |
| 53 | +``` |
| 54 | + |
| 55 | +Access via `http://localhost:8080`. |
| 56 | + |
| 57 | +### 2) Cloudflare Worker proxy |
| 58 | + |
| 59 | +* See `kshitijrajsharma/tm-cors-proxy` a ready-made Cloudflare Worker proxy implementation that forwards browser requests to the TM API and injects CORS headers. |
| 60 | + |
| 61 | +--- |
0 commit comments