-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace allowUnsafeMethod with a Method Access validator interface. P…
…rovide a NoOp method access and a default one (#493)
- Loading branch information
1 parent
ba51c35
commit 4753ce4
Showing
18 changed files
with
454 additions
and
108 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
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
41 changes: 41 additions & 0 deletions
41
...va/com/mitchellbosecke/pebble/attributes/methodaccess/BlacklistMethodAccessValidator.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,41 @@ | ||
package com.mitchellbosecke.pebble.attributes.methodaccess; | ||
|
||
import java.lang.reflect.AccessibleObject; | ||
import java.lang.reflect.Method; | ||
|
||
public class BlacklistMethodAccessValidator implements MethodAccessValidator { | ||
|
||
private static final String[] FORBIDDEN_METHODS = {"getClass", | ||
"wait", | ||
"notify", | ||
"notifyAll"}; | ||
|
||
@Override | ||
public boolean isMethodAccessAllowed(Object object, Method method) { | ||
boolean methodForbidden = object instanceof Class | ||
|| object instanceof Runtime | ||
|| object instanceof Thread | ||
|| object instanceof ThreadGroup | ||
|| object instanceof System | ||
|| object instanceof AccessibleObject | ||
|| this.isUnsafeMethod(method); | ||
return !methodForbidden; | ||
} | ||
|
||
private boolean isUnsafeMethod(Method member) { | ||
return this.isAnyOfMethods(member, FORBIDDEN_METHODS); | ||
} | ||
|
||
private boolean isAnyOfMethods(Method member, String... methods) { | ||
for (String method : methods) { | ||
if (this.isMethodWithName(member, method)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private boolean isMethodWithName(Method member, String method) { | ||
return member.getName().equals(method); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...c/main/java/com/mitchellbosecke/pebble/attributes/methodaccess/MethodAccessValidator.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,8 @@ | ||
package com.mitchellbosecke.pebble.attributes.methodaccess; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
public interface MethodAccessValidator { | ||
|
||
boolean isMethodAccessAllowed(Object object, Method method); | ||
} |
11 changes: 11 additions & 0 deletions
11
...in/java/com/mitchellbosecke/pebble/attributes/methodaccess/NoOpMethodAccessValidator.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,11 @@ | ||
package com.mitchellbosecke.pebble.attributes.methodaccess; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
public class NoOpMethodAccessValidator implements MethodAccessValidator { | ||
|
||
@Override | ||
public boolean isMethodAccessAllowed(Object object, Method method) { | ||
return true; | ||
} | ||
} |
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
Oops, something went wrong.