Skip to content

Commit fd2a351

Browse files
author
Sam Pullara
committed
add a xss encoding test
1 parent a61fa3e commit fd2a351

File tree

7 files changed

+55
-0
lines changed

7 files changed

+55
-0
lines changed

builder/src/test/java/com/sampullara/mustache/InterpreterTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.sampullara.mustache;
22

33
import com.google.common.base.Function;
4+
import com.google.common.collect.ImmutableMap;
45
import com.google.common.util.concurrent.SettableFuture;
56
import com.sampullara.util.FutureWriter;
67
import com.sampullara.util.TemplateFunction;
78
import com.sampullara.util.http.JSONHttpRequest;
89
import junit.framework.TestCase;
910
import org.codehaus.jackson.JsonFactory;
11+
import org.codehaus.jackson.JsonGenerator;
1012
import org.codehaus.jackson.JsonNode;
1113
import org.codehaus.jackson.JsonParser;
1214
import org.codehaus.jackson.map.MappingJsonFactory;
@@ -56,6 +58,25 @@ int taxed_value() {
5658
assertEquals(getContents(root, "simple.txt"), sw.toString());
5759
}
5860

61+
public void testXSS() throws MustacheException, IOException, ExecutionException, InterruptedException {
62+
MustacheBuilder c = new MustacheBuilder(root);
63+
Mustache m = c.parseFile("xss.html");
64+
final StringWriter json = new StringWriter();
65+
ImmutableMap<String, Object> of = ImmutableMap.<String, Object>of("foo", "bar", "\"baz\"", 42);
66+
MappingJsonFactory jf = new MappingJsonFactory();
67+
JsonGenerator jg = jf.createJsonGenerator(json);
68+
jg.writeObject(of);
69+
jg.flush();
70+
StringWriter sw = new StringWriter();
71+
FutureWriter writer = new FutureWriter(sw);
72+
m.execute(writer, new Scope(new Object() {
73+
String message = "I <3 Ponies!";
74+
String object = json.toString();
75+
}));
76+
writer.flush();
77+
assertEquals(getContents(root, "xss.txt"), sw.toString());
78+
}
79+
5980
public void testIdentitySimple() throws MustacheException, IOException, ExecutionException, InterruptedException {
6081
MustacheBuilder c = new MustacheBuilder(root);
6182
Mustache m = c.parseFile("simple.html");

builder/src/test/java/com/sampullara/mustache/UnexecuteTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,27 @@ public void testPartial() throws MustacheException, IOException {
174174
assertEquals(getContents(root, "template_partial.txt"), sw.toString());
175175
}
176176

177+
@Test
178+
public void testPartial2() throws MustacheException, IOException {
179+
MustacheBuilder c = init();
180+
Mustache m = c.parseFile("template_partial2.html");
181+
StringWriter sw = new StringWriter();
182+
Scope scope = new Scope();
183+
scope.put("title", "Welcome");
184+
scope.put("template_partial_2", new Object() {
185+
String again = "Goodbye";
186+
});
187+
scope.put("test", true);
188+
m.execute(sw, scope);
189+
assertEquals(getContents(root, "template_partial2.txt"), sw.toString());
190+
191+
scope = m.unexecute(sw.toString());
192+
sw = new StringWriter();
193+
m.execute(sw, scope);
194+
System.out.println(scope);
195+
assertEquals(getContents(root, "template_partial2.txt"), sw.toString());
196+
}
197+
177198
@Test
178199
public void testSimpleLamda() throws MustacheException, IOException {
179200
MustacheBuilder c = new MustacheBuilder(root);

core/src/main/java/com/sampullara/mustache/Mustache.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ protected Scope unexecute(String text, AtomicInteger position) throws MustacheEx
8787
line.set(compiled[i].getLine());
8888
}
8989
Code[] truncate = truncate(compiled, i + 1);
90+
System.out.println("Unexecute " + compiled[i].toString());
9091
current = compiled[i].unexecute(current, text, position, truncate);
9192
}
9293
return current;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h1>{{title}}</h1>
2+
{{#test}}
3+
{{>template_partial_2}}
4+
{{/test}}
5+
Test: {{template_partial_2.again}}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h1>Welcome</h1>
2+
Again, Goodbye!
3+
Test: Goodbye

src/test/resources/xss.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<b>{{message}}</b>
2+
<button onclick="foo({{object}})"></button>

src/test/resources/xss.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<b>I &lt;3 Ponies!</b>
2+
<button onclick="foo({&quot;foo&quot;:&quot;bar&quot;,&quot;\&quot;baz\&quot;&quot;:42})"></button>

0 commit comments

Comments
 (0)