Skip to content

fix(turnstile): route CAPTCHA verification through the configured proxy - #38412

Merged
bircni merged 4 commits into
go-gitea:mainfrom
singhlovepreet9:fix-turnstile-proxy
Jul 12, 2026
Merged

fix(turnstile): route CAPTCHA verification through the configured proxy#38412
bircni merged 4 commits into
go-gitea:mainfrom
singhlovepreet9:fix-turnstile-proxy

Conversation

@singhlovepreet9

@singhlovepreet9 singhlovepreet9 commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Fixes #38217

Problem

Turnstile CAPTCHA verification uses http.DefaultClient, so the request to challenges.cloudflare.com bypasses Gitea's configured HTTP proxy — unlike other outbound HTTP clients such as the update checker (modules/updatechecker/update_checker.go) and migrations. In deployments where egress is only permitted through the configured proxy, verification fails.

Fix

Build the client with proxy.Proxy() as the transport proxy, mirroring the update checker:

func httpClient() *http.Client {
	return &http.Client{
		Transport: &http.Transport{
			Proxy: proxy.Proxy(),
		},
	}
}

The client is built per call (rather than a package-level var) because proxy.Proxy() reads setting.Proxy when invoked; building it at request time ensures it reflects the loaded settings. When no proxy is configured, behavior is unchanged (proxy.Proxy() returns a no-op / http.ProxyFromEnvironment).

Turnstile verification used http.DefaultClient, so the request to
challenges.cloudflare.com bypassed Gitea's configured HTTP proxy, unlike
other outbound clients (e.g. the update checker). In environments where
egress is only allowed through the proxy, verification failed.

Build the client with proxy.Proxy() as the transport proxy, the same way
the update checker does, so Turnstile honors the proxy settings.
@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jul 11, 2026
@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Jul 11, 2026
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Jul 12, 2026
Comment thread modules/turnstile/turnstile_test.go Outdated
@wxiaoguang wxiaoguang added the backport/v1.27 This PR should be backported to Gitea 1.27 label Jul 12, 2026
@wxiaoguang
wxiaoguang marked this pull request as draft July 12, 2026 03:05
@wxiaoguang
wxiaoguang marked this pull request as ready for review July 12, 2026 03:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the Turnstile CAPTCHA server-side verification HTTP client to honor Gitea’s configured outbound proxy settings, addressing deployments where egress is only allowed via proxy.

Changes:

  • Route Turnstile verification HTTP requests through proxy.Proxy() instead of http.DefaultClient.
  • Introduce a resettable util.OnceValue[T] helper to lazily initialize and cache a value.
  • Add unit tests validating the Turnstile HTTP client’s proxy behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
