From c6e4bc53aad371210f0cb670e36c57132087b230 Mon Sep 17 00:00:00 2001 From: John Olheiser Date: Tue, 8 Sep 2020 17:06:39 -0500 Subject: [PATCH] Check passwords against HaveIBeenPwned (#12716) * Implement pwn Signed-off-by: jolheiser * Update module Signed-off-by: jolheiser * Apply suggestions mrsdizzie Co-authored-by: mrsdizzie * Add link to HIBP Signed-off-by: jolheiser * Add more details to admin command Signed-off-by: jolheiser * Add context to pwn Signed-off-by: jolheiser * Consistency and making some noise ;) Signed-off-by: jolheiser Co-authored-by: mrsdizzie Co-authored-by: zeripath --- cmd/admin.go | 8 +++ custom/conf/app.example.ini | 4 +- .../doc/advanced/config-cheat-sheet.en-us.md | 1 + go.mod | 1 + go.sum | 5 +- modules/password/password.go | 9 ++- modules/password/pwn.go | 30 +++++++++ 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 | 58 +++++++++++++++++ vendor/modules.txt | 3 + 22 files changed, 309 insertions(+), 8 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 a049f7f2c..9f81f5284 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -6,6 +6,7 @@ package cmd import ( + "context" "errors" "fmt" "os" @@ -265,6 +266,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(context.Background(), c.String("password")) + if err != nil { + 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.\nFor more details, see https://haveibeenpwned.com/Passwords") + } 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 bb65c4f08..af3418f70 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 86f60e082..7f969add2 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -344,6 +344,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 e19e2d232..38fb7f839 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.3 golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a golang.org/x/net v0.0.0-20200904194848-62affa334b73 golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d diff --git a/go.sum b/go.sum index 6970a6ee4..e4a0a2562 100644 --- a/go.sum +++ b/go.sum @@ -918,8 +918,9 @@ 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.5 h1:XAzx9gjCb0Rxj7EoqcClPD1d5ZBxZJk0jbuoPHenBt0= go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ= +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= go.mongodb.org/mongo-driver v1.3.0/go.mod h1:MSWZXKOynuguX+JSvwP8i+58jYCXxbia8HS3gZBapIE= go.mongodb.org/mongo-driver v1.3.4/go.mod h1:MSWZXKOynuguX+JSvwP8i+58jYCXxbia8HS3gZBapIE= @@ -977,7 +978,6 @@ golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCc golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= -golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= @@ -1012,7 +1012,6 @@ golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20200602114024-627f9648deb9/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= -golang.org/x/net v0.0.0-20200707034311-ab3426394381 h1:VXak5I6aEWmAXeQjA+QSZzlgNrpq9mjcfDemuexIKsU= golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20200904194848-62affa334b73 h1:MXfv8rhZWmFeqX3GNZRsd6vOLoaCHjYEX3qkRo3YBUA= golang.org/x/net v0.0.0-20200904194848-62affa334b73/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= diff --git a/modules/password/password.go b/modules/password/password.go index 1c4b9c514..e1f1f769e 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" @@ -88,7 +89,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 +102,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(goContext.Background(), 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 000000000..938524e6d --- /dev/null +++ b/modules/password/pwn.go @@ -0,0 +1,30 @@ +// 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 ( + "context" + + "code.gitea.io/gitea/modules/setting" + + "go.jolheiser.com/pwn" +) + +// 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 + } + + client := pwn.New(pwn.WithContext(ctx)) + 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 c93cc9280..7d7eacba6 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -146,6 +146,7 @@ var ( OnlyAllowPushIfGiteaEnvironmentSet bool PasswordComplexity []string PasswordHashAlgo string + PasswordCheckPwn bool // UI settings UI = struct { @@ -744,6 +745,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 a7b917ae1..ce3537505 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 = 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] activate_account = Please activate your account diff --git a/routers/admin/users.go b/routers/admin/users.go index a28db2b44..531f81b8b 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(ctx.Req.Context(), 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(ctx.Req.Context(), 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 a8a573eaf..dc095f3a1 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(ctx.Req.Context(), form.Password) + if pwned { + if err != nil { + log.Error(err.Error()) + } + 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(ctx.Req.Context(), form.Password) + if pwned { + if err != nil { + log.Error(err.Error()) + } + 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 4e6ac9c87..96a73c9dd 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(ctx.Req.Context(), 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(ctx.Req.Context(), 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 27f0bf1c8..99e20177b 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(ctx.Req.Context(), 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 000000000..b73cf3849 --- /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 000000000..0f02641b2 --- /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 000000000..7f5474401 --- /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 000000000..9b3c37eb2 --- /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 000000000..2c73ed5ce --- /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 000000000..46b73c8e5 --- /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 000000000..848ac4918 --- /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 000000000..399284b48 --- /dev/null +++ b/vendor/go.jolheiser.com/pwn/pwn.go @@ -0,0 +1,58 @@ +package pwn + +import ( + "context" + "io" + "net/http" +) + +const ( + libVersion = "0.0.3" + userAgent = "go.jolheiser.com/pwn v" + libVersion +) + +// 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 87b0687cd..72ef7b58e 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -760,6 +760,9 @@ github.com/yuin/goldmark-highlighting github.com/yuin/goldmark-meta # go.etcd.io/bbolt v1.3.5 go.etcd.io/bbolt +# go.jolheiser.com/pwn v0.0.3 +## 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