diff --git a/pom.xml b/pom.xml
index 4fee441..fc2aace 100644
--- a/pom.xml
+++ b/pom.xml
@@ -50,6 +50,14 @@
jackson-dataformat-yaml${jackson.version}
+
+
+ org.yaml
+ snakeyaml
+ 2.3
+
diff --git a/src/main/java/io/floci/cli/commands/config/ConfigValidateCommand.java b/src/main/java/io/floci/cli/commands/config/ConfigValidateCommand.java
index d792f94..af11486 100644
--- a/src/main/java/io/floci/cli/commands/config/ConfigValidateCommand.java
+++ b/src/main/java/io/floci/cli/commands/config/ConfigValidateCommand.java
@@ -1,8 +1,6 @@
package io.floci.cli.commands.config;
import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.floci.cli.GlobalOptions;
import io.floci.cli.ProductProfile;
import io.floci.cli.output.Ansi;
@@ -26,8 +24,6 @@ import java.util.concurrent.Callable;
)
public class ConfigValidateCommand implements Callable {
- private static final ObjectMapper YAML = new ObjectMapper(new YAMLFactory());
-
protected final ProductProfile profile;
private final boolean dockerSockRequired;
private final String dockerSockNeededFor;
@@ -71,7 +67,7 @@ public class ConfigValidateCommand implements Callable {
JsonNode compose = null;
String unreadable = null;
try {
- compose = YAML.readTree(content);
+ compose = YamlTree.read(content);
if (compose == null || !compose.isObject()) {
unreadable = "Could not read " + composeFile + " as a Compose file: it has no top-level mapping";
}
diff --git a/src/main/java/io/floci/cli/commands/config/YamlTree.java b/src/main/java/io/floci/cli/commands/config/YamlTree.java
new file mode 100644
index 0000000..4f27ae1
--- /dev/null
+++ b/src/main/java/io/floci/cli/commands/config/YamlTree.java
@@ -0,0 +1,126 @@
+package io.floci.cli.commands.config;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.yaml.snakeyaml.DumperOptions;
+import org.yaml.snakeyaml.LoaderOptions;
+import org.yaml.snakeyaml.Yaml;
+import org.yaml.snakeyaml.constructor.SafeConstructor;
+import org.yaml.snakeyaml.error.YAMLException;
+import org.yaml.snakeyaml.nodes.Tag;
+import org.yaml.snakeyaml.representer.Representer;
+import org.yaml.snakeyaml.resolver.Resolver;
+
+import java.io.IOException;
+import java.math.BigInteger;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+/**
+ * Reads a YAML document into a tree with its anchors, aliases and merge keys resolved, the way a
+ * YAML consumer such as Compose sees it. Jackson's own {@code readTree} leaves them unresolved:
+ * {@code volumes: *common} arrives as the string "common" and {@code <<: *base} as a field named
+ * {@code <<}, so a service built from an anchor looks empty. Its parser also drops anchors on
+ * scalars ({@code image: &img ...}), so they cannot be resolved on top of it either; SnakeYAML,
+ * which Jackson's YAML module is built on, does all three.
+ */
+public final class YamlTree {
+
+ /**
+ * The most nodes a document may have once its aliases are expanded. An alias costs nothing in
+ * the file and a whole copy in the tree, so a few nested aliases describe millions of nodes;
+ * no Compose file comes near this, and stopping here keeps such a file from using up memory.
+ */
+ static final int MAX_NODES = 100_000;
+
+ /**
+ * The deepest nesting a document may have. A recursive alias ({@code x: &a [*a]}) is a list
+ * that contains itself: without a limit, copying it would recurse until the stack overflows,
+ * long before the node budget is spent. Real Compose files are a handful of levels deep.
+ */
+ static final int MAX_DEPTH = 64;
+
+ private static final JsonNodeFactory NODES = JsonNodeFactory.instance;
+
+ private YamlTree() {}
+
+ /**
+ * Scalar typing as Compose reads it (YAML 1.2): only {@code true} and {@code false} are
+ * booleans. SnakeYAML's default follows YAML 1.1, where {@code yes}, {@code no}, {@code on}
+ * and {@code off} are booleans too, which would rename a service called {@code on} to
+ * {@code true}. Timestamps are left as text for the same reason: nothing here wants a date.
+ */
+ private static final class ComposeResolver extends Resolver {
+
+ private static final Pattern TRUE_OR_FALSE = Pattern.compile("^(?:true|True|TRUE|false|False|FALSE)$");
+
+ @Override
+ protected void addImplicitResolvers() {
+ addImplicitResolver(Tag.BOOL, TRUE_OR_FALSE, "tTfF");
+ addImplicitResolver(Tag.INT, INT, "-+0123456789");
+ addImplicitResolver(Tag.FLOAT, FLOAT, "-+0123456789.");
+ addImplicitResolver(Tag.MERGE, MERGE, "<");
+ addImplicitResolver(Tag.NULL, NULL, "~nN\0");
+ addImplicitResolver(Tag.NULL, EMPTY, null);
+ }
+ }
+
+ /**
+ * The first document of {@code yaml}; a missing node for an empty one.
+ *
+ * @throws IOException when the text is not YAML, expands to more than {@link #MAX_NODES}
+ * nodes or nests deeper than {@link #MAX_DEPTH}, with a one-line reason
+ */
+ public static JsonNode read(String yaml) throws IOException {
+ Object first;
+ try {
+ // SafeConstructor builds only maps, lists and scalars, never arbitrary classes.
+ LoaderOptions options = new LoaderOptions();
+ DumperOptions unused = new DumperOptions();
+ Yaml reader = new Yaml(new SafeConstructor(options), new Representer(unused), unused, options, new ComposeResolver());
+ Iterator