From f44abdbfcf71ccad4685af39e2e72d1d52a30a17 Mon Sep 17 00:00:00 2001 From: Christian Galo Date: Mon, 21 Sep 2026 03:36:52 -0500 Subject: [PATCH] fix(#46, #59): handle CORS in Gitea instead of a traefik middleware --- .env.sample | 3 +++ README.md | 14 ++++++++++++++ abra.sh | 2 +- app.ini.tmpl | 6 ++++++ compose.anubis.yml | 2 +- compose.yml | 6 ------ release/next | 12 ++++++++++++ 7 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 release/next diff --git a/.env.sample b/.env.sample index 75839f9..8d65d7e 100644 --- a/.env.sample +++ b/.env.sample @@ -72,6 +72,9 @@ SECRET_SECRET_KEY_VERSION=v1 # length=64 # GITEA_ACCOUNT_LINKING=replace-me # GITEA_OAUTH2_CLIENT_ENABLED=replace-me +# CORS (for Decap CMS, Sveltia CMS etc.) +# GITEA_CORS_ALLOW_DOMAIN=https://cms.example.com,https://other.example.org + # Lifetime of an OAuth2 refresh token in hours, prolly no need to edit. We # were hitting issues with infrequently pushed to repos that were not picked # up by drone after a month of inactivity, hence the option. diff --git a/README.md b/README.md index bc2691b..0bbff00 100644 --- a/README.md +++ b/README.md @@ -71,3 +71,17 @@ ssh -T -p 2222 git@my.gitea.example.com ``` Note that gitea should be configured to listen to port 2222, i.e. `GITEA_SSH_PORT=2222` in the gitea config. + +## CORS + +To let a browser app on another domain (Decap CMS, Sveltia CMS) use the API: + +```sh +GITEA_CORS_ALLOW_DOMAIN=https://cms.example.com +``` + +Full origins, comma-separated for more than one. Check with: + +```sh +curl -sI -H 'Origin: https://cms.example.com' https://my.gitea.example.com/api/v1/version | grep -i access-control +``` diff --git a/abra.sh b/abra.sh index 68fe314..9662632 100644 --- a/abra.sh +++ b/abra.sh @@ -1,4 +1,4 @@ -export APP_INI_VERSION=v23 +export APP_INI_VERSION=v24 export DOCKER_SETUP_SH_VERSION=v1 export PG_BACKUP_VERSION=v1 diff --git a/app.ini.tmpl b/app.ini.tmpl index 49979eb..54dcfd8 100644 --- a/app.ini.tmpl +++ b/app.ini.tmpl @@ -72,6 +72,12 @@ REVERSE_PROXY_LIMIT = 1 REVERSE_PROXY_TRUSTED_PROXIES = * SECRET_KEY = {{ secret "secret_key" }} +{{ if ne (env "GITEA_CORS_ALLOW_DOMAIN") "" }} +[cors] +ENABLED = true +ALLOW_DOMAIN = {{ env "GITEA_CORS_ALLOW_DOMAIN" }} +{{ end }} + [admin] DISABLE_REGULAR_ORG_CREATION = {{ env "GITEA_DISABLE_REGULAR_ORG_CREATION" }} diff --git a/compose.anubis.yml b/compose.anubis.yml index 2e3fd28..a8c4c85 100644 --- a/compose.anubis.yml +++ b/compose.anubis.yml @@ -4,4 +4,4 @@ services: app: deploy: labels: - - "traefik.http.routers.${STACK_NAME}.middlewares=anubis,${STACK_NAME}_cors" + - "traefik.http.routers.${STACK_NAME}.middlewares=anubis" diff --git a/compose.yml b/compose.yml index ac093e9..8007109 100644 --- a/compose.yml +++ b/compose.yml @@ -81,12 +81,6 @@ services: - "traefik.tcp.routers.${STACK_NAME}-ssh.rule=HostSNI(`*`)" - "traefik.tcp.routers.${STACK_NAME}-ssh.entrypoints=gitea-ssh" - "traefik.tcp.services.${STACK_NAME}-ssh.loadbalancer.server.port=${GITEA_SSH_PORT}" - - "traefik.http.routers.${STACK_NAME}.middlewares=${STACK_NAME}_cors" - - "traefik.http.middlewares.${STACK_NAME}_cors.headers.accesscontrolallowmethods=GET,OPTIONS,PUT" - - "traefik.http.middlewares.${STACK_NAME}_cors.headers.accesscontrolallowheaders=content-type,authorization" - - "traefik.http.middlewares.${STACK_NAME}_cors.headers.accesscontrolalloworiginlist=https://${GITEA_CORS_ALLOW_DOMAIN}" - - "traefik.http.middlewares.${STACK_NAME}_cors.headers.accesscontrolmaxage=100" - - "traefik.http.middlewares.${STACK_NAME}_cors.headers.addvaryheader=true" - coop-cloud.${STACK_NAME}.version=3.6.7+1.27.3-rootless diff --git a/release/next b/release/next new file mode 100644 index 0000000..ea7804d --- /dev/null +++ b/release/next @@ -0,0 +1,12 @@ +CORS is now handled by Gitea itself ([cors] in app.ini) instead of a traefik +headers middleware. If you set GITEA_CORS_ALLOW_DOMAIN, it now takes full +origins rather than bare hostnames, so change + + GITEA_CORS_ALLOW_DOMAIN=cms.example.com + +to + + GITEA_CORS_ALLOW_DOMAIN=https://cms.example.com + +before upgrading, or browser requests from that site will start failing. +Several origins can be listed, comma-separated. -- 2.54.0