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.

12.3 KB
Raw Blame
package opensuite.engine;

import opensuite.gate.GateRules;
import opensuite.testkit.Expect;
import opensuite.testkit.Harness;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

/**
 * The parts of a scan that could make it do less than it should, and the
 * proof that each now asks before it leaves anything out: the crawl loop
 * check, the session the add-on sends, the hold on one scan, and the notice
 * when a crawl is cut short by its time limit.
 */
public final class LoopCheckTest {

    private static String page(String title) {
        StringBuilder b = new StringBuilder("<html><head><title>" + title + "</title></head><body>\n");
        for (int i = 0; i < 20; i++) b.append("<p>line ").append(i).append(" of ").append(title).append("</p>\n");
        return b.append("</body></html>").toString();
    }

    public void run() {
        Harness.suite("crawl loop check and session rules");

        Harness.test("a repeated folder name is found, and the repeats taken out", () -> {
            Expect.equal(LoopCheck.repeatedName("http://shop.test/assets/public/assets/public/assets/main.js"), "assets", "the name");
            Expect.equal(LoopCheck.repeatedName("http://shop.test/users/5/users/7/users/9"), "users", "a real-looking one has the shape too");
            Expect.isTrue(LoopCheck.repeatedName("http://shop.test/a/b/a/b") == null, "twice is not three times");
            Expect.isTrue(LoopCheck.repeatedName("http://shop.test/grid/1/1/1") == null, "numbers are not a name");
            Expect.equal(LoopCheck.shorter("http://shop.test/x/a/b/a/c/a/d?q=1", "a"), "http://shop.test/x/a/d?q=1", "first to last, query kept");
            Expect.equal(LoopCheck.shorter("http://shop.test/users/5/users/7/users/9", "users"), "http://shop.test/users/9", "a nested one");
            Expect.equal(LoopCheck.shorter("http://shop.test/a/b", "a"), "http://shop.test/a/b", "nothing to take out");
        });

        Harness.test("a loop is confirmed by asking, and only then skipped", () -> {
            // Every URL comes back as the same page: an application that answers anything.
            List<String> asked = new ArrayList<>();
            LoopCheck c = new LoopCheck(url -> { asked.add(url); return new LoopCheck.Page(200, page("Shop")); });
            List<LoopCheck.Finding> f = c.examine("http://shop.test", List.of("http://shop.test/", "http://shop.test/assets/a",
                    "http://shop.test/assets/public/assets/public/assets/x"));
            Expect.equal(f.size(), 1, "one name settled");
            Expect.isTrue(f.get(0).loop(), "the same page both ways: a loop");
            Expect.equal(asked, List.of("http://shop.test/assets/public/assets/public/assets/x", "http://shop.test/assets/x"), "the URL and the one without its repeats, and no other");
            Expect.isTrue(c.isLoop("http://shop.test/media/assets/1/assets/2/assets/3"), "others of the form are loops from then on");
            Expect.isTrue("http://shop.test/media/assets/1/assets/2/assets/3".matches(f.get(0).pattern()), "and its pattern says so to the engine");
            Expect.isFalse(c.isLoop("http://shop.test/assets/public/assets/logo.png"), "twice is still a page");
            Expect.isFalse(c.isLoop("http://other.test/assets/1/assets/2/assets/3"), "and another site is not touched");
            Expect.equal(c.examine("http://shop.test", List.of("http://shop.test/assets/9/assets/9/assets/9")).size(), 0, "a settled name is not asked about again");
        });

        Harness.test("a real page with the same shape is kept", () -> {
            LoopCheck c = new LoopCheck(url -> new LoopCheck.Page(200, page(url)));   // every URL its own page
            List<LoopCheck.Finding> f = c.examine("http://api.test", List.of("http://api.test/users/5/users/7/users/9"));
            Expect.equal(f.size(), 1, "settled");
            Expect.isFalse(f.get(0).loop(), "different pages: not a loop");
            Expect.isFalse(c.isLoop("http://api.test/users/5/users/7/users/9"), "so it is not skipped");
            Expect.equal(c.patterns().size(), 0, "and nothing is excluded");
        });

        Harness.test("no answer settles nothing", () -> {
            int[] calls = {0};
            LoopCheck c = new LoopCheck(url -> { calls[0]++; return null; });
            for (int i = 0; i < 6; i++) c.examine("http://shop.test", List.of("http://shop.test/a/1/a/2/a/" + i));
            Expect.isFalse(c.isLoop("http://shop.test/a/1/a/2/a/3"), "nothing skipped without proof");
            Expect.isTrue(calls[0] <= 3, "and it stops asking after a few tries (asked " + calls[0] + " times)");
        });

        Harness.test("the same page is told from a different one", () -> {
            LoopCheck.Page shop = new LoopCheck.Page(200, page("Shop"));
            Expect.isTrue(LoopCheck.samePage(shop, new LoopCheck.Page(200, page("Shop"))), "equal");
            Expect.isTrue(LoopCheck.samePage(shop, new LoopCheck.Page(200, page("Shop") + "\n<!-- served 12:30:01 -->")), "one line apart");
            Expect.isFalse(LoopCheck.samePage(shop, new LoopCheck.Page(404, page("Shop"))), "another status");
            Expect.isFalse(LoopCheck.samePage(shop, new LoopCheck.Page(200, page("Basket"))), "another page");
            Expect.isFalse(LoopCheck.samePage(new LoopCheck.Page(200, "{\"id\":5}"), new LoopCheck.Page(200, "{\"id\":9}")), "short answers must be equal");
            Expect.isFalse(LoopCheck.samePage(shop, null), "no answer");
        });

        Harness.test("the add-on reads the session Open Suite writes", () -> {
            Map<String, LinkedHashSet<String>> seen = new LinkedHashMap<>();
            List<ScanAuth.Header> first = List.of(new ScanAuth.Header("http://shop.test:3000", "Cookie", "token=abc 1; lang=en"),
                    new ScanAuth.Header("http://shop.test:3000", "X-Api-Key", "k=1&z"));
            List<GateRules.Rule> rules = GateRules.parse(SessionGuard.sessionRules(first, seen));
            Expect.equal(rules.size(), 3, "two cookies and a header");
            Expect.isTrue(rules.get(0).cookie() && rules.get(0).name().equals("token") && rules.get(0).value().equals("abc 1"), "a cookie, spaces and all");
            Expect.isTrue(!rules.get(2).cookie() && rules.get(2).name().equals("X-Api-Key") && rules.get(2).value().equals("k=1&z"), "a header, marks and all");
            Expect.isTrue(rules.get(0).url().matcher("http://shop.test:3000/rest/basket/1").matches(), "for the site's pages");
            Expect.isFalse(rules.get(0).url().matcher("http://other.test/rest").matches(), "and no other site's");
            // The session is renewed: the old value is remembered as one the session had.
            List<ScanAuth.Header> second = List.of(new ScanAuth.Header("http://shop.test:3000", "Cookie", "token=def; lang=en"));
            rules = GateRules.parse(SessionGuard.sessionRules(second, seen));
            Expect.equal(rules.get(0).value(), "def", "the new value");
            Expect.equal(rules.get(0).past(), List.of("abc 1"), "the old one kept as past");
            Expect.equal(rules.get(1).past(), List.of(), "a cookie that did not change has no past");
        });

        Harness.test("a check's own cookie value goes through, and the session is put on everything else", () -> {
            Map<String, LinkedHashSet<String>> seen = new LinkedHashMap<>();
            SessionGuard.sessionRules(List.of(new ScanAuth.Header("http://shop.test", "Cookie", "token=old; lang=en")), seen);
            List<GateRules.Rule> r = GateRules.parse(SessionGuard.sessionRules(
                    List.of(new ScanAuth.Header("http://shop.test", "Cookie", "token=new; lang=en")), seen));
            // The crawler: the session's cookies and no others, whatever it had.
            Expect.equal(GateRules.cookiesToSend("token=stale; tracking=1", r, false), "token=new; lang=en", "a crawler's request");
            Expect.equal(GateRules.cookiesToSend(null, r, false), "token=new; lang=en", "a crawler's request with none");
            // The auditor, request untouched: brought up to date.
            Expect.equal(GateRules.cookiesToSend("token=new; lang=en", r, true), "token=new; lang=en", "current");
            Expect.equal(GateRules.cookiesToSend("token=old; lang=en", r, true), "token=new; lang=en", "recorded under the older session");
            Expect.equal(GateRules.cookiesToSend(null, r, true), "token=new; lang=en", "none");
            // The auditor, a check testing a cookie: its value reaches the application.
            Expect.equal(GateRules.cookiesToSend("token=new' OR '1'='1; lang=en", r, true), "token=new' OR '1'='1; lang=en", "the session cookie under test");
            Expect.equal(GateRules.cookiesToSend("token=old; lang=<script>alert(1)</script>", r, true), "token=new; lang=<script>alert(1)</script>",
                    "another cookie under test: its value kept, the session still renewed");
            Expect.equal(GateRules.cookiesToSend("token=new; lang=en; extra=1", r, true), "token=new; lang=en; extra=1", "a cookie the check added is kept");
        });

        Harness.test("a check's own header value goes through", () -> {
            Map<String, LinkedHashSet<String>> seen = new LinkedHashMap<>();
            SessionGuard.sessionRules(List.of(new ScanAuth.Header("http://shop.test", "X-Auth", "one")), seen);
            GateRules.Rule r = GateRules.parse(SessionGuard.sessionRules(List.of(new ScanAuth.Header("http://shop.test", "X-Auth", "two")), seen)).get(0);
            Expect.equal(GateRules.headerToSend(null, r, true), "two", "missing: the session's");
            Expect.equal(GateRules.headerToSend("one", r, true), "two", "the older session's: brought up to date");
            Expect.isTrue(GateRules.headerToSend("two'--", r, true) == null, "changed by a check: left as the check wrote it");
            Expect.equal(GateRules.headerToSend("two'--", r, false), "two", "a crawler has no checks: the session's");
        });

        Harness.test("a hold names the one scan it is for", () -> {
            Expect.equal(ScanGate.value(1700000000000L, "app.test:443", "A5"), "1700000000000 app.test:443 A5", "an audit");
            Expect.equal(ScanGate.value(1700000000000L, "app.test:443", ""), "1700000000000 app.test:443", "none named: the whole site, as before");
            String[] parts = "1700000000000 app.test:443 127.0.0.1:3000 S3".split(" ");
            Expect.isTrue(GateRules.holds(parts, "127.0.0.1:3000", 1600000000000L), "the site is held");
            Expect.isFalse(GateRules.holds(parts, "127.0.0.1:3000", 1800000000000L), "until its time passes");
            Expect.isFalse(GateRules.holds(parts, "other.test:80", 1600000000000L), "and no other site");
            Expect.equal(GateRules.scansOf(parts), List.of("S3"), "for crawl 3, the sites not taken for scans");
            Expect.equal(GateRules.scansOf("1700000000000 app.test:443".split(" ")), List.of(), "an older hold names none");
            Expect.isTrue(GateRules.crawlThread("ZAP-SpiderThreadPool-3-thread-2", "3"), "crawl 3's thread");
            Expect.isFalse(GateRules.crawlThread("ZAP-SpiderThreadPool-31-thread-2", "3"), "not crawl 31's");
            Expect.isFalse(GateRules.crawlThread("ZAP-SpiderThreadPool-4-thread-1", "3"), "nor crawl 4's");
        });

        Harness.test("a crawl cut short by its time limit says so", () -> {
            Expect.isTrue(ZapScanner.timeLimitNote("Crawl", 0, false, 0) == null, "no limit, no notice");
            Expect.isTrue(ZapScanner.timeLimitNote("Crawl", 5, false, 1_000) == null, "ended by itself well inside the limit");
            String cut = ZapScanner.timeLimitNote("Crawl", 5, true, 4_000);
            Expect.isTrue(cut != null && cut.contains("5 minutes") && cut.contains("still discovering"), "at the limit and still finding: " + cut);
            String quiet = ZapScanner.timeLimitNote("AJAX crawl", 1, true, 200_000);
            Expect.isTrue(quiet != null && quiet.contains("1 minute)") && !quiet.contains("still discovering"), "at the limit with nothing new: " + quiet);
        });

        Harness.test("Deep has no time limit on either crawl", () -> {
            ScanConfig deep = ScanConfig.preset("Deep");
            Expect.equal(deep.crawlDurationMin(), 0, "the crawl");
            Expect.equal(deep.ajaxMaxDurationMin(), 0, "the browser crawl");
            Expect.isTrue(ScanConfig.preset("Balanced").crawlDurationMin() > 0, "the quicker ones keep theirs");
        });
    }
}