Skip to content

Commit c4eb1ac

Browse files
authored
Merge pull request #48101 from nextcloud/backport/46140/stable30
[stable30] fix(config): Add missing handling for `envCache` in `getKeys()`
2 parents cea10d6 + 344c968 commit c4eb1ac

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

lib/private/Config.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __construct($configDir, $fileName = 'config.php') {
4949
* @return array an array of key names
5050
*/
5151
public function getKeys() {
52-
return array_keys($this->cache);
52+
return array_merge(array_keys($this->cache), array_keys($this->envCache));
5353
}
5454

5555
/**
@@ -64,9 +64,8 @@ public function getKeys() {
6464
* @return mixed the value or $default
6565
*/
6666
public function getValue($key, $default = null) {
67-
$envKey = self::ENV_PREFIX . $key;
68-
if (isset($this->envCache[$envKey])) {
69-
return $this->envCache[$envKey];
67+
if (isset($this->envCache[$key])) {
68+
return $this->envCache[$key];
7069
}
7170

7271
if (isset($this->cache[$key])) {
@@ -226,7 +225,16 @@ private function readData() {
226225
}
227226
}
228227

229-
$this->envCache = getenv();
228+
// grab any "NC_" environment variables
229+
$envRaw = getenv();
230+
// only save environment variables prefixed with "NC_" in the cache
231+
$envPrefixLen = strlen(self::ENV_PREFIX);
232+
foreach ($envRaw as $rawEnvKey => $rawEnvValue) {
233+
if (str_starts_with($rawEnvKey, self::ENV_PREFIX)) {
234+
$realKey = substr($rawEnvKey, $envPrefixLen);
235+
$this->envCache[$realKey] = $rawEnvValue;
236+
}
237+
}
230238
}
231239

232240
/**

tests/lib/ConfigTest.php

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

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

0 commit comments

Comments
 (0)