mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
fix(cli): keep values in env load hints from running as shell code
The hint is a comment in the output people feed to eval, so a line break in a selector value ended the comment and ran the rest. Such a value now prints as a placeholder. Fish and PowerShell always get quotes, because the plain-word rule is POSIX and @team is a splat in PowerShell. Addresses the review of #74. Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
3 files changed
+58
-1
No files matched your search
@@ -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.
|
||||
*
|
||||
* <p>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 "<value>";
|
||||
return ShellExport.isPosix(shell) && PLAIN_WORD.matcher(value).matches()
|
||||
? value : ShellExport.quoted(shell, value);
|
||||
});
|
||||
return profile != null ? selector + " -o text" : selector;
|
||||
}
|
||||
|
||||
|
||||
@@ -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()) {
|
||||
|
||||
@@ -116,6 +116,39 @@ class InstanceHintsTest {
|
||||
assertTrue(out.contains("--profile 'team\\'s' -o text --shell fish | source"), out);
|
||||
}
|
||||
}
|
||||
private List<Callable<Integer>> 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<Integer> 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 <value>"), result.out());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@Test
|
||||
void nonPosixShellsQuoteEvenAPlainValue() throws Exception {
|
||||
for (Callable<Integer> env : allEnvCommands()) {
|
||||
String out = run(env, "--container", "@team", "--endpoint", "http://localhost:1", "--shell", "powershell").out();
|
||||
assertTrue(out.contains("--container '@team'"), out);
|
||||
}
|
||||
for (Callable<Integer> env : allEnvCommands()) {
|
||||
String out = run(env, "--container", "@team", "--endpoint", "http://localhost:1", "--shell", "fish").out();
|
||||
assertTrue(out.contains("--container '@team'"), out);
|
||||
}
|
||||
for (Callable<Integer> 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");
|
||||
|
||||
Reference in new issue
Block a user