Skip to content

Commit

Permalink
fix(Config): Prevent setting or deleting config values that are overw…
Browse files Browse the repository at this point in the history
…ritten by additional config files

Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed Dec 19, 2024
1 parent 53aa690 commit 0030523
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/private/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ public function setValue($key, $value) {
*/
protected function set($key, $value) {
if (!isset($this->cache[$key]) || $this->cache[$key] !== $value) {
foreach ($this->cachePaths as $file => $keys) {
if ($file !== $this->configFilePath && in_array($key, $keys)) {
throw new HintException('The config key "' . $key . '" is already specified in "' . $file . '" and thus can not be overwritten.');
}
}

// Add change
$this->cache[$key] = $value;
return true;
Expand Down Expand Up @@ -154,6 +160,12 @@ public function deleteKey($key) {
*/
protected function delete($key) {
if (isset($this->cache[$key])) {
foreach ($this->cachePaths as $file => $keys) {
if ($file !== $this->configFilePath && in_array($key, $keys)) {
throw new HintException('The config key "' . $key . '" is already specified in "' . $file . '" and thus can not be overwritten.');
}
}

// Delete key from cache
unset($this->cache[$key]);
return true;
Expand Down
22 changes: 22 additions & 0 deletions tests/lib/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Test;

use OC\Config;
use OCP\HintException;

class ConfigTest extends TestCase {
public const TESTCONTENT = '<?php $CONFIG=array("foo"=>"bar", "beers" => array("Appenzeller", "Guinness", "Kölsch"), "alcohol_free" => false);';
Expand Down Expand Up @@ -175,4 +176,25 @@ public function testConfigMerge(): void {
// Cleanup
unlink($additionalConfigPath);
}

public function testConfigAdditionalSetDelete(): void {
$additionalConfig = '<?php $CONFIG=array("php53"=>"totallyOutdated");';
$additionalConfigPath = $this->randomTmpDir . 'additionalConfig.testconfig.php';
file_put_contents($additionalConfigPath, $additionalConfig);

$config = new Config($this->randomTmpDir, 'testconfig.php');

$this->assertSame('totallyOutdated', $config->getValue('php53', 'bogusValue'));
$this->assertEquals(self::TESTCONTENT, file_get_contents($this->configFile));

$this->expectException(HintException::class);

$this->expectExceptionMessage('The config key "php53" is already specified in "' . $additionalConfigPath . '" and thus can not be overwritten.');
$config->setValue('php53', 'actuallyNew');

$this->expectExceptionMessage('The config key "php53" is specified in "' . $additionalConfigPath . '" and thus can not be deleted.');
$config->deleteKey('php53');

unlink($additionalConfigPath);
}
}

0 comments on commit 0030523

Please sign in to comment.