fix(auth)!: disable query-token authentication by default (#40297)

Query-string tokens end up in proxy and access logs, so
`DISABLE_QUERY_AUTH_TOKEN` now defaults to `true`. The `token` and
`access_token` query parameters are ignored on the API and on web routes
that accept tokens, like raw, media, archive and attachment downloads.

## ⚠️ BREAKING ⚠️

Clients that send a token as `?token=` or `?access_token=` are treated
as anonymous. Send it in the `Authorization: token <token>` header
instead, or as the basic auth username (`https://<token>@host/...`)
where only a URL can be configured. Admins can restore the old behavior
for now with `[security] DISABLE_QUERY_AUTH_TOKEN = false`, which logs a
deprecation warning at startup.

Docs: https://gitea.com/gitea/docs/pulls/665

Assisted-by: Codex:gpt-6-astra
Assisted-by: Claude Code:claude-opus-5-5

---------

Signed-off-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
authored and GitHub committed 2026-11-27 21:36:16 +00:00
1 parent 32cb4740e2
commit 3edd96b2a1
11 files changed
+75 -42

No files matched your search

+2 -4
View File
@@ -510,10 +510,8 @@ INTERNAL_TOKEN =
;; Validate against https://haveibeenpwned.com/Passwords to see if a password has been exposed
;PASSWORD_CHECK_PWN = false
;;
;; Reject API tokens sent in URL query string (Accept Header-based API tokens only). This avoids security vulnerabilities
;; stemming from cached/logged plain-text API tokens.
;; In future releases, this will become the default behavior
;DISABLE_QUERY_AUTH_TOKEN = false
;; Ignore access tokens sent in the "token" or "access_token" URL query parameter. Setting it to false is deprecated.
;DISABLE_QUERY_AUTH_TOKEN = true
;;
;; On user registration, record the IP address and user agent of the user to help identify potential abuse.
;; RECORD_USER_SIGNUP_METADATA = false
+4 -9
View File
@@ -216,15 +216,10 @@ func loadSecurityFrom(rootCfg ConfigProvider) {
}
}
sectionHasDisableQueryAuthToken := sec.HasKey("DISABLE_QUERY_AUTH_TOKEN")
// TODO: default value should be true in future releases
DisableQueryAuthToken = sec.Key("DISABLE_QUERY_AUTH_TOKEN").MustBool(false)
DisableQueryAuthToken = sec.Key("DISABLE_QUERY_AUTH_TOKEN").MustBool(true)
if !DisableQueryAuthToken {
LogStartupProblem(1, log.WARN, "Deprecation: config option `[security].DISABLE_QUERY_AUTH_TOKEN = false` enables URL query tokens, clients should send tokens in the Authorization header instead")
}
RecordUserSignupMetadata = sec.Key("RECORD_USER_SIGNUP_METADATA").MustBool(false)
// warn if the setting is set to false explicitly
if sectionHasDisableQueryAuthToken && !DisableQueryAuthToken {
log.Warn("Enabling Query API Auth tokens is not recommended. DISABLE_QUERY_AUTH_TOKEN will default to true in gitea 1.23 and will be removed in gitea 1.24.")
}
}
+2 -11
View File
@@ -31,12 +31,12 @@
// type: apiKey
// name: token
// in: query
// description: This authentication option is deprecated for removal in Gitea 1.23. Please use AuthorizationHeaderToken instead.
// description: This authentication option is deprecated and disabled by default. Set DISABLE_QUERY_AUTH_TOKEN=false temporarily to restore it. Please use AuthorizationHeaderToken instead.
// AccessToken:
// type: apiKey
// name: access_token
// in: query
// description: This authentication option is deprecated for removal in Gitea 1.23. Please use AuthorizationHeaderToken instead.
// description: This authentication option is deprecated and disabled by default. Set DISABLE_QUERY_AUTH_TOKEN=false temporarily to restore it. Please use AuthorizationHeaderToken instead.
// AuthorizationHeaderToken:
// type: apiKey
// name: Authorization
@@ -942,13 +942,6 @@ func individualPermsChecker(ctx *context.APIContext) {
}
}
// check for and warn against deprecated authentication options
func checkDeprecatedAuthMethods(ctx *context.APIContext) {
if ctx.FormString("token") != "" || ctx.FormString("access_token") != "" {
ctx.Resp.Header().Set("X-Gitea-Warning", "token and access_token API authentication is deprecated and will be removed in gitea 1.23. Please use AuthorizationHeaderToken instead. Existing queries will continue to work but without authorization.")
}
}
// Routes registers all v1 APIs routes to web application.
func Routes() *web.Router {
m := web.NewRouter()
@@ -968,8 +961,6 @@ func Routes() *web.Router {
m.AfterRouting(context.APIContexter())
m.AfterRouting(common.AuditOrigin(audit_model.OriginAPI))
m.AfterRouting(checkDeprecatedAuthMethods)
// Get user from session if logged in.
m.AfterRouting(apiAuth(buildAuthGroup()))
+53
View File
@@ -5,17 +5,70 @@ package auth
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
auth_model "gitea.dev/models/auth"
"gitea.dev/models/unittest"
user_model "gitea.dev/models/user"
"gitea.dev/modules/setting"
"gitea.dev/modules/test"
"gitea.dev/services/actions"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseTokenQueryParametersRequireCompatibilitySetting(t *testing.T) {
tests := []struct {
name string
disableQueryAuth bool
query string
header string
wantToken string
}{
{
name: "query token rejected when disabled",
disableQueryAuth: true,
query: "token=query-token",
},
{
name: "legacy token accepted when explicitly enabled",
disableQueryAuth: false,
query: "token=query-token",
wantToken: "query-token",
},
{
name: "legacy access token accepted when explicitly enabled",
disableQueryAuth: false,
query: "access_token=query-access-token",
wantToken: "query-access-token",
},
{
name: "authorization header remains valid when query auth is disabled",
disableQueryAuth: true,
query: "token=query-token",
header: "token header-token",
wantToken: "header-token",
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
defer test.MockVariableValue(&setting.DisableQueryAuthToken, testCase.disableQueryAuth)()
req := httptest.NewRequest(http.MethodGet, "/api/v1/user?"+testCase.query, nil)
if testCase.header != "" {
req.Header.Set("Authorization", testCase.header)
}
token, ok := parseToken(req)
assert.Equal(t, testCase.wantToken != "", ok)
assert.Equal(t, testCase.wantToken, token)
})
}
}
func TestUserIDFromToken(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
+2 -2
View File
@@ -11761,7 +11761,7 @@
},
"securitySchemes": {
"AccessToken": {
"description": "This authentication option is deprecated for removal in Gitea 1.23. Please use AuthorizationHeaderToken instead.",
"description": "This authentication option is deprecated and disabled by default. Set DISABLE_QUERY_AUTH_TOKEN=false temporarily to restore it. Please use AuthorizationHeaderToken instead.",
"in": "query",
"name": "access_token",
"type": "apiKey"
@@ -11795,7 +11795,7 @@
"type": "apiKey"
},
"Token": {
"description": "This authentication option is deprecated for removal in Gitea 1.23. Please use AuthorizationHeaderToken instead.",
"description": "This authentication option is deprecated and disabled by default. Set DISABLE_QUERY_AUTH_TOKEN=false temporarily to restore it. Please use AuthorizationHeaderToken instead.",
"in": "query",
"name": "token",
"type": "apiKey"
+2 -2
View File
@@ -35680,7 +35680,7 @@
},
"securityDefinitions": {
"AccessToken": {
"description": "This authentication option is deprecated for removal in Gitea 1.23. Please use AuthorizationHeaderToken instead.",
"description": "This authentication option is deprecated and disabled by default. Set DISABLE_QUERY_AUTH_TOKEN=false temporarily to restore it. Please use AuthorizationHeaderToken instead.",
"type": "apiKey",
"name": "access_token",
"in": "query"
@@ -35713,7 +35713,7 @@
"in": "header"
},
"Token": {
"description": "This authentication option is deprecated for removal in Gitea 1.23. Please use AuthorizationHeaderToken instead.",
"description": "This authentication option is deprecated and disabled by default. Set DISABLE_QUERY_AUTH_TOKEN=false temporarily to restore it. Please use AuthorizationHeaderToken instead.",
"type": "apiKey",
"name": "token",
"in": "query"
+10 -10
View File
@@ -100,8 +100,8 @@ func testAPIListIssues(t *testing.T) {
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadIssue)
link, _ := url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/issues", owner.Name, repo.Name))
link.RawQuery = url.Values{"token": {token}, "state": {"all"}}.Encode()
resp := MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK)
link.RawQuery = url.Values{"state": {"all"}}.Encode()
resp := MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusOK)
apiIssues := DecodeJSON(t, resp, []*api.Issue{})
assert.Len(t, apiIssues, unittest.GetCount(t, &issues_model.Issue{RepoID: repo.ID}))
for _, apiIssue := range apiIssues {
@@ -109,30 +109,30 @@ func testAPIListIssues(t *testing.T) {
}
// test milestone filter
link.RawQuery = url.Values{"token": {token}, "state": {"all"}, "type": {"all"}, "milestones": {"ignore,milestone1,3,4"}}.Encode()
resp = MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK)
link.RawQuery = url.Values{"state": {"all"}, "type": {"all"}, "milestones": {"ignore,milestone1,3,4"}}.Encode()
resp = MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusOK)
apiIssues = DecodeJSON(t, resp, []*api.Issue{})
if assert.Len(t, apiIssues, 2) {
assert.EqualValues(t, 3, apiIssues[0].Milestone.ID)
assert.EqualValues(t, 1, apiIssues[1].Milestone.ID)
}
link.RawQuery = url.Values{"token": {token}, "state": {"all"}, "created_by": {"user2"}}.Encode()
resp = MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK)
link.RawQuery = url.Values{"state": {"all"}, "created_by": {"user2"}}.Encode()
resp = MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusOK)
apiIssues = DecodeJSON(t, resp, []*api.Issue{})
if assert.Len(t, apiIssues, 1) {
assert.EqualValues(t, 5, apiIssues[0].ID)
}
link.RawQuery = url.Values{"token": {token}, "state": {"all"}, "assigned_by": {"user1"}}.Encode()
resp = MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK)
link.RawQuery = url.Values{"state": {"all"}, "assigned_by": {"user1"}}.Encode()
resp = MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusOK)
apiIssues = DecodeJSON(t, resp, []*api.Issue{})
if assert.Len(t, apiIssues, 1) {
assert.EqualValues(t, 1, apiIssues[0].ID)
}
link.RawQuery = url.Values{"token": {token}, "state": {"all"}, "mentioned_by": {"user4"}}.Encode()
resp = MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK)
link.RawQuery = url.Values{"state": {"all"}, "mentioned_by": {"user4"}}.Encode()
resp = MakeRequest(t, NewRequest(t, "GET", link.String()).AddTokenAuth(token), http.StatusOK)
apiIssues = DecodeJSON(t, resp, []*api.Issue{})
if assert.Len(t, apiIssues, 1) {
assert.EqualValues(t, 1, apiIssues[0].ID)
-1
View File
@@ -86,7 +86,6 @@ LEVEL = Debug
INSTALL_LOCK = true
SECRET_KEY = 9pCviYTWSb
INTERNAL_TOKEN = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0OTU1NTE2MTh9.hhSVGOANkaKk3vfCd2jDOIww4pUk0xtg9JRde5UogyQ
DISABLE_QUERY_AUTH_TOKEN = true
[storage]
STORAGE_TYPE = azureblob
-1
View File
@@ -89,7 +89,6 @@ LEVEL = Debug
INSTALL_LOCK = true
SECRET_KEY = 9pCviYTWSb
INTERNAL_TOKEN = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0OTU1NTE2MTh9.hhSVGOANkaKk3vfCd2jDOIww4pUk0xtg9JRde5UogyQ
DISABLE_QUERY_AUTH_TOKEN = true
[packages]
ENABLED = true
-1
View File
@@ -87,7 +87,6 @@ LEVEL = Debug
INSTALL_LOCK = true
SECRET_KEY = 9pCviYTWSb
INTERNAL_TOKEN = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0OTU1NTE2MTh9.hhSVGOANkaKk3vfCd2jDOIww4pUk0xtg9JRde5UogyQ
DISABLE_QUERY_AUTH_TOKEN = true
[lfs]
S3_BASE_PATH = lfs/
-1
View File
@@ -84,7 +84,6 @@ LEVEL = Debug
INSTALL_LOCK = true
SECRET_KEY = 9pCviYTWSb
INTERNAL_TOKEN = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0OTI3OTU5ODN9.OQkH5UmzID2XBdwQ9TAI6Jj2t1X-wElVTjbE7aoN4I8
DISABLE_QUERY_AUTH_TOKEN = true
[oauth2]
JWT_SECRET = KZb_QLUd4fYVyxetjxC4eZkrBgWM2SndOOWDNtgUUko