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.script;

import opensuite.core.Bytes;
import opensuite.core.HttpMessage;
import opensuite.core.HttpService;
import opensuite.script.api.HttpRequest;
import opensuite.script.api.HttpRequestResponse;
import opensuite.script.api.Utilities;
import opensuite.testkit.Expect;
import opensuite.testkit.Harness;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * The request, response and utilities a script works with: copies never touch
 * the original, an untouched request keeps its exact bytes, and the hashes
 * match published vectors.
 */
public final class ScriptApiTest {

    private static final HttpService SVC = new HttpService("example.test", 443, true);

    private static HttpRequest req(String raw) { return HttpRequest.of(SVC, new HttpMessage(Bytes.bytes(raw))); }
    private static HttpRequest req(byte[] raw) { return HttpRequest.of(SVC, new HttpMessage(raw)); }
    private static Utilities utils() { return new Utilities(s -> { }); }

    public void run() {
        Harness.suite("script api");

        Harness.test("with methods return copies", () -> {
            HttpRequest r = req("GET /a HTTP/1.1\r\nHost: x\r\n\r\n");
            HttpRequest r2 = r.withHeader("X-A", "1");
            Expect.isNull(r.header("X-A"), "original header");
            Expect.equal(r2.header("X-A"), "1", "copy header");
        });

        Harness.test("headers lists every header, the first included", () -> {
            Expect.equal(req("GET / HTTP/1.1\r\nHost: x\r\nX-A: 1\r\n\r\n").headers(), List.of("Host: x", "X-A: 1"), "headers");
        });

        Harness.test("untouched request round-trips byte-exact", () -> {
            for (String raw : new String[]{"GET /a HTTP/1.1\r\nHost: x\r\nX-Odd:  spaced \r\n\r\n", "GARBAGE\n\nbody"}) {
                HttpMessage m = new HttpMessage(Bytes.bytes(raw));
                Expect.isTrue(Arrays.equals(HttpRequest.of(SVC, m).message().raw(), m.raw()), "same bytes for " + raw);
            }
        });

        Harness.test("withBody fixes Content-Length", () -> {
            HttpRequest r = req("POST /p HTTP/1.1\r\nHost: x\r\nContent-Length: 3\r\n\r\nabc").withBody("hello");
            Expect.equal(r.header("Content-Length"), "5", "content length");
            Expect.equal(r.bodyText(), "hello", "body");
        });

        Harness.test("bodyText uses the declared charset, else UTF-8", () -> {
            byte[] head = Bytes.bytes("POST /p HTTP/1.1\r\nHost: x\r\n\r\n");
            HttpRequest utf8 = req(Bytes.concat(head, "caf\u00e9".getBytes(StandardCharsets.UTF_8)));
            Expect.equal(utf8.bodyText(), "caf\u00e9", "utf-8 default");
            byte[] latinHead = Bytes.bytes("POST /p HTTP/1.1\r\nHost: x\r\nContent-Type: text/plain; charset=ISO-8859-1\r\n\r\n");
            HttpRequest latin = req(Bytes.concat(latinHead, new byte[]{'c', 'a', 'f', (byte) 0xE9}));
            Expect.equal(latin.bodyText(), "caf\u00e9", "declared latin-1");
        });

        Harness.test("binary body hmac is over the raw bytes", () -> {
            byte[] body = {0x1f, (byte) 0x8b, 0x08, 0x00};
            HttpRequest r = req(Bytes.concat(Bytes.bytes("POST /p HTTP/1.1\r\nHost: x\r\n\r\n"), body));
            Utilities u = utils();
            byte[] key = u.bytes("k");
            Expect.equal(u.hex(u.hmacSha256(key, r.body())), u.hex(u.hmacSha256(key, body)), "hmac");
            Expect.isTrue(Arrays.equals(r.message().raw(), r.withHeader("X-A", "1").withRemovedHeader("X-A").message().raw()), "header add then remove keeps the body");
        });

        Harness.test("hmac sha256 matches RFC 4231 case 2", () -> {
            Utilities u = utils();
            Expect.equal(u.hex(u.hmacSha256(u.bytes("Jefe"), u.bytes("what do ya want for nothing?"))),
                    "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843", "hmac");
        });

        Harness.test("sha256, md5 and base64 match known values", () -> {
            Utilities u = utils();
            Expect.equal(u.hex(u.sha256(u.bytes("abc"))), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", "sha256");
            Expect.equal(u.hex(u.md5(u.bytes("abc"))), "900150983cd24fb0d6963f7d28e17f72", "md5");
            Expect.equal(u.base64Encode(u.bytes("hi")), "aGk=", "base64");
            Expect.equal(new String(u.base64Decode("aGk="), StandardCharsets.UTF_8), "hi", "base64 decode");
            Expect.equal(u.urlEncode("a b&c"), "a+b%26c", "url encode");
        });

        Harness.test("log is capped at 1000 lines", () -> {
            Utilities u = utils();
            for (int i = 0; i < 1005; i++) u.log("line " + i);
            Expect.equal(u.logLines().size(), 1000, "lines kept");
            Expect.isTrue(u.logCut(), "cut flagged");
        });

        Harness.test("a line too long for the output is shortened, not dropped", () -> {
            Utilities u = utils();
            u.log("x".repeat(70_000));
            Expect.equal(u.logLines().size(), 1, "lines kept");
            Expect.equal(u.logLines().get(0).length(), Utilities.MAX_CHARS, "kept up to the limit");
            Expect.isTrue(u.logCut(), "cut flagged");
        });

        Harness.test("clipboard goes to the sink", () -> {
            List<String> got = new ArrayList<>();
            new Utilities(got::add).clipboard("copied");
            Expect.equal(got, List.of("copied"), "clipboard");
        });

        Harness.test("empty request reads safely", () -> {
            HttpRequest r = req(new byte[0]);
            Expect.isNull(r.header("Host"), "no host");
            Expect.equal(r.body().length, 0, "empty body");
        });

        Harness.test("setRequest is recorded", () -> {
            HttpRequest r = req("GET / HTTP/1.1\r\nHost: x\r\n\r\n");
            HttpRequestResponse rr = new HttpRequestResponse(r, null);
            Expect.isNull(rr.replacement(), "before");
            Expect.isFalse(rr.hasResponse(), "no response");
            HttpRequest changed = r.withMethod("POST").withPath("/new");
            rr.setRequest(changed);
            Expect.equal(rr.replacement().method(), "POST", "method");
            Expect.equal(rr.replacement().path(), "/new", "path");
        });
    }
}