Merge pull request #12986 from appwrite/cursor/appwrite-community-bug-fixes-aaad

fix: preserve multiline .env secrets across install/upgrade
This commit is contained in:
Chirag Aggarwal authored and GitHub committed 2026-07-27 12:46:07 +05:30
commit 4919ebcc38
4 files changed
+291 -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";
}
}
?>
+181 -19
View File
@@ -5,31 +5,168 @@ 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) {
$lineEnd = \strpos($data, "\n", $offset);
if ($lineEnd === false) {
$lineEnd = $length;
}
$line = \substr($data, $offset, $lineEnd - $offset);
// Normalize Windows newlines for line-oriented parsing of keys.
if (\str_ends_with($line, "\r")) {
$line = \substr($line, 0, -1);
}
$trimmed = \ltrim($line, " \t");
// Skip blank lines and comments (including indented comments).
if ($trimmed === '' || $trimmed[0] === '#') {
$offset = $lineEnd < $length ? $lineEnd + 1 : $length;
continue;
}
$equals = \strpos($line, '=');
if ($equals === false) {
// Skip malformed lines without an assignment on this line.
$offset = $lineEnd < $length ? $lineEnd + 1 : $length;
continue;
}
$key = \trim(\substr($line, 0, $equals));
if ($key === '') {
$offset = $lineEnd < $length ? $lineEnd + 1 : $length;
continue;
}
// Value starts after '=' in the full buffer so quoted values may span lines.
$valueOffset = $offset + $equals + 1;
// Consume optional whitespace before the value.
while ($valueOffset < $length && ($data[$valueOffset] === ' ' || $data[$valueOffset] === "\t")) {
$valueOffset++;
}
if ($valueOffset >= $length || $data[$valueOffset] === "\n" || $data[$valueOffset] === "\r") {
$this->vars[$key] = '';
$offset = $valueOffset < $length ? (($data[$valueOffset] === "\r" && $valueOffset + 1 < $length && $data[$valueOffset + 1] === "\n") ? $valueOffset + 2 : $valueOffset + 1) : $length;
continue;
}
$quote = $data[$valueOffset];
if ($quote === '"' || $quote === "'") {
$contentStart = $valueOffset + 1;
$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;
}
if ($quote === '"' && $char === '\\' && $scanOffset + 1 < $length) {
$next = $data[$scanOffset + 1];
$value .= match ($next) {
'n' => "\n",
'r' => "\r",
't' => "\t",
'\\' => '\\',
'"' => '"',
"'" => "'",
'$' => '$',
'`' => '`',
// Preserve unknown escapes as a literal backslash + next char
// (e.g. Windows paths or patterns like `\d`, `\q`).
default => '\\' . $next,
};
$scanOffset += 2;
continue;
}
$value .= $char;
$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++;
}
if ($valueOffset < $length) {
if ($data[$valueOffset] === "\r" && $valueOffset + 1 < $length && $data[$valueOffset + 1] === "\n") {
$valueOffset += 2;
} else {
$valueOffset++;
}
}
$this->vars[$key] = $value;
$offset = $valueOffset;
continue;
}
// Unquoted value: read until end of line, trim trailing whitespace.
$end = \strpos($data, "\n", $valueOffset);
if ($end === false) {
$raw = \substr($data, $valueOffset);
$offset = $length;
} else {
$raw = \substr($data, $valueOffset, $end - $valueOffset);
$offset = $end + 1;
}
if (\str_ends_with($raw, "\r")) {
$raw = \substr($raw, 0, -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");
}
}
@@ -66,6 +203,26 @@ 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\$`");
}
/**
* 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
*/
@@ -74,7 +231,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';
+105 -5
View File
@@ -36,10 +36,110 @@ 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")
);
}
public function testUnknownEscapesPreserveBackslash(): void
{
// .env contains unknown escapes \q and \d; both must keep the backslash.
$data = "_APP_PATTERN=\"prefix\\q and \\d digits\"\n";
$env = new Env($data);
$this->assertSame('prefix\\q and \\d digits', $env->getVar('_APP_PATTERN'));
}
public function testIndentedCommentDoesNotCaptureNextAssignment(): void
{
$data = " # indented note\n_APP_DOMAIN=example.com\n \n_APP_OPTIONS_FORCE_HTTPS=enabled\n";
$env = new Env($data);
$this->assertSame('example.com', $env->getVar('_APP_DOMAIN'));
$this->assertSame('enabled', $env->getVar('_APP_OPTIONS_FORCE_HTTPS'));
$this->assertArrayNotHasKey(' # indented note', $env->list());
$this->assertArrayNotHasKey('# indented note', $env->list());
}
public function testLineWithoutAssignmentIsSkipped(): void
{
$data = "NOT_AN_ASSIGNMENT\n_APP_DOMAIN=example.com\n";
$env = new Env($data);
$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'));
}
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'));
}
}