Repository navigation
Enforce path containment in FolderTheme.getTemplate() - #50098
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens theme template resolution by ensuring FolderTheme.getTemplate() enforces path containment (consistent with getResourceAsStream()), preventing ../-style traversal from escaping the theme directory. It also adds a regression test to validate both valid and traversal-like template names.
Changes:
- Route
FolderTheme.getTemplate()throughResourceLoader.getFile(...)to apply the existing containment check. - Add a new
FolderThemeTest.testGetTemplatePathContainmentcovering a normal template and traversal/encoded traversal-like names.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| services/src/main/java/org/keycloak/theme/FolderTheme.java | Uses ResourceLoader.getFile(themeDir, name) to enforce path containment when resolving templates. |
| services/src/test/java/org/keycloak/theme/FolderThemeTest.java | Adds a regression test for template path containment behavior. |
| assertNotNull(theme.getTemplate("login.ftl")); | ||
| assertNull(theme.getTemplate(NONE + "forbidden.ftl")); | ||
| assertNull(theme.getTemplate(SINGLE + "forbidden.ftl")); | ||
| assertNull(theme.getTemplate(DOUBLE + "forbidden.ftl")); |
There was a problem hiding this comment.
This is not testing changes as it is passing without your fix.
|
|
||
| assertNotNull(theme.getTemplate("login.ftl")); | ||
| assertNull(theme.getTemplate(NONE + "forbidden.ftl")); | ||
| assertNull(theme.getTemplate(SINGLE + "forbidden.ftl")); |
There was a problem hiding this comment.
Same here - This is not testing changes as it is passing without your fix.
There was a problem hiding this comment.
Updated the test to follow ResourceLoaderTest — using assertGetTemplate(..., false, true) for ../forbidden.ftl so we prove the file exists outside themeDir but is rejected, and removed the encoded literal path checks that passed without the fix.
| public void testGetTemplatePathContainment() throws Exception { | ||
| Path tempDirectory = Files.createTempDirectory("folder-theme-test"); | ||
| File themeDir = new File(tempDirectory.toFile(), "login"); | ||
| org.junit.Assert.assertTrue("Failed to create theme directory: " + themeDir, themeDir.mkdirs()); |
d407cbc to
795204d
Compare
| @@ -37,7 +43,37 @@ public void testLocaleWithExtension() throws Exception { | |||
|
|
|||
| for (String localeCode : someValidISOLocaleCodes) { | |||
| Locale locale = Locale.forLanguageTag(localeCode); | |||
| assertNotNull(theme.getMessages("base", locale)); | |||
| Assert.assertNotNull(theme.getMessages("base", locale)); | |||
There was a problem hiding this comment.
Sorry for nitpick, but this change is bit unnecessary as it has nothing to do with your PR. It is fine to apply your preference on the code you write, but I'd not change existing assertions. Thank you
There was a problem hiding this comment.
Thanks for pointing it out. I’ve fixed it and restored the original assertions.
| @@ -56,7 +56,7 @@ public String processTemplate(Object data, String templateName, Theme theme) thr | |||
| } | |||
|
|
|||
| private Template getTemplate(String templateName, Theme theme) throws IOException { | |||
| Configuration cfg = new Configuration(); | |||
| Configuration cfg = new Configuration(Configuration.VERSION_2_3_32); | |||
There was a problem hiding this comment.
Can you elaborate on this change?
There was a problem hiding this comment.
Sorry, that was pushed by mistake. I’ve fixed it now.
done |
There was a problem hiding this comment.
Thanks very much for you patience, just one last round and that is it. I realize they are marginal things, but it's better to do it right.
|
|
||
| @Test | ||
| public void testGetTemplatePathContainment() throws Exception { | ||
| Path tempDirectory = Files.createTempDirectory("folder-theme-test"); |
There was a problem hiding this comment.
| Path tempDirectory = Files.createTempDirectory("folder-theme-test"); | |
| File tempDirectory = temporaryFolder.newFolder(); |
and:
@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();| assertGetTemplate(theme, themeDir, "login.ftl", true, true); | ||
| assertGetTemplate(theme, themeDir, NONE + "forbidden.ftl", false, true); | ||
| assertGetTemplate(theme, themeDir, "one/" + NONE + NONE + "forbidden.ftl", false, true); | ||
| } | ||
|
|
||
| private void assertGetTemplate(FolderTheme theme, File themeDir, String name, boolean expectValid) throws IOException { | ||
| URL template = theme.getTemplate(name); | ||
| if (expectValid) { | ||
| assertNotNull(template); | ||
| } else { | ||
| assertNull(template); | ||
| } | ||
|
|
||
| if (expectFileToExist) { | ||
| assertTrue(new File(themeDir, name).getCanonicalFile().isFile()); | ||
| } | ||
| } |
| public URL getTemplate(String name) throws IOException { | ||
| File file = new File(themeDir, name); | ||
| return file.isFile() ? file.toURI().toURL() : null; | ||
| File file = ResourceLoader.getFile(themeDir, name); | ||
| return file != null && file.isFile() ? file.toURI().toURL() : null; | ||
| } |
b9b1f55 to
bbc6e0f
Compare
| public URL getTemplate(String name) throws IOException { | ||
| File file = new File(themeDir, name); | ||
| return file.isFile() ? file.toURI().toURL() : null; | ||
| File file = ResourceLoader.getFile(themeDir, name); | ||
| return file != null && file.isFile() ? file.toURI().toURL() : null; |
| public final TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
|
|
||
| private static final String NONE = "../"; | ||
|
|
|
|
||
| FolderTheme theme = new FolderTheme(themeDir, "name", LOGIN); | ||
|
|
||
| assertGetTemplate(theme, themeDir, "login.ftl", true, true); |
There was a problem hiding this comment.
My previous comment was not addressed, but you resolved it anyway. expectFileToExist is alwasy true, I propose to drop it.
bbc6e0f to
6f69c3e
Compare
| private void assertGetTemplate(FolderTheme theme, File themeDir, String name, boolean expectValid) throws IOException { | ||
| URL template = theme.getTemplate(name); | ||
| if (expectValid) { | ||
| assertNotNull(template); | ||
| } else { | ||
| assertNull(template); | ||
| } | ||
|
|
||
| assertTrue(new File(themeDir, name).getCanonicalFile().isFile()); | ||
| } | ||
| } |
|
@keycloak/cloud-native wanna review? |
There was a problem hiding this comment.
Thanks @MdTanwer for the PR and @michalvavrik for the review. LGTM! Probably I would not have used the NONE constant and just uses the .. directly for the tests. But it's just a nitpick, so approving this. I'm starting the tests and if they run OK it is ready.
Route template resolution through ResourceLoader.getFile() so getTemplate() applies the same normalization and root-path guard as getResourceAsStream(). Closes keycloak#49961 Signed-off-by: MdTanwer <tanw9004167@gmail.com>
6f69c3e to
1050a82
Compare
|
hey @rmartinc I think this PR ready to merge :) |
Route template resolution through ResourceLoader.getFile() so getTemplate() applies the same normalization and root-path guard as getResourceAsStream(). Closes keycloak#49961 Signed-off-by: MdTanwer <tanw9004167@gmail.com>
Summary
FolderTheme.getTemplate()throughResourceLoader.getFile()for parity withgetResourceAsStream()../or encoded traversal sequences that escapethemeDirtestGetTemplatePathContainmentcovering valid and traversal pathsTest plan
mvn test -Dtest=FolderThemeTest,ResourceLoaderTestinservicesmodulelogin.ftl)Closes Themes: FolderTheme.getTemplate() missing path-containment check #49961