gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. // Security settings
  13. var (
  14. InstallLock bool
  15. SecretKey string
  16. InternalToken string // internal access token
  17. LogInRememberDays int
  18. CookieRememberName string
  19. ReverseProxyAuthUser string
  20. ReverseProxyAuthEmail string
  21. ReverseProxyAuthFullName string
  22. ReverseProxyLimit int
  23. ReverseProxyTrustedProxies []string
  24. MinPasswordLength int
  25. ImportLocalPaths bool
  26. DisableGitHooks = true
  27. DisableWebhooks bool
  28. OnlyAllowPushIfGiteaEnvironmentSet bool
  29. PasswordComplexity []string
  30. PasswordHashAlgo string
  31. PasswordCheckPwn bool
  32. SuccessfulTokensCacheSize int
  33. DisableQueryAuthToken bool
  34. CSRFCookieName = "_csrf"
  35. CSRFCookieHTTPOnly = true
  36. RecordUserSignupMetadata = false
  37. TwoFactorAuthEnforced = false
  38. )
  39. // loadSecret load the secret from ini by uriKey or verbatimKey, only one of them could be set
  40. // If the secret is loaded from uriKey (file), the file should be non-empty, to guarantee the behavior stable and clear.
  41. func loadSecret(sec ConfigSection, uriKey, verbatimKey string) string {
  42. // don't allow setting both URI and verbatim string
  43. uri := sec.Key(uriKey).String()
  44. verbatim := sec.Key(verbatimKey).String()
  45. if uri != "" && verbatim != "" {
  46. log.Fatal("Cannot specify both %s and %s", uriKey, verbatimKey)
  47. }
  48. // if we have no URI, use verbatim
  49. if uri == "" {
  50. return verbatim
  51. }
  52. tempURI, err := url.Parse(uri)
  53. if err != nil {
  54. log.Fatal("Failed to parse %s (%s): %v", uriKey, uri, err)
  55. }
  56. switch tempURI.Scheme {
  57. case "file":
  58. buf, err := os.ReadFile(tempURI.RequestURI())
  59. if err != nil {
  60. log.Fatal("Failed to read %s (%s): %v", uriKey, tempURI.RequestURI(), err)
  61. }
  62. val := strings.TrimSpace(string(buf))
  63. if val == "" {
  64. // The file shouldn't be empty, otherwise we can not know whether the user has ever set the KEY or KEY_URI
  65. // For example: if INTERNAL_TOKEN_URI=file:///empty-file,
  66. // Then if the token is re-generated during installation and saved to INTERNAL_TOKEN
  67. // Then INTERNAL_TOKEN and INTERNAL_TOKEN_URI both exist, that's a fatal error (they shouldn't)
  68. log.Fatal("Failed to read %s (%s): the file is empty", uriKey, tempURI.RequestURI())
  69. }
  70. return val
  71. // only file URIs are allowed
  72. default:
  73. log.Fatal("Unsupported URI-Scheme %q (%q = %q)", tempURI.Scheme, uriKey, uri)
  74. return ""
  75. }
  76. }
  77. // generateSaveInternalToken generates and saves the internal token to app.ini
  78. func generateSaveInternalToken(rootCfg ConfigProvider) {
  79. token, err := generate.NewInternalToken()
  80. if err != nil {
  81. log.Fatal("Error generate internal token: %v", err)
  82. }
  83. InternalToken = token
  84. saveCfg, err := rootCfg.PrepareSaving()
  85. if err != nil {
  86. log.Fatal("Error saving internal token: %v", err)
  87. }
  88. rootCfg.Section("security").Key("INTERNAL_TOKEN").SetValue(token)
  89. saveCfg.Section("security").Key("INTERNAL_TOKEN").SetValue(token)
  90. if err = saveCfg.Save(); err != nil {
  91. log.Fatal("Error saving internal token: %v", err)
  92. }
  93. }
  94. func loadSecurityFrom(rootCfg ConfigProvider) {
  95. sec := rootCfg.Section("security")
  96. InstallLock = HasInstallLock(rootCfg)
  97. LogInRememberDays = sec.Key("LOGIN_REMEMBER_DAYS").MustInt(31)
  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&*("
  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. twoFactorAuth := sec.Key("TWO_FACTOR_AUTH").String()
  128. switch twoFactorAuth {
  129. case "":
  130. case "enforced":
  131. TwoFactorAuthEnforced = true
  132. default:
  133. log.Fatal("Invalid two-factor auth option: %s", twoFactorAuth)
  134. }
  135. InternalToken = loadSecret(sec, "INTERNAL_TOKEN_URI", "INTERNAL_TOKEN")
  136. if InstallLock && InternalToken == "" {
  137. // if Gitea has been installed but the InternalToken hasn't been generated (upgrade from an old release), we should generate
  138. // some users do cluster deployment, they still depend on this auto-generating behavior.
  139. generateSaveInternalToken(rootCfg)
  140. }
  141. cfgdata := sec.Key("PASSWORD_COMPLEXITY").Strings(",")
  142. if len(cfgdata) == 0 {
  143. cfgdata = []string{"off"}
  144. }
  145. PasswordComplexity = make([]string, 0, len(cfgdata))
  146. for _, name := range cfgdata {
  147. name := strings.ToLower(strings.Trim(name, `"`))
  148. if name != "" {
  149. PasswordComplexity = append(PasswordComplexity, name)
  150. }
  151. }
  152. sectionHasDisableQueryAuthToken := sec.HasKey("DISABLE_QUERY_AUTH_TOKEN")
  153. // TODO: default value should be true in future releases
  154. DisableQueryAuthToken = sec.Key("DISABLE_QUERY_AUTH_TOKEN").MustBool(false)
  155. RecordUserSignupMetadata = sec.Key("RECORD_USER_SIGNUP_METADATA").MustBool(false)
  156. // warn if the setting is set to false explicitly
  157. if sectionHasDisableQueryAuthToken && !DisableQueryAuthToken {
  158. 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.")
  159. }
  160. }