fix(start): refuse a relative --persist path for a remote daemon

A remote daemon cannot resolve a path relative to the CLI's working directory, and docker reads a bare name as a named volume, so the state would land somewhere a later restart does not carry over.
This commit is contained in:
Hector Ventura committed 2026-10-10 17:52:02 -05:00
1 parent 1a3b402030
commit 0a7d3364d0
2 files changed
+39 -4

No files matched your search

@@ -146,6 +146,11 @@ public class StartCommand implements Callable<Integer> {
* resolving it here would apply this machine's rules to it (a Windows CLI turns
* {@code /srv/data} into a drive path). Only a local daemon gets {@link #persistPath()}.
*/
// Absolute on the daemon's machine, whichever system that is: a leading slash, a drive
// letter with a separator, or a UNC share.
// Not decided by this machine's Path rules, which know only its own file system.
private static final Pattern REMOTE_ABSOLUTE = Pattern.compile("^(/|[A-Za-z]:[\\\\/]|\\\\\\\\)");
public String bindSource(DockerClient.DockerHost daemon) {
return daemon.kind() == DockerClient.Kind.TCP ? persistDir : persistPath();
}
@@ -170,8 +175,13 @@ public class StartCommand implements Callable<Integer> {
if (persistDir == null || persistDir.isBlank()) return null;
// A remote daemon reads the bind source on its own machine, where this CLI can neither
// create nor check it; it is passed on untouched (bindSource), so nothing here can fail
// on it later either.
if (daemon.kind() == DockerClient.Kind.TCP) return null;
// on it later either. It does have to be absolute: there is no working directory to
// resolve it against over there, and docker reads a bare name as a named volume.
if (daemon.kind() == DockerClient.Kind.TCP) {
return REMOTE_ABSOLUTE.matcher(persistDir).find() ? null
: "With a remote Docker daemon, --persist must be an absolute path on the daemon's machine, but was '"
+ persistDir + "'.\nPass an absolute path, for example /srv/floci-data.";
}
Path path;
try {
path = Path.of(persistPath());
@@ -8,6 +8,8 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import picocli.CommandLine.ParseResult;
import java.io.ByteArrayOutputStream;
@@ -286,7 +288,30 @@ class StartCommandArgsTest {
assertEquals(resolved, start.bindSource(LOCAL));
assertTrue(start.dockerRunArgs(SOCKET, LOCAL).contains(resolved + ":/app/data"));
assertEquals("./data", start.bindSource(REMOTE));
}
/**
* A remote daemon has no working directory of ours to resolve against, and docker reads a bare
* name as a named volume: a relative path is refused before anything is started or stopped.
*/
@ParameterizedTest
@ValueSource(strings = {"data", "./data", "../state", "C:data"})
void aRemoteDaemonRefusesARelativePersistPath(String relative) {
String error = parse("start", "--persist", relative).preparePersistDir(REMOTE);
assertNotNull(error);
assertTrue(error.contains("must be an absolute path on the daemon's machine"), error);
assertTrue(error.contains("'" + relative + "'"), error);
}
/** Absolute for the daemon's system, whichever it is; this machine's path rules do not decide. */
@ParameterizedTest
@ValueSource(strings = {"/srv/floci-data", "C:\\floci\\data", "c:/floci/data", "\\\\nas\\share\\floci"})
void aRemoteDaemonAcceptsAnAbsolutePathOfEitherSystem(String absolute) {
StartCommand start = parse("start", "--persist", absolute);
assertNull(start.preparePersistDir(REMOTE));
assertEquals(absolute, start.bindSource(REMOTE));
}
/**
@@ -295,7 +320,7 @@ class StartCommandArgsTest {
*/
@Test
void aPathThisMachineCannotParseStillBuildsForARemoteDaemon() {
StartCommand start = parse("start", "--persist", "data\u0000dir");
StartCommand start = parse("start", "--persist", "/srv/data\u0000dir");
assertNull(start.preparePersistDir(REMOTE));
assertDoesNotThrow(() -> start.dockerRunArgs(SOCKET, REMOTE));