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.

8.0 KB
Raw Blame
package opensuite.ui;

import opensuite.core.Issue;

import java.util.List;

/**
 * Renders an issue as the advisory shown on the target and dashboard tabs.
 *
 * <p>Both tabs used to build this markup themselves and had drifted apart, so
 * the same finding read differently depending on where you clicked. One
 * renderer keeps them identical and means a new issue field has one place to be
 * added.
 *
 * <p>Everything interpolated is escaped, and reference URLs are rendered as
 * plain text rather than anchors. The advisory pane is a Swing HTML view, and a
 * live anchor in a pane that also displays attacker-influenced strings is an
 * invitation; a tester can copy the URL.
 */
public final class Advisory {

    private Advisory() {}

    private static String colourFor(Issue.Severity s) {
        return switch (s) {
            case HIGH        -> "#E0474C";
            case MEDIUM      -> "#7E57C2";
            case LOW         -> "#4477CC";
            case INFORMATION -> "#888888";
        };
    }

    /** The full advisory, for the target tab's wide panel. */
    public static String render(Issue i) {
        return render(i, true);
    }

    /** A colour as CSS hex, so the advisory follows the light or dark theme. */
    private static String hex(java.awt.Color c) {
        return String.format("#%02X%02X%02X", c.getRed(), c.getGreen(), c.getBlue());
    }

    /** Settings > Display > Issues: how an issue's evidence is laid out, read each time an advisory is drawn. */
    private static volatile java.util.function.BooleanSupplier grouped = () -> true, expanded = () -> false;

    public static void bind(opensuite.core.Settings s) {
        grouped = () -> !"ungrouped".equals(s.get("ui.issueEvidence", "grouped"));
        expanded = () -> s.getBool("ui.expandEvidence", false);
    }

    /** How many locations a group that is not expanded lists before it gives the count of the rest. */
    static final int COLLAPSED = 5;

    /** The advisory, optionally without the location table for a narrow panel. */
    public static String render(Issue i, boolean withLocation) {
        return render(i, withLocation, grouped.getAsBoolean(), expanded.getAsBoolean());
    }

