Skip to content

Commit f27ef19

Browse files
unknwonmpsonntag
authored andcommitted
admin: use POST to run operations (#5997)
* admin: use POST to run operations Fixed CSRF reported by Wenxu Wu of Tencent's Xuanwu Lab. * Update CHANGELOG
1 parent 2a6cc3d commit f27ef19

File tree

7 files changed

+100
-103
lines changed

7 files changed

+100
-103
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ All notable changes to Gogs are documented in this file.
4040
- [Security] Potential ability to delete files outside a repository.
4141
- [Security] Potential ability to set primary email on others' behalf from their verified emails.
4242
- [Security] Potential XSS attack via `.ipynb`. [#5170](https://github.com/gogs/gogs/issues/5170)
43+
- [Security] Potential CSRF attack in admin panel. [#5367](https://github.com/gogs/gogs/issues/5367)
4344
- [Security] Potential RCE on mirror repositories. [#5767](https://github.com/gogs/gogs/issues/5767)
4445
- [Security] Potential XSS attack with raw markdown API. [#5907](https://github.com/gogs/gogs/pull/5907)
4546
- Open/close milestone redirects to a 404 page. [#5677](https://github.com/gogs/gogs/issues/5677)

conf/locale/locale_en-US.ini

+1
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,7 @@ dashboard.system_status = System Monitor Status
10341034
dashboard.statistic_info = GIN database has <b>%d</b> users, <b>%d</b> organizations, <b>%d</b> public keys, <b>%d</b> repositories, <b>%d</b> watches, <b>%d</b> stars, <b>%d</b> actions, <b>%d</b> accesses, <b>%d</b> issues, <b>%d</b> comments, <b>%d</b> social accounts, <b>%d</b> follows, <b>%d</b> mirrors, <b>%d</b> releases, <b>%d</b> login sources, <b>%d</b> webhooks, <b>%d</b> milestones, <b>%d</b> labels, <b>%d</b> hook tasks, <b>%d</b> teams, <b>%d</b> update tasks, <b>%d</b> attachments.
10351035
dashboard.operation_name = Operation Name
10361036
dashboard.operation_switch = Switch
1037+
dashboard.select_operation_to_run = Please select operation to run
10371038
dashboard.operation_run = Run
10381039
dashboard.clean_unbind_oauth = Clean unbound OAuthes
10391040
dashboard.clean_unbind_oauth_success = All unbind OAuthes have been deleted successfully.

internal/assets/conf/conf_gen.go

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/assets/templates/templates_gen.go

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/cmd/web.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ func runWeb(c *cli.Context) error {
280280

281281
// ***** START: Admin *****
282282
m.Group("/admin", func() {
283-
m.Get("", admin.Dashboard)
283+
m.Combo("").Get(admin.Dashboard).Post(admin.Operation) // "/admin"
284284
m.Get("/config", admin.Config)
285285
m.Post("/config/test_mail", admin.SendTestMail)
286286
m.Get("/monitor", admin.Monitor)

internal/route/admin/admin.go

+49-58
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"time"
1212

1313
"github.com/json-iterator/go"
14-
"github.com/unknwon/com"
1514

1615
"github.com/G-Node/gogs/internal/conf"
1716
"github.com/G-Node/gogs/internal/context"
@@ -110,68 +109,11 @@ func updateSystemStatus() {
110109
sysStatus.NumGC = m.NumGC
111110
}
112111

113-
// Operation types.
114-
type AdminOperation int
115-
116-
const (
117-
CLEAN_INACTIVATE_USER AdminOperation = iota + 1
118-
CLEAN_REPO_ARCHIVES
119-
CLEAN_MISSING_REPOS
120-
GIT_GC_REPOS
121-
SYNC_SSH_AUTHORIZED_KEY
122-
SYNC_REPOSITORY_HOOKS
123-
REINIT_MISSING_REPOSITORY
124-
REBUILD_SEARCH_INDEX
125-
)
126-
127112
func Dashboard(c *context.Context) {
128113
c.Title("admin.dashboard")
129114
c.PageIs("Admin")
130115
c.PageIs("AdminDashboard")
131116

132-
// Run operation.
133-
op, _ := com.StrTo(c.Query("op")).Int()
134-
if op > 0 {
135-
var err error
136-
var success string
137-
138-
switch AdminOperation(op) {
139-
case CLEAN_INACTIVATE_USER:
140-
success = c.Tr("admin.dashboard.delete_inactivate_accounts_success")
141-
err = db.DeleteInactivateUsers()
142-
case CLEAN_REPO_ARCHIVES:
143-
success = c.Tr("admin.dashboard.delete_repo_archives_success")
144-
err = db.DeleteRepositoryArchives()
145-
case CLEAN_MISSING_REPOS:
146-
success = c.Tr("admin.dashboard.delete_missing_repos_success")
147-
err = db.DeleteMissingRepositories()
148-
case GIT_GC_REPOS:
149-
success = c.Tr("admin.dashboard.git_gc_repos_success")
150-
err = db.GitGcRepos()
151-
case SYNC_SSH_AUTHORIZED_KEY:
152-
success = c.Tr("admin.dashboard.resync_all_sshkeys_success")
153-
err = db.RewriteAuthorizedKeys()
154-
case SYNC_REPOSITORY_HOOKS:
155-
success = c.Tr("admin.dashboard.resync_all_hooks_success")
156-
err = db.SyncRepositoryHooks()
157-
case REINIT_MISSING_REPOSITORY:
158-
success = c.Tr("admin.dashboard.reinit_missing_repos_success")
159-
err = db.ReinitMissingRepositories()
160-
case REBUILD_SEARCH_INDEX:
161-
// TODO: Add success message to locale files
162-
success = "All repositories have been submitted to the indexing service successfully."
163-
err = db.RebuildIndex()
164-
}
165-
166-
if err != nil {
167-
c.Flash.Error(err.Error())
168-
} else {
169-
c.Flash.Success(success)
170-
}
171-
c.RedirectSubpath("/admin")
172-
return
173-
}
174-
175117
c.Data["GitVersion"] = conf.Git.Version
176118
c.Data["GoVersion"] = runtime.Version()
177119
c.Data["BuildTime"] = conf.BuildTime
@@ -184,6 +126,55 @@ func Dashboard(c *context.Context) {
184126
c.Success(DASHBOARD)
185127
}
186128

129+
// Operation types.
130+
type AdminOperation int
131+
132+
const (
133+
CleanInactivateUser AdminOperation = iota + 1
134+
CleanRepoArchives
135+
CleanMissingRepos
136+
GitGCRepos
137+
SyncSSHAuthorizedKey
138+
SyncRepositoryHooks
139+
ReinitMissingRepository
140+
)
141+
142+
func Operation(c *context.Context) {
143+
var err error
144+
var success string
145+
switch AdminOperation(c.QueryInt("op")) {
146+
case CleanInactivateUser:
147+
success = c.Tr("admin.dashboard.delete_inactivate_accounts_success")
148+
err = db.DeleteInactivateUsers()
149+
case CleanRepoArchives:
150+
success = c.Tr("admin.dashboard.delete_repo_archives_success")
151+
err = db.DeleteRepositoryArchives()
152+
case CleanMissingRepos:
153+
success = c.Tr("admin.dashboard.delete_missing_repos_success")
154+
err = db.DeleteMissingRepositories()
155+
case GitGCRepos:
156+
success = c.Tr("admin.dashboard.git_gc_repos_success")
157+
err = db.GitGcRepos()
158+
case SyncSSHAuthorizedKey:
159+
success = c.Tr("admin.dashboard.resync_all_sshkeys_success")
160+
err = db.RewriteAuthorizedKeys()
161+
case SyncRepositoryHooks:
162+
success = c.Tr("admin.dashboard.resync_all_hooks_success")
163+
err = db.SyncRepositoryHooks()
164+
case ReinitMissingRepository:
165+
success = c.Tr("admin.dashboard.reinit_missing_repos_success")
166+
err = db.ReinitMissingRepositories()
167+
}
168+
169+
if err != nil {
170+
c.Flash.Error(err.Error())
171+
} else {
172+
c.Flash.Success(success)
173+
}
174+
c.RedirectSubpath("/admin")
175+
return
176+
}
177+
187178
func SendTestMail(c *context.Context) {
188179
emailAddr := c.Query("email")
189180
// Send a test email to the user's email address and redirect back to Config

0 commit comments

Comments
 (0)