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

@@ -2,13 +2,10 @@ package io.floci.cli.commands;
import io.floci.cli.GlobalOptions;
import io.floci.cli.ProductProfile;
import io.floci.cli.config.Profile;
import io.floci.cli.config.ProfileStore;
import io.floci.cli.output.Printer;
import picocli.CommandLine.*;
import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.Callable;
@Command(
@@ -44,6 +41,11 @@ public class RestartCommand implements Callable<Integer> {
public Integer call() {
Printer printer = global.printer();
// Resolve the profile BEFORE stopping anything. A failure here must not leave the
// container stopped, and the values must not be re-read from a file that could change
// during the stop plus the one second wait below.
StartCommand start = buildStartCommand();
StopCommand stop = new StopCommand(profile);
stop.global = global;
stop.remove = false;
@@ -54,40 +56,30 @@ public class RestartCommand implements Callable<Integer> {
// Minimal wait to avoid port-already-in-use races
try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); }
return buildStartCommand().call();
return start.call();
}
/**
* The {@code start} this restart will run. Picocli never parses this instance, so the profile
* that {@code ProfileDefaultValueProvider} applied to the parsed command line has to be
* carried across by hand — without it, {@code restart --profile x} would silently drop the
* profile's persistence directory and service list.
* has to be applied by hand, and copying fields out of the Profile bean is not good enough:
* values reach a real {@code start} through picocli, which interpolates {@code ${env:HOME}}
* and friends. Going through {@link StartCommand#resolvedFor} keeps the two paths on the same
* provider, the same precedence and the same interpolation, so one profile cannot mean one
* directory on {@code start} and a different one on {@code restart}.
*/
public StartCommand buildStartCommand() {
StartCommand start = new StartCommand(profile);
StartCommand start = StartCommand.resolvedFor(profile, store, global.profile);
// The real invocation's globals win: they already carry the profile plus any flag the
// user passed, resolved once by the outer parse.
//
// Known residual: those globals come from the outer parse's read of the profile while the
// fields above come from resolvedFor's, so a profile edited between the two would mix
// snapshots. Closing it means making the outer provider reachable from commands, which is
// a wider change than this fix; tracked as batman LAY-1.
start.global = global;
start.pull = "missing";
start.detach = false;
// port and image already hold the product defaults, set by the StartCommand constructor.
if (global.profile != null) {
loadProfile().ifPresent(p -> {
if (p.port != null) start.port = p.port;
if (p.image != null) start.image = p.image;
if (p.persistDir != null) start.persistDir = p.persistDir;
if (p.services != null) start.services = p.services;
});
}
return start;
}
private Optional<Profile> loadProfile() {
try {
return store.get(global.profile);
} catch (IOException | IllegalArgumentException e) {
// Parsing already resolved this name, so a failure here cannot happen in practice;
// keeping the product defaults beats aborting a restart that has already stopped.
return Optional.empty();
}
}
}