SystemConfig.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC;
  8. use OCP\IConfig;
  9. /**
  10. * Class which provides access to the system config values stored in config.php
  11. * Internal class for bootstrap only.
  12. * fixes cyclic DI: AllConfig needs AppConfig needs Database needs AllConfig
  13. */
  14. class SystemConfig {
  15. /** @var array */
  16. protected $sensitiveValues = [
  17. 'instanceid' => true,
  18. 'datadirectory' => true,
  19. 'dbname' => true,
  20. 'dbhost' => true,
  21. 'dbpassword' => true,
  22. 'dbuser' => true,
  23. 'dbreplica' => true,
  24. 'activity_dbname' => true,
  25. 'activity_dbhost' => true,
  26. 'activity_dbpassword' => true,
  27. 'activity_dbuser' => true,
  28. 'mail_from_address' => true,
  29. 'mail_domain' => true,
  30. 'mail_smtphost' => true,
  31. 'mail_smtpname' => true,
  32. 'mail_smtppassword' => true,
  33. 'passwordsalt' => true,
  34. 'secret' => true,
  35. 'updater.secret' => true,
  36. 'updater.server.url' => true,
  37. 'trusted_proxies' => true,
  38. 'preview_imaginary_url' => true,
  39. 'preview_imaginary_key' => true,
  40. 'proxyuserpwd' => true,
  41. 'sentry.dsn' => true,
  42. 'sentry.public-dsn' => true,
  43. 'zammad.download.secret' => true,
  44. 'zammad.portal.secret' => true,
  45. 'zammad.secret' => true,
  46. 'github.client_id' => true,
  47. 'github.client_secret' => true,
  48. 'log.condition' => [
  49. 'shared_secret' => true,
  50. 'matches' => true,
  51. ],
  52. 'license-key' => true,
  53. 'redis' => [
  54. 'host' => true,
  55. 'password' => true,
  56. ],
  57. 'redis.cluster' => [
  58. 'seeds' => true,
  59. 'password' => true,
  60. ],
  61. 'objectstore' => [
  62. 'arguments' => [
  63. // Legacy Swift (https://github.com/nextcloud/server/pull/17696#discussion_r341302207)
  64. 'options' => [
  65. 'credentials' => [
  66. 'key' => true,
  67. 'secret' => true,
  68. ]
  69. ],
  70. // S3
  71. 'key' => true,
  72. 'secret' => true,
  73. 'sse_c_key' => true,
  74. // Swift v2
  75. 'username' => true,
  76. 'password' => true,
  77. // Swift v3
  78. 'user' => [
  79. 'name' => true,
  80. 'password' => true,
  81. ],
  82. ],
  83. ],
  84. 'objectstore_multibucket' => [
  85. 'arguments' => [
  86. 'options' => [
  87. 'credentials' => [
  88. 'key' => true,
  89. 'secret' => true,
  90. ]
  91. ],
  92. // S3
  93. 'key' => true,
  94. 'secret' => true,
  95. // Swift v2
  96. 'username' => true,
  97. 'password' => true,
  98. // Swift v3
  99. 'user' => [
  100. 'name' => true,
  101. 'password' => true,
  102. ],
  103. ],
  104. ],
  105. 'onlyoffice' => [
  106. 'jwt_secret' => true,
  107. ],
  108. 'PASS' => true,
  109. ];
  110. public function __construct(
  111. private Config $config,
  112. ) {
  113. }
  114. /**
  115. * Since system config is admin controlled, we can tell psalm to ignore any taint
  116. *
  117. * @psalm-taint-escape sql
  118. * @psalm-taint-escape html
  119. * @psalm-taint-escape ldap
  120. * @psalm-taint-escape callable
  121. * @psalm-taint-escape file
  122. * @psalm-taint-escape ssrf
  123. * @psalm-taint-escape cookie
  124. * @psalm-taint-escape header
  125. * @psalm-taint-escape has_quotes
  126. * @psalm-pure
  127. */
  128. public static function trustSystemConfig(mixed $value): mixed {
  129. return $value;
  130. }
  131. /**
  132. * Lists all available config keys
  133. * @return array an array of key names
  134. */
  135. public function getKeys() {
  136. return $this->config->getKeys();
  137. }
  138. /**
  139. * Sets a new system wide value
  140. *
  141. * @param string $key the key of the value, under which will be saved
  142. * @param mixed $value the value that should be stored
  143. */
  144. public function setValue($key, $value) {
  145. $this->config->setValue($key, $value);
  146. }
  147. /**
  148. * Sets and deletes values and writes the config.php
  149. *
  150. * @param array $configs Associative array with `key => value` pairs
  151. * If value is null, the config key will be deleted
  152. */
  153. public function setValues(array $configs) {
  154. $this->config->setValues($configs);
  155. }
  156. /**
  157. * Looks up a system wide defined value
  158. *
  159. * @param string $key the key of the value, under which it was saved
  160. * @param mixed $default the default value to be returned if the value isn't set
  161. * @return mixed the value or $default
  162. */
  163. public function getValue($key, $default = '') {
  164. return $this->trustSystemConfig($this->config->getValue($key, $default));
  165. }
  166. /**
  167. * Looks up a system wide defined value and filters out sensitive data
  168. *
  169. * @param string $key the key of the value, under which it was saved
  170. * @param mixed $default the default value to be returned if the value isn't set
  171. * @return mixed the value or $default
  172. */
  173. public function getFilteredValue($key, $default = '') {
  174. $value = $this->getValue($key, $default);
  175. if (isset($this->sensitiveValues[$key])) {
  176. $value = $this->removeSensitiveValue($this->sensitiveValues[$key], $value);
  177. }
  178. return $value;
  179. }
  180. /**
  181. * Delete a system wide defined value
  182. *
  183. * @param string $key the key of the value, under which it was saved
  184. */
  185. public function deleteValue($key) {
  186. $this->config->deleteKey($key);
  187. }
  188. /**
  189. * @param bool|array $keysToRemove
  190. * @param mixed $value
  191. * @return mixed
  192. */
  193. protected function removeSensitiveValue($keysToRemove, $value) {
  194. if ($keysToRemove === true) {
  195. return IConfig::SENSITIVE_VALUE;
  196. }
  197. if (is_array($value)) {
  198. foreach ($keysToRemove as $keyToRemove => $valueToRemove) {
  199. if (isset($value[$keyToRemove])) {
  200. $value[$keyToRemove] = $this->removeSensitiveValue($valueToRemove, $value[$keyToRemove]);
  201. }
  202. }
  203. }
  204. return $value;
  205. }
  206. }