Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

542 server side template injection #678

Merged
merged 1 commit into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import freemarker.cache.StringTemplateLoader;
import freemarker.core.ParseException;
import freemarker.core.TemplateClassResolver;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;

Expand Down Expand Up @@ -212,6 +213,9 @@ private GeneratedTemplate generateAmwTemplateModel(Configuration cfg, TemplateDe

public static Configuration getConfiguration(AMWTemplateExceptionHandler templateExceptionHandler) {
Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);
// prevents Server-Side Template Injection
cfg.setNewBuiltinClassResolver(TemplateClassResolver.ALLOWS_NOTHING_RESOLVER);
cfg.setAPIBuiltinEnabled(false);
cfg.setTemplateExceptionHandler(templateExceptionHandler);
cfg.setShowErrorTips(false);
cfg.setLogTemplateExceptions(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,45 @@ public void shouldConcatByMacro() throws IOException {
assertTrue(result.isSuccess());
}

@Test
public void shouldFailCallingNewBuiltIn() throws IOException {
// given
Set<TemplateDescriptorEntity> templates = Sets.newHashSet();

TemplateDescriptorEntity templateDescriptorEntity = createTemplate("<#assign ex = \"freemarker.template.utility.Execute\"?new()>${ex(\"id\")}");

templates.add(templateDescriptorEntity);

GenerationUnit unit = new GenerationUnit(null, null, templates, null);

// when
GenerationUnitGenerationResult result = processor.generateResourceTemplates(unit, getAmwTemplateModel(null, null));

//then
GeneratedTemplate generatedTemplate = result.getGeneratedTemplates().get(0);
assertFalse(result.isSuccess());
assertTrue(generatedTemplate.getErrorMessages().size() > 0);
}

@Test
public void shouldFailCallingApiBuiltIn() throws IOException {
// given
Set<TemplateDescriptorEntity> templates = Sets.newHashSet();

TemplateDescriptorEntity templateDescriptorEntity = createTemplate("<#assign uri=object?api.class.getResource(\"/\").toURI()>${uri}");

templates.add(templateDescriptorEntity);

GenerationUnit unit = new GenerationUnit(null, null, templates, null);

// when
GenerationUnitGenerationResult result = processor.generateResourceTemplates(unit, getAmwTemplateModel(null, null));

//then
GeneratedTemplate generatedTemplate = result.getGeneratedTemplates().get(0);
assertFalse(result.isSuccess());
assertTrue(generatedTemplate.getErrorMessages().size() > 0);
}

private TemplateDescriptorEntity createTemplate(String templateContent) {
TemplateDescriptorEntity templateDescriptorEntity = new TemplateDescriptorEntity();
Expand Down