Skip to content

Enforce path containment in FolderTheme.getTemplate() - #50098

Merged
rmartinc merged 1 commit into
keycloak:mainfrom
MdTanwer:fix/folder-theme-path-containment-49961
Jun 23, 2026
Merged

rmartinc merged 1 commit into
keycloak:mainfrom
MdTanwer:fix/folder-theme-path-containment-49961

Conversation

@MdTanwer

Copy link
Copy Markdown
Contributor

Summary

  • Route FolderTheme.getTemplate() through ResourceLoader.getFile() for parity with getResourceAsStream()
  • Reject template names containing ../ or encoded traversal sequences that escape themeDir
  • Add testGetTemplatePathContainment covering valid and traversal paths

Test plan

@MdTanwer
MdTanwer marked this pull request as ready for review June 18, 2026 05:26
@MdTanwer
MdTanwer requested a review from a team as a code owner June 18, 2026 05:26
Copilot AI review requested due to automatic review settings June 18, 2026 05:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() through ResourceLoader.getFile(...) to apply the existing containment check.
  • Add a new FolderThemeTest.testGetTemplatePathContainment covering 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.

Copilot AI review requested due to automatic review settings June 18, 2026 11:41
@MdTanwer
MdTanwer requested a review from michalvavrik June 18, 2026 11:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

assertNotNull(theme.getTemplate("login.ftl"));
assertNull(theme.getTemplate(NONE + "forbidden.ftl"));
assertNull(theme.getTemplate(SINGLE + "forbidden.ftl"));
assertNull(theme.getTemplate(DOUBLE + "forbidden.ftl"));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here - This is not testing changes as it is passing without your fix.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot AI review requested due to automatic review settings June 19, 2026 02:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Copilot AI review requested due to automatic review settings June 19, 2026 03:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

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());
Copilot AI review requested due to automatic review settings June 19, 2026 03:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Copilot AI review requested due to automatic review settings June 19, 2026 03:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@MdTanwer
MdTanwer force-pushed the fix/folder-theme-path-containment-49961 branch from d407cbc to 795204d Compare June 19, 2026 03:52
@MdTanwer
MdTanwer requested a review from michalvavrik June 19, 2026 03:53
@@ -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));

@michalvavrik michalvavrik Jun 19, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate on this change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, that was pushed by mistake. I’ve fixed it now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np!

@MdTanwer

Copy link
Copy Markdown
Contributor Author

@MdTanwer you need to sing your commit, please see the failing DCO workflow and https://github.com/keycloak/keycloak/blob/main/CONTRIBUTING.md#developers-certificate-of-origin for details how to resolve it.

done

@michalvavrik michalvavrik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Path tempDirectory = Files.createTempDirectory("folder-theme-test");
File tempDirectory = temporaryFolder.newFolder();

and:

@Rule
    public final TemporaryFolder temporaryFolder = new TemporaryFolder();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copilot AI review requested due to automatic review settings June 19, 2026 12:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment on lines +64 to +80
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());
}
}
Comment on lines 86 to 89
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;
}
@MdTanwer
MdTanwer force-pushed the fix/folder-theme-path-containment-49961 branch from b9b1f55 to bbc6e0f Compare June 20, 2026 03:47
@MdTanwer
MdTanwer requested a review from michalvavrik June 20, 2026 03:49
@michalvavrik
michalvavrik requested a review from Copilot June 20, 2026 07:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment on lines 86 to +88
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;
Comment on lines +39 to +42
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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My previous comment was not addressed, but you resolved it anyway. expectFileToExist is alwasy true, I propose to drop it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@MdTanwer
MdTanwer force-pushed the fix/folder-theme-path-containment-49961 branch from bbc6e0f to 6f69c3e Compare June 20, 2026 08:06
@MdTanwer
MdTanwer requested a review from michalvavrik June 20, 2026 08:06
@michalvavrik
michalvavrik requested a review from Copilot June 20, 2026 08:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment on lines +72 to 82
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());
}
}

@michalvavrik michalvavrik left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@michalvavrik

Copy link
Copy Markdown
Member

@keycloak/cloud-native wanna review?

rmartinc
rmartinc previously approved these changes Jun 23, 2026

@rmartinc rmartinc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rmartinc rmartinc self-assigned this Jun 23, 2026
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>
@MdTanwer

Copy link
Copy Markdown
Contributor Author

hey @rmartinc I think this PR ready to merge :)

@rmartinc rmartinc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@rmartinc
rmartinc merged commit da3cc05 into keycloak:main Jun 23, 2026
96 of 98 checks passed
sashyo pushed a commit to tide-foundation/keycloak-IGA that referenced this pull request Jul 20, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Themes: FolderTheme.getTemplate() missing path-containment check

4 participants