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.8 KB
Raw Blame
package opensuite.engine;

import opensuite.core.HttpService;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * A hard stop for a scan's requests, kept by Open Suite's own add-on inside
 * the engine (engine-addon/). The engine's pause lets tests under way go on
 * sending; a hold here makes every request the crawler or the auditor is about
 * to send to the scan's sites wait until it is let go.
 *
 * <p>A hold is for one scan: the task says which of the engine's scans it is
 * running, and only that scan's requests wait. Another scan of the same site
 * goes on. (The first version of the add-on could not tell scans apart and
 * held the whole site; with that add-on it still does.)
 *
 * <p>A hold is a lease: it names a time a few seconds ahead, and is renewed
 * while it is wanted. If Open Suite stops renewing it, for any reason, the
 * engine lets the requests go by itself, so a scan cannot be left stuck.
 * An engine without the add-on ignores all of this and pauses softly as before.
 */
public final class ScanGate {

    static final String PREFIX = "opensuite.hold.";
    private static final long LEASE_MS = 10_000, RENEW_MS = 3_000;

    private record Hold(ZapApi api, String sites) {}

    private static final Map<Integer, Hold> HELD = new ConcurrentHashMap<>();
    /** What each task has the engine doing now: "S3" a crawl, "A5" an audit, "J" the browser crawl. */
    private static final Map<Integer, String> RUNNING = new ConcurrentHashMap<>();

    /**
     * Say which of the engine's scans a task is running now, or null for
     * none. A hold then stops that scan alone, and another scan of the same
     * site goes on. A hold already in place is moved to the new scan at once.
     */
    public static void running(int task, String engineScan) {
        if (engineScan == null) RUNNING.remove(task); else RUNNING.put(task, engineScan);
        Hold h = HELD.get(task);
        if (h != null) write(task, h);
    }

    /** Whether this engine's add-on can hold one scan by itself. An older one holds the whole site. */
    private static boolean scoped(ZapApi api) { return api.gateVersion() >= 2; }
    private static Thread renewer;

    private ScanGate() {}

    /** Hold the requests of this task to these sites ("http://host:port"). Asking again changes nothing. */
    public static void hold(ZapApi api, int task, Collection<String> sites) {
        if (api == null || sites == null || sites.isEmpty()) return;
        Hold h = new Hold(api, sitesValue(sites));
        if (h.sites().isEmpty()) return;
        HELD.put(task, h);
        write(task, h);
        startRenewer();
    }

    /** Let this task's requests go. */
    public static void release(ZapApi api, int task) {
        Hold h = HELD.remove(task);
        if (h == null) return;
        try { h.api().clearGlobalVar(PREFIX + task); } catch (Exception ignore) { /* the lease runs out by itself */ }
    }

    public static boolean holding(int task) { return HELD.containsKey(task); }

    /**
     * The number of another task that is holding one of these sites, or null.
     * The add-on tells requests apart by where they go and not by which scan
     * sent them, so one scan's hold stops every scan of the same site; a scan
     * that is stopped so has to be able to say whose hold it is waiting on.
     */
    public static Integer heldByOther(int task, Collection<String> sites) {
        List<String> mine = List.of(sitesValue(sites).split(" "));
        for (Map.Entry<Integer, Hold> e : HELD.entrySet()) {
            if (e.getKey() == task || scoped(e.getValue().api())) continue;   // a hold on one scan holds no other
            for (String theirs : e.getValue().sites().split(" "))
                if (!theirs.isEmpty() && mine.contains(theirs)) return e.getKey();
        }
        return null;
    }

    private static void write(int task, Hold h) {
        String scan = scoped(h.api()) ? RUNNING.getOrDefault(task, NONE) : "";
        try { h.api().setGlobalVar(PREFIX + task, value(System.currentTimeMillis() + LEASE_MS, h.sites(), scan)); }
        catch (Exception ignore) { /* an engine that cannot be reached is not sending either */ }
    }

    private static synchronized void startRenewer() {
        if (renewer != null && renewer.isAlive()) return;
        renewer = new Thread(() -> {
            try {
                while (!HELD.isEmpty()) {
                    Thread.sleep(RENEW_MS);
                    for (Map.Entry<Integer, Hold> e : HELD.entrySet()) write(e.getKey(), e.getValue());
                }
            } catch (InterruptedException ignore) { }
        }, "scan-gate");
        renewer.setDaemon(true);
        renewer.start();
    }

    /** A hold for a task that has no scan running in the engine: it holds nothing until one starts. */
    static final String NONE = "N";

    /** What the add-on reads: the time the hold runs out, then each site as host:port. */
    static String value(long until, String sites) { return until + " " + sites; }

    /** The same, naming the one engine scan the hold is for. With none named, every scan of the sites is held. */
    static String value(long until, String sites, String engineScan) {
        return engineScan == null || engineScan.isEmpty() ? value(until, sites) : value(until, sites) + " " + engineScan;
    }

    /** Sites written "http://host:port" as the add-on compares them: "host:port", the usual port filled in. */
    static String sitesValue(Collection<String> sites) {
        List<String> out = new ArrayList<>();
        for (String s : sites) {
            try {
                HttpService svc = HttpService.parse(s);
                String hp = svc.host() + ":" + svc.port();
                if (!out.contains(hp)) out.add(hp);
            } catch (RuntimeException ignore) { }
        }
        return String.join(" ", out);
    }
}