diff --git a/CHANGELOG.md b/CHANGELOG.md index 092cb97..8d7a04c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index bf78634..7dde45c 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/main/java/io/floci/cli/commands/EnvCommand.java b/src/main/java/io/floci/cli/commands/EnvCommand.java index 066d506..80c83b6 100644 --- a/src/main/java/io/floci/cli/commands/EnvCommand.java +++ b/src/main/java/io/floci/cli/commands/EnvCommand.java @@ -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 { } for (Map.Entry 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(); diff --git a/src/main/java/io/floci/cli/commands/az/AzEnvCommand.java b/src/main/java/io/floci/cli/commands/az/AzEnvCommand.java index 99c2e1c..2451230 100644 --- a/src/main/java/io/floci/cli/commands/az/AzEnvCommand.java +++ b/src/main/java/io/floci/cli/commands/az/AzEnvCommand.java @@ -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 { 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 { }; } - 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(); diff --git a/src/main/java/io/floci/cli/commands/gcp/GcpEnvCommand.java b/src/main/java/io/floci/cli/commands/gcp/GcpEnvCommand.java index eb18de6..26838a1 100644 --- a/src/main/java/io/floci/cli/commands/gcp/GcpEnvCommand.java +++ b/src/main/java/io/floci/cli/commands/gcp/GcpEnvCommand.java @@ -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 { } for (Map.Entry 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 { 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(); diff --git a/src/main/java/io/floci/cli/commands/oci/OciEnvCommand.java b/src/main/java/io/floci/cli/commands/oci/OciEnvCommand.java index 19032cf..ff01250 100644 --- a/src/main/java/io/floci/cli/commands/oci/OciEnvCommand.java +++ b/src/main/java/io/floci/cli/commands/oci/OciEnvCommand.java @@ -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 { } for (Map.Entry 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("'", "'\\''") + "'"; - }; - } } diff --git a/src/main/java/io/floci/cli/doctor/checks/AwsCliEndpointCheck.java b/src/main/java/io/floci/cli/doctor/checks/AwsCliEndpointCheck.java index 53f376a..ecb4436 100644 --- a/src/main/java/io/floci/cli/doctor/checks/AwsCliEndpointCheck.java +++ b/src/main/java/io/floci/cli/doctor/checks/AwsCliEndpointCheck.java @@ -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) { diff --git a/src/main/java/io/floci/cli/doctor/checks/AzCliConnectionStringCheck.java b/src/main/java/io/floci/cli/doctor/checks/AzCliConnectionStringCheck.java index 33ce515..782a7f0 100644 --- a/src/main/java/io/floci/cli/doctor/checks/AzCliConnectionStringCheck.java +++ b/src/main/java/io/floci/cli/doctor/checks/AzCliConnectionStringCheck.java @@ -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) { diff --git a/src/main/java/io/floci/cli/output/ShellExport.java b/src/main/java/io/floci/cli/output/ShellExport.java new file mode 100644 index 0000000..9bab23d --- /dev/null +++ b/src/main/java/io/floci/cli/output/ShellExport.java @@ -0,0 +1,28 @@ +package io.floci.cli.output; + +/** + * Renders {@code export KEY=VALUE} lines for the {@code env} commands of every product tree. + * + *

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("'", "'\\''") + "'"; + }; + } +} diff --git a/src/test/java/io/floci/cli/unit/OciEnvFormatTest.java b/src/test/java/io/floci/cli/unit/OciEnvFormatTest.java deleted file mode 100644 index 6d9e7e8..0000000 --- a/src/test/java/io/floci/cli/unit/OciEnvFormatTest.java +++ /dev/null @@ -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")); - } -} diff --git a/src/test/java/io/floci/cli/unit/ShellExportTest.java b/src/test/java/io/floci/cli/unit/ShellExportTest.java new file mode 100644 index 0000000..ae8c11b --- /dev/null +++ b/src/test/java/io/floci/cli/unit/ShellExportTest.java @@ -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")); + } +}