From 7b0f2cc7fa508dae7b2c66c74b335f4d0a2dbac3 Mon Sep 17 00:00:00 2001 From: Sylvain Jermini Date: Wed, 25 Dec 2024 13:58:14 +0100 Subject: [PATCH] add circular inclusion check --- data/circular/circular-1.mjml | 5 +++++ data/circular/circular-2.mjml | 5 +++++ data/circular/include-circular.mjml | 14 ++++++++++++++ src/main/java/ch/digitalfondue/mjml4j/Mjml4j.java | 5 ++++- .../digitalfondue/mjml4j/BaseComponentTests.java | 2 +- .../digitalfondue/mjml4j/ComplexTemplateTests.java | 7 +++++++ 6 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 data/circular/circular-1.mjml create mode 100644 data/circular/circular-2.mjml create mode 100644 data/circular/include-circular.mjml diff --git a/data/circular/circular-1.mjml b/data/circular/circular-1.mjml new file mode 100644 index 0000000..baeaa44 --- /dev/null +++ b/data/circular/circular-1.mjml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/data/circular/circular-2.mjml b/data/circular/circular-2.mjml new file mode 100644 index 0000000..85e1930 --- /dev/null +++ b/data/circular/circular-2.mjml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/data/circular/include-circular.mjml b/data/circular/include-circular.mjml new file mode 100644 index 0000000..059629a --- /dev/null +++ b/data/circular/include-circular.mjml @@ -0,0 +1,14 @@ + + + + + + +

This is the Index!

+
+
+
+ + +
+
\ No newline at end of file diff --git a/src/main/java/ch/digitalfondue/mjml4j/Mjml4j.java b/src/main/java/ch/digitalfondue/mjml4j/Mjml4j.java index 0ce1da2..919550e 100644 --- a/src/main/java/ch/digitalfondue/mjml4j/Mjml4j.java +++ b/src/main/java/ch/digitalfondue/mjml4j/Mjml4j.java @@ -501,7 +501,10 @@ private static BaseComponent handleInclude(Element element, BaseComponent parent try { resolvedPath = includeResolver.resolvePath(path, context.currentResourcePaths.peek()); resource = includeResolver.readResource(resolvedPath); - } catch (IOException | IllegalStateException e) { + if (context.currentResourcePaths.contains(resolvedPath)) { + throw new IllegalStateException("Circular inclusion detected on file : " + resolvedPath); + } + } catch (IOException e) { resource = ""; return new MjmlComponentRaw(element, parent, context, resource); } diff --git a/src/test/java/ch/digitalfondue/mjml4j/BaseComponentTests.java b/src/test/java/ch/digitalfondue/mjml4j/BaseComponentTests.java index a0ded53..49233dd 100644 --- a/src/test/java/ch/digitalfondue/mjml4j/BaseComponentTests.java +++ b/src/test/java/ch/digitalfondue/mjml4j/BaseComponentTests.java @@ -5,7 +5,7 @@ import static ch.digitalfondue.mjml4j.Helpers.testTemplate; /** - * Unit test for simple App. + * Unit test for base cases. */ class BaseComponentTests { diff --git a/src/test/java/ch/digitalfondue/mjml4j/ComplexTemplateTests.java b/src/test/java/ch/digitalfondue/mjml4j/ComplexTemplateTests.java index 405e6ea..e3e0e14 100644 --- a/src/test/java/ch/digitalfondue/mjml4j/ComplexTemplateTests.java +++ b/src/test/java/ch/digitalfondue/mjml4j/ComplexTemplateTests.java @@ -1,5 +1,6 @@ package ch.digitalfondue.mjml4j; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import static ch.digitalfondue.mjml4j.Helpers.testTemplate; @@ -127,4 +128,10 @@ void testIncludeAbout() { void testDividerAttributeAll() { testTemplate("mj-divider-mj-all"); } + + @Test + void testCircularInclude() { + var t = Assertions.assertThrows(IllegalStateException.class, () -> testTemplate("circular/include-circular")); + Assertions.assertTrue(t.getMessage().startsWith("Circular inclusion detected on file")); + } }