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.3 KB
Raw Blame
package opensuite.proxy;

import opensuite.core.HttpMessage;
import opensuite.core.HttpService;

import java.util.List;
import java.util.regex.Pattern;

/**
 * Builds the "Don't intercept requests" quick rules offered while a request is
 * held: for this host, this file extension, or this directory. Each appends an
 * {@code and ... does not match} rule to the request interception chain, so a
 * matching request is forwarded without being stalled. The chain is folded
 * left to right, so an appended {@code and} clause gates the whole result.
 */
public final class InterceptExclusion {
    private InterceptExclusion() {}

    public enum Kind { HOST, EXTENSION, DIRECTORY }

    /** The menu label for a kind, given the request it was raised on. */
    public static String label(Kind kind, HttpService svc, HttpMessage req) {
        return switch (kind) {
            case HOST -> "For this host: " + svc.host();
            case EXTENSION -> {
                String ext = req == null ? "" : req.fileExtension();
                yield ext.isEmpty() ? "For this file extension" : "For this file extension: ." + ext;
            }
            case DIRECTORY -> "For this directory: " + directory(req);
        };
    }

    /** Whether a kind applies to the request (an extension rule needs an extension). */
    public static boolean applies(Kind kind, HttpMessage req) {
        if (kind == Kind.EXTENSION) return req != null && !req.fileExtension().isEmpty();
        return true;
    }

    /**
     * The rule that excludes this request's host, extension or directory from
     * interception. Returns null when the kind does not apply.
     */
    public static ProxyRule rule(Kind kind, HttpService svc, HttpMessage req) {
        return switch (kind) {
            case HOST -> new ProxyRule(true, "and", "domain name", "does not match",
                    "^" + Pattern.quote(svc.host()) + "$");
            case EXTENSION -> {
                String ext = req == null ? "" : req.fileExtension();
                if (ext.isEmpty()) yield null;
                yield new ProxyRule(true, "and", "file extension", "does not match",
                        "^" + Pattern.quote(ext) + "$");
            }
            case DIRECTORY -> new ProxyRule(true, "and", "URL", "does not match",
                    "^" + Pattern.quote(directory(req)));
        };
    }

    /**
     * Add the rule to the chain unless a rule with the same item and condition
     * is already there. Returns true when it was added.
     */
    public static boolean add(List<ProxyRule> chain, Kind kind, HttpService svc, HttpMessage req) {
        ProxyRule r = rule(kind, svc, req);
        if (r == null) return false;
        for (ProxyRule e : chain) {
            if (e.matchItem.equals(r.matchItem) && e.condition.equals(r.condition)
                    && e.relationship.equals(r.relationship)) {
                return false;
            }
        }
        chain.add(r);
        return true;
    }

    /** The directory part of a request target: the path up to and including the last slash. */
    static String directory(HttpMessage req) {
        String path = req == null ? "/" : req.path();
        if (path == null || path.isEmpty()) return "/";
        int slash = path.lastIndexOf('/');
        return slash < 0 ? "/" : path.substring(0, slash + 1);
    }
}