Skip to content

Commit 42c2aa2

Browse files
authored
Explicitly disable Mustache partials (#138944)
1 parent 8e41ac6 commit 42c2aa2

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomMustacheFactory.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public CustomMustacheFactory() {
8686
}
8787

8888
private CustomMustacheFactory(String mediaType, boolean detectMissingParams) {
89-
super();
89+
super(resourceName -> null); // we do not resolve templates via files or the classpath, etc.
9090
setObjectHandler(new CustomReflectionObjectHandler(detectMissingParams));
9191
this.encoder = createEncoder(mediaType);
9292
}
@@ -137,6 +137,13 @@ public void iterable(TemplateContext templateContext, String variable, Mustache
137137
list.add(new IterableCode(templateContext, df, mustache, variable));
138138
}
139139
}
140+
141+
@Override
142+
public void partial(TemplateContext tc, String variable, String indent) {
143+
// throwing a mustache exception here is important because this gets caught, handled (closing readers, etc),
144+
// and re-thrown in the mustache parser itself
145+
throw new MustacheException(Strings.format("Cannot expand '%s' because partial templates are not supported", variable));
146+
}
140147
}
141148

142149
/**

modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MustacheTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,18 @@ public void testUrlEncoderWithJoin() {
475475
);
476476
}
477477

478+
public void testsUnsupportedPartials() {
479+
ScriptException e;
480+
481+
final String script1 = "{{>foobar}}";
482+
e = expectThrows(ScriptException.class, () -> compile(script1));
483+
assertThat(e.getMessage(), equalTo("Cannot expand 'foobar' because partial templates are not supported"));
484+
485+
final String script2 = "{{>*foobar}}";
486+
e = expectThrows(ScriptException.class, () -> compile(script2));
487+
assertThat(e.getMessage(), equalTo("Cannot expand '*foobar' because partial templates are not supported"));
488+
}
489+
478490
private void assertScript(String script, Map<String, Object> vars, Matcher<String> matcher) {
479491
String result = compile(script).newInstance(vars).execute();
480492
assertThat(result, matcher);

modules/lang-mustache/src/yamlRestTest/resources/rest-api-spec/test/lang_mustache/20_render_search_template.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
- match: { template_output.aggs.my_terms.terms.field: "my_other_field" }
4848

4949
- do:
50-
catch: /Improperly.closed.variable.in.query-template/
50+
catch: "/Improperly.closed.variable(: my_value)?.in.query-template/"
5151
render_search_template:
5252
body: { "source": { "query": { "match": { "text": "{{{my_value}}" } }, "aggs": { "my_terms": { "terms": { "field": "{{my_field}}" } } } }, "params": { "my_value": "bar", "my_field": "field1" } }
5353
---
@@ -99,7 +99,7 @@
9999
- match: { template_output.size: 100 }
100100

101101
- do:
102-
catch: /Improperly.closed.variable.in.query-template/
102+
catch: "/Improperly.closed.variable(: my_value)?.in.query-template/"
103103
render_search_template:
104104
body: { "source": "{ \"query\": { \"match\": { \"text\": \"{{{my_value}}\" } }, \"size\": {{my_size}} }", "params": { "my_value": "bar", "my_size": 100 } }
105105

0 commit comments

Comments
 (0)