fix: preserve multiline .env secrets across install/upgrade

The Docker Env parser treated each physical line as a separate assignment,
so quoted multiline values (notably _APP_VCS_GITHUB_PRIVATE_KEY PEMs) were
split and corrupted when the installer rewrote .env during upgrades.

Parse double-quoted values with escapes/multiline support, encode newlines
when writing .env, and stop rejecting multiline install inputs.

Co-authored-by: Chirag Aggarwal <chiragaggarwal5k@gmail.com>
This commit is contained in:
Cursor AgentandChirag Aggarwal committed 2026-07-25 03:35:41 +00:00
1 parent 0c77d0321f
commit e4337538c2
4 files changed
+174 -33

No files matched your search

+3 -1
View File
@@ -6,7 +6,9 @@ foreach ($vars as $key => $value) {
if ($value === null || $value === '') {
echo $key . "=\n";
} else {
echo $key . '="' . addcslashes((string) $value, '"\\$`') . '"' . "\n";
// Escape newlines and special chars so multiline secrets (e.g. GitHub App
// private keys) survive install/upgrade rewrites as a single .env line.
echo $key . '="' . \Appwrite\Docker\Env::encodeValue((string) $value) . '"' . "\n";
}
}
?>
+118 -19
View File
@@ -5,31 +5,117 @@ namespace Appwrite\Docker;
class Env
{
/**
* @var array
* @var array<string, string|null>
*/
protected $vars = [];
public function __construct(string $data)
{
$data = explode("\n", $data);
$length = \strlen($data);
$offset = 0;
foreach ($data as &$row) {
$row = explode('=', $row, 2);
$key = trim($row[0]);
$value = (isset($row[1])) ? (function (string $v): string {
$v = trim($v);
if (
(\str_starts_with($v, '"') && \str_ends_with($v, '"')) ||
(\str_starts_with($v, "'") && \str_ends_with($v, "'"))
) {
return \substr($v, 1, -1);
}
return $v;
})(trim($row[1])) : null;
if ($key) {
$this->vars[$key] = $value;
while ($offset < $length) {
// Skip blank lines and comments.
if ($data[$offset] === "\n" || $data[$offset] === "\r") {
$offset++;
continue;
}
if ($data[$offset] === '#') {
$nextLine = \strpos($data, "\n", $offset);
$offset = $nextLine === false ? $length : $nextLine + 1;
continue;
}
$equals = \strpos($data, '=', $offset);
if ($equals === false) {
break;
}
$key = \trim(\substr($data, $offset, $equals - $offset));
$offset = $equals + 1;
if ($key === '') {
// Skip malformed lines without a key.
$nextLine = \strpos($data, "\n", $offset);
$offset = $nextLine === false ? $length : $nextLine + 1;
continue;
}
// Consume optional whitespace before the value.
while ($offset < $length && ($data[$offset] === ' ' || $data[$offset] === "\t")) {
$offset++;
}
if ($offset >= $length || $data[$offset] === "\n" || $data[$offset] === "\r") {
$this->vars[$key] = '';
if ($offset < $length) {
$offset++;
}
continue;
}
$quote = $data[$offset];
if ($quote === '"' || $quote === "'") {
$offset++;
$value = '';
while ($offset < $length) {
$char = $data[$offset];
if ($char === $quote) {
$offset++;
break;
}
if ($quote === '"' && $char === '\\' && $offset + 1 < $length) {
$next = $data[$offset + 1];
$value .= match ($next) {
'n' => "\n",
'r' => "\r",
't' => "\t",
'\\' => '\\',
'"' => '"',
"'" => "'",
'$' => '$',
'`' => '`',
default => $next,
};
$offset += 2;
continue;
}
$value .= $char;
$offset++;
}
// Consume trailing characters until end of line (comments/whitespace).
while ($offset < $length && $data[$offset] !== "\n" && $data[$offset] !== "\r") {
$offset++;
}
if ($offset < $length) {
$offset++;
}
$this->vars[$key] = $value;
continue;
}
// Unquoted value: read until end of line, trim trailing whitespace.
$end = \strpos($data, "\n", $offset);
if ($end === false) {
$raw = \substr($data, $offset);
$offset = $length;
} else {
$raw = \substr($data, $offset, $end - $offset);
$offset = $end + 1;
}
// Strip inline comments preceded by whitespace.
if (\preg_match('/\s+#/', $raw, $matches, PREG_OFFSET_CAPTURE) === 1) {
$raw = \substr($raw, 0, $matches[0][1]);
}
$this->vars[$key] = \rtrim($raw, " \t\r");
}
}
@@ -66,6 +152,14 @@ class Env
return $this->vars;
}
/**
* Encode a value for a double-quoted .env assignment.
*/
public static function encodeValue(string $value): string
{
return \addcslashes($value, "\"\\\n\r\t\$`");
}
/**
* @return string
*/
@@ -74,7 +168,12 @@ class Env
$output = '';
foreach ($this->vars as $key => $value) {
$output .= $key . '=' . $value . "\n";
if ($value === null || $value === '') {
$output .= $key . "=\n";
continue;
}
$output .= $key . '="' . self::encodeValue((string) $value) . "\"\n";
}
return $output;
+2 -8
View File
@@ -426,14 +426,8 @@ class Install extends Action
}
}
foreach ($input as $key => $value) {
if (!is_string($value)) {
continue;
}
if (str_contains($value, "\n") || str_contains($value, "\r")) {
throw new \InvalidArgumentException('Invalid value for ' . $key);
}
}
// Multiline values (e.g. GitHub App PEM private keys) are allowed; env.phtml
// encodes them as escaped single-line double-quoted assignments.
// Set database-specific connection details
$database = $input['_APP_DB_ADAPTER'] ?? 'postgresql';
+51 -5
View File
@@ -36,10 +36,56 @@ final class EnvTest extends TestCase
public function testExport(): void
{
$this->assertSame("_APP_X=value1
_APP_Y=value2
_APP_Z=value3
_APP_W=value5=
", $this->object->export());
$this->assertSame(
"_APP_X=\"value1\"\n_APP_Y=\"value2\"\n_APP_Z=\"value3\"\n_APP_W=\"value5=\"\n",
$this->object->export()
);
}
public function testMultilineDoubleQuotedPrivateKey(): void
{
$pem = "-----BEGIN RSA PRIVATE KEY-----\nABCDEF\n-----END RSA PRIVATE KEY-----";
$data = "_APP_VCS_GITHUB_PRIVATE_KEY=\"-----BEGIN RSA PRIVATE KEY-----\nABCDEF\n-----END RSA PRIVATE KEY-----\"\n_APP_OTHER=ok\n";
$env = new Env($data);
$this->assertSame($pem, $env->getVar('_APP_VCS_GITHUB_PRIVATE_KEY'));
$this->assertSame('ok', $env->getVar('_APP_OTHER'));
}
public function testEscapedNewlineInDoubleQuotedValue(): void
{
$data = "_APP_VCS_GITHUB_PRIVATE_KEY=\"-----BEGIN RSA PRIVATE KEY-----\\nABCDEF\\n-----END RSA PRIVATE KEY-----\"\n";
$env = new Env($data);
$this->assertSame(
"-----BEGIN RSA PRIVATE KEY-----\nABCDEF\n-----END RSA PRIVATE KEY-----",
$env->getVar('_APP_VCS_GITHUB_PRIVATE_KEY')
);
}
public function testRoundTripPreservesMultilineSecret(): void
{
$pem = "-----BEGIN RSA PRIVATE KEY-----\nLINE1\nLINE2\n-----END RSA PRIVATE KEY-----";
$env = new Env('');
$env->setVar('_APP_VCS_GITHUB_PRIVATE_KEY', $pem);
$env->setVar('_APP_DOMAIN', 'example.com');
$exported = $env->export();
$reloaded = new Env($exported);
$this->assertSame($pem, $reloaded->getVar('_APP_VCS_GITHUB_PRIVATE_KEY'));
$this->assertSame('example.com', $reloaded->getVar('_APP_DOMAIN'));
$this->assertStringNotContainsString("\nLINE1\n", $exported);
$this->assertStringContainsString('\\nLINE1\\n', $exported);
}
public function testEncodeValueEscapesSpecialCharacters(): void
{
$this->assertSame(
'say\\"hi\\nnext\\$path',
Env::encodeValue("say\"hi\nnext\$path")
);
}
}