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.6 KB
Raw Blame
package opensuite.core;

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

/** Settings > Project > Scope, "Drop all out-of-scope requests". */
public final class DropOutOfScopeTest {

    private static HttpMessage get(String path) {
        return new HttpMessage(("GET " + path + " HTTP/1.1\r\nHost: x\r\n\r\n").getBytes(java.nio.charset.StandardCharsets.ISO_8859_1));
    }

    public void run() {
        Harness.suite("drop out-of-scope requests");

        Harness.test("nothing is sent outside the scope once the option is on", () -> {
            SuiteContext ctx = new SuiteContext();
            Settings s = ctx.settings();
            // A port nothing listens on: a request that is let through fails to connect instead.
            HttpService in = new HttpService("127.0.0.1", 9, false), out = new HttpService("127.0.0.2", 9, false);
            try {
                ctx.scope().include().add(Scope.Rule.prefix("http://127.0.0.1:9/app", false));
                Expect.isTrue(ctx.dropReason(out, get("/")) == null, "off: everything may be sent");
                s.setBool(SuiteContext.DROP_OUT_OF_SCOPE, true);
                Expect.isTrue(ctx.dropReason(in, get("/app/x")) == null, "in scope");
                Expect.isTrue(ctx.dropReason(in, get("/other")) != null, "same host, outside the prefix");
                Expect.isTrue(ctx.dropReason(out, get("/app/x")) != null, "another host");
                HttpClient.Options opt = new HttpClient.Options();
                opt.connectTimeoutMs = 300;
                HttpClient.Result r = new HttpClient().send(out, get("/"), opt);
                Expect.isTrue(r.error.startsWith("dropped: out of scope"), "the client refuses it: " + r.error);
                r = new HttpClient().send(in, get("/app/x"), opt);
                Expect.isTrue(!r.error.startsWith("dropped"), "and tries the in-scope one: " + r.error);
            } finally { s.remove(SuiteContext.DROP_OUT_OF_SCOPE); HttpClient.setGate(null); }
        });

        Harness.test("a custom scope decides instead of the suite's, and is kept in the settings", () -> {
            SuiteContext ctx = new SuiteContext();
            Settings s = ctx.settings();
            HttpService a = new HttpService("a.test", 80, false), b = new HttpService("b.test", 80, false);
            try {
                ctx.scope().include().add(Scope.Rule.prefix("http://a.test/", false));
                ctx.dropScope().include().add(Scope.Rule.prefix("http://b.test/", false));
                ctx.saveDropScope();
                s.setBool(SuiteContext.DROP_OUT_OF_SCOPE, true);
                s.set(SuiteContext.DROP_MODE, "custom");
                Expect.isTrue(ctx.dropReason(a, get("/")) != null && ctx.dropReason(b, get("/")) == null, "the custom scope is the one used");
                String kept = s.get(SuiteContext.DROP_CUSTOM, "");
                Expect.isTrue(!kept.isEmpty(), "saved");
                // Another context over the same settings, as after reopening the project.
                SuiteContext again = new SuiteContext();
                again.settings().set(SuiteContext.DROP_CUSTOM, kept);
                Expect.isTrue(again.dropScope().include().size() == 1, "read back: " + again.dropScope().include().size());
                again.settings().set(SuiteContext.DROP_CUSTOM, "bm90IGEgc2NvcGU=");
                Expect.isTrue(again.dropScope().include().isEmpty(), "a value that is not a scope leaves it empty");
            } finally {
                s.remove(SuiteContext.DROP_OUT_OF_SCOPE); s.remove(SuiteContext.DROP_MODE); s.remove(SuiteContext.DROP_CUSTOM);
                HttpClient.setGate(null);
            }
        });
    }
}