mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
fix(config): report a self-referencing alias instead of overflowing the stack in config validate
A recursive alias is a collection that contains itself. The reader now stops at 64 levels of nesting and reports the file, so the command still prints its validation report. Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
2 files changed
+53
-6
No files matched your search
@@ -515,6 +515,42 @@ class ConfigValidateTest {
|
||||
assertTrue(json.path("errors").get(0).asText().contains("once its aliases are expanded"), r.out());
|
||||
}
|
||||
|
||||
/**
|
||||
* An alias that refers to itself is a collection containing itself. It is reported like any
|
||||
* other unreadable file, not left to overflow the stack.
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"x: &a [*a]\n", "x: &a {self: *a}\n", "x: &a [[[*a]]]\nservices: {}\n"})
|
||||
void aRecursiveAliasIsReportedNotFollowed(String yaml) {
|
||||
IOException e = assertThrows(IOException.class, () -> YamlTree.read(yaml));
|
||||
|
||||
assertTrue(e.getMessage().contains("an alias refers to itself"), e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theCommandReportsARecursiveAlias() throws Exception {
|
||||
Path file = Files.writeString(tempDir.resolve("compose.yml"), "x-loop: &a [*a]\nservices: {}\n");
|
||||
|
||||
Run json = run("config", "validate", "-f", file.toString(), "-o", "json");
|
||||
Run text = run("config", "validate", "-f", file.toString());
|
||||
|
||||
assertEquals(1, json.exit());
|
||||
assertFalse(new ObjectMapper().readTree(json.out()).path("valid").asBoolean());
|
||||
assertTrue(json.out().contains("an alias refers to itself"), json.out());
|
||||
assertEquals(1, text.exit());
|
||||
assertTrue(text.err().contains("an alias refers to itself"), text.err());
|
||||
assertFalse(text.err().contains("StackOverflow"), text.err());
|
||||
}
|
||||
|
||||
/** Ordinary nesting, well past what a Compose file uses, is still read. */
|
||||
@Test
|
||||
void deepButFiniteNestingIsRead() throws Exception {
|
||||
StringBuilder yaml = new StringBuilder("a: ");
|
||||
yaml.append("[".repeat(40)).append("x").append("]".repeat(40)).append("\n");
|
||||
|
||||
assertTrue(YamlTree.read(yaml.toString()).path("a").isArray());
|
||||
}
|
||||
|
||||
/** A real Compose file with many aliases stays far below the budget. */
|
||||
@Test
|
||||
void manyOrdinaryAliasesAreFine() throws Exception {
|
||||
|
||||
Reference in new issue
Block a user