    /**
     * The advisory with the evidence laid out as asked: grouped, the places
     * the issue was seen form one list under the evidence (cut to the first
     * few unless expanded); ungrouped, each place is an item of its own.
     */
    static String render(Issue i, boolean withLocation, boolean grouped, boolean expanded) {
        if (i == null) return "<html><body></body></html>";
        StringBuilder b = new StringBuilder(1024);
        b.append("<html><body style='font-family:sans-serif;font-size:11px;padding:6px;color:")
         .append(hex(Theme.TEXT)).append("'>");
        b.append("<h2 style='margin:0 0 6px 0'>").append(esc(i.title())).append("</h2>");

        b.append("<table cellpadding=2>");
        row(b, "Severity", i.falsePositive() ? "<span style='color:#888888'>False positive</span> <i>(reported as "
                + esc(i.severity().label()) + ")</i>"
                : "<span style='color:" + colourFor(i.severity()) + "'>" + esc(i.severity().label()) + "</span>");
        row(b, "Confidence", esc(i.confidence().label()));
        if (withLocation) {
            row(b, "Host", esc(i.service() == null ? "" : i.service().url()));
            if (i.locationCount() > 1) row(b, "Path", esc(i.path()) + "  <i>(+" + (i.locationCount() - 1) + " more below)</i>");
            else row(b, "Path", esc(i.path()));
        }
        if (i.param() != null && !i.param().isBlank()) row(b, "Parameter", esc(i.param()));

        // Classification, only the parts the raising module supplied.
        if (i.moduleId() > 0) row(b, "Module", String.valueOf(i.moduleId()));
        b.append("</table>");

        String host = i.service() == null ? "" : i.service().url();
        List<String> places = i.locations();
        String shown = i.evidence().isBlank() ? "" : "<pre style='font-family:monospace;font-size:11px;background:" + hex(Theme.SURFACE_2)
                + ";padding:4px;white-space:pre-wrap'>" + esc(trim(i.evidence(), 2000)) + "</pre>";
        if (withLocation && places.size() > 1 && !grouped) {
            // One item to a place; the evidence text was captured at the first.
            for (int n = 0; n < places.size(); n++) {
                b.append("<h3>Evidence ").append(n + 1).append("</h3><div>").append(esc(host)).append(esc(places.get(n))).append("</div>");
                if (n == 0) b.append(shown);
            }
        } else {
            if (withLocation && places.size() > 1) {
                int listed = expanded ? places.size() : Math.min(COLLAPSED, places.size());
                b.append("<h3>Affected locations (").append(places.size()).append(")</h3><ul style='margin:0 0 0 14px;padding:0'>");
                for (int n = 0; n < listed; n++) b.append("<li>").append(esc(host)).append(esc(places.get(n))).append("</li>");
                b.append("</ul>");
                if (listed < places.size()) b.append("<div style='color:").append(hex(Theme.MUTED)).append("'>and ")
                        .append(places.size() - listed).append(" more. Settings &gt; Display &gt; Issues lists them all.</div>");
            }
            if (!shown.isEmpty()) b.append("<h3>Evidence</h3>").append(shown);
        }
        section(b, "Issue detail", i.detail());
        section(b, "Issue background", i.background());
        section(b, "Remediation", i.remediation());

        List<String> refs = opensuite.core.Classifications.references(i);
        if (!refs.isEmpty()) {
            b.append("<h3>References</h3><ul style='margin:0 0 0 14px;padding:0'>");
            for (String line : refs) b.append("<li>").append(linked(line, line)).append("</li>");
            b.append("</ul>");
        }
        List<opensuite.core.Classifications.Entry> classes = opensuite.core.Classifications.of(i);
        if (!classes.isEmpty()) {
            b.append("<h3>Vulnerability classifications</h3><ul style='margin:0 0 0 14px;padding:0'>");
            for (opensuite.core.Classifications.Entry c : classes) b.append("<li>").append(linked(c.label(), c.link())).append("</li>");
            b.append("</ul>");
        }
        b.append("</body></html>");
        return b.toString();
    }

    /** Make the links in an advisory pane open in the system browser. */
    public static void clickable(javax.swing.JEditorPane pane) {
        pane.addHyperlinkListener(e -> {
            if (e.getEventType() == javax.swing.event.HyperlinkEvent.EventType.ACTIVATED && e.getURL() != null) {
                AboutDialog.open(e.getURL().toString());
            }
        });
    }

    /** Text shown as a link when it leads somewhere on the web, and plainly otherwise. */
    private static String linked(String text, String to) {
        if (to == null || !(to.startsWith("http://") || to.startsWith("https://"))) return esc(text);
        return "<a href='" + esc(to) + "'>" + esc(text) + "</a>";
    }

    /** A compact single-finding advisory for the dashboard's narrower pane. */
    public static String renderCompact(Issue i) {
        return render(i, false);
    }

    private static void row(StringBuilder b, String label, String value) {
        b.append("<tr><td valign='top'><b>").append(label).append(":</b></td><td>")
         .append(value).append("</td></tr>");
    }

    private static void section(StringBuilder b, String heading, String body) {
        if (body == null || body.isBlank()) return;
        b.append("<h3>").append(heading).append("</h3><p>")
         .append(esc(body).replace("\n", "<br>")).append("</p>");
    }

    private static String trim(String s, int max) {
        if (s == null) return "";
        return s.length() <= max ? s : s.substring(0, max) + "...";
    }

    /** Escape for HTML. Must be applied to every interpolated value. */
    public static String esc(String s) {
        if (s == null) return "";
        return s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
                .replace("\"", "&quot;");
    }
}