Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
GraalVM: forbidding methods of certain classes to be called from scripts

We are using GraalVM for some scripting requirements in our product. The version of the GraalVM is 21.2.0 We are using JavaScript and Groovy. We want to forbid some methods on certain classes from using in scripts. Example :

mytest.js

var testService=Java.type('mypackage.TestService');
    new testService().forbiddenJavaMethod();  // this should not be called

TestService.java

package mypackage;
    
    public class TestService{
        
        public void forbiddenJavaMethod(){
            // business logic
        }
    
    }

Is there a way to achieve this in Graal ? I could not find a way to do "method" filtering. Any other approach to solve this?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can configure host access when configuring the context. Namely

public Context.Builder allowHostAccess(HostAccess config)

https://www.graalvm.org/truffle/javadoc/org/graalvm/polyglot/Context.Builder.html#allowHostAccess-org.graalvm.polyglot.HostAccess-

Where the host access can be

HostAccess.EXPLICIT - Java host methods or fields, must be public and be annotated with @Export to make them accessible to the guest language.

https://www.graalvm.org/truffle/javadoc/org/graalvm/polyglot/HostAccess.html

Or you can go into a lot more fine-grained control using the HostAccess.Builder

https://www.graalvm.org/truffle/javadoc/org/graalvm/polyglot/HostAccess.Builder.html

about 4 years ago · Juan Pablo Isaza Report

0

If you can't use HostAccess.EXPLICIT for some reason (eg. you want to keep access to java.util etc), then you can create custom wrapper classes that only expose the relevant methods. With allowHostClassLookup() you can limit which classes are exposed in the engine.

public class Example {
    public static void main(String[] args) {
        TestService service = new TestService();
        Context.Builder builder = Context.newBuilder("js")
            .allowHostAccess(HostAccess.ALL)
            .allowHostClassLookup(path -> path.endsWith("TestServiceScriptable"));
        GraalJSScriptEngine engine = GraalJSScriptEngine.create(null, builder);
        engine.put("service", TestServiceScriptable.newInstance(service));
    }

}


public class TestService {
    public void forbiddenJavaMethod() {
        System.out.println("forbidden!");
    }
    public void allowedMethod() {
        System.out.println("allowed");
    }
}

public class TestServiceScriptable {
    private final TestService service;

    private TestServiceScriptable(TestService service) {
        this.service = service;
    }

    public static TestServiceScriptable newInstance(TestService service) {
        return new TestServiceScriptable(service);
    }


    public void allowedMethod() {
        service.allowedMethod();
    }
}

Then:

// Should work
service.allowedMethod();

// Shouldn't work
service.forbiddenMethod();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!