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.0 KB
Raw Blame
package opensuite.target;

import java.net.URI;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Finds the links in an HTML response, for the live passive crawl: every
 * {@code href}, {@code src} and {@code action} value, resolved against the
 * page's own URL. Nothing is requested; the links only tell the site map what
 * else the application has.
 *
 * <p>Pure text functions with no Swing or network in them, so they are unit tested.
 */
public final class LinkFinder {
    private LinkFinder() {}

    /** Bodies above this size are not searched: the cost is not worth the few links at the end. */
    static final int MAX_CHARS = 1_000_000;

    /** A page with more links than this is truncated, so one huge index page cannot flood the site map. */
    static final int MAX_LINKS = 500;

    private static final Pattern ATTR = Pattern.compile(
            "(?i)[\\s\"'](?:href|src|action)\\s*=\\s*(?:\"([^\"]*)\"|'([^']*)'|([^\\s\"'>]+))");

    /**
     * The absolute http(s) URLs linked from {@code html}, in document order and
     * without repeats. Fragments are dropped; script, mail, data and other
     * non-web schemes are ignored, as is anything that will not parse.
     */
    public static List<String> links(String baseUrl, String html) {
        List<String> out = new ArrayList<>();
        if (baseUrl == null || html == null || html.isEmpty()) return out;
        if (html.length() > MAX_CHARS) html = html.substring(0, MAX_CHARS);
        URI base;
        try { base = new URI(baseUrl); } catch (Exception e) { return out; }
        Set<String> seen = new LinkedHashSet<>();
        Matcher m = ATTR.matcher(html);
        while (m.find() && seen.size() < MAX_LINKS) {
            String v = m.group(1) != null ? m.group(1) : m.group(2) != null ? m.group(2) : m.group(3);
            String abs = resolve(base, v);
            if (abs != null) seen.add(abs);
        }
        out.addAll(seen);
        return out;
    }

    /** Resolve one attribute value, or null when it is not a web link. */
    static String resolve(URI base, String value) {
        if (value == null) return null;
        String v = value.trim().replace("&amp;", "&");
        if (v.isEmpty() || v.startsWith("#")) return null;
        String lower = v.toLowerCase(Locale.ROOT);
        if (lower.startsWith("javascript:") || lower.startsWith("mailto:") || lower.startsWith("data:")
                || lower.startsWith("tel:") || lower.startsWith("about:") || lower.startsWith("blob:")) return null;
        int hash = v.indexOf('#');
        if (hash >= 0) v = v.substring(0, hash);
        try {
            URI u = base.resolve(new URI(v.replace(" ", "%20")));
            String scheme = u.getScheme();
            if (scheme == null || u.getHost() == null) return null;
            scheme = scheme.toLowerCase(Locale.ROOT);
            if (!scheme.equals("http") && !scheme.equals("https")) return null;
            String path = u.getRawPath() == null || u.getRawPath().isEmpty() ? "/" : u.getRawPath();
            String query = u.getRawQuery() == null ? "" : "?" + u.getRawQuery();
            boolean defaultPort = u.getPort() < 0 || u.getPort() == (scheme.equals("https") ? 443 : 80);
            return scheme + "://" + u.getHost().toLowerCase(Locale.ROOT) + (defaultPort ? "" : ":" + u.getPort()) + path + query;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * The path and query of {@code url} when it is on the site at {@code origin}
     * (scheme, host and port, e.g. {@code http://example.com}), otherwise null.
     */
    public static String pathOn(String origin, String url) {
        if (origin == null || url == null) return null;
        String o = origin.endsWith("/") ? origin.substring(0, origin.length() - 1) : origin;
        if (url.length() <= o.length() || !url.regionMatches(true, 0, o, 0, o.length())) return null;
        return url.charAt(o.length()) == '/' ? url.substring(o.length()) : null;
    }
}