You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pwn.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package pwn
  4. import (
  5. "context"
  6. "crypto/sha1"
  7. "encoding/hex"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "net/http"
  12. "strconv"
  13. "strings"
  14. "code.gitea.io/gitea/modules/setting"
  15. )
  16. const passwordURL = "https://api.pwnedpasswords.com/range/"
  17. // ErrEmptyPassword is an empty password error
  18. var ErrEmptyPassword = errors.New("password cannot be empty")
  19. // Client is a HaveIBeenPwned client
  20. type Client struct {
  21. ctx context.Context
  22. http *http.Client
  23. }
  24. // New returns a new HaveIBeenPwned Client
  25. func New(options ...ClientOption) *Client {
  26. client := &Client{
  27. ctx: context.Background(),
  28. http: http.DefaultClient,
  29. }
  30. for _, opt := range options {
  31. opt(client)
  32. }
  33. return client
  34. }
  35. // ClientOption is a way to modify a new Client
  36. type ClientOption func(*Client)
  37. // WithHTTP will set the http.Client of a Client
  38. func WithHTTP(httpClient *http.Client) func(pwnClient *Client) {
  39. return func(pwnClient *Client) {
  40. pwnClient.http = httpClient
  41. }
  42. }
  43. // WithContext will set the context.Context of a Client
  44. func WithContext(ctx context.Context) func(pwnClient *Client) {
  45. return func(pwnClient *Client) {
  46. pwnClient.ctx = ctx
  47. }
  48. }
  49. func newRequest(ctx context.Context, method, url string, body io.ReadCloser) (*http.Request, error) {
  50. req, err := http.NewRequestWithContext(ctx, method, url, body)
  51. if err != nil {
  52. return nil, err
  53. }
  54. req.Header.Add("User-Agent", "Gitea "+setting.AppVer)
  55. return req, nil
  56. }
  57. // CheckPassword returns the number of times a password has been compromised
  58. // Adding padding will make requests more secure, however is also slower
  59. // because artificial responses will be added to the response
  60. // For more information, see https://www.troyhunt.com/enhancing-pwned-passwords-privacy-with-padding/
  61. func (c *Client) CheckPassword(pw string, padding bool) (int, error) {
  62. if strings.TrimSpace(pw) == "" {
  63. return -1, ErrEmptyPassword
  64. }
  65. sha := sha1.New()
  66. sha.Write([]byte(pw))
  67. enc := hex.EncodeToString(sha.Sum(nil))
  68. prefix, suffix := enc[:5], enc[5:]
  69. req, err := newRequest(c.ctx, http.MethodGet, fmt.Sprintf("%s%s", passwordURL, prefix), nil)
  70. if err != nil {
  71. return -1, nil
  72. }
  73. if padding {
  74. req.Header.Add("Add-Padding", "true")
  75. }
  76. resp, err := c.http.Do(req)
  77. if err != nil {
  78. return -1, err
  79. }
  80. body, err := io.ReadAll(resp.Body)
  81. if err != nil {
  82. return -1, err
  83. }
  84. defer resp.Body.Close()
  85. for _, pair := range strings.Split(string(body), "\n") {
  86. parts := strings.Split(pair, ":")
  87. if len(parts) != 2 {
  88. continue
  89. }
  90. if strings.EqualFold(suffix, parts[0]) {
  91. count, err := strconv.ParseInt(strings.TrimSpace(parts[1]), 10, 64)
  92. if err != nil {
  93. return -1, err
  94. }
  95. return int(count), nil
  96. }
  97. }
  98. return 0, nil
  99. }