diff --git a/src/main/java/io/floci/cli/GlobalOptions.java b/src/main/java/io/floci/cli/GlobalOptions.java index d2d0700..191fba9 100644 --- a/src/main/java/io/floci/cli/GlobalOptions.java +++ b/src/main/java/io/floci/cli/GlobalOptions.java @@ -113,9 +113,18 @@ public class GlobalOptions { * The flags for a hint that loads {@code env} in {@code shell}: the selector, quoted for that * shell, and {@code -o text} after a profile, which may set {@code output: json} that no shell * can load. + * + *

This hint is a comment in the output people feed to {@code eval}, so a line break in a + * value would end the comment and run the rest as code. No quoting survives that: such a value + * becomes a placeholder. PLAIN_WORD is a POSIX rule ({@code @team} is a splat in PowerShell), + * so other shells always get quotes. */ public String envLoadFlags(String shell) { - String selector = selector(value -> PLAIN_WORD.matcher(value).matches() ? value : ShellExport.quoted(shell, value)); + String selector = selector(value -> { + if (ShellExport.breaksLine(value)) return ""; + return ShellExport.isPosix(shell) && PLAIN_WORD.matcher(value).matches() + ? value : ShellExport.quoted(shell, value); + }); return profile != null ? selector + " -o text" : selector; } diff --git a/src/main/java/io/floci/cli/output/ShellExport.java b/src/main/java/io/floci/cli/output/ShellExport.java index ef7d0ce..d732250 100644 --- a/src/main/java/io/floci/cli/output/ShellExport.java +++ b/src/main/java/io/floci/cli/output/ShellExport.java @@ -39,6 +39,21 @@ public final class ShellExport { }; } + /** Whether {@code shell} is one of the POSIX family, as opposed to fish or PowerShell. */ + public static boolean isPosix(String shell) { + return switch (shell.toLowerCase()) { + case "fish", "powershell", "pwsh", "ps1" -> false; + default -> true; + }; + } + + /** Whether {@code value} could end the comment line it is printed in, in any shell. */ + public static boolean breaksLine(String value) { + return value.chars().anyMatch(c -> Character.isISOControl(c) + || Character.getType(c) == Character.LINE_SEPARATOR + || Character.getType(c) == Character.PARAGRAPH_SEPARATOR); + } + /** {@code value} as one single-quoted argument in the given shell. */ public static String quoted(String shell, String value) { return switch (shell.toLowerCase()) { diff --git a/src/test/java/io/floci/cli/unit/InstanceHintsTest.java b/src/test/java/io/floci/cli/unit/InstanceHintsTest.java index ebbb296..b1a3bb9 100644 --- a/src/test/java/io/floci/cli/unit/InstanceHintsTest.java +++ b/src/test/java/io/floci/cli/unit/InstanceHintsTest.java @@ -116,6 +116,39 @@ class InstanceHintsTest { assertTrue(out.contains("--profile 'team\\'s' -o text --shell fish | source"), out); } } + private List> allEnvCommands() { + return List.of(new EnvCommand(new FakeDocker()), new GcpEnvCommand(new FakeDocker()), new OciEnvCommand(new FakeDocker()), + new AzEnvCommand(temp, endpoint -> { throw new AssertionError("no cert expected"); }, new FakeDocker())); + } + @Test + void aValueWithALineBreakCannotEscapeTheHintComment() throws Exception { + for (String breaker : List.of("\n", "\r", "\u0085", "\u2028", "\u2029")) { + for (String shell : List.of("bash", "fish", "powershell")) { + for (Callable env : allEnvCommands()) { + Run result = run(env, "--container", "x" + breaker + "touch injected" + breaker + "#", + "--endpoint", "http://localhost:1", "--shell", shell); + assertEquals(0, result.exit(), result.err()); + assertFalse(result.out().contains("touch injected"), result.out()); + assertTrue(result.out().contains("--container "), result.out()); + } + } + } + } + @Test + void nonPosixShellsQuoteEvenAPlainValue() throws Exception { + for (Callable env : allEnvCommands()) { + String out = run(env, "--container", "@team", "--endpoint", "http://localhost:1", "--shell", "powershell").out(); + assertTrue(out.contains("--container '@team'"), out); + } + for (Callable env : allEnvCommands()) { + String out = run(env, "--container", "@team", "--endpoint", "http://localhost:1", "--shell", "fish").out(); + assertTrue(out.contains("--container '@team'"), out); + } + for (Callable env : allEnvCommands()) { + String out = run(env, "--container", "@team", "--endpoint", "http://localhost:1").out(); + assertTrue(out.contains("--container @team"), out); + } + } @Test void aProfileThatAsksForJsonStillGetsALoadableHint() throws Exception { Files.writeString(temp.resolve("json.yaml"), "container: other\nendpoint: http://127.0.0.1:1\noutput: json\n");