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.

security.go 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package setting
  4. import (
  5. "net/url"
  6. "os"
  7. "strings"
  8. "code.gitea.io/gitea/modules/auth/password/hash"
  9. "code.gitea.io/gitea/modules/generate"
  10. "code.gitea.io/gitea/modules/log"
  11. )
  12. var (
  13. // Security settings
  14. InstallLock bool
  15. SecretKey string
  16. InternalToken string // internal access token
  17. LogInRememberDays int
  18. CookieUserName string
  19. CookieRememberName string
  20. ReverseProxyAuthUser string
  21. ReverseProxyAuthEmail string
  22. ReverseProxyAuthFullName string
  23. ReverseProxyLimit int
  24. ReverseProxyTrustedProxies []string
  25. MinPasswordLength int
  26. ImportLocalPaths bool
  27. DisableGitHooks bool
  28. DisableWebhooks bool
  29. OnlyAllowPushIfGiteaEnvironmentSet bool
  30. PasswordComplexity []string
  31. PasswordHashAlgo string
  32. PasswordCheckPwn bool
  33. SuccessfulTokensCacheSize int
  34. DisableQueryAuthToken bool
  35. CSRFCookieName = "_csrf"
  36. CSRFCookieHTTPOnly = true
  37. )
  38. // loadSecret load the secret from ini by uriKey or verbatimKey, only one of them could be set
  39. // If the secret is loaded from uriKey (file), the file should be non-empty, to guarantee the behavior stable and clear.
  40. func loadSecret(sec ConfigSection, uriKey, verbatimKey string) string {
  41. // don't allow setting both URI and verbatim string
  42. uri := sec.Key(uriKey).String()
  43. verbatim := sec.Key(verbatimKey).String()
  44. if uri != "" && verbatim != "" {
  45. log.Fatal("Cannot specify both %s and %s", uriKey, verbatimKey)
  46. }
  47. // if we have no URI, use verbatim
  48. if uri == "" {
  49. return verbatim
  50. }
  51. tempURI, err := url.Parse(uri)
  52. if err != nil {
  53. log.Fatal("Failed to parse %s (%s): %v", uriKey, uri, err)
  54. }
  55. switch tempURI.Scheme {
  56. case "file":
  57. buf, err := os.ReadFile(tempURI.RequestURI())
  58. if err != nil {
  59. log.Fatal("Failed to read %s (%s): %v", uriKey, tempURI.RequestURI(), err)
  60. }
  61. val := strings.TrimSpace(string(buf))
  62. if val == "" {
  63. // The file shouldn't be empty, otherwise we can not know whether the user has ever set the KEY or KEY_URI
  64. // For example: if INTERNAL_TOKEN_URI=file:///empty-file,
  65. // Then if the token is re-generated during installation and saved to INTERNAL_TOKEN
  66. // Then INTERNAL_TOKEN and INTERNAL_TOKEN_URI both exist, that's a fatal error (they shouldn't)
  67. log.Fatal("Failed to read %s (%s): the file is empty", uriKey, tempURI.RequestURI())
  68. }
  69. return val
  70. // only file URIs are allowed
  71. default:
  72. log.Fatal("Unsupported URI-Scheme %q (%q = %q)", tempURI.Scheme, uriKey, uri)
  73. return ""
  74. }
  75. }
  76. // generateSaveInternalToken generates and saves the internal token to app.ini
  77. func generateSaveInternalToken(rootCfg ConfigProvider) {
  78. token, err := generate.NewInternalToken()
  79. if err != nil {
  80. log.Fatal("Error generate internal token: %v", err)
  81. }
  82. InternalToken = token
  83. saveCfg, err := rootCfg.PrepareSaving()
  84. if err != nil {
  85. log.Fatal("Error saving internal token: %v", err)
  86. }
  87. rootCfg.Section("security").Key("INTERNAL_TOKEN").SetValue(token)
  88. saveCfg.Section("security").Key("INTERNAL_TOKEN").SetValue(token)
  89. if err = saveCfg.Save(); err != nil {
  90. log.Fatal("Error saving internal token: %v", err)
  91. }
  92. }
  93. func loadSecurityFrom(rootCfg ConfigProvider) {
  94. sec := rootCfg.Section("security")
  95. InstallLock = HasInstallLock(rootCfg)
  96. LogInRememberDays = sec.Key("LOGIN_REMEMBER_DAYS").MustInt(7)
  97. CookieUserName = sec.Key("COOKIE_USERNAME").MustString("gitea_awesome")
  98. SecretKey = loadSecret(sec, "SECRET_KEY_URI", "SECRET_KEY")
  99. if SecretKey == "" {
  100. // FIXME: https://github.com/go-gitea/gitea/issues/16832
  101. // Until it supports rotating an existing secret key, we shouldn't move users off of the widely used default value
  102. SecretKey = "!#@FDEWREWR&*(" //nolint:gosec
  103. }
  104. CookieRememberName = sec.Key("COOKIE_REMEMBER_NAME").MustString("gitea_incredible")
  105. ReverseProxyAuthUser = sec.Key("REVERSE_PROXY_AUTHENTICATION_USER").MustString("X-WEBAUTH-USER")
  106. ReverseProxyAuthEmail = sec.Key("REVERSE_PROXY_AUTHENTICATION_EMAIL").MustString("X-WEBAUTH-EMAIL")
  107. ReverseProxyAuthFullName = sec.Key("REVERSE_PROXY_AUTHENTICATION_FULL_NAME").MustString("X-WEBAUTH-FULLNAME")
  108. ReverseProxyLimit = sec.Key("REVERSE_PROXY_LIMIT").MustInt(1)
  109. ReverseProxyTrustedProxies = sec.Key("REVERSE_PROXY_TRUSTED_PROXIES").Strings(",")
  110. if len(ReverseProxyTrustedProxies) == 0 {
  111. ReverseProxyTrustedProxies = []string{"127.0.0.0/8", "::1/128"}
  112. }
  113. MinPasswordLength = sec.Key("MIN_PASSWORD_LENGTH").MustInt(8)
  114. ImportLocalPaths = sec.Key("IMPORT_LOCAL_PATHS").MustBool(false)
  115. DisableGitHooks = sec.Key("DISABLE_GIT_HOOKS").MustBool(true)
  116. DisableWebhooks = sec.Key("DISABLE_WEBHOOKS").MustBool(false)
  117. OnlyAllowPushIfGiteaEnvironmentSet = sec.Key("ONLY_ALLOW_PUSH_IF_GITEA_ENVIRONMENT_SET").MustBool(true)
  118. // Ensure that the provided default hash algorithm is a valid hash algorithm
  119. var algorithm *hash.PasswordHashAlgorithm
  120. PasswordHashAlgo, algorithm = hash.SetDefaultPasswordHashAlgorithm(sec.Key("PASSWORD_HASH_ALGO").MustString(""))
  121. if algorithm == nil {
  122. log.Fatal("The provided password hash algorithm was invalid: %s", sec.Key("PASSWORD_HASH_ALGO").MustString(""))
  123. }
  124. CSRFCookieHTTPOnly = sec.Key("CSRF_COOKIE_HTTP_ONLY").MustBool(true)
  125. PasswordCheckPwn = sec.Key("PASSWORD_CHECK_PWN").MustBool(false)
  126. SuccessfulTokensCacheSize = sec.Key("SUCCESSFUL_TOKENS_CACHE_SIZE").MustInt(20)
  127. InternalToken = loadSecret(sec, "INTERNAL_TOKEN_URI", "INTERNAL_TOKEN")
  128. if InstallLock && InternalToken == "" {
  129. // if Gitea has been installed but the InternalToken hasn't been generated (upgrade from an old release), we should generate
  130. // some users do cluster deployment, they still depend on this auto-generating behavior.
  131. generateSaveInternalToken(rootCfg)
  132. }
  133. cfgdata := sec.Key("PASSWORD_COMPLEXITY").Strings(",")
  134. if len(cfgdata) == 0 {
  135. cfgdata = []string{"off"}
  136. }
  137. PasswordComplexity = make([]string, 0, len(cfgdata))
  138. for _, name := range cfgdata {
  139. name := strings.ToLower(strings.Trim(name, `"`))
  140. if name != "" {
  141. PasswordComplexity = append(PasswordComplexity, name)
  142. }
  143. }
  144. // TODO: default value should be true in future releases
  145. DisableQueryAuthToken = sec.Key("DISABLE_QUERY_AUTH_TOKEN").MustBool(false)
  146. if !DisableQueryAuthToken {
  147. 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.")
  148. }
  149. }