Skip to content

Commit e97e883

Browse files
lunnyGiteaBot
andauthored
Add reverseproxy auth for API back with default disabled (#26703)
This feature was removed by #22219 to avoid possible CSRF attack. This PR takes reverseproxy auth for API back but with default disabled. To prevent possbile CSRF attack, the responsibility will be the reverseproxy but not Gitea itself. For those want to enable this `ENABLE_REVERSE_PROXY_AUTHENTICATION_API`, they should know what they are doing. --------- Co-authored-by: Giteabot <[email protected]>
1 parent 1221221 commit e97e883

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

custom/conf/app.example.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,8 @@ LEVEL = Info
759759
;;
760760
;; More detail: https://github.com/gogits/gogs/issues/165
761761
;ENABLE_REVERSE_PROXY_AUTHENTICATION = false
762+
; Enable this to allow reverse proxy authentication for API requests, the reverse proxy is responsible for ensuring that no CSRF is possible.
763+
;ENABLE_REVERSE_PROXY_AUTHENTICATION_API = false
762764
;ENABLE_REVERSE_PROXY_AUTO_REGISTRATION = false
763765
;ENABLE_REVERSE_PROXY_EMAIL = false
764766
;ENABLE_REVERSE_PROXY_FULL_NAME = false

docs/content/administration/config-cheat-sheet.en-us.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,8 @@ And the following unique queues:
621621
BASIC and the user's password. Please note if you disable this you will not be able to access the
622622
tokens API endpoints using a password. Further, this only disables BASIC authentication using the
623623
password - not tokens or OAuth Basic.
624-
- `ENABLE_REVERSE_PROXY_AUTHENTICATION`: **false**: Enable this to allow reverse proxy authentication.
624+
- `ENABLE_REVERSE_PROXY_AUTHENTICATION`: **false**: Enable this to allow reverse proxy authentication for web requests
625+
- `ENABLE_REVERSE_PROXY_AUTHENTICATION_API`: **false**: Enable this to allow reverse proxy authentication for API requests, the reverse proxy is responsible for ensuring that no CSRF is possible.
625626
- `ENABLE_REVERSE_PROXY_AUTO_REGISTRATION`: **false**: Enable this to allow auto-registration
626627
for reverse authentication.
627628
- `ENABLE_REVERSE_PROXY_EMAIL`: **false**: Enable this to allow to auto-registration with a

modules/setting/service.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ var Service = struct {
4646
EnableNotifyMail bool
4747
EnableBasicAuth bool
4848
EnableReverseProxyAuth bool
49+
EnableReverseProxyAuthAPI bool
4950
EnableReverseProxyAutoRegister bool
5051
EnableReverseProxyEmail bool
5152
EnableReverseProxyFullName bool
@@ -157,6 +158,7 @@ func loadServiceFrom(rootCfg ConfigProvider) {
157158
Service.RequireSignInView = sec.Key("REQUIRE_SIGNIN_VIEW").MustBool()
158159
Service.EnableBasicAuth = sec.Key("ENABLE_BASIC_AUTHENTICATION").MustBool(true)
159160
Service.EnableReverseProxyAuth = sec.Key("ENABLE_REVERSE_PROXY_AUTHENTICATION").MustBool()
161+
Service.EnableReverseProxyAuthAPI = sec.Key("ENABLE_REVERSE_PROXY_AUTHENTICATION_API").MustBool()
160162
Service.EnableReverseProxyAutoRegister = sec.Key("ENABLE_REVERSE_PROXY_AUTO_REGISTRATION").MustBool()
161163
Service.EnableReverseProxyEmail = sec.Key("ENABLE_REVERSE_PROXY_EMAIL").MustBool()
162164
Service.EnableReverseProxyFullName = sec.Key("ENABLE_REVERSE_PROXY_FULL_NAME").MustBool()

routers/api/v1/api.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,11 @@ func reqExploreSignIn() func(ctx *context.APIContext) {
333333
}
334334
}
335335

336-
func reqBasicAuth() func(ctx *context.APIContext) {
336+
func reqBasicOrRevProxyAuth() func(ctx *context.APIContext) {
337337
return func(ctx *context.APIContext) {
338+
if ctx.IsSigned && setting.Service.EnableReverseProxyAuthAPI && ctx.Data["AuthedMethod"].(string) == auth.ReverseProxyMethodName {
339+
return
340+
}
338341
if !ctx.IsBasicAuth {
339342
ctx.Error(http.StatusUnauthorized, "reqBasicAuth", "auth required")
340343
return
@@ -698,6 +701,9 @@ func buildAuthGroup() *auth.Group {
698701
&auth.HTTPSign{},
699702
&auth.Basic{}, // FIXME: this should be removed once we don't allow basic auth in API
700703
)
704+
if setting.Service.EnableReverseProxyAuthAPI {
705+
group.Add(&auth.ReverseProxy{})
706+
}
701707
specialAdd(group)
702708

703709
return group
@@ -800,7 +806,7 @@ func Routes() *web.Route {
800806
m.Combo("").Get(user.ListAccessTokens).
801807
Post(bind(api.CreateAccessTokenOption{}), reqToken(), user.CreateAccessToken)
802808
m.Combo("/{id}").Delete(reqToken(), user.DeleteAccessToken)
803-
}, reqBasicAuth())
809+
}, reqBasicOrRevProxyAuth())
804810

805811
m.Get("/activities/feeds", user.ListUserActivityFeeds)
806812
}, context_service.UserAssignmentAPI())

0 commit comments

Comments
 (0)