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.

5.1 KB
Raw Blame
package opensuite.ui;

import opensuite.core.Bytes;
import opensuite.core.HttpMessage;
import opensuite.core.HttpService;
import opensuite.core.SuiteContext;
import opensuite.script.RunResult;
import opensuite.script.ScriptEngine;
import opensuite.testkit.Expect;
import opensuite.testkit.Harness;

import java.awt.Component;
import java.awt.Container;
import java.util.Arrays;
import java.util.List;
import javax.swing.Action;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;
import javax.tools.ToolProvider;

/**
 * A custom action's new request lands in the request editor beside the
 * response, as one change that a single undo takes back.
 */
public final class CustomActionRunTest {

    private static final HttpService SVC = new HttpService("example.test", 80, false);
    private static final String RAW = "GET /a HTTP/1.1\r\nHost: example.test\r\n\r\n";

    private static void onEdt(Runnable r) {
        try { SwingUtilities.invokeAndWait(r); } catch (Exception e) { throw new AssertionError(e); }
    }

    /** The editable text component showing {@code containing}. */
    private static JTextComponent textOf(Container root, String containing) {
        for (Component c : root.getComponents()) {
            if (c instanceof JTextComponent t && t.isEditable() && t.getText().contains(containing)) return t;
            if (c instanceof Container k) {
                JTextComponent t = textOf(k, containing);
                if (t != null) return t;
            }
        }
        return null;
    }

    /** The text component whose text the editor shows now, with its undo action. */
    private static Action undoOf(Container root, String containing) {
        for (Component c : root.getComponents()) {
            if (c instanceof JTextComponent t && t.getText().contains(containing) && t.getActionMap().get("opensuite-undo") != null) {
                return t.getActionMap().get("opensuite-undo");
            }
            if (c instanceof Container k) {
                Action a = undoOf(k, containing);
                if (a != null) return a;
            }
        }
        return null;
    }

    public void run() {
        Harness.suite("custom action run");

        Harness.test("a new request replaces the edited one, and one undo takes it back", () -> {
            SuiteContext ctx = new SuiteContext();
            MessageEditor[] eds = new MessageEditor[2];
            onEdt(() -> {
                eds[0] = new MessageEditor(ctx, MessageEditor.REQUEST, true);
                eds[1] = new MessageEditor(ctx, MessageEditor.RESPONSE, false);
                eds[1].pairWith(eds[0]);
                eds[0].setMessage(SVC, new HttpMessage(Bytes.bytes(RAW)));
            });
            Expect.isTrue(eds[1].requestSource() == eds[0], "the response's request source is the paired editor");
            HttpMessage changed = new HttpMessage(Bytes.bytes(RAW));
            changed.setHeader("X-A", "1");
            onEdt(() -> eds[0].replaceMessageUndoably(changed));
            Expect.equal(eds[0].message().header("X-A"), "1", "header after the action");
            Expect.isTrue(Arrays.equals(eds[0].message().raw(), changed.raw()), "the exact bytes of the new request");

            Action undo = undoOf(eds[0], "X-A");
            Expect.notNull(undo, "an undo action on the shown text");
            onEdt(() -> undo.actionPerformed(null));
            Expect.isNull(eds[0].message().header("X-A"), "header after one undo");
            Expect.equal(Bytes.str(eds[0].message().raw()), RAW, "the request as it was");
        });

        Harness.test("typing after an action keeps the request's line endings", () -> {
            SuiteContext ctx = new SuiteContext();
            MessageEditor[] ed = new MessageEditor[1];
            onEdt(() -> {
                ed[0] = new MessageEditor(ctx, MessageEditor.REQUEST, true);
                ed[0].setMessage(SVC, new HttpMessage(Bytes.bytes(RAW)));
            });
            HttpMessage changed = new HttpMessage(Bytes.bytes(RAW));
            changed.setHeader("X-A", "1");
            onEdt(() -> ed[0].replaceMessageUndoably(changed));
            JTextComponent shown = textOf(ed[0], "X-A");
            Expect.notNull(shown, "the shown text");
            onEdt(() -> {
                try { shown.getDocument().insertString(shown.getDocument().getLength(), "z", null); }
                catch (javax.swing.text.BadLocationException e) { throw new AssertionError(e); }
            });
            Expect.equal(Bytes.str(ed[0].message().raw()), Bytes.str(changed.raw()) + "z", "the request after typing one character");
        });

        Harness.test("an action on an empty request runs cleanly", () -> {
            RunResult r = new ScriptEngine(ToolProvider.getSystemJavaCompiler(), 5000).runCustomAction(
                    "utilities.log(requestResponse.request().header(\"Host\"));", null, new HttpMessage(new byte[0]), null, s -> { }, new java.util.concurrent.atomic.AtomicBoolean());
            Expect.equal(r.status(), RunResult.Status.OK, "status: " + r.error());
            Expect.equal(r.log(), List.of("null"), "log");
        });
    }
}