Skip to content

Commit 0a20c69

Browse files
authored
Merge pull request #48100 from nextcloud/backport/46140/stable29
[stable29] fix(config): Add missing handling for `envCache` in `getKeys()`
2 parents 1c7b5c8 + 75555d3 commit 0a20c69

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

lib/private/Config.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function __construct($configDir, $fileName = 'config.php') {
8080
* @return array an array of key names
8181
*/
8282
public function getKeys() {
83-
return array_keys($this->cache);
83+
return array_merge(array_keys($this->cache), array_keys($this->envCache));
8484
}
8585

8686
/**
@@ -95,9 +95,8 @@ public function getKeys() {
9595
* @return mixed the value or $default
9696
*/
9797
public function getValue($key, $default = null) {
98-
$envKey = self::ENV_PREFIX . $key;
99-
if (isset($this->envCache[$envKey])) {
100-
return $this->envCache[$envKey];
98+
if (isset($this->envCache[$key])) {
99+
return $this->envCache[$key];
101100
}
102101

103102
if (isset($this->cache[$key])) {
@@ -257,7 +256,16 @@ private function readData() {
257256
}
258257
}
259258

260-
$this->envCache = getenv();
259+
// grab any "NC_" environment variables
260+
$envRaw = getenv();
261+
// only save environment variables prefixed with "NC_" in the cache
262+
$envPrefixLen = strlen(self::ENV_PREFIX);
263+
foreach ($envRaw as $rawEnvKey => $rawEnvValue) {
264+
if (str_starts_with($rawEnvKey, self::ENV_PREFIX)) {
265+
$realKey = substr($rawEnvKey, $envPrefixLen);
266+
$this->envCache[$realKey] = $rawEnvValue;
267+
}
268+
}
261269
}
262270

263271
/**

tests/lib/ConfigTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ public function testGetKeys() {
4242
$this->assertSame($expectedConfig, $this->getConfig()->getKeys());
4343
}
4444

45+
public function testGetKeysReturnsEnvironmentKeysIfSet() {
46+
$expectedConfig = ['foo', 'beers', 'alcohol_free', 'taste'];
47+
putenv('NC_taste=great');
48+
$this->assertSame($expectedConfig, $this->getConfig()->getKeys());
49+
putenv('NC_taste');
50+
}
51+
4552
public function testGetValue() {
4653
$config = $this->getConfig();
4754
$this->assertSame('bar', $config->getValue('foo'));

0 commit comments

Comments
 (0)