Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save initial signup information for users to aid in spam prevention #31852

Merged
merged 14 commits into from
Sep 9, 2024
3 changes: 3 additions & 0 deletions custom/conf/app.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,9 @@ INTERNAL_TOKEN =
;; stemming from cached/logged plain-text API tokens.
;; In future releases, this will become the default behavior
;DISABLE_QUERY_AUTH_TOKEN = false
;;
;; On user registration, record the IP address and user agent of the user to help identify potential abuse.
;; RECORD_INITIAL_IP = false
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down
21 changes: 21 additions & 0 deletions models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ type User struct {
// (ex: in private Gitea instances user won't be allowed to see even organizations/repositories that are set as public)
IsRestricted bool `xorm:"NOT NULL DEFAULT false"`

// Store the initial registration of the user, to aid in spam prevention
// Ensure that one IP isn't creating many accounts (following mediawiki approach)
InitialIP string `xorm:"-"`
InitialUserAgent string `xorm:"-"`
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved

AllowGitHook bool
AllowImportLocal bool // Allow migrate repository by local path
AllowCreateOrganization bool `xorm:"DEFAULT true"`
Expand Down Expand Up @@ -745,6 +750,22 @@ func createUser(ctx context.Context, u *User, createdByAdmin bool, overwriteDefa
return err
}

if setting.RecordInitialIP {
// insert initial IP and UserAgent
if err = SetUserSetting(ctx, u.ID, "initial_ip", u.InitialIP); err != nil {
return err
}

// trim user agent string to a reasonable length, if necessary
userAgent := strings.TrimSpace(u.InitialUserAgent)
if len(userAgent) > 255 {
userAgent = userAgent[:255]
}
if err = SetUserSetting(ctx, u.ID, "initial_user_agent", userAgent); err != nil {
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
return err
}
}

// insert email address
if err := db.Insert(ctx, &EmailAddress{
UID: u.ID,
Expand Down
3 changes: 3 additions & 0 deletions modules/setting/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var (
DisableQueryAuthToken bool
CSRFCookieName = "_csrf"
CSRFCookieHTTPOnly = true
RecordInitialIP = false
)

// loadSecret load the secret from ini by uriKey or verbatimKey, only one of them could be set
Expand Down Expand Up @@ -164,6 +165,8 @@ func loadSecurityFrom(rootCfg ConfigProvider) {
// TODO: default value should be true in future releases
DisableQueryAuthToken = sec.Key("DISABLE_QUERY_AUTH_TOKEN").MustBool(false)

RecordInitialIP = sec.Key("RECORD_INITIAL_IP").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.")
Expand Down
2 changes: 2 additions & 0 deletions routers/web/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,8 @@ func createAndHandleCreatedUser(ctx *context.Context, tpl base.TplName, form any
// createUserInContext creates a user and handles errors within a given context.
// Optionally a template can be specified.
func createUserInContext(ctx *context.Context, tpl base.TplName, form any, u *user_model.User, overwrites *user_model.CreateUserOverwriteOptions, gothUser *goth.User, allowLink bool) (ok bool) {
u.InitialIP = ctx.RemoteAddr()
u.InitialUserAgent = ctx.Req.UserAgent()
if err := user_model.CreateUser(ctx, u, overwrites); err != nil {
if allowLink && (user_model.IsErrUserAlreadyExist(err) || user_model.IsErrEmailAlreadyUsed(err)) {
if setting.OAuth2Client.AccountLinking == setting.OAuth2AccountLinkingAuto {
Expand Down
1 change: 1 addition & 0 deletions services/context/access_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func AccessLogger() func(http.Handler) http.Handler {
ResponseWriter: rw,
Ctx: map[string]any{
"RemoteAddr": req.RemoteAddr,
"UserAgent": req.UserAgent,
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
"RemoteHost": reqHost,
"Req": req,
},
Expand Down