From 92c90d9e9af67beed5ba937c01a2d445e8f555c2 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Fri, 4 Sep 2020 11:59:32 -0500 Subject: [PATCH 1/7] Implement pwn Signed-off-by: jolheiser --- cmd/admin.go | 7 +++ custom/conf/app.example.ini | 4 +- .../doc/advanced/config-cheat-sheet.en-us.md | 1 + go.mod | 1 + go.sum | 2 + modules/password/password.go | 8 ++- modules/password/pwn.go | 27 ++++++++ modules/setting/setting.go | 2 + options/locale/locale_en-US.ini | 2 + routers/admin/users.go | 22 +++++++ routers/api/v1/admin/user.go | 19 +++++- routers/user/auth.go | 22 ++++++- routers/user/setting/account.go | 7 +++ vendor/go.jolheiser.com/pwn/.gitignore | 6 ++ vendor/go.jolheiser.com/pwn/LICENSE | 7 +++ vendor/go.jolheiser.com/pwn/Makefile | 18 ++++++ vendor/go.jolheiser.com/pwn/README.md | 13 ++++ vendor/go.jolheiser.com/pwn/error.go | 15 +++++ vendor/go.jolheiser.com/pwn/go.mod | 3 + vendor/go.jolheiser.com/pwn/password.go | 62 +++++++++++++++++++ vendor/go.jolheiser.com/pwn/pwn.go | 55 ++++++++++++++++ vendor/modules.txt | 3 + 22 files changed, 301 insertions(+), 5 deletions(-) create mode 100644 modules/password/pwn.go create mode 100644 vendor/go.jolheiser.com/pwn/.gitignore create mode 100644 vendor/go.jolheiser.com/pwn/LICENSE create mode 100644 vendor/go.jolheiser.com/pwn/Makefile create mode 100644 vendor/go.jolheiser.com/pwn/README.md create mode 100644 vendor/go.jolheiser.com/pwn/error.go create mode 100644 vendor/go.jolheiser.com/pwn/go.mod create mode 100644 vendor/go.jolheiser.com/pwn/password.go create mode 100644 vendor/go.jolheiser.com/pwn/pwn.go diff --git a/cmd/admin.go b/cmd/admin.go index a049f7f2cf17b..300359e8df796 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -265,6 +265,13 @@ func runChangePassword(c *cli.Context) error { if !pwd.IsComplexEnough(c.String("password")) { return errors.New("Password does not meet complexity requirements") } + pwned, err := pwd.IsPwned(c.String("password")) + if err != nil { + return err + } + if pwned { + return errors.New("Password has been pwned too many times") + } uname := c.String("username") user, err := models.GetUserByName(uname) if err != nil { diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index bb65c4f08dc9f..af3418f70c9b0 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -433,7 +433,7 @@ REPO_INDEXER_TYPE = bleve ; Index file used for code search. REPO_INDEXER_PATH = indexers/repos.bleve ; Code indexer connection string, available when `REPO_INDEXER_TYPE` is elasticsearch. i.e. http://elastic:changeme@localhost:9200 -REPO_INDEXER_CONN_STR = +REPO_INDEXER_CONN_STR = ; Code indexer name, available when `REPO_INDEXER_TYPE` is elasticsearch REPO_INDEXER_NAME = gitea_codes @@ -512,6 +512,8 @@ PASSWORD_COMPLEXITY = off PASSWORD_HASH_ALGO = argon2 ; Set false to allow JavaScript to read CSRF cookie CSRF_COOKIE_HTTP_ONLY = true +; Validate against https://haveibeenpwned.com/Passwords to see if a password has been exposed +PASSWORD_CHECK_PWN = false [openid] ; diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index f86415c2888a7..d7d68c7dd971a 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -333,6 +333,7 @@ set name for unique queues. Individual queues will default to - digit - use one or more digits - spec - use one or more special characters as ``!"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~`` - off - do not check password complexity +- `PASSWORD_CHECK_PWN`: **false**: Check [HaveIBeenPwned](https://haveibeenpwned.com/Passwords) to see if a password has been exposed. ## OpenID (`openid`) diff --git a/go.mod b/go.mod index ab5c376c6e5cc..845c1eb1479c5 100644 --- a/go.mod +++ b/go.mod @@ -98,6 +98,7 @@ require ( github.com/yuin/goldmark v1.2.1 github.com/yuin/goldmark-highlighting v0.0.0-20200307114337-60d527fdb691 github.com/yuin/goldmark-meta v0.0.0-20191126180153-f0638e958b60 + go.jolheiser.com/pwn v0.0.2 golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a golang.org/x/net v0.0.0-20200707034311-ab3426394381 golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d diff --git a/go.sum b/go.sum index 08a1d20ffd191..4d68cf4a1a305 100644 --- a/go.sum +++ b/go.sum @@ -914,6 +914,8 @@ github.com/ziutek/mymysql v1.5.4/go.mod h1:LMSpPZ6DbqWFxNCHW77HeMg9I646SAhApZ/wK go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.etcd.io/bbolt v1.3.4 h1:hi1bXHMVrlQh6WwxAy+qZCV/SYIlqo+Ushwdpa4tAKg= go.etcd.io/bbolt v1.3.4/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ= +go.jolheiser.com/pwn v0.0.2 h1:8znQ0TYCudf9PWmYu7kaGXzcEwjPTMAMXzjcR6ySfNg= +go.jolheiser.com/pwn v0.0.2/go.mod h1:/j5Dl8ftNqqJ8Dlx3YTrJV1wIR2lWOTyrNU3Qe7rk6I= go.mongodb.org/mongo-driver v1.0.3/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM= go.mongodb.org/mongo-driver v1.1.1 h1:Sq1fR+0c58RME5EoqKdjkiQAmPjmfHlZOoRI6fTUOcs= go.mongodb.org/mongo-driver v1.1.1/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM= diff --git a/modules/password/password.go b/modules/password/password.go index 1c4b9c514a459..1416cc9397213 100644 --- a/modules/password/password.go +++ b/modules/password/password.go @@ -88,7 +88,7 @@ func IsComplexEnough(pwd string) bool { return true } -// Generate a random password +// Generate a random password func Generate(n int) (string, error) { NewComplexity() buffer := make([]byte, n) @@ -101,7 +101,11 @@ func Generate(n int) (string, error) { } buffer[j] = validChars[rnd.Int64()] } - if IsComplexEnough(string(buffer)) && string(buffer[0]) != " " && string(buffer[n-1]) != " " { + pwned, err := IsPwned(string(buffer)) + if err != nil { + return "", err + } + if IsComplexEnough(string(buffer)) && !pwned && string(buffer[0]) != " " && string(buffer[n-1]) != " " { return string(buffer), nil } } diff --git a/modules/password/pwn.go b/modules/password/pwn.go new file mode 100644 index 0000000000000..626d01247ccac --- /dev/null +++ b/modules/password/pwn.go @@ -0,0 +1,27 @@ +// Copyright 2020 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package password + +import ( + "code.gitea.io/gitea/modules/setting" + + "go.jolheiser.com/pwn" +) + +// IsPwned checks whether a password has been pwned too many times +// according to threshold +func IsPwned(password string) (bool, error) { + if !setting.PasswordCheckPwn { + return false, nil + } + + client := pwn.New() + count, err := client.CheckPassword(password, true) + if err != nil { + return true, err + } + + return count > 0, nil +} diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 5b8aefdaa4caf..7bc2d6b1b6cf0 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -158,6 +158,7 @@ var ( OnlyAllowPushIfGiteaEnvironmentSet bool PasswordComplexity []string PasswordHashAlgo string + PasswordCheckPwn bool // UI settings UI = struct { @@ -821,6 +822,7 @@ func NewContext() { OnlyAllowPushIfGiteaEnvironmentSet = sec.Key("ONLY_ALLOW_PUSH_IF_GITEA_ENVIRONMENT_SET").MustBool(true) PasswordHashAlgo = sec.Key("PASSWORD_HASH_ALGO").MustString("argon2") CSRFCookieHTTPOnly = sec.Key("CSRF_COOKIE_HTTP_ONLY").MustBool(true) + PasswordCheckPwn = sec.Key("PASSWORD_CHECK_PWN").MustBool(false) InternalToken = loadInternalToken(sec) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 94d7ab27fb1b5..7d7d2c75205b5 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -300,6 +300,8 @@ authorization_failed = Authorization failed authorization_failed_desc = The authorization failed because we detected an invalid request. Please contact the maintainer of the app you've tried to authorize. disable_forgot_password_mail = Account recovery is disabled. Please contact your site administrator. sspi_auth_failed = SSPI authentication failed +password_pwned = Password has been pwned too many times +password_pwned_err = Could not complete request to HaveIBeenPwned [mail] activate_account = Please activate your account diff --git a/routers/admin/users.go b/routers/admin/users.go index a28db2b445312..ec770ea276171 100644 --- a/routers/admin/users.go +++ b/routers/admin/users.go @@ -108,6 +108,17 @@ func NewUserPost(ctx *context.Context, form auth.AdminCreateUserForm) { ctx.RenderWithErr(password.BuildComplexityError(ctx), tplUserNew, &form) return } + pwned, err := password.IsPwned(form.Password) + if pwned { + ctx.Data["Err_Password"] = true + errMsg := ctx.Tr("auth.password_pwned") + if err != nil { + log.Error(err.Error()) + errMsg = ctx.Tr("auth.password_pwned_err") + } + ctx.RenderWithErr(errMsg, tplUserNew, &form) + return + } u.MustChangePassword = form.MustChangePassword } if err := models.CreateUser(u); err != nil { @@ -224,6 +235,17 @@ func EditUserPost(ctx *context.Context, form auth.AdminEditUserForm) { ctx.RenderWithErr(password.BuildComplexityError(ctx), tplUserEdit, &form) return } + pwned, err := password.IsPwned(form.Password) + if pwned { + ctx.Data["Err_Password"] = true + errMsg := ctx.Tr("auth.password_pwned") + if err != nil { + log.Error(err.Error()) + errMsg = ctx.Tr("auth.password_pwned_err") + } + ctx.RenderWithErr(errMsg, tplUserNew, &form) + return + } if u.Salt, err = models.GetUserSalt(); err != nil { ctx.ServerError("UpdateUser", err) return diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go index a8a573eaf0759..05da1ff8a57e3 100644 --- a/routers/api/v1/admin/user.go +++ b/routers/api/v1/admin/user.go @@ -87,6 +87,15 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) { ctx.Error(http.StatusBadRequest, "PasswordComplexity", err) return } + pwned, err := password.IsPwned(form.Password) + if err != nil { + log.Error(err.Error()) + } + if pwned { + ctx.Data["Err_Password"] = true + ctx.Error(http.StatusBadRequest, "PasswordPwned", errors.New("PasswordPwned")) + return + } if err := models.CreateUser(u); err != nil { if models.IsErrUserAlreadyExist(err) || models.IsErrEmailAlreadyUsed(err) || @@ -151,7 +160,15 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) { ctx.Error(http.StatusBadRequest, "PasswordComplexity", err) return } - var err error + pwned, err := password.IsPwned(form.Password) + if err != nil { + log.Error(err.Error()) + } + if pwned { + ctx.Data["Err_Password"] = true + ctx.Error(http.StatusBadRequest, "PasswordPwned", errors.New("PasswordPwned")) + return + } if u.Salt, err = models.GetUserSalt(); err != nil { ctx.Error(http.StatusInternalServerError, "UpdateUser", err) return diff --git a/routers/user/auth.go b/routers/user/auth.go index 4e6ac9c87f88b..4af4481d62d64 100644 --- a/routers/user/auth.go +++ b/routers/user/auth.go @@ -1110,6 +1110,17 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo ctx.RenderWithErr(password.BuildComplexityError(ctx), tplSignUp, &form) return } + pwned, err := password.IsPwned(form.Password) + if pwned { + errMsg := ctx.Tr("auth.password_pwned") + if err != nil { + log.Error(err.Error()) + errMsg = ctx.Tr("auth.password_pwned_err") + } + ctx.Data["Err_Password"] = true + ctx.RenderWithErr(errMsg, tplSignUp, &form) + return + } u := &models.User{ Name: form.UserName, @@ -1409,6 +1420,16 @@ func ResetPasswdPost(ctx *context.Context) { ctx.Data["Err_Password"] = true ctx.RenderWithErr(password.BuildComplexityError(ctx), tplResetPassword, nil) return + } else if pwned, err := password.IsPwned(passwd); pwned || err != nil { + errMsg := ctx.Tr("auth.password_pwned") + if err != nil { + log.Error(err.Error()) + errMsg = ctx.Tr("auth.password_pwned_err") + } + ctx.Data["IsResetForm"] = true + ctx.Data["Err_Password"] = true + ctx.RenderWithErr(errMsg, tplResetPassword, nil) + return } // Handle two-factor @@ -1443,7 +1464,6 @@ func ResetPasswdPost(ctx *context.Context) { } } } - var err error if u.Rands, err = models.GetUserSalt(); err != nil { ctx.ServerError("UpdateUser", err) diff --git a/routers/user/setting/account.go b/routers/user/setting/account.go index 27f0bf1c86b41..9aba50514724d 100644 --- a/routers/user/setting/account.go +++ b/routers/user/setting/account.go @@ -54,6 +54,13 @@ func AccountPost(ctx *context.Context, form auth.ChangePasswordForm) { ctx.Flash.Error(ctx.Tr("form.password_not_match")) } else if !password.IsComplexEnough(form.Password) { ctx.Flash.Error(password.BuildComplexityError(ctx)) + } else if pwned, err := password.IsPwned(form.Password); pwned || err != nil { + errMsg := ctx.Tr("auth.password_pwned") + if err != nil { + log.Error(err.Error()) + errMsg = ctx.Tr("auth.password_pwned_err") + } + ctx.Flash.Error(errMsg) } else { var err error if ctx.User.Salt, err = models.GetUserSalt(); err != nil { diff --git a/vendor/go.jolheiser.com/pwn/.gitignore b/vendor/go.jolheiser.com/pwn/.gitignore new file mode 100644 index 0000000000000..b73cf3849a614 --- /dev/null +++ b/vendor/go.jolheiser.com/pwn/.gitignore @@ -0,0 +1,6 @@ +# GoLand +.idea/ + +# Binaries +/pwn +/pwn.exe \ No newline at end of file diff --git a/vendor/go.jolheiser.com/pwn/LICENSE b/vendor/go.jolheiser.com/pwn/LICENSE new file mode 100644 index 0000000000000..0f02641b29ecf --- /dev/null +++ b/vendor/go.jolheiser.com/pwn/LICENSE @@ -0,0 +1,7 @@ +Copyright 2020 John Olheiser + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/vendor/go.jolheiser.com/pwn/Makefile b/vendor/go.jolheiser.com/pwn/Makefile new file mode 100644 index 0000000000000..7f5474401164f --- /dev/null +++ b/vendor/go.jolheiser.com/pwn/Makefile @@ -0,0 +1,18 @@ +GO ?= go +VERSION ?= $(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//') + +.PHONY: fmt +fmt: + $(GO) fmt ./... + +.PHONY: test +test: + $(GO) test -race ./... + +.PHONY: vet +vet: + $(GO) vet ./... + +.PHONY: build +build: + $(GO) build -ldflags '-s -w -X "main.Version=$(VERSION)"' go.jolheiser.com/pwn/cmd/pwn \ No newline at end of file diff --git a/vendor/go.jolheiser.com/pwn/README.md b/vendor/go.jolheiser.com/pwn/README.md new file mode 100644 index 0000000000000..a3fbe46612f11 --- /dev/null +++ b/vendor/go.jolheiser.com/pwn/README.md @@ -0,0 +1,13 @@ +# Have I Been Pwned + +Go library for interacting with [haveibeenpwned](https://haveibeenpwned.com/). + +Implemented: + +* [ ] Breaches +* [ ] Pastes +* [x] Passwords + +## License + +[MIT](LICENSE) \ No newline at end of file diff --git a/vendor/go.jolheiser.com/pwn/error.go b/vendor/go.jolheiser.com/pwn/error.go new file mode 100644 index 0000000000000..2c73ed5cee679 --- /dev/null +++ b/vendor/go.jolheiser.com/pwn/error.go @@ -0,0 +1,15 @@ +package pwn + +// ErrEmptyPassword is an empty password error +type ErrEmptyPassword struct{} + +// Error fulfills the error interface +func (e ErrEmptyPassword) Error() string { + return "password cannot be empty" +} + +// IsErrEmptyPassword checks if an error is ErrEmptyPassword +func IsErrEmptyPassword(err error) bool { + _, ok := err.(ErrEmptyPassword) + return ok +} diff --git a/vendor/go.jolheiser.com/pwn/go.mod b/vendor/go.jolheiser.com/pwn/go.mod new file mode 100644 index 0000000000000..46b73c8e5c7eb --- /dev/null +++ b/vendor/go.jolheiser.com/pwn/go.mod @@ -0,0 +1,3 @@ +module go.jolheiser.com/pwn + +go 1.15 diff --git a/vendor/go.jolheiser.com/pwn/password.go b/vendor/go.jolheiser.com/pwn/password.go new file mode 100644 index 0000000000000..848ac49188bbf --- /dev/null +++ b/vendor/go.jolheiser.com/pwn/password.go @@ -0,0 +1,62 @@ +package pwn + +import ( + "crypto/sha1" + "encoding/hex" + "fmt" + "io/ioutil" + "net/http" + "strconv" + "strings" +) + +const passwordURL = "https://api.pwnedpasswords.com/range/" + +// CheckPassword returns the number of times a password has been compromised +// Adding padding will make requests more secure, however is also slower +// because artificial responses will be added to the response +// For more information, see https://www.troyhunt.com/enhancing-pwned-passwords-privacy-with-padding/ +func (c *Client) CheckPassword(pw string, padding bool) (int, error) { + if strings.TrimSpace(pw) == "" { + return -1, ErrEmptyPassword{} + } + + sha := sha1.New() + sha.Write([]byte(pw)) + enc := hex.EncodeToString(sha.Sum(nil)) + prefix, suffix := enc[:5], enc[5:] + + req, err := newRequest(c.ctx, http.MethodGet, fmt.Sprintf("%s%s", passwordURL, prefix), nil) + if err != nil { + return -1, nil + } + if padding { + req.Header.Add("Add-Padding", "true") + } + + resp, err := c.http.Do(req) + if err != nil { + return -1, err + } + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return -1, err + } + defer resp.Body.Close() + + for _, pair := range strings.Split(string(body), "\n") { + parts := strings.Split(pair, ":") + if len(parts) != 2 { + continue + } + if strings.EqualFold(suffix, parts[0]) { + count, err := strconv.ParseInt(strings.TrimSpace(parts[1]), 10, 64) + if err != nil { + return -1, err + } + return int(count), nil + } + } + return 0, nil +} diff --git a/vendor/go.jolheiser.com/pwn/pwn.go b/vendor/go.jolheiser.com/pwn/pwn.go new file mode 100644 index 0000000000000..0b17a93514e32 --- /dev/null +++ b/vendor/go.jolheiser.com/pwn/pwn.go @@ -0,0 +1,55 @@ +package pwn + +import ( + "context" + "io" + "net/http" +) + +const userAgent = "Go-Pwn" + +// Client is a HaveIBeenPwned client +type Client struct { + ctx context.Context + http *http.Client +} + +// New returns a new HaveIBeenPwned Client +func New(options ...ClientOption) *Client { + client := &Client{ + ctx: context.Background(), + http: http.DefaultClient, + } + + for _, opt := range options { + opt(client) + } + + return client +} + +// ClientOption is a way to modify a new Client +type ClientOption func(*Client) + +// WithHTTP will set the http.Client of a Client +func WithHTTP(httpClient *http.Client) func(pwnClient *Client) { + return func(pwnClient *Client) { + pwnClient.http = httpClient + } +} + +// WithContext will set the context.Context of a Client +func WithContext(ctx context.Context) func(pwnClient *Client) { + return func(pwnClient *Client) { + pwnClient.ctx = ctx + } +} + +func newRequest(ctx context.Context, method, url string, body io.ReadCloser) (*http.Request, error) { + req, err := http.NewRequestWithContext(ctx, method, url, body) + if err != nil { + return nil, err + } + req.Header.Add("User-Agent", userAgent) + return req, nil +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 4a5634246343d..4aa927af06c78 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -756,6 +756,9 @@ github.com/yuin/goldmark-highlighting github.com/yuin/goldmark-meta # go.etcd.io/bbolt v1.3.4 go.etcd.io/bbolt +# go.jolheiser.com/pwn v0.0.2 +## explicit +go.jolheiser.com/pwn # go.mongodb.org/mongo-driver v1.3.5 go.mongodb.org/mongo-driver/bson go.mongodb.org/mongo-driver/bson/bsoncodec From 5da95e6b7b33958478afab069df28f84ab1f2f17 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Fri, 4 Sep 2020 14:54:47 -0500 Subject: [PATCH 2/7] Update module Signed-off-by: jolheiser --- go.mod | 2 +- go.sum | 4 ++-- vendor/go.jolheiser.com/pwn/README.md | 2 +- vendor/go.jolheiser.com/pwn/pwn.go | 5 ++++- vendor/modules.txt | 2 +- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 845c1eb1479c5..f78dbed95b0f0 100644 --- a/go.mod +++ b/go.mod @@ -98,7 +98,7 @@ require ( github.com/yuin/goldmark v1.2.1 github.com/yuin/goldmark-highlighting v0.0.0-20200307114337-60d527fdb691 github.com/yuin/goldmark-meta v0.0.0-20191126180153-f0638e958b60 - go.jolheiser.com/pwn v0.0.2 + go.jolheiser.com/pwn v0.0.3 golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a golang.org/x/net v0.0.0-20200707034311-ab3426394381 golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d diff --git a/go.sum b/go.sum index 4d68cf4a1a305..7f715d87fbb1f 100644 --- a/go.sum +++ b/go.sum @@ -914,8 +914,8 @@ github.com/ziutek/mymysql v1.5.4/go.mod h1:LMSpPZ6DbqWFxNCHW77HeMg9I646SAhApZ/wK go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.etcd.io/bbolt v1.3.4 h1:hi1bXHMVrlQh6WwxAy+qZCV/SYIlqo+Ushwdpa4tAKg= go.etcd.io/bbolt v1.3.4/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ= -go.jolheiser.com/pwn v0.0.2 h1:8znQ0TYCudf9PWmYu7kaGXzcEwjPTMAMXzjcR6ySfNg= -go.jolheiser.com/pwn v0.0.2/go.mod h1:/j5Dl8ftNqqJ8Dlx3YTrJV1wIR2lWOTyrNU3Qe7rk6I= +go.jolheiser.com/pwn v0.0.3 h1:MQowb3QvCL5r5NmHmCPxw93SdjfgJ0q6rAwYn4i1Hjg= +go.jolheiser.com/pwn v0.0.3/go.mod h1:/j5Dl8ftNqqJ8Dlx3YTrJV1wIR2lWOTyrNU3Qe7rk6I= go.mongodb.org/mongo-driver v1.0.3/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM= go.mongodb.org/mongo-driver v1.1.1 h1:Sq1fR+0c58RME5EoqKdjkiQAmPjmfHlZOoRI6fTUOcs= go.mongodb.org/mongo-driver v1.1.1/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM= diff --git a/vendor/go.jolheiser.com/pwn/README.md b/vendor/go.jolheiser.com/pwn/README.md index a3fbe46612f11..9b3c37eb23f9e 100644 --- a/vendor/go.jolheiser.com/pwn/README.md +++ b/vendor/go.jolheiser.com/pwn/README.md @@ -1,6 +1,6 @@ # Have I Been Pwned -Go library for interacting with [haveibeenpwned](https://haveibeenpwned.com/). +Go library for interacting with [HaveIBeenPwned](https://haveibeenpwned.com/). Implemented: diff --git a/vendor/go.jolheiser.com/pwn/pwn.go b/vendor/go.jolheiser.com/pwn/pwn.go index 0b17a93514e32..399284b487fa0 100644 --- a/vendor/go.jolheiser.com/pwn/pwn.go +++ b/vendor/go.jolheiser.com/pwn/pwn.go @@ -6,7 +6,10 @@ import ( "net/http" ) -const userAgent = "Go-Pwn" +const ( + libVersion = "0.0.3" + userAgent = "go.jolheiser.com/pwn v" + libVersion +) // Client is a HaveIBeenPwned client type Client struct { diff --git a/vendor/modules.txt b/vendor/modules.txt index 4aa927af06c78..830a7dfbb7911 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -756,7 +756,7 @@ github.com/yuin/goldmark-highlighting github.com/yuin/goldmark-meta # go.etcd.io/bbolt v1.3.4 go.etcd.io/bbolt -# go.jolheiser.com/pwn v0.0.2 +# go.jolheiser.com/pwn v0.0.3 ## explicit go.jolheiser.com/pwn # go.mongodb.org/mongo-driver v1.3.5 From cf29f8a8bd1eee6735c71bb4149b1f94f8c710b7 Mon Sep 17 00:00:00 2001 From: John Olheiser Date: Fri, 4 Sep 2020 15:24:56 -0500 Subject: [PATCH 3/7] Apply suggestions mrsdizzie Co-authored-by: mrsdizzie --- cmd/admin.go | 2 +- options/locale/locale_en-US.ini | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/admin.go b/cmd/admin.go index 300359e8df796..0b22df7a81e21 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -270,7 +270,7 @@ func runChangePassword(c *cli.Context) error { return err } if pwned { - return errors.New("Password has been pwned too many times") + return errors.New("The password you chose is on a list of stolen passwords previously exposed in public data breaches. Please try again with a different password.") } uname := c.String("username") user, err := models.GetUserByName(uname) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 7d7d2c75205b5..02e761602e903 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -300,7 +300,7 @@ authorization_failed = Authorization failed authorization_failed_desc = The authorization failed because we detected an invalid request. Please contact the maintainer of the app you've tried to authorize. disable_forgot_password_mail = Account recovery is disabled. Please contact your site administrator. sspi_auth_failed = SSPI authentication failed -password_pwned = Password has been pwned too many times +password_pwned = The password you chose is on a list of stolen passwords previously exposed in public data breaches. Please try again with a different password. password_pwned_err = Could not complete request to HaveIBeenPwned [mail] From 3b55f893d7938b76d0f27c4c0d8765d20ded96e7 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Fri, 4 Sep 2020 15:29:36 -0500 Subject: [PATCH 4/7] Add link to HIBP Signed-off-by: jolheiser --- options/locale/locale_en-US.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index a517b25f963aa..89014d5daf538 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -300,7 +300,7 @@ authorization_failed = Authorization failed authorization_failed_desc = The authorization failed because we detected an invalid request. Please contact the maintainer of the app you've tried to authorize. disable_forgot_password_mail = Account recovery is disabled. Please contact your site administrator. sspi_auth_failed = SSPI authentication failed -password_pwned = The password you chose is on a list of stolen passwords previously exposed in public data breaches. Please try again with a different password. +password_pwned = The password you chose is on a list of stolen passwords previously exposed in public data breaches. Please try again with a different password. password_pwned_err = Could not complete request to HaveIBeenPwned [mail] From d71d45d8ff41c4ae64916af9849d7ff97cfd2ba9 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Fri, 4 Sep 2020 15:31:56 -0500 Subject: [PATCH 5/7] Add more details to admin command Signed-off-by: jolheiser --- cmd/admin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/admin.go b/cmd/admin.go index 0b22df7a81e21..69d1a0f01257e 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -270,7 +270,7 @@ func runChangePassword(c *cli.Context) error { return err } if pwned { - return errors.New("The password you chose is on a list of stolen passwords previously exposed in public data breaches. Please try again with a different password.") + return errors.New("The password you chose is on a list of stolen passwords previously exposed in public data breaches. Please try again with a different password.\nFor more details, see https://haveibeenpwned.com/Passwords") } uname := c.String("username") user, err := models.GetUserByName(uname) From 7fd20634dc16ffbc4b38ecce7b073a5621df52f7 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Fri, 4 Sep 2020 20:25:30 -0500 Subject: [PATCH 6/7] Add context to pwn Signed-off-by: jolheiser --- cmd/admin.go | 3 ++- modules/password/password.go | 3 ++- modules/password/pwn.go | 6 ++++-- routers/admin/users.go | 4 ++-- routers/api/v1/admin/user.go | 4 ++-- routers/user/auth.go | 4 ++-- routers/user/setting/account.go | 2 +- 7 files changed, 15 insertions(+), 11 deletions(-) diff --git a/cmd/admin.go b/cmd/admin.go index 69d1a0f01257e..9f81f5284dd6d 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -6,6 +6,7 @@ package cmd import ( + "context" "errors" "fmt" "os" @@ -265,7 +266,7 @@ func runChangePassword(c *cli.Context) error { if !pwd.IsComplexEnough(c.String("password")) { return errors.New("Password does not meet complexity requirements") } - pwned, err := pwd.IsPwned(c.String("password")) + pwned, err := pwd.IsPwned(context.Background(), c.String("password")) if err != nil { return err } diff --git a/modules/password/password.go b/modules/password/password.go index 1416cc9397213..e1f1f769ec73e 100644 --- a/modules/password/password.go +++ b/modules/password/password.go @@ -6,6 +6,7 @@ package password import ( "bytes" + goContext "context" "crypto/rand" "math/big" "strings" @@ -101,7 +102,7 @@ func Generate(n int) (string, error) { } buffer[j] = validChars[rnd.Int64()] } - pwned, err := IsPwned(string(buffer)) + pwned, err := IsPwned(goContext.Background(), string(buffer)) if err != nil { return "", err } diff --git a/modules/password/pwn.go b/modules/password/pwn.go index 626d01247ccac..67118912f8e49 100644 --- a/modules/password/pwn.go +++ b/modules/password/pwn.go @@ -5,6 +5,8 @@ package password import ( + "context" + "code.gitea.io/gitea/modules/setting" "go.jolheiser.com/pwn" @@ -12,12 +14,12 @@ import ( // IsPwned checks whether a password has been pwned too many times // according to threshold -func IsPwned(password string) (bool, error) { +func IsPwned(ctx context.Context, password string) (bool, error) { if !setting.PasswordCheckPwn { return false, nil } - client := pwn.New() + client := pwn.New(pwn.WithContext(ctx)) count, err := client.CheckPassword(password, true) if err != nil { return true, err diff --git a/routers/admin/users.go b/routers/admin/users.go index ec770ea276171..531f81b8b51e4 100644 --- a/routers/admin/users.go +++ b/routers/admin/users.go @@ -108,7 +108,7 @@ func NewUserPost(ctx *context.Context, form auth.AdminCreateUserForm) { ctx.RenderWithErr(password.BuildComplexityError(ctx), tplUserNew, &form) return } - pwned, err := password.IsPwned(form.Password) + pwned, err := password.IsPwned(ctx.Req.Context(), form.Password) if pwned { ctx.Data["Err_Password"] = true errMsg := ctx.Tr("auth.password_pwned") @@ -235,7 +235,7 @@ func EditUserPost(ctx *context.Context, form auth.AdminEditUserForm) { ctx.RenderWithErr(password.BuildComplexityError(ctx), tplUserEdit, &form) return } - pwned, err := password.IsPwned(form.Password) + pwned, err := password.IsPwned(ctx.Req.Context(), form.Password) if pwned { ctx.Data["Err_Password"] = true errMsg := ctx.Tr("auth.password_pwned") diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go index 05da1ff8a57e3..0b9e9f9d4ff33 100644 --- a/routers/api/v1/admin/user.go +++ b/routers/api/v1/admin/user.go @@ -87,7 +87,7 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) { ctx.Error(http.StatusBadRequest, "PasswordComplexity", err) return } - pwned, err := password.IsPwned(form.Password) + pwned, err := password.IsPwned(ctx.Req.Context(), form.Password) if err != nil { log.Error(err.Error()) } @@ -160,7 +160,7 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) { ctx.Error(http.StatusBadRequest, "PasswordComplexity", err) return } - pwned, err := password.IsPwned(form.Password) + pwned, err := password.IsPwned(ctx.Req.Context(), form.Password) if err != nil { log.Error(err.Error()) } diff --git a/routers/user/auth.go b/routers/user/auth.go index 4af4481d62d64..96a73c9dd4632 100644 --- a/routers/user/auth.go +++ b/routers/user/auth.go @@ -1110,7 +1110,7 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo ctx.RenderWithErr(password.BuildComplexityError(ctx), tplSignUp, &form) return } - pwned, err := password.IsPwned(form.Password) + pwned, err := password.IsPwned(ctx.Req.Context(), form.Password) if pwned { errMsg := ctx.Tr("auth.password_pwned") if err != nil { @@ -1420,7 +1420,7 @@ func ResetPasswdPost(ctx *context.Context) { ctx.Data["Err_Password"] = true ctx.RenderWithErr(password.BuildComplexityError(ctx), tplResetPassword, nil) return - } else if pwned, err := password.IsPwned(passwd); pwned || err != nil { + } else if pwned, err := password.IsPwned(ctx.Req.Context(), passwd); pwned || err != nil { errMsg := ctx.Tr("auth.password_pwned") if err != nil { log.Error(err.Error()) diff --git a/routers/user/setting/account.go b/routers/user/setting/account.go index 9aba50514724d..99e20177bc986 100644 --- a/routers/user/setting/account.go +++ b/routers/user/setting/account.go @@ -54,7 +54,7 @@ func AccountPost(ctx *context.Context, form auth.ChangePasswordForm) { ctx.Flash.Error(ctx.Tr("form.password_not_match")) } else if !password.IsComplexEnough(form.Password) { ctx.Flash.Error(password.BuildComplexityError(ctx)) - } else if pwned, err := password.IsPwned(form.Password); pwned || err != nil { + } else if pwned, err := password.IsPwned(ctx.Req.Context(), form.Password); pwned || err != nil { errMsg := ctx.Tr("auth.password_pwned") if err != nil { log.Error(err.Error()) From b7bb54cb46523752579738d03a31620bd1656198 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Fri, 4 Sep 2020 20:29:17 -0500 Subject: [PATCH 7/7] Consistency and making some noise ;) Signed-off-by: jolheiser --- modules/password/pwn.go | 5 +++-- routers/api/v1/admin/user.go | 12 ++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/modules/password/pwn.go b/modules/password/pwn.go index 67118912f8e49..938524e6dee28 100644 --- a/modules/password/pwn.go +++ b/modules/password/pwn.go @@ -12,8 +12,9 @@ import ( "go.jolheiser.com/pwn" ) -// IsPwned checks whether a password has been pwned too many times -// according to threshold +// IsPwned checks whether a password has been pwned +// NOTE: This func returns true if it encounters an error under the assumption that you ALWAYS want to check against +// HIBP, so not getting a response should block a password until it can be verified. func IsPwned(ctx context.Context, password string) (bool, error) { if !setting.PasswordCheckPwn { return false, nil diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go index 0b9e9f9d4ff33..dc095f3a1351a 100644 --- a/routers/api/v1/admin/user.go +++ b/routers/api/v1/admin/user.go @@ -88,10 +88,10 @@ func CreateUser(ctx *context.APIContext, form api.CreateUserOption) { return } pwned, err := password.IsPwned(ctx.Req.Context(), form.Password) - if err != nil { - log.Error(err.Error()) - } if pwned { + if err != nil { + log.Error(err.Error()) + } ctx.Data["Err_Password"] = true ctx.Error(http.StatusBadRequest, "PasswordPwned", errors.New("PasswordPwned")) return @@ -161,10 +161,10 @@ func EditUser(ctx *context.APIContext, form api.EditUserOption) { return } pwned, err := password.IsPwned(ctx.Req.Context(), form.Password) - if err != nil { - log.Error(err.Error()) - } if pwned { + if err != nil { + log.Error(err.Error()) + } ctx.Data["Err_Password"] = true ctx.Error(http.StatusBadRequest, "PasswordPwned", errors.New("PasswordPwned")) return