mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-24 12:16:37 +09:00
Adds audit actions for security-relevant changes that were not logged at all, and records a few remaining events on paths that skipped them: - Account login prohibition (`user:status:prohibitlogin`) and the organization setting that lets repository admins change team access - Actions runner registration tokens issued, runners removed or enabled/disabled, and Actions variables, for every scope - Instance config changes, including maintenance mode (`system:config:update`) - Deploy token regeneration and pull mirror address changes - Review deletion and dismissal, package removal, and release and tag removal - Repository restores via `gitea admin restore-repo`, OAuth2 secret rotation on API update, and SSH keys added or removed by authentication source sync The authentication source SSH key sync moved from the asymkey model to the asymkey service so it can record events. Follow-up to https://github.com/go-gitea/gitea/pull/40317 and https://github.com/go-gitea/gitea/pull/40322. --------- Co-authored-by: silverwind <me@silverwind.io>
196 lines
7.8 KiB
Go
196 lines
7.8 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package audit
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
audit_model "gitea.dev/models/audit"
|
|
issues_model "gitea.dev/models/issues"
|
|
packages_model "gitea.dev/models/packages"
|
|
repo_model "gitea.dev/models/repo"
|
|
user_model "gitea.dev/models/user"
|
|
"gitea.dev/modules/git"
|
|
"gitea.dev/modules/log"
|
|
notify_service "gitea.dev/services/notify"
|
|
)
|
|
|
|
func init() {
|
|
notify_service.RegisterNotifier(new(auditNotifier))
|
|
}
|
|
|
|
type auditNotifier struct {
|
|
notify_service.NullNotifier
|
|
}
|
|
|
|
var _ notify_service.Notifier = new(auditNotifier)
|
|
|
|
func (n *auditNotifier) CreateRepository(ctx context.Context, doer, _ *user_model.User, repo *repo_model.Repository) {
|
|
RecordAs(ctx, doer, audit_model.RepositoryCreate, repo)
|
|
}
|
|
|
|
func (n *auditNotifier) MigrateRepository(ctx context.Context, doer, _ *user_model.User, repo *repo_model.Repository) {
|
|
RecordAs(ctx, doer, audit_model.RepositoryCreate, repo)
|
|
}
|
|
|
|
func (n *auditNotifier) ForkRepository(ctx context.Context, doer *user_model.User, oldRepo, repo *repo_model.Repository) {
|
|
RecordAs(ctx, doer, audit_model.RepositoryCreateFork, repo, "base_repo", oldRepo.FullName())
|
|
}
|
|
|
|
func (n *auditNotifier) RenameRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldRepoName string) {
|
|
RecordAs(ctx, doer, audit_model.RepositoryName, repo, "previous_name", oldRepoName)
|
|
}
|
|
|
|
func (n *auditNotifier) TransferRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, oldOwner *user_model.User) {
|
|
RecordAs(ctx, doer, audit_model.RepositoryTransferFinish, repo, "old_owner", oldOwner.Name, "new_owner", repo.OwnerName)
|
|
}
|
|
|
|
func (n *auditNotifier) RepoPendingTransfer(ctx context.Context, doer, newOwner *user_model.User, repo *repo_model.Repository) {
|
|
RecordAs(ctx, doer, audit_model.RepositoryTransferStart, repo, "new_owner", newOwner.Name)
|
|
}
|
|
|
|
func (n *auditNotifier) ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository) {
|
|
Record(ctx, audit_model.RepositoryBranchDefault, repo, "default_branch", repo.DefaultBranch)
|
|
}
|
|
|
|
func issueLabel(issue *issues_model.Issue) string {
|
|
return fmt.Sprintf("#%d", issue.Index)
|
|
}
|
|
|
|
func loadIssueRepo(ctx context.Context, issue *issues_model.Issue) *repo_model.Repository {
|
|
if issue.Repo == nil {
|
|
if err := issue.LoadRepo(ctx); err != nil {
|
|
log.Error("audit: LoadRepo for issue %d: %v", issue.ID, err)
|
|
return nil
|
|
}
|
|
}
|
|
return issue.Repo
|
|
}
|
|
|
|
func loadPullRequestRepo(ctx context.Context, pr *issues_model.PullRequest) *repo_model.Repository {
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
|
log.Error("audit: LoadIssue for pull request %d: %v", pr.ID, err)
|
|
return nil
|
|
}
|
|
return loadIssueRepo(ctx, pr.Issue)
|
|
}
|
|
|
|
func issueOrPR(issue *issues_model.Issue, issueAction, prAction audit_model.Action) (audit_model.Action, string) {
|
|
if issue.IsPull {
|
|
return prAction, "pull_request"
|
|
}
|
|
return issueAction, "issue"
|
|
}
|
|
|
|
// issueMeta keeps the label, ID and title keys of every issue and pull request
|
|
// event consistent; the ID is always the issue row ID the label refers to.
|
|
func issueMeta(issue *issues_model.Issue, key string) []any {
|
|
return []any{key, issueLabel(issue), key + "_id", issue.ID, "title", issue.Title}
|
|
}
|
|
|
|
func (n *auditNotifier) NewIssue(ctx context.Context, issue *issues_model.Issue, _ []*user_model.User) {
|
|
repo := loadIssueRepo(ctx, issue)
|
|
if repo == nil {
|
|
return
|
|
}
|
|
RecordAs(ctx, issue.Poster, audit_model.IssueCreate, repo, issueMeta(issue, "issue")...)
|
|
}
|
|
|
|
func (n *auditNotifier) DeleteIssue(ctx context.Context, doer *user_model.User, issue *issues_model.Issue) {
|
|
repo := loadIssueRepo(ctx, issue)
|
|
if repo == nil {
|
|
return
|
|
}
|
|
action, key := issueOrPR(issue, audit_model.IssueDelete, audit_model.PullRequestDelete)
|
|
RecordAs(ctx, doer, action, repo, issueMeta(issue, key)...)
|
|
}
|
|
|
|
func (n *auditNotifier) NewPullRequest(ctx context.Context, pr *issues_model.PullRequest, _ []*user_model.User) {
|
|
repo := loadPullRequestRepo(ctx, pr)
|
|
if repo == nil {
|
|
return
|
|
}
|
|
RecordAs(ctx, pr.Issue.Poster, audit_model.PullRequestCreate, repo, issueMeta(pr.Issue, "pull_request")...)
|
|
}
|
|
|
|
func (n *auditNotifier) MergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
|
|
repo := loadPullRequestRepo(ctx, pr)
|
|
if repo == nil {
|
|
return
|
|
}
|
|
RecordAs(ctx, doer, audit_model.PullRequestMerge, repo, issueMeta(pr.Issue, "pull_request")...)
|
|
}
|
|
|
|
func (n *auditNotifier) AutoMergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
|
|
n.MergePullRequest(ctx, doer, pr)
|
|
}
|
|
|
|
var reviewTypeNames = map[issues_model.ReviewType]string{
|
|
issues_model.ReviewTypeApprove: "approve",
|
|
issues_model.ReviewTypeComment: "comment",
|
|
issues_model.ReviewTypeReject: "reject",
|
|
}
|
|
|
|
func (n *auditNotifier) PullRequestReview(ctx context.Context, pr *issues_model.PullRequest, review *issues_model.Review, comment *issues_model.Comment, _ []*user_model.User) {
|
|
if repo := loadPullRequestRepo(ctx, pr); repo != nil {
|
|
RecordAs(ctx, review.Reviewer, audit_model.PullRequestCommentCreate, repo, "pull_request", issueLabel(pr.Issue), "comment_id", comment.ID, "review_id", review.ID, "review_type", reviewTypeNames[review.Type])
|
|
}
|
|
}
|
|
|
|
func (n *auditNotifier) PullRequestCodeComment(ctx context.Context, pr *issues_model.PullRequest, comment *issues_model.Comment, _ []*user_model.User) {
|
|
if repo := loadPullRequestRepo(ctx, pr); repo != nil {
|
|
RecordAs(ctx, comment.Poster, audit_model.PullRequestCommentCreate, repo, "pull_request", issueLabel(pr.Issue), "comment_id", comment.ID)
|
|
}
|
|
}
|
|
|
|
func (n *auditNotifier) CreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, comment *issues_model.Comment, _ []*user_model.User) {
|
|
action, key := issueOrPR(issue, audit_model.IssueCommentCreate, audit_model.PullRequestCommentCreate)
|
|
RecordAs(ctx, doer, action, repo, key, issueLabel(issue), "comment_id", comment.ID)
|
|
}
|
|
|
|
func (n *auditNotifier) DeleteComment(ctx context.Context, doer *user_model.User, comment *issues_model.Comment) {
|
|
if comment.Issue == nil {
|
|
if err := comment.LoadIssue(ctx); err != nil {
|
|
log.Error("audit: LoadIssue for comment %d: %v", comment.ID, err)
|
|
return
|
|
}
|
|
}
|
|
repo := loadIssueRepo(ctx, comment.Issue)
|
|
if repo == nil {
|
|
return
|
|
}
|
|
action, key := issueOrPR(comment.Issue, audit_model.IssueCommentDelete, audit_model.PullRequestCommentDelete)
|
|
RecordAs(ctx, doer, action, repo, key, issueLabel(comment.Issue), "comment_id", comment.ID)
|
|
}
|
|
|
|
func (n *auditNotifier) NewWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, _ string) {
|
|
RecordAs(ctx, doer, audit_model.WikiPageCreate, repo, "page", page)
|
|
}
|
|
|
|
func (n *auditNotifier) EditWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, _ string) {
|
|
RecordAs(ctx, doer, audit_model.WikiPageUpdate, repo, "page", page)
|
|
}
|
|
|
|
func (n *auditNotifier) DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page string) {
|
|
RecordAs(ctx, doer, audit_model.WikiPageDelete, repo, "page", page)
|
|
}
|
|
|
|
func (n *auditNotifier) PullReviewDismiss(ctx context.Context, doer *user_model.User, review *issues_model.Review, _ *issues_model.Comment) {
|
|
if repo := loadIssueRepo(ctx, review.Issue); repo != nil {
|
|
RecordAs(ctx, doer, audit_model.PullRequestReviewDismiss, repo, "pull_request", issueLabel(review.Issue), "review_id", review.ID)
|
|
}
|
|
}
|
|
|
|
// DeleteRef only records tags, branch deletions are as routine as pushes.
|
|
func (n *auditNotifier) DeleteRef(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, refFullName git.RefName) {
|
|
if refFullName.IsTag() {
|
|
RecordAs(ctx, doer, audit_model.RepositoryTagRemove, repo, "tag", refFullName.TagName())
|
|
}
|
|
}
|
|
|
|
func (n *auditNotifier) PackageDelete(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) {
|
|
RecordScoped(WithDoer(ctx, doer), pd.Owner, nil, PackageRemove, "package", pd.Package.Name, "version", pd.Version.Version)
|
|
}
|