Class SandboxPolicy

java.lang.Object
org.codehaus.commons.compiler.sandbox.SandboxPolicy

public final class SandboxPolicy extends Object
An allowlist of the fields, methods and constructors that sandboxed code may use, and of the classes and interfaces that it may extend or implement. Everything that is not explicitly allowed is forbidden.

A SandboxPolicy is immutable; use a SandboxPolicy.Builder to create one:

    SandboxPolicy policy = SandboxPolicy.builder()
        .include(SandboxPolicy.JAVA_LANG_BASIC)
        .include(SandboxPolicy.COLLECTIONS)
        .allowAllMembers("com.acme.ScriptApi")
        .build();

Rules always apply to the declaring class of a member; e.g. ArrayList.toString() is declared by java.util.AbstractCollection.

Some members are never allowed (see isNeverAllowed(MemberRef)), because they would allow sandboxed code to escape the sandbox or to affect the entire JVM, e.g. reflection, class loading, access control (AccessController.doPrivileged()), threads, file and network access, and system properties. Class-wide rules (SandboxPolicy.Builder.allowAllMembers(String...), SandboxPolicy.Builder.allowConstructors(String...)) never enable them; a host that really wants to allow such a member must name it explicitly (SandboxPolicy.Builder.allowMethod(String, String, String), SandboxPolicy.Builder.allowMethods(String, String...), SandboxPolicy.Builder.allowField(String, String)).

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
    Creates SandboxPolicys.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final SandboxPolicy
    The core collections of java.util: the collection interfaces and their common implementations, Collections, Arrays and Optional.
    static final SandboxPolicy
    The functional interfaces of java.util.function (Function, Predicate, Supplier, ...); sandboxed code may also implement them.
    static final SandboxPolicy
    Basic functionality of java.lang: Object (without wait() and notify()), Class.getName() and getSimpleName(), String, StringBuilder, Math, the wrapper types, Enum, the common exceptions, and java.util.Objects.
    static final SandboxPolicy
    The date and time API of java.time, including the format and temporal packages and the ISO calendar system of the chrono package.
    static final SandboxPolicy
    java.math: BigInteger, BigDecimal, MathContext and RoundingMode.
    static final SandboxPolicy
    java.util.regex: Pattern, Matcher, MatchResult and PatternSyntaxException.
    static final SandboxPolicy
    The sequential streams of java.util.stream (Stream, IntStream, LongStream, DoubleStream, Collectors, ...) and the summary statistics of java.util; includes FUNCTIONAL.
    static final SandboxPolicy
    The number, date and message formats of java.text (NumberFormat, DecimalFormat, SimpleDateFormat, MessageFormat, ...), plus Collator and Normalizer.
    static final SandboxPolicy
    Utility classes of java.util: Random, SplittableRandom, ThreadLocalRandom, UUID, StringJoiner, StringTokenizer, BitSet, Base64, Locale, Date, Calendar, GregorianCalendar, TimeZone, and Formatter (without the constructors that open files).
  • Method Summary

    Modifier and Type
    Method
    Description
     
    boolean
     
    boolean
     
    static boolean
     
    boolean
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • JAVA_LANG_BASIC

      public static final SandboxPolicy JAVA_LANG_BASIC
      Basic functionality of java.lang: Object (without wait() and notify()), Class.getName() and getSimpleName(), String, StringBuilder, Math, the wrapper types, Enum, the common exceptions, and java.util.Objects.
    • COLLECTIONS

      public static final SandboxPolicy COLLECTIONS
      The core collections of java.util: the collection interfaces and their common implementations, Collections, Arrays and Optional.
    • FUNCTIONAL

      public static final SandboxPolicy FUNCTIONAL
      The functional interfaces of java.util.function (Function, Predicate, Supplier, ...); sandboxed code may also implement them.
    • STREAMS

      public static final SandboxPolicy STREAMS
      The sequential streams of java.util.stream (Stream, IntStream, LongStream, DoubleStream, Collectors, ...) and the summary statistics of java.util; includes FUNCTIONAL. Parallel streams are never allowed.
    • MATH

      public static final SandboxPolicy MATH
      java.math: BigInteger, BigDecimal, MathContext and RoundingMode.
    • REGEX

      public static final SandboxPolicy REGEX
      java.util.regex: Pattern, Matcher, MatchResult and PatternSyntaxException.
    • JAVA_TIME

      public static final SandboxPolicy JAVA_TIME
      The date and time API of java.time, including the format and temporal packages and the ISO calendar system of the chrono package.
    • TEXT

      public static final SandboxPolicy TEXT
      The number, date and message formats of java.text (NumberFormat, DecimalFormat, SimpleDateFormat, MessageFormat, ...), plus Collator and Normalizer.
    • UTILITIES

      public static final SandboxPolicy UTILITIES
      Utility classes of java.util: Random, SplittableRandom, ThreadLocalRandom, UUID, StringJoiner, StringTokenizer, BitSet, Base64, Locale, Date, Calendar, GregorianCalendar, TimeZone, and Formatter (without the constructors that open files).
  • Method Details

    • builder

      public static SandboxPolicy.Builder builder()
      Returns:
      A new builder for an initially empty policy, i.e. a policy that allows nothing
    • isAllowed

      public boolean isAllowed(MemberRef member)
      Parameters:
      member - A field, method or constructor, identified by its declaring class
      Returns:
      Whether sandboxed code may use the member
    • isSubclassingAllowed

      public boolean isSubclassingAllowed(String className)
      Returns:
      Whether sandboxed code may declare classes that extend or implement the named class or interface
    • isExecutorRequired

      public boolean isExecutorRequired()
      Returns:
      Whether code that JANINO compiles with this policy may only be executed by a SandboxExecutor; see SandboxPolicy.Builder.requireExecutor()
    • isNeverAllowed

      public static boolean isNeverAllowed(MemberRef member)
      Returns:
      Whether the member is one of the members that are never allowed unless a policy names them explicitly; see the class documentation