mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
fix: do not treat later KEY=" as closing an open quote
When a quoted value never closes, a subsequent assignment's opening quote must trigger line-local recovery instead of terminating the value. Co-authored-by: Chirag Aggarwal <chiragaggarwal5k@gmail.com>
This commit is contained in:
1 parent
0b329f3247
commit
cb48a23ab3
2 files changed
+33
No files matched your search
@@ -67,11 +67,21 @@ class Env
|
||||
$scanOffset = $contentStart;
|
||||
$value = '';
|
||||
$closed = false;
|
||||
$crossedNewline = false;
|
||||
|
||||
while ($scanOffset < $length) {
|
||||
$char = $data[$scanOffset];
|
||||
|
||||
if ($char === "\n" || $char === "\r") {
|
||||
$crossedNewline = true;
|
||||
}
|
||||
|
||||
if ($char === $quote) {
|
||||
// A later line's opening quote (KEY=") must not close an
|
||||
// earlier unterminated value, or upgrade will drop that key.
|
||||
if ($crossedNewline && self::isAssignmentOpener($data, $scanOffset)) {
|
||||
break;
|
||||
}
|
||||
$scanOffset++;
|
||||
$closed = true;
|
||||
break;
|
||||
@@ -201,6 +211,18 @@ class Env
|
||||
return \addcslashes($value, "\"\\\n\r\t\$`");
|
||||
}
|
||||
|
||||
/**
|
||||
* True when $quoteOffset points at a quote that opens a new KEY="..." assignment.
|
||||
*/
|
||||
private static function isAssignmentOpener(string $data, int $quoteOffset): bool
|
||||
{
|
||||
$lineStart = \strrpos(\substr($data, 0, $quoteOffset), "\n");
|
||||
$lineStart = $lineStart === false ? 0 : $lineStart + 1;
|
||||
$before = \substr($data, $lineStart, $quoteOffset - $lineStart);
|
||||
|
||||
return \preg_match('/^\s*[A-Za-z_][A-Za-z0-9_]*\s*=\s*$/', $before) === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
|
||||
@@ -131,4 +131,15 @@ final class EnvTest extends TestCase
|
||||
$this->assertSame('example.com', $env->getVar('_APP_DOMAIN'));
|
||||
$this->assertSame('enabled', $env->getVar('_APP_OPTIONS_FORCE_HTTPS'));
|
||||
}
|
||||
|
||||
public function testUnterminatedQuoteDoesNotAbsorbLaterQuotedAssignment(): void
|
||||
{
|
||||
$data = "_APP_BROKEN=\"partial-value\n_APP_DOMAIN=\"example.com\"\n_APP_OPTIONS_FORCE_HTTPS=enabled\n";
|
||||
|
||||
$env = new Env($data);
|
||||
|
||||
$this->assertSame('partial-value', $env->getVar('_APP_BROKEN'));
|
||||
$this->assertSame('example.com', $env->getVar('_APP_DOMAIN'));
|
||||
$this->assertSame('enabled', $env->getVar('_APP_OPTIONS_FORCE_HTTPS'));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user