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.

4.8 KB
Raw Blame
package opensuite.gate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Which rule the engine has started on which address, for one scan, in the
 * order it started them.
 *
 * <p>The engine works rule by rule: it takes one rule across every address it
 * is auditing, then the next. Its own progress figures say how far each rule
 * has got as a count, not which addresses that count covers. It does announce
 * each start, though, as the rule's name and the address; those are kept
 * here, so a caller can say of one address which rules have been started on
 * it. A step is one rule started on one address.
 *
 * <p>Names and addresses are numbered as they are first met and a step is the
 * two numbers, so a scan of many thousands of addresses stays small. A caller
 * reads the steps in batches, each time from where it stopped, with the names
 * and addresses it has not had yet.
 */
public final class RuleSteps {

    /** A step is one number: the rule in the high part, the address in the low. */
    private static final int ADDRESS_BITS = 22, RULES = 1 << 10, ADDRESSES = 1 << ADDRESS_BITS;
    /** The most steps kept for a scan. Beyond it the record is marked full and takes no more. */
    static final int MOST = 8_000_000;

    private final List<String> names = new ArrayList<>(), addresses = new ArrayList<>();
    private final Map<String, Integer> nameAt = new HashMap<>(), addressAt = new HashMap<>();
    private int[] steps = new int[256];
    private int count;
    private boolean full;

    /**
     * The rule and the address in one of the engine's progress lines, which
     * read "rule name: address", or null for a line that is not one of those.
     */
    public static String[] parse(String line) {
        if (line == null) return null;
        int at = line.indexOf(": http://");
        int secure = line.indexOf(": https://");
        if (at < 0 || (secure >= 0 && secure < at)) at = secure;
        if (at <= 0) return null;
        return new String[] {line.substring(0, at), address(line.substring(at + 2))};
    }

    /** An address as it is kept: without its query or fragment. */
    public static String address(String url) {
        int cut = url.length();
        int q = url.indexOf('?'), h = url.indexOf('#');
        if (q >= 0) cut = q;
        if (h >= 0 && h < cut) cut = h;
        return url.substring(0, cut);
    }

    /** Note that the rule of this name has been started on this address. */
    public synchronized void started(String name, String address) {
        if (full || name == null || address == null) return;
        Integer n = nameAt.get(name), a = addressAt.get(address);
        if ((n == null && names.size() >= RULES) || (a == null && addresses.size() >= ADDRESSES) || count >= MOST) {
            full = true;
            return;
        }
        if (n == null) { n = names.size(); names.add(name); nameAt.put(name, n); }
        if (a == null) { a = addresses.size(); addresses.add(address); addressAt.put(address, a); }
        if (count == steps.length) steps = java.util.Arrays.copyOf(steps, count * 2);
        steps[count++] = (n << ADDRESS_BITS) | a;
    }

    /** Whether steps were lost: the record took no more, so it no longer holds every start. */
    public synchronized boolean full() { return full; }
    public synchronized int count() { return count; }

    /**
     * What a caller has not had yet. Steps are written as runs of one rule,
     * "rule:address,address;rule:address", by number; names and addresses are
     * one to a line, in the order of their numbers.
     */
    public record Batch(String steps, String names, String addresses, int next, boolean more, boolean full) {}

    public synchronized Batch after(int after, int namesFrom, int addressesFrom, int most) {
        int from = Math.max(0, Math.min(after, count)), to = (int) Math.min(count, (long) from + Math.max(1, most));
        StringBuilder out = new StringBuilder();
        int rule = -1;
        for (int i = from; i < to; i++) {
            int r = steps[i] >>> ADDRESS_BITS, a = steps[i] & (ADDRESSES - 1);
            if (r != rule) {
                if (out.length() > 0) out.append(';');
                out.append(r).append(':');
                rule = r;
            } else {
                out.append(',');
            }
            out.append(a);
        }
        return new Batch(out.toString(), lines(names, namesFrom), lines(addresses, addressesFrom), to, to < count, full);
    }

    private static String lines(List<String> all, int from) {
        StringBuilder out = new StringBuilder();
        for (int i = Math.max(0, from); i < all.size(); i++) {
            if (out.length() > 0) out.append('\n');
            out.append(all.get(i).replace('\n', ' ').replace('\r', ' '));
        }
        return out.toString();
    }
}