fix: keep unterminated .env quotes from swallowing keys

If a quoted assignment never closes, take only the remainder of that
line so install/upgrade cannot drop later environment variables.

Co-authored-by: Chirag Aggarwal <chiragaggarwal5k@gmail.com>
This commit is contained in:
Cursor AgentandChirag Aggarwal committed 2026-07-27 05:42:37 +00:00
1 parent c7f24a13d8
commit 0b329f3247
2 files changed
+43 -8

No files matched your search

+32 -8
View File
@@ -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++;
+11
View File
@@ -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'));
}
}