modules/util/sync.go Adds a resettable, lazy “once value” helper used to cache initialized values.
modules/util/sync_test.go Adds unit tests for util.OnceValue behavior (repeat calls, panic caching, reset).
modules/turnstile/turnstile.go Switches Turnstile verification to use an HTTP client that applies Gitea proxy configuration.
modules/turnstile/turnstile_test.go Adds a test ensuring the Turnstile HTTP client honors the configured proxy.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread modules/turnstile/turnstile.go Outdated
Comment thread modules/turnstile/turnstile.go
Comment thread modules/util/sync.go Outdated
Comment thread modules/turnstile/turnstile_test.go
Comment thread modules/turnstile/turnstile_test.go
@bircni
bircni enabled auto-merge (squash) July 12, 2026 10:56
@bircni bircni added the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Jul 12, 2026
@bircni
bircni merged commit 65c5a5f into go-gitea:main Jul 12, 2026
24 of 26 checks passed
@GiteaBot GiteaBot added this to the 28.0.0 milestone Jul 12, 2026
@GiteaBot GiteaBot removed the reviewed/wait-merge This pull request is part of the merge queue. It will be merged soon. label Jul 12, 2026
@GiteaBot GiteaBot added the backport/done All backports for this PR have been created label Jul 12, 2026
wxiaoguang added a commit that referenced this pull request Jul 12, 2026
…xy (#38412) (#38420)

Backport #38412

Fixes #38217

Co-authored-by: Lovepreet Singh <lovepreet.singh61182@gmail.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
zjjhot added a commit to zjjhot/gitea that referenced this pull request Jul 13, 2026
* 'main' of https://github.com/go-gitea/gitea:
  fix: various security fixes (go-gitea#38406)
  fix(util): reject invalid characters between time-estimate units (go-gitea#38416)
  feat(actions): implement adaptive auto-refresh for workflow runs list (go-gitea#38329)
  fix(turnstile): route CAPTCHA verification through the configured proxy (go-gitea#38412)
  fix: represent a deleted assignee team as a Ghost team (go-gitea#38413)
  [skip ci] Updated translations via Crowdin
  fix: refresh pull request merge box when the commit status is pending (go-gitea#38410)
  chore: remove Yarden Shoham from maintainers (go-gitea#38407)
  fix: actions task state concurrent update (go-gitea#38405)
  fix(actions): keep workflow run trailing on one row with long branch names (go-gitea#38382)
  fix(pull): re-evaluate review official flag on target branch change (go-gitea#38319)
  fix(web): use locale-aware date formatting for contribution calendar tooltips (go-gitea#38398)
  fix(security): harden access checks and migration validation (go-gitea#38324)
  fix: enforce public-only token scope and harden push options / locale parsing (go-gitea#38323)
  fix: co-author detection (go-gitea#38392)
  fix(api): stop leaking private repo metadata after access revocation (go-gitea#38321)
  fix(lfs): require proof of possession for cross-repo objects (go-gitea#38322)
  fix: incorrect co-author detection on commit page (go-gitea#38386)
zjjhot added a commit to zjjhot/gitea that referenced this pull request Jul 13, 2026
* main:
  fix: various security fixes (go-gitea#38406)
  fix(util): reject invalid characters between time-estimate units (go-gitea#38416)
  feat(actions): implement adaptive auto-refresh for workflow runs list (go-gitea#38329)
  fix(turnstile): route CAPTCHA verification through the configured proxy (go-gitea#38412)
  fix: represent a deleted assignee team as a Ghost team (go-gitea#38413)
  [skip ci] Updated translations via Crowdin
  fix: refresh pull request merge box when the commit status is pending (go-gitea#38410)
  chore: remove Yarden Shoham from maintainers (go-gitea#38407)
  fix: actions task state concurrent update (go-gitea#38405)
  fix(actions): keep workflow run trailing on one row with long branch names (go-gitea#38382)
  fix(pull): re-evaluate review official flag on target branch change (go-gitea#38319)
  fix(web): use locale-aware date formatting for contribution calendar tooltips (go-gitea#38398)
  fix(security): harden access checks and migration validation (go-gitea#38324)
  fix: enforce public-only token scope and harden push options / locale parsing (go-gitea#38323)
  fix: co-author detection (go-gitea#38392)
  fix(api): stop leaking private repo metadata after access revocation (go-gitea#38321)
  fix(lfs): require proof of possession for cross-repo objects (go-gitea#38322)
  fix: incorrect co-author detection on commit page (go-gitea#38386)
zeekay pushed a commit to hanzoai/git that referenced this pull request Jul 26, 2026
…xy (go-gitea#38412)

Fixes go-gitea#38217

## Problem

Turnstile CAPTCHA verification uses `http.DefaultClient`, so the request
to `challenges.cloudflare.com` bypasses Gitea's configured HTTP proxy —
unlike other outbound HTTP clients such as the update checker
(`modules/updatechecker/update_checker.go`) and migrations. In
deployments where egress is only permitted through the configured proxy,
verification fails.

## Fix

Build the client with `proxy.Proxy()` as the transport proxy, mirroring
the update checker:

```go
func httpClient() *http.Client {
	return &http.Client{
		Transport: &http.Transport{
			Proxy: proxy.Proxy(),
		},
	}
}
```

The client is built per call (rather than a package-level var) because
`proxy.Proxy()` reads `setting.Proxy` when invoked; building it at
request time ensures it reflects the loaded settings. When no proxy is
configured, behavior is unchanged (`proxy.Proxy()` returns a no-op /
`http.ProxyFromEnvironment`).

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport/done All backports for this PR have been created backport/v1.27 This PR should be backported to Gitea 1.27 lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Not using HTTP proxy for Turnstile challenge

7 participants