Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of additional config files #49926

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions build/psalm-baseline-security.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
</TaintedCallable>
</file>
<file src="lib/private/Config.php">
<TaintedHtml>
<code><![CDATA[$this->cache]]></code>
</TaintedHtml>
<TaintedHtml/>
</file>
<file src="lib/private/EventSource.php">
<TaintedHeader>
Expand Down
29 changes: 28 additions & 1 deletion lib/private/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

/** @var array Associative array ($key => $value) */
protected $cache = [];
/** @var array<string, list<string>> */
protected array $cachePaths = [];
/** @var array */
protected $envCache = [];
/** @var string */
Expand Down Expand Up @@ -122,6 +124,12 @@
*/
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 @@ -152,6 +160,12 @@
*/
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 Expand Up @@ -223,6 +237,7 @@
}
if (isset($CONFIG) && is_array($CONFIG)) {
$this->cache = array_merge($this->cache, $CONFIG);
$this->cachePaths[$file] = array_keys($CONFIG);
}
}

Expand Down Expand Up @@ -253,10 +268,22 @@
throw new HintException(sprintf('Configuration was not read or initialized correctly, not overwriting %s', $this->configFilePath));
}

// Do not save any of the values that came from the extra config file into the main config file
$values = $this->cache;
foreach (array_keys($this->cachePaths) as $file) {
if ($file === $this->configFilePath) {
continue;
}

foreach ($this->cachePaths[$file] as $key) {
unset($values[$key]);
}
}

// Create a php file ...
$content = "<?php\n";
$content .= '$CONFIG = ';
$content .= var_export($this->cache, true);
$content .= var_export($values, true);

Check failure on line 286 in lib/private/Config.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-security

TaintedHtml

lib/private/Config.php:286:26: TaintedHtml: Detected tainted HTML (see https://psalm.dev/245)

Check failure

Code scanning / Psalm

TaintedHtml Error

Detected tainted HTML
$content .= ";\n";

touch($this->configFilePath);
Expand Down
26 changes: 24 additions & 2 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 @@ -154,7 +155,7 @@ public function testDeleteKey(): void {

public function testConfigMerge(): void {
// Create additional config
$additionalConfig = '<?php $CONFIG=array("php53"=>"totallyOutdated");';
$additionalConfig = '<?php $CONFIG=array("php53"=>"totallyOutdated","alcohol_free"=>true);';
$additionalConfigPath = $this->randomTmpDir . 'additionalConfig.testconfig.php';
file_put_contents($additionalConfigPath, $additionalConfig);

Expand All @@ -168,11 +169,32 @@ public function testConfigMerge(): void {
// Write a new value to the config
$config->setValue('CoolWebsites', ['demo.owncloud.org', 'owncloud.org', 'owncloud.com']);
$expected = "<?php\n\$CONFIG = array (\n 'foo' => 'bar',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
" 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n 'php53' => 'totallyOutdated',\n 'CoolWebsites' => \n array (\n " .
" 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'CoolWebsites' => \n array (\n " .
" 0 => 'demo.owncloud.org',\n 1 => 'owncloud.org',\n 2 => 'owncloud.com',\n ),\n);\n";
$this->assertEquals($expected, file_get_contents($this->configFile));

// 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);
}
}
Loading