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.

3.5 KB
Raw Blame
package opensuite.core;

import opensuite.testkit.Expect;
import opensuite.testkit.Harness;

import java.util.ArrayList;
import java.util.List;

/** The REST API: who is let in, and what each endpoint answers. */
public final class RestApiTest {

    public void run() {
        Harness.suite("rest api");

        Harness.test("a request needs an enabled key unless keys are switched off", () -> {
            SuiteContext ctx = new SuiteContext();
            Settings s = ctx.settings();
            String good = RestApi.newKey(), disabled = RestApi.newKey();
            try {
                s.set(RestApi.KEYS, "true\tci\t2026-01-01\t" + RestApi.hash(good) + "\nfalse\told\t2025-01-01\t" + RestApi.hash(disabled) + "\n");
                RestApi api = ctx.restApi();
                Expect.isTrue(api.handle("GET", "/v0.1/", "")[0].startsWith("401"), "no key");
                Expect.isTrue(api.handle("GET", "/not-the-key/v0.1/", "")[0].startsWith("401"), "a wrong key");
                Expect.isTrue(api.handle("GET", "/" + disabled + "/v0.1/", "")[0].startsWith("401"), "a disabled key");
                Expect.isTrue(api.handle("GET", "/" + good + "/v0.1/", "")[0].startsWith("200"), "the key");
                Expect.isTrue(!s.get(RestApi.KEYS, "").contains(good), "the key itself is not what is stored");
                s.set(RestApi.NO_KEY, "true");
                Expect.isTrue(api.handle("GET", "/v0.1/", "")[0].startsWith("200"), "keys switched off");
            } finally { s.remove(RestApi.KEYS); s.remove(RestApi.NO_KEY); }
        });

        Harness.test("a scan is started for each URL and can be followed by its task id", () -> {
            SuiteContext ctx = new SuiteContext();
            Settings s = ctx.settings();
            try {
                s.set(RestApi.NO_KEY, "true");
                RestApi api = ctx.restApi();
                Expect.isTrue(api.handle("POST", "/v0.1/scan", "{\"urls\":[\"http://a.test/\"]}")[0].startsWith("503"), "no scanner yet");
                List<String> launched = new ArrayList<>();
                ctx.setScanLauncher((url, audit) -> launched.add(url + " " + audit));
                Expect.isTrue(api.handle("POST", "/v0.1/scan", "{}")[0].startsWith("400"), "no URLs");
                Expect.isTrue(api.handle("POST", "/v0.1/scan", "{\"urls\":[\"file:///etc/passwd\"]}")[0].startsWith("400"), "only http and https are scanned");
                Expect.isTrue(launched.isEmpty(), "and nothing was launched for the refused requests");
                String[] made = api.handle("POST", "/v0.1/scan", "{\"urls\": [\"http://a.test/\", \"https://b.test/x\"], \"audit\": false}");
                Expect.isTrue(made[0].startsWith("201") && made.length == 3, "created, with a Location");
                Expect.isTrue(launched.equals(List.of("http://a.test/ false", "https://b.test/x false")), "launched " + launched);
                String[] status = api.handle("GET", "/v0.1/scan/" + made[2], "");
                Expect.isTrue(status[0].startsWith("200") && status[1].contains("\"scan_status\":\"initializing\""), status[1]);
                Expect.isTrue(api.handle("GET", "/v0.1/scan/999", "")[0].startsWith("404"), "an unknown task id");
                Expect.isTrue(api.handle("GET", "/v0.1/scan", "")[0].startsWith("405"), "the wrong method");
            } finally { s.remove(RestApi.NO_KEY); }
        });

        Harness.test("text is escaped in the JSON it answers with", () -> {
            Expect.isTrue("\"a\\\"b\\\\c\\nd\\u0001\"".equals(RestApi.str("a\"b\\c\nd\u0001")), RestApi.str("a\"b\\c\nd\u0001"));
        });
    }
}