fix(profile): keep legacy profile names usable and expand values consistently (#25)

* fix(profile): keep legacy profile names usable and expand values consistently

Two edges of the 0.2.2 profile work, both found by an oversight pass over what
landed since 0.2.1.

Profile names that earlier versions accepted became unreachable. The path
traversal guard added in #23 was an allow-list over the whole character set
([A-Za-z0-9._-]+), which also rejects names 0.2.1 created happily: a space, a
plus, an at sign. Such a profile stayed visible in 'config profile list' while
show, --profile and delete all refused it, so it could not even be removed
through the CLI.

The guard is now a deny-list on what actually traverses ('.', '..', and path
separators), and the guarantee is carried by resolving the file and asserting
its parent is the profiles directory. A name that is illegal on the running
platform but legal elsewhere, such as a colon on Windows, now reports a clean
message instead of InvalidPathException.

Profile values were expanded on one path and not the others. Interpolation of
${env:HOME} and friends is picocli's, applied when it fills an option default,
so 'start --profile p' expanded it while 'restart --profile p' and
'config show --profile p' read the Profile bean directly and got the literal
text. Restart therefore asked docker for a directory named '${env:HOME}/...',
which is the silent state loss #23 set out to fix.

Both now resolve through StartCommand.resolvedFor, the same provider and the
same precedence a real start invocation uses, so one profile cannot mean two
different directories. That also removes the duplicated field mapping in
RestartCommand: ProfileDefaults is the only place a profile field maps to a
flag again.

Restart now resolves the profile before stopping the container, so a profile
that fails to resolve no longer leaves the container stopped.

* fix(profile): stop denying backslash, and read the profile once in config show

Two P2s from the review on this PR, both correct.

Backslash is a separator on Windows and an ordinary file-name character on
Unix. Denying it outright orphaned a 'team\alpha' profile that 0.2.1 could
create and that list() still returns, which is the same bug class this PR
exists to fix, left half fixed. The character check is now forward slash only;
resolveInProfilesDir already rejects a real Windows traversal on the parent
assertion, so the guarantee is unchanged.

config show read the profile twice: key presence from one read, values from a
second through resolvedFor. A concurrent edit could print half of one version
next to half of another. ProfileDefaultValueProvider already memoizes the
profile it resolved, so exposing that memo gives both from one snapshot, and
resolvedFor gained a provider overload to pass it through. If the profile has
gone since the outer parse, the command now fails loudly instead of printing a
mixed row.

RestartCommand has the same shape between the outer parse and resolvedFor.
Closing it means making the outer provider reachable from commands, which is
wider than this fix; documented in place rather than left silent.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-09-14 21:50:32 -05:00
1 parent 3159a653b4
commit 7343a539c2
9 files changed
+231 -49

No files matched your search

@@ -5,6 +5,7 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
@@ -17,7 +18,18 @@ public class ProfileStore {
// A profile name becomes a file name, so it must not be able to traverse out of the
// profiles directory: 'floci config profile delete ../../foo' resolved the raw name.
private static final Pattern VALID_NAME = Pattern.compile("[A-Za-z0-9._-]+");
//
// This is a deny-list on purpose. An allow-list over the whole character set also rejects
// names earlier versions accepted (a space, '+', '@'), which left those profiles listed by
// 'config profile list' and unreachable by show, --profile and delete, with no way to remove
// them through the CLI. The guarantee is carried by resolveInProfilesDir below, not by the
// character rules.
//
// Backslash is deliberately NOT here. Java NIO treats it as a separator on Windows, where
// resolveInProfilesDir already rejects '..\\..\\escape' on the parent check, and as an
// ordinary file-name character on Unix, where 0.2.1 could create 'team\\alpha.yaml' and
// list() still returns it. Denying it would orphan that profile on Unix for no gain.
private static final Pattern PATH_SEPARATOR = Pattern.compile("/");
private final Path profilesDir;
@@ -39,9 +51,9 @@ public class ProfileStore {
"Profile name must not be empty.\n"
+ "Run 'floci config profile list' to see available profiles.");
}
if (".".equals(name) || "..".equals(name) || !VALID_NAME.matcher(name).matches()) {
if (".".equals(name) || "..".equals(name) || PATH_SEPARATOR.matcher(name).find()) {
throw new IllegalArgumentException(
"Invalid profile name '" + name + "'. Use only letters, digits, '.', '_' and '-'.\n"
"Invalid profile name '" + name + "'. It must not be '.', '..', or contain '/'.\n"
+ "Run 'floci config profile list' to see available profiles.");
}
return name;
@@ -85,7 +97,28 @@ public class ProfileStore {
/** Where {@link #save} writes {@code name}. Always {@code .yaml}. */
public Path profileFile(String name) {
return profilesDir.resolve(validateName(name) + ".yaml");
return resolveInProfilesDir(validateName(name) + ".yaml");
}
// The traversal guarantee: whatever the name looks like, the file it resolves to must sit
// directly in profilesDir. Also the place a name that is illegal on this platform but legal
// on another (a colon on Windows) turns into a clean message instead of InvalidPathException.
private Path resolveInProfilesDir(String fileName) {
Path dir = profilesDir.toAbsolutePath().normalize();
Path resolved;
try {
resolved = dir.resolve(fileName).normalize();
} catch (InvalidPathException e) {
throw new IllegalArgumentException(
"Invalid profile name '" + fileName + "': not a valid file name on this platform.\n"
+ "Run 'floci config profile list' to see available profiles.");
}
if (!dir.equals(resolved.getParent())) {
throw new IllegalArgumentException(
"Invalid profile name '" + fileName + "': it would resolve outside " + profilesDir + ".\n"
+ "Run 'floci config profile list' to see available profiles.");
}
return resolved;
}
// list() has always accepted .yml, so reads must too — otherwise a .yml profile shows up in
@@ -93,7 +126,7 @@ public class ProfileStore {
private Path existingProfileFile(String name) {
Path yaml = profileFile(name);
if (Files.exists(yaml)) return yaml;
Path yml = profilesDir.resolve(validateName(name) + ".yml");
Path yml = resolveInProfilesDir(validateName(name) + ".yml");
return Files.exists(yml) ? yml : yaml;
}