Files
floci-cli/src/main/java/io/floci/cli/output/ShellExport.java
T
Hector Ventura 6496795708 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
2026-08-26 22:12:29 -05:00

29 lines
1.4 KiB
Java

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