fix(env): single-quote exported values in every product tree (#19)

`floci az env` printed the semicolon-separated Azure connection string
unquoted, so `eval $(floci az env)` set only
AZURE_STORAGE_CONNECTION_STRING=DefaultEndpointsProtocol=http and ran the
remaining segments as commands — `az` then reported "Invalid connection
string".

`floci env` and `floci gcp env` carried a byte-identical copy of the same
unquoted emitter (their values are `;`-free URLs today, so nobody hit it),
and the aws.cli.endpoint / az.cli.connection-string doctor checks suggested
export lines with the same flaw.

Extract the correct emitter that `floci oci env` already had into
io.floci.cli.output.ShellExport and use it everywhere: values are single
quoted with per-shell escaping for bash, fish, and PowerShell, so `;`, `$`,
backticks, and quotes can neither break nor inject through `eval`.

OciEnvFormatTest becomes ShellExportTest, keeping its four assertions and
adding the connection-string regression from the issue.

Closes #18
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-08-26 22:12:29 -05:00
1 parent fee32a76a1
commit 6496795708
11 files changed
+99 -91

No files matched your search

@@ -1,39 +0,0 @@
package io.floci.cli.unit;
import io.floci.cli.commands.oci.OciEnvCommand;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/** Pins the shell-quoting of `floci oci env` output — it is documented as `eval` input. */
class OciEnvFormatTest {
@Test
void bashExportsAreSingleQuoted() {
assertEquals("export OCI_CLI_ENDPOINT='http://localhost:4599'",
OciEnvCommand.formatExport("bash", "OCI_CLI_ENDPOINT", "http://localhost:4599"));
}
@Test
void hostileValuesCannotEscapeBashQuoting() {
String hostile = "http://x\"; echo pwned; $(touch /tmp/pwned) `id` $HOME";
String export = OciEnvCommand.formatExport("bash", "K", hostile);
// Entire value stays inside single quotes; no interpolation possible.
assertEquals("export K='" + hostile + "'", export);
String withQuote = "it's";
assertEquals("export K='it'\\''s'", OciEnvCommand.formatExport("bash", "K", withQuote));
}
@Test
void fishEscapesBackslashesAndQuotes() {
assertEquals("set -x K 'a\\\\b\\'c'",
OciEnvCommand.formatExport("fish", "K", "a\\b'c"));
}
@Test
void powershellDoublesSingleQuotes() {
assertEquals("$env:K = 'it''s $x'",
OciEnvCommand.formatExport("powershell", "K", "it's $x"));
}
}
@@ -0,0 +1,49 @@
package io.floci.cli.unit;
import io.floci.cli.output.ShellExport;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/** Pins the shell-quoting of every tree's `env` output — it is documented as `eval` input. */
class ShellExportTest {
@Test
void bashExportsAreSingleQuoted() {
assertEquals("export OCI_CLI_ENDPOINT='http://localhost:4599'",
ShellExport.formatExport("bash", "OCI_CLI_ENDPOINT", "http://localhost:4599"));
}
/** Regression for #18: the semicolons must not terminate the export under `eval`. */
@Test
void semicolonSeparatedAzureConnectionStringStaysOneAssignment() {
String conn = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;"
+ "AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMh0==;"
+ "BlobEndpoint=http://localhost.floci.io:4577/devstoreaccount1;";
assertEquals("export AZURE_STORAGE_CONNECTION_STRING='" + conn + "'",
ShellExport.formatExport("bash", "AZURE_STORAGE_CONNECTION_STRING", conn));
}
@Test
void hostileValuesCannotEscapeBashQuoting() {
String hostile = "http://x\"; echo pwned; $(touch /tmp/pwned) `id` $HOME";
String export = ShellExport.formatExport("bash", "K", hostile);
// Entire value stays inside single quotes; no interpolation possible.
assertEquals("export K='" + hostile + "'", export);
String withQuote = "it's";
assertEquals("export K='it'\\''s'", ShellExport.formatExport("bash", "K", withQuote));
}
@Test
void fishEscapesBackslashesAndQuotes() {
assertEquals("set -x K 'a\\\\b\\'c'",
ShellExport.formatExport("fish", "K", "a\\b'c"));
}
@Test
void powershellDoublesSingleQuotes() {
assertEquals("$env:K = 'it''s $x'",
ShellExport.formatExport("powershell", "K", "it's $x"));
}
}