Skip to content

Commit

Permalink
Moving use of 'canInvokeSuperMethod' to 'canInvokeMethod' because the…
Browse files Browse the repository at this point in the history
… previous doesn't make that much sense at all
  • Loading branch information
Net-0 committed Sep 11, 2024
1 parent c96b917 commit d9b74ea
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/main/java/bsh/Name.java
Original file line number Diff line number Diff line change
Expand Up @@ -812,8 +812,8 @@ public Object invokeMethod(
Object instance = classNameSpace.getClassInstance();
Class<?> classStatic = classNameSpace.classStatic;

// Validate if can invoke this super method
Interpreter.mainSecurityGuard.canInvokeSuperMethod(instance.getClass().getSuperclass(), instance, methodName, args);
// Validate if can invoke this method
Interpreter.mainSecurityGuard.canInvokeMethod(instance, methodName, args);

return ClassGenerator.getClassGenerator()
.invokeSuperclassMethod( bcm, instance, classStatic, methodName, args );
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/bsh/security/MainSecurityGuard.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,6 @@ public void canInvokeLocalMethod(String methodName, Object[] args) throws Securi
throw SecurityError.cantInvokeLocalMethod(methodName, _args);
}

/** Validate if can call a method of super class */
public void canInvokeSuperMethod(Class<?> superClass, Object thisArg, String methodName, Object[] args) throws SecurityError {
final Object[] _args = Primitive.unwrap(args);
for (SecurityGuard guard: this.securityGuards)
if (!guard.canInvokeSuperMethod(superClass, thisArg, methodName, _args))
throw SecurityError.cantInvokeSuperMethod(superClass, methodName, _args);
}

/** Validate if can get a field of a specific object */
public void canGetField(Object thisArg, String fieldName) throws SecurityError {
for (SecurityGuard guard: this.securityGuards)
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/bsh/security/SecurityGuard.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ public default boolean canInvokeLocalMethod(String methodName, Object[] args) {
return true;
}

/** Validate and return if can call a method of super class */
public default boolean canInvokeSuperMethod(Class<?> superClass, Object thisArg, String methodName, Object[] args) {
return true;
}

// TODO: implement a 'canSetField' and 'canSetStaticField'

/** Validate and return if can get a field of a specific object */
Expand Down
12 changes: 4 additions & 8 deletions src/test/java/SimpleSecurityGuardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ public boolean canInvokeLocalMethod(String methodName, Object[] args) {
if (methodName.equals("eval")) return false;
return true;
}
public boolean canInvokeSuperMethod(Class<?> superClass, Object thisArg, String methodName, Object[] args) {
if (methodName.equals("method")) return false;
return true;
}
public boolean canGetField(Object thisArg, String fieldName) {
if (fieldName.equals("nums2")) return false;
return true;
Expand Down Expand Up @@ -672,13 +668,13 @@ public void can_invoke_super_method() {
public void cant_invoke_super_method() {
try {
TestUtil.eval(
"class Cls1 { int method() { return 1; } };",
"class Cls2 extends Cls1 { int method2() { return super.method(); } };",
"new Cls2().method2();"
"class Cls1 { int someMethod() { return 1; } };",
"class Cls2 extends Cls1 { int method() { return super.someMethod(); } };",
"new Cls2().method();"
);
Assert.fail("The code must throw an Exception!");
} catch (Exception ex) {
final String expectedMsg = "SecurityError: Can't invoke this super method: Cls1.method()";
final String expectedMsg = "SecurityError: Can't invoke this method: Cls2.someMethod()";
Assert.assertTrue("Unexpected Exception Message: " + ex, ex.toString().contains(expectedMsg));
}
}
Expand Down
12 changes: 4 additions & 8 deletions src/test/java/bsh/security/SecurityGuardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ public boolean canInvokeLocalMethod(String methodName, Object[] args) {
if (methodName.equals("eval")) return false;
return true;
}
public boolean canInvokeSuperMethod(Class<?> superClass, Object thisArg, String methodName, Object[] args) {
if (methodName.equals("method")) return false;
return true;
}
public boolean canGetField(Object thisArg, String fieldName) {
if (fieldName.equals("nums2")) return false;
return true;
Expand Down Expand Up @@ -672,13 +668,13 @@ public void can_invoke_super_method() {
public void cant_invoke_super_method() {
try {
TestUtil.eval(
"class Cls1 { int method() { return 1; } };",
"class Cls2 extends Cls1 { int method2() { return super.method(); } };",
"new Cls2().method2();"
"class Cls1 { int someMethod() { return 1; } };",
"class Cls2 extends Cls1 { int method() { return super.someMethod(); } };",
"new Cls2().method();"
);
Assert.fail("The code must throw an Exception!");
} catch (Exception ex) {
final String expectedMsg = "SecurityError: Can't invoke this super method: Cls1.method()";
final String expectedMsg = "SecurityError: Can't invoke this method: Cls2.someMethod()";
Assert.assertTrue("Unexpected Exception Message: " + ex, ex.toString().contains(expectedMsg));
}
}
Expand Down

0 comments on commit d9b74ea

Please sign in to comment.