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

+4
View File
@@ -7,6 +7,10 @@ This project uses [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Fixed
- `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`. All four trees now share one emitter (`ShellExport`) that single-quotes values with per-shell escaping for bash, fish, and PowerShell (the form `floci oci env` already used), so `;`, `$`, backticks, and quotes in a value can no longer break or inject through `eval`. `floci env` and `floci gcp env` carried the same unquoted emitter, and the `aws.cli.endpoint` / `az.cli.connection-string` doctor hints suggested export lines with the same flaw; both are fixed ([#18](https://github.com/floci-io/floci-cli/issues/18))
## [0.2.0] — 2026-07-28
### Changed
+1
View File
@@ -443,6 +443,7 @@ Variables exported (per enabled service):
### `floci az env`
Prints Azure connection variables for the running Floci Azure instance.
The connection string contains `;` separators, so the emitted value is single-quoted (as it is in every tree's `env` output) and is safe to `eval`.
```sh
eval $(floci az env) # connection string (default)
@@ -5,6 +5,7 @@ import io.floci.cli.docker.DockerClient;
import io.floci.cli.output.Ansi;
import io.floci.cli.output.OutputFormat;
import io.floci.cli.output.Printer;
import io.floci.cli.output.ShellExport;
import picocli.CommandLine.*;
import java.net.URI;
@@ -59,21 +60,13 @@ public class EnvCommand implements Callable<Integer> {
}
for (Map.Entry<String, String> entry : vars.entrySet()) {
printer.println(formatExport(entry.getKey(), entry.getValue()));
printer.println(ShellExport.formatExport(shell, entry.getKey(), entry.getValue()));
}
printer.println("");
printer.println(Ansi.gray("# Run: eval $(floci env)"));
return 0;
}
private String formatExport(String key, String value) {
return switch (shell.toLowerCase()) {
case "fish" -> "set -x " + key + " \"" + value + "\"";
case "powershell", "ps1" -> "$env:" + key + " = \"" + value + "\"";
default -> "export " + key + "=" + value;
};
}
private int extractPort(String endpoint) {
try {
int port = URI.create(endpoint).getPort();
@@ -7,6 +7,7 @@ import io.floci.cli.doctor.checks.AzCliConnectionStringCheck;
import io.floci.cli.output.Ansi;
import io.floci.cli.output.OutputFormat;
import io.floci.cli.output.Printer;
import io.floci.cli.output.ShellExport;
import picocli.CommandLine.*;
import java.net.URI;
@@ -90,14 +91,14 @@ public class AzEnvCommand implements Callable<Integer> {
if (!sdkVarsMode) {
String connStr = AzCliConnectionStringCheck.buildConnectionString(account, host, port);
printer.println(formatExport("AZURE_STORAGE_CONNECTION_STRING", connStr));
printer.println(ShellExport.formatExport(shell, "AZURE_STORAGE_CONNECTION_STRING", connStr));
printer.println("");
printer.println(Ansi.gray("# Run: eval $(floci az env)"));
} else {
printer.println(formatExport("AZURE_STORAGE_ACCOUNT", account));
printer.println(formatExport("AZURE_STORAGE_KEY", DEV_KEY));
printer.println(ShellExport.formatExport(shell, "AZURE_STORAGE_ACCOUNT", account));
printer.println(ShellExport.formatExport(shell, "AZURE_STORAGE_KEY", DEV_KEY));
for (String svc : requestedServices) {
printer.println(formatExport(envVarName(svc), buildEndpointUrl(svc, host, port, account)));
printer.println(ShellExport.formatExport(shell, envVarName(svc), buildEndpointUrl(svc, host, port, account)));
}
printer.println("");
printer.println(Ansi.gray("# Run: eval $(floci az env --format sdk-vars)"));
@@ -143,14 +144,6 @@ public class AzEnvCommand implements Callable<Integer> {
};
}
private String formatExport(String key, String value) {
return switch (shell.toLowerCase()) {
case "fish" -> "set -x " + key + " \"" + value + "\"";
case "powershell", "ps1" -> "$env:" + key + " = \"" + value + "\"";
default -> "export " + key + "=" + value;
};
}
private int extractPort(String endpoint) {
try {
int p = URI.create(endpoint).getPort();
@@ -6,6 +6,7 @@ import io.floci.cli.docker.DockerClient;
import io.floci.cli.output.Ansi;
import io.floci.cli.output.OutputFormat;
import io.floci.cli.output.Printer;
import io.floci.cli.output.ShellExport;
import picocli.CommandLine.*;
import java.net.URI;
@@ -63,7 +64,7 @@ public class GcpEnvCommand implements Callable<Integer> {
}
for (Map.Entry<String, String> entry : vars.entrySet()) {
printer.println(formatExport(entry.getKey(), entry.getValue()));
printer.println(ShellExport.formatExport(shell, entry.getKey(), entry.getValue()));
}
printer.println("");
printer.println(Ansi.gray("# Run: eval $(floci gcp env)"));
@@ -103,14 +104,6 @@ public class GcpEnvCommand implements Callable<Integer> {
return hostPort;
}
private String formatExport(String key, String value) {
return switch (shell.toLowerCase()) {
case "fish" -> "set -x " + key + " \"" + value + "\"";
case "powershell", "ps1" -> "$env:" + key + " = \"" + value + "\"";
default -> "export " + key + "=" + value;
};
}
private String extractHost(String endpoint) {
try {
String h = URI.create(endpoint).getHost();
@@ -6,6 +6,7 @@ import io.floci.cli.docker.DockerClient;
import io.floci.cli.output.Ansi;
import io.floci.cli.output.OutputFormat;
import io.floci.cli.output.Printer;
import io.floci.cli.output.ShellExport;
import picocli.CommandLine.*;
import java.nio.file.Path;
@@ -59,27 +60,10 @@ public class OciEnvCommand implements Callable<Integer> {
}
for (Map.Entry<String, String> entry : vars.entrySet()) {
printer.println(formatExport(shell, entry.getKey(), entry.getValue()));
printer.println(ShellExport.formatExport(shell, entry.getKey(), entry.getValue()));
}
printer.println("");
printer.println(Ansi.gray("# Run: eval $(floci oci env)"));
return 0;
}
/**
* Renders one export line. The output is documented as {@code eval} input, so values
* are single-quoted with per-shell escaping — no interpolation of {@code $}, backticks,
* or quotes can occur even for hostile {@code --endpoint} / env-var values.
*/
public static String formatExport(String shell, String key, String value) {
return switch (shell.toLowerCase()) {
// fish single quotes: only \' and \\ are escapes, backslash must be doubled first
case "fish" -> "set -x " + key + " '"
+ value.replace("\\", "\\\\").replace("'", "\\'") + "'";
// PowerShell single quotes: literal except '' for a quote
case "powershell", "ps1" -> "$env:" + key + " = '" + value.replace("'", "''") + "'";
// POSIX single quotes: close, escaped quote, reopen
default -> "export " + key + "='" + value.replace("'", "'\\''") + "'";
};
}
}
@@ -2,6 +2,7 @@ package io.floci.cli.doctor.checks;
import io.floci.cli.doctor.Check;
import io.floci.cli.doctor.CheckResult;
import io.floci.cli.output.ShellExport;
import java.net.URI;
import java.net.URISyntaxException;
@@ -21,14 +22,14 @@ public class AwsCliEndpointCheck implements Check {
String corrected = replacePort(envVar, effectivePort);
return CheckResult.warn("aws.cli.endpoint",
"AWS_ENDPOINT_URL=" + envVar + " (port mismatch — Floci is on port " + effectivePort + ")",
"export AWS_ENDPOINT_URL=" + corrected);
ShellExport.formatExport("bash", "AWS_ENDPOINT_URL", corrected));
}
return CheckResult.ok("aws.cli.endpoint", "AWS_ENDPOINT_URL=" + envVar);
}
String suggested = "http://localhost.floci.io:" + (effectivePort != -1 ? effectivePort : 4566);
return CheckResult.warn("aws.cli.endpoint",
"AWS_ENDPOINT_URL is not set",
"export AWS_ENDPOINT_URL=" + suggested);
ShellExport.formatExport("bash", "AWS_ENDPOINT_URL", suggested));
}
private int extractPort(String url) {
@@ -2,6 +2,7 @@ package io.floci.cli.doctor.checks;
import io.floci.cli.doctor.Check;
import io.floci.cli.doctor.CheckResult;
import io.floci.cli.output.ShellExport;
import java.net.URI;
import java.util.regex.Matcher;
@@ -25,7 +26,7 @@ public class AzCliConnectionStringCheck implements Check {
String corrected = connStr.replaceAll(":(\\d+)/", ":" + effectivePort + "/");
return CheckResult.warn("az.cli.connection-string",
"AZURE_STORAGE_CONNECTION_STRING port mismatch — floci-az is on port " + effectivePort,
"export AZURE_STORAGE_CONNECTION_STRING=\"" + corrected + "\"");
ShellExport.formatExport("bash", "AZURE_STORAGE_CONNECTION_STRING", corrected));
}
}
return CheckResult.ok("az.cli.connection-string", "AZURE_STORAGE_CONNECTION_STRING is set");
@@ -35,7 +36,7 @@ public class AzCliConnectionStringCheck implements Check {
String suggested = buildConnectionString("devstoreaccount1", "localhost.floci.io", port);
return CheckResult.warn("az.cli.connection-string",
"AZURE_STORAGE_CONNECTION_STRING is not set",
"export AZURE_STORAGE_CONNECTION_STRING=\"" + suggested + "\"");
ShellExport.formatExport("bash", "AZURE_STORAGE_CONNECTION_STRING", suggested));
}
private int extractPort(String url) {
@@ -0,0 +1,28 @@
package io.floci.cli.output;
/**
* Renders {@code export KEY=VALUE} lines for the {@code env} commands of every product tree.
*
* <p>The output is documented as {@code eval} input, so values are single-quoted with per-shell
* escaping — no interpolation of {@code $}, backticks, or quotes can occur even for hostile
* {@code --endpoint} / {@code --host} / env-var values, and values containing {@code ;}
* (the Azure connection string, OCI's {@code TF_VAR_CLIENT_HOST_OVERRIDES}) survive intact.
*/
public final class ShellExport {
private ShellExport() {
}
/** Renders one export line for the given shell ({@code bash}, {@code fish}, {@code powershell}). */
public static String formatExport(String shell, String key, String value) {
return switch (shell.toLowerCase()) {
// fish single quotes: only \' and \\ are escapes, backslash must be doubled first
case "fish" -> "set -x " + key + " '"
+ value.replace("\\", "\\\\").replace("'", "\\'") + "'";
// PowerShell single quotes: literal except '' for a quote
case "powershell", "ps1" -> "$env:" + key + " = '" + value.replace("'", "''") + "'";
// POSIX single quotes: close, escaped quote, reopen
default -> "export " + key + "='" + value.replace("'", "'\\''") + "'";
};
}
}
@@ -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"));
}
}