fix: various security fixes (#38406)

Addresses a batch of privately reported security issues, grouped by
area:

- **SSRF** - migration PR-patch/asset fetches, OAuth2 avatar & OpenID
discovery, pull-mirror URL re-validation, and the outbound proxy path.
- **Access-token scope** - prevent scope escalation on token creation;
keep public-only tokens confined (feeds, packages, Actions listings,
star/watch lists, limited/private owners).
- **Access control / disclosure** - go-get default-branch leak, webhook
authorization-header leak, watch clearing on private transitions,
label/attachment scoping.
- **Denial of service** - input bounds for npm dist-tags, Debian control
files, Arch file lists, and SSH keys.

### 📌 Attention for site admins

Not breaking - existing configs keep working - but two changes are worth
a look:

- **New SSRF protection** Outbound requests (migrations, OAuth2 avatars,
OpenID discovery, pull mirrors, proxy path) are now validated against
the allow/block host lists. If your instance legitimately reaches
internal hosts, you may need to add them to
`[security].ALLOWED_HOST_LIST` (and the relevant `ALLOW_LOCALNETWORKS`
settings).
- **Deprecation** `[webhook].ALLOWED_HOST_LIST` is deprecated and will
be removed in a future release. Use `[security].ALLOWED_HOST_LIST`
instead; the old key still works for now.

---------

Co-authored-by: TheFox0x7 <thefox0x7@gmail.com>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: Zettat123 <zettat123@gmail.com>
This commit is contained in:
bircni
2026-07-12 19:14:09 +02:00
committed by GitHub
parent d2bd1589fe
commit f69e15afe7
93 changed files with 1715 additions and 138 deletions
+56
View File
@@ -30,6 +30,62 @@ func TestAPICreateAndDeleteToken(t *testing.T) {
deleteAPIAccessToken(t, newAccessToken, user)
}
// TestAPICreateTokenScopeEscalation ensures a token-authenticated request cannot
// mint a new token with a broader scope than the authenticating token.
func TestAPICreateTokenScopeEscalation(t *testing.T) {
defer tests.PrepareTestEnv(t)()
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
// a write:user-scoped token authenticates the create requests below
writeUserToken := getUserToken(t, user.Name, auth_model.AccessTokenScopeWriteUser)
// requesting a broader scope ("all") than the authenticating token is rejected
req := NewRequestWithJSON(t, "POST", "/api/v1/users/"+user.LoginName+"/tokens", map[string]any{
"name": "escalated",
"scopes": []string{"all"},
})
req.Request.SetBasicAuth(user.Name, writeUserToken)
MakeRequest(t, req, http.StatusForbidden)
// requesting a subset scope ("read:user") is allowed
req = NewRequestWithJSON(t, "POST", "/api/v1/users/"+user.LoginName+"/tokens", map[string]any{
"name": "subset",
"scopes": []string{"read:user"},
})
req.Request.SetBasicAuth(user.Name, writeUserToken)
MakeRequest(t, req, http.StatusCreated)
// password (non-token) auth may still create a token with any scope
req = NewRequestWithJSON(t, "POST", "/api/v1/users/"+user.LoginName+"/tokens", map[string]any{
"name": "by-password",
"scopes": []string{"all"},
}).AddBasicAuth(user.Name)
MakeRequest(t, req, http.StatusCreated)
// a public-only token must not mint a token that drops the public-only restriction
publicOnlyToken := getUserToken(t, user.Name, auth_model.AccessTokenScopeWriteUser, auth_model.AccessTokenScopePublicOnly)
req = NewRequestWithJSON(t, "POST", "/api/v1/users/"+user.LoginName+"/tokens", map[string]any{
"name": "still-public-only",
"scopes": []string{"write:user"},
})
req.Request.SetBasicAuth(user.Name, publicOnlyToken)
resp := MakeRequest(t, req, http.StatusCreated)
var createdToken api.AccessToken
DecodeJSON(t, resp, &createdToken)
assert.Contains(t, createdToken.Scopes, string(auth_model.AccessTokenScopePublicOnly))
// an unrestricted parent token may create a narrower public-only child: public-only is a restriction,
// not a grantable permission, so the subset check must not reject it
req = NewRequestWithJSON(t, "POST", "/api/v1/users/"+user.LoginName+"/tokens", map[string]any{
"name": "narrower-public-only",
"scopes": []string{"write:user", "public-only"},
})
req.Request.SetBasicAuth(user.Name, writeUserToken)
resp = MakeRequest(t, req, http.StatusCreated)
DecodeJSON(t, resp, &createdToken)
assert.Contains(t, createdToken.Scopes, string(auth_model.AccessTokenScopePublicOnly))
}
// TestAPIDeleteMissingToken ensures that error is thrown when token not found
func TestAPIDeleteMissingToken(t *testing.T) {
defer tests.PrepareTestEnv(t)()