Skip to content

A free, open-source alternative to Burp Suite: an intercepting proxy and web app security testing suite with Repeater, Intruder, Collaborator and a built-in OWASP ZAP scanner. Linux, AGPL-3.0.

5.0 KB
Raw Blame
package opensuite.script;

import opensuite.core.HttpMessage;
import opensuite.script.api.HttpRequest;
import opensuite.script.api.HttpRequestResponse;
import opensuite.script.api.Utilities;
import opensuite.testkit.Expect;
import opensuite.testkit.Harness;

import java.util.List;
import javax.tools.ToolProvider;

/**
 * A script body is compiled in memory, its errors are given at the script's
 * own lines, the same source is compiled once, and a script that would close
 * Open Suite is refused.
 */
public final class ScriptCompilerTest {

    private static ScriptCompiler compiler() { return new ScriptCompiler(ToolProvider.getSystemJavaCompiler()); }

    private static HttpRequestResponse empty() {
        return new HttpRequestResponse(HttpRequest.of(null, new HttpMessage(new byte[0])), null);
    }

    private static int lineOf(String error) {
        Expect.isTrue(error.startsWith("line "), "an error with a line: " + error);
        return Integer.parseInt(error.substring(5, error.indexOf(':')));
    }

    public void run() {
        Harness.suite("script compiler");

        Harness.test("compiles and runs a body", () -> {
            Compiled c = compiler().compileCustomAction("utilities.log(\"hi\");");
            Expect.isTrue(c.ok(), "compiled: " + c.errors());
            Utilities u = new Utilities(s -> { });
            try { c.fn().action(empty(), u); } catch (Exception e) { throw new AssertionError(e); }
            Expect.equal(u.logLines(), List.of("hi"), "log");
        });

        Harness.test("a compile error is given at the script's line", () -> {
            Compiled c = compiler().compileCustomAction("int a = 1;\nutilities.lg(\"x\");\n");
            Expect.isFalse(c.ok(), "compiled");
            Expect.equal(c.errors().size(), 1, "one error: " + c.errors());
            Expect.equal(lineOf(c.errors().get(0)), 2, "line");
        });

        Harness.test("an unknown name says which name", () -> {
            Compiled c = compiler().compileCustomAction("utilities.lg(\"x\");");
            Expect.equal(c.errors(), List.of("line 1: cannot find symbol (method lg(String))"), "errors");
        });

        Harness.test("unbalanced braces stay inside the script's lines", () -> {
            Compiled c = compiler().compileCustomAction("if (true) {\nutilities.log(1);\n");
            Expect.isFalse(c.ok(), "compiled");
            for (String e : c.errors()) {
                int n = lineOf(e);
                Expect.isTrue(n >= 1 && n <= 2, "line in 1..2: " + e);
            }
        });

        Harness.test("a return value in the action is an error at its line", () -> {
            Compiled c = compiler().compileCustomAction("return 5;");
            Expect.isFalse(c.ok(), "compiled");
            Expect.equal(lineOf(c.errors().get(0)), 1, "line");
        });

        Harness.test("the same source is compiled once", () -> {
            ScriptCompiler sc = compiler();
            Compiled first = sc.compileCustomAction("int a = 1;");
            Expect.isTrue(sc.compileCustomAction("int a = 1;") == first, "the cached result for the same source");
            Expect.isTrue(sc.compileCustomAction("int a = 2;") != first, "a new result for a changed source");
        });

        Harness.test("a runtime error is given at the script's line", () -> {
            Compiled c = compiler().compileCustomAction("utilities.log(1);\nObject o = null;\no.toString();");
            Expect.isTrue(c.ok(), "compiled: " + c.errors());
            try {
                c.fn().action(empty(), new Utilities(s -> { }));
                throw new AssertionError("expected a NullPointerException");
            } catch (NullPointerException e) {
                Expect.equal(ScriptCompiler.scriptLine(c.className(), e.getStackTrace()), 3, "line");
            } catch (Exception e) {
                throw new AssertionError(e);
            }
        });

        Harness.test("a script that would exit Open Suite is refused", () -> {
            ScriptCompiler sc = compiler();
            Compiled c = sc.compileCustomAction("int x = 1;\nSystem.exit(0);");
            Expect.equal(c.errors(), List.of("line 2: a script may not exit Open Suite (System.exit / Runtime.halt)"), "errors");
            Expect.isFalse(sc.compileCustomAction("Runtime r = Runtime.getRuntime();\nr.halt(1);").ok(), "halt through a variable");
        });

        Harness.test("exit named in a comment or a string is not a call", () -> {
            Compiled c = compiler().compileCustomAction("// System.exit(0) would close Open Suite\nutilities.log(\"System.exit(0)\");");
            Expect.isTrue(c.ok(), "compiled: " + c.errors());
        });

        Harness.test("no compiler is reported", () -> {
            ScriptCompiler sc = new ScriptCompiler(null);
            Expect.isFalse(sc.available(), "available");
            Compiled c = sc.compileCustomAction("int a;");
            Expect.isFalse(c.ok(), "compiled");
            Expect.equal(c.errors(), List.of("no Java compiler is available in this runtime"), "error");
        });
    }
}