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.6KB

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