diff --git a/src/Appwrite/Docker/Env.php b/src/Appwrite/Docker/Env.php index 128b3fba92..ef614d7a32 100644 --- a/src/Appwrite/Docker/Env.php +++ b/src/Appwrite/Docker/Env.php @@ -63,18 +63,22 @@ class Env $quote = $data[$valueOffset]; if ($quote === '"' || $quote === "'") { - $valueOffset++; + $contentStart = $valueOffset + 1; + $scanOffset = $contentStart; $value = ''; - while ($valueOffset < $length) { - $char = $data[$valueOffset]; + $closed = false; + + while ($scanOffset < $length) { + $char = $data[$scanOffset]; if ($char === $quote) { - $valueOffset++; + $scanOffset++; + $closed = true; break; } - if ($quote === '"' && $char === '\\' && $valueOffset + 1 < $length) { - $next = $data[$valueOffset + 1]; + if ($quote === '"' && $char === '\\' && $scanOffset + 1 < $length) { + $next = $data[$scanOffset + 1]; $value .= match ($next) { 'n' => "\n", 'r' => "\r", @@ -88,14 +92,34 @@ class Env // (e.g. Windows paths or patterns like `\d`, `\q`). default => '\\' . $next, }; - $valueOffset += 2; + $scanOffset += 2; continue; } $value .= $char; - $valueOffset++; + $scanOffset++; } + if (!$closed) { + // Unterminated quote: keep only the remainder of the starting line so + // later assignments are not absorbed into this value during upgrade. + $end = \strpos($data, "\n", $contentStart); + if ($end === false) { + $raw = \substr($data, $contentStart); + $offset = $length; + } else { + $raw = \substr($data, $contentStart, $end - $contentStart); + $offset = $end + 1; + } + if (\str_ends_with($raw, "\r")) { + $raw = \substr($raw, 0, -1); + } + $this->vars[$key] = $raw; + continue; + } + + $valueOffset = $scanOffset; + // Consume trailing characters until end of line (comments/whitespace). while ($valueOffset < $length && $data[$valueOffset] !== "\n" && $data[$valueOffset] !== "\r") { $valueOffset++; diff --git a/tests/unit/Docker/EnvTest.php b/tests/unit/Docker/EnvTest.php index d669b01bab..fa00cc4446 100644 --- a/tests/unit/Docker/EnvTest.php +++ b/tests/unit/Docker/EnvTest.php @@ -120,4 +120,15 @@ final class EnvTest extends TestCase $this->assertSame('example.com', $env->getVar('_APP_DOMAIN')); $this->assertCount(1, $env->list()); } + + public function testUnterminatedQuoteDoesNotAbsorbLaterAssignments(): 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')); + } }