-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ISSUE-264: introduction of a SafeMustacheFactory for use when using u…
…ntrusted templates
- Loading branch information
Showing
6 changed files
with
121 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
compiler/src/main/java/com/github/mustachejava/SafeMustacheFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.github.mustachejava; | ||
|
||
import com.github.mustachejava.codes.ValueCode; | ||
import com.github.mustachejava.reflect.SimpleObjectHandler; | ||
import com.github.mustachejava.resolver.DefaultResolver; | ||
|
||
import java.io.File; | ||
import java.io.Reader; | ||
import java.io.Writer; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Modifier; | ||
import java.util.Set; | ||
|
||
import static com.github.mustachejava.util.HtmlEscaper.escape; | ||
|
||
public class SafeMustacheFactory extends DefaultMustacheFactory { | ||
|
||
// Only allow public access | ||
public static final SimpleObjectHandler OBJECT_HANDLER = new SimpleObjectHandler() { | ||
@Override | ||
protected void checkMethod(Method member) throws NoSuchMethodException { | ||
if ((member.getModifiers() & Modifier.PUBLIC) != Modifier.PUBLIC) { | ||
throw new NoSuchMethodException("Only public members allowed"); | ||
} | ||
} | ||
|
||
@Override | ||
protected void checkField(Field member) throws NoSuchFieldException { | ||
if ((member.getModifiers() & Modifier.PUBLIC) != Modifier.PUBLIC) { | ||
throw new NoSuchFieldException("Only public members allowed"); | ||
} | ||
} | ||
}; | ||
|
||
public SafeMustacheFactory(Set<String> allowedResourceNames, String resourceRoot) { | ||
super(new DefaultResolver(resourceRoot) { | ||
@Override | ||
public Reader getReader(String resourceName) { | ||
// Only allow allowed resources | ||
if (allowedResourceNames.contains(resourceName)) { | ||
return super.getReader(resourceName); | ||
} | ||
throw new MustacheException("Disallowed: resource requested"); | ||
} | ||
}); | ||
setup(); | ||
} | ||
|
||
public SafeMustacheFactory(Set<String> allowedResourceNames, File fileRoot) { | ||
super(new DefaultResolver(fileRoot) { | ||
@Override | ||
public Reader getReader(String resourceName) { | ||
// Only allow allowed resources | ||
if (allowedResourceNames.contains(resourceName)) { | ||
return super.getReader(resourceName); | ||
} | ||
throw new MustacheException("Disallowed: resource requested"); | ||
} | ||
}); | ||
setup(); | ||
} | ||
|
||
private void setup() { | ||
setObjectHandler(OBJECT_HANDLER); | ||
mc.setAllowChangingDelimeters(false); | ||
} | ||
|
||
@Override | ||
public MustacheVisitor createMustacheVisitor() { | ||
return new DefaultMustacheVisitor(this) { | ||
@Override | ||
public void pragma(TemplateContext tc, String pragma, String args) { | ||
throw new MustacheException("Disallowed: pragmas in templates"); | ||
} | ||
|
||
@Override | ||
public void value(TemplateContext tc, String variable, boolean encoded) { | ||
if (!encoded) { | ||
throw new MustacheException("Disallowed: non-encoded text in templates"); | ||
} | ||
list.add(new ValueCode(tc, df, variable, encoded)); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public void encode(String value, Writer writer) { | ||
escape(value, writer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters