Merge pull request #3706 from Icinga/fix/multiline-values-in-ini-files-broken-3705

Fix multiline values in ini files broken
This commit is contained in:
Johannes Meyer 2019-04-05 08:44:46 +02:00 committed by GitHub
commit ccc2f487f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 10 deletions

View File

@ -157,11 +157,10 @@ class Directive
{ {
$str = trim($str); $str = trim($str);
$str = str_replace('\\', '\\\\', $str); $str = str_replace('\\', '\\\\', $str);
$str = str_replace('"', '\\"', $str); $str = str_replace('"', '\"', $str);
$str = str_replace("\r", '\r', $str);
$str = str_replace("\n", '\n', $str);
// line breaks in the value should always match the current system EOL sequence
// to assure editable configuration files
$str = preg_replace("/(\r\n)|(\n)/", PHP_EOL, $str);
return $str; return $str;
} }
} }

View File

@ -284,8 +284,8 @@ class IniParser
*/ */
protected static function unescapeSectionName($str) protected static function unescapeSectionName($str)
{ {
$str = str_replace('\\"', '"', $str); $str = str_replace('\"', '"', $str);
$str = str_replace('\\;', ';', $str); $str = str_replace('\;', ';', $str);
return str_replace('\\\\', '\\', $str); return str_replace('\\\\', '\\', $str);
} }
@ -299,8 +299,11 @@ class IniParser
*/ */
protected static function unescapeOptionValue($str) protected static function unescapeOptionValue($str)
{ {
$str = str_replace('\\"', '"', $str); $str = str_replace('\n', "\n", $str);
$str = str_replace('\r', "\r", $str);
$str = str_replace('\"', '"', $str);
$str = str_replace('\\\\', '\\', $str);
return str_replace('\\\\', '\\', $str); return $str;
} }
} }

View File

@ -132,7 +132,42 @@ EOD;
$this->assertEquals( $this->assertEquals(
2, 2,
count(explode("\n", $doc->getSection('section')->getDirective('key1')->getValue())), count(explode("\n", $doc->getSection('section')->getDirective('key1')->getValue())),
'IniParser does not recognize multi-line values' 'IniParser::parseIni does not recognize multi-line values'
);
(new IniWriter(Config::fromArray([
'section' => [
'key1' => "with some\nnewline in the value"
]
]), $this->tempFile))->write();
$doc = IniParser::parseIniFile($this->tempFile);
$this->assertEquals(
2,
count(explode("\n", $doc->getSection('section')->get('key1'))),
'IniParser::parseIniFile does not recognize multi-line values'
);
}
public function testEnvironmentVariableResolutionDoesNotWork()
{
(new IniWriter(Config::fromArray([
'section' => [
'key1' => '${PATH}_${APACHE_RUN_DIR}',
'key2' => '${APACHE_RUN_USER}'
]
]), $this->tempFile))->write();
$configObject = IniParser::parseIniFile($this->tempFile);
$this->assertEquals(
'${PATH}_${APACHE_RUN_DIR}',
$configObject->getSection('section')->get('key1'),
'IniParser::parseIniFile does resolve environment variables'
);
$this->assertEquals(
'${APACHE_RUN_USER}',
$configObject->getSection('section')->get('key2'),
'IniParser::parseIniFile does resolve environment variables'
); );
} }
} }

View File

@ -275,9 +275,14 @@ inkey' => 'blarg'
); );
$rendered = $writer->render(); $rendered = $writer->render();
$this->assertRegExp(
'~linebreak\\\\nin line~',
$rendered,
'newlines in values are not escaped'
);
$this->assertEquals( $this->assertEquals(
4,
count(explode("\n", $rendered)), count(explode("\n", $rendered)),
5,
'generated config should not contain more than three line breaks' 'generated config should not contain more than three line breaks'
); );
} }