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.

6.2 KB
Raw Blame
package opensuite.target;

import opensuite.core.HttpService;
import opensuite.core.LogEntry;

import javax.swing.tree.DefaultMutableTreeNode;
import java.util.*;

/**
 * A deduplicated structural view of observed traffic.
 *
 * Unlike the proxy history (which keeps every exchange), the site map keeps one
 * entry per unique method + URL (path and query). Re-requesting the same URL
 * updates the existing entry with the latest request and response rather than
 * adding a duplicate. Different query values are different entries.
 */
public class SiteMap {

    public static class Node {
        public final String label;
        public final boolean isHost;
        public HttpService service;
        public String path = "";
        public LogEntry entry;              // representative leaf request, if any
        public final Map<String, Node> children = new LinkedHashMap<>();

        // Deduplicated: key = method + " " + pathAndQuery -> latest exchange.
        private final Map<String, LogEntry> entryMap = new LinkedHashMap<>();

        Node(String label, boolean isHost) { this.label = label; this.isHost = isHost; }

        LogEntry put(LogEntry e) {
            return entryMap.put(key(e), e);   // replaces any earlier exchange for the same URL
        }

        /** The deduplicated entries at or below this node. */
        public List<LogEntry> entries() { return new ArrayList<>(entryMap.values()); }

        @Override public String toString() { return label; }
    }

    static String key(LogEntry e) {
        return e.method() + " " + e.pathAndQuery();
    }

    private final Node root = new Node("", false);

    /** Items known only from a link in a response: in the map, but never requested. */
    private final java.util.Set<LogEntry> linked =
            java.util.Collections.newSetFromMap(new java.util.IdentityHashMap<>());

    /** True for an item that was found as a link and has not been requested. */
    public synchronized boolean isLinked(LogEntry e) { return e != null && linked.contains(e); }

    /**
     * Add an item found as a link. It never displaces a real exchange: if the
     * URL is already in the map nothing happens. Returns whether it was added.
     */
    public synchronized boolean addLinked(LogEntry e) {
        if (e.service() == null || !e.hasRequest()) return false;
        Node host = root.children.get(e.service().url());
        if (host != null && host.entryMap.containsKey(key(e))) return false;
        linked.add(e);
        add(e);
        return true;
    }

    public Node root() { return root; }

    /**
     * Take a host, folder or item out of the map, with everything under it.
     * The folders above it stay, without the items that went. Returns whether
     * the node was in the map.
     */
    public synchronized boolean remove(Node target) {
        List<Node> above = new ArrayList<>();
        if (target == null || target == root || !path(root, target, above)) return false;
        List<LogEntry> gone = target.entries();
        if (target.entry != null) gone.add(target.entry);
        above.get(above.size() - 1).children.values().remove(target);
        for (Node a : above) {
            for (LogEntry e : gone) {
                a.entryMap.remove(key(e), e);
                if (a.entry == e) a.entry = null;
            }
        }
        for (LogEntry e : gone) linked.remove(e);
        return true;
    }

    /** Fill {@code above} with the nodes from {@code from} down to the parent of {@code target}. */
    private static boolean path(Node from, Node target, List<Node> above) {
        above.add(from);
        for (Node c : from.children.values()) {
            if (c == target || path(c, target, above)) return true;
        }
        above.remove(above.size() - 1);
        return false;
    }

    public synchronized void add(LogEntry e) {
        if (e.service() == null || !e.hasRequest()) return;
        if (e.offSiteMap()) return;   // deleted by the user; the same address requested again is a new item and returns
        String hostKey = e.service().url();
        Node host = root.children.computeIfAbsent(hostKey, k -> {
            Node n = new Node(e.service().url(), true);
            n.service = e.service();
            return n;
        });
        LogEntry before = host.put(e);
        if (before != null && before != e) linked.remove(before);   // a linked item that has now been requested

        String path = e.requestPath();
        String[] parts = path.split("/");
        Node cur = host;
        StringBuilder acc = new StringBuilder();
        for (String p : parts) {
            if (p.isEmpty()) continue;
            acc.append('/').append(p);
            final String seg = p;
            Node child = cur.children.computeIfAbsent(seg, k -> {
                Node n = new Node(seg, false);
                n.service = e.service();
                n.path = acc.toString();
                return n;
            });
            child.put(e);
            cur = child;
        }
        cur.entry = e;   // latest representative for the leaf

        // Parameterised requests branch under the endpoint, so /x?a=1 and
        // /x?a=2 are distinct children of x (each query variant is its own node).
        String query = e.requestQuery();
        if (query != null && !query.isEmpty()) {
            final Node leaf = cur;
            String ckey = "?" + e.method() + " " + query;
            Node pchild = cur.children.computeIfAbsent(ckey, k -> {
                Node n = new Node("?" + query, false);
                n.service = e.service();
                n.path = leaf.path;
                return n;
            });
            pchild.put(e);
            pchild.entry = e;
        }
    }

    public synchronized DefaultMutableTreeNode toTreeNode() {
        DefaultMutableTreeNode r = new DefaultMutableTreeNode(root);
        for (Node host : root.children.values()) addChildren(r, host);
        return r;
    }

    private void addChildren(DefaultMutableTreeNode parent, Node node) {
        DefaultMutableTreeNode tn = new DefaultMutableTreeNode(node);
        parent.add(tn);
        for (Node c : node.children.values()) addChildren(tn, c);
    }

    public synchronized void clear() { root.children.clear(); linked.clear(); }
}