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

import opensuite.core.Settings;

import java.util.ArrayList;
import java.util.List;

/**
 * The TLS pass-through list: destinations whose TLS connections the proxy
 * relays untouched instead of intercepting. Use it for hosts that reject the
 * proxy's certificate (pinned mobile apps, update services) or that are simply
 * not of interest; nothing sent over a passed-through connection is visible in
 * the history.
 *
 * <p>An entry is a host with an optional port, {@code host} or
 * {@code host:port}. The host may use {@code *} as a wildcard, and a leading
 * {@code *.} also covers the bare domain. With no port the entry covers every
 * port.
 */
public final class TlsPassThrough {
    private TlsPassThrough() {}

    /** The entries, one per line. */
    public static final String LIST_KEY = "proxy.tls.passthrough";
    /** Add a host to the list by itself when its client refuses the proxy's certificate. */
    public static final String AUTO_ADD_KEY = "proxy.tls.passthrough.autoadd";

    public static List<String> entries(Settings s) {
        List<String> out = new ArrayList<>();
        for (String line : s.get(LIST_KEY, "").split("\n")) {
            String t = line.trim();
            if (!t.isEmpty()) out.add(t);
        }
        return out;
    }

    public static void setEntries(Settings s, List<String> entries) {
        s.set(LIST_KEY, String.join("\n", entries));
    }

    public static boolean autoAdd(Settings s) { return s.getBool(AUTO_ADD_KEY, false); }

    /** Add an entry unless an existing one already covers it. Returns true when added. */
    public static boolean add(Settings s, String entry) {
        String e = normalise(entry);
        if (e.isEmpty()) return false;
        List<String> cur = entries(s);
        for (String c : cur) if (c.equalsIgnoreCase(e)) return false;
        cur.add(e);
        setEntries(s, cur);
        return true;
    }

    /**
     * Tidy what the user typed: a pasted URL is reduced to its host and
     * explicit port, and surrounding space is dropped.
     */
    public static String normalise(String entry) {
        if (entry == null) return "";
        String e = entry.trim();
        int scheme = e.indexOf("://");
        if (scheme >= 0) e = e.substring(scheme + 3);
        int slash = e.indexOf('/');
        if (slash >= 0) e = e.substring(0, slash);
        return e.trim();
    }

    /** True when any entry covers the destination. */
    public static boolean matches(List<String> entries, String host, int port) {
        if (host == null) return false;
        for (String e : entries) if (matches(e, host, port)) return true;
        return false;
    }

    static boolean matches(String entry, String host, int port) {
        String e = normalise(entry);
        if (e.isEmpty()) return false;
        String pattern = e;
        // A trailing :digits is a port. A bracketed IPv6 literal keeps its colons.
        int colon = e.lastIndexOf(':');
        if (colon > 0 && colon > e.lastIndexOf(']') && colon < e.length() - 1
                && e.substring(colon + 1).chars().allMatch(Character::isDigit)) {
            try {
                if (Integer.parseInt(e.substring(colon + 1)) != port) return false;
            } catch (NumberFormatException ex) {
                return false;
            }
            pattern = e.substring(0, colon);
        }
        String h = host.toLowerCase();
        String p = pattern.toLowerCase();
        if (p.startsWith("*.") && h.equals(p.substring(2))) return true;
        return wildcard(p, h);
    }

    /** Glob match where {@code *} stands for any run of characters. */
    private static boolean wildcard(String pattern, String text) {
        int p = 0, t = 0, star = -1, mark = 0;
        while (t < text.length()) {
            if (p < pattern.length() && pattern.charAt(p) == '*') { star = p++; mark = t; }
            else if (p < pattern.length() && pattern.charAt(p) == text.charAt(t)) { p++; t++; }
            else if (star >= 0) { p = star + 1; t = ++mark; }
            else return false;
        }
        while (p < pattern.length() && pattern.charAt(p) == '*') p++;
        return p == pattern.length();
    }
}