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

@@ -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("'", "'\\''") + "'";
};
}
}