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.6 KB
Raw Blame
package opensuite.core;

import opensuite.testkit.Expect;
import opensuite.testkit.Harness;

/** Tests the editor's "Change body encoding" conversion. */
public final class BodyEncodingTest {

    private static HttpMessage post(String type, String body) {
        return new HttpMessage(Bytes.bytes("POST /login HTTP/1.1\r\nHost: a.test\r\nContent-Type: " + type
                + "\r\nContent-Length: " + body.length() + "\r\n\r\n" + body));
    }

    public void run() {
        Harness.suite("body encoding");

        Harness.test("a form body becomes JSON and comes back the same", () -> {
            HttpMessage m = new HttpMessage(Bytes.bytes("POST /a HTTP/1.1\r\nHost: x\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 13\r\n\r\na=1&b=two+%26"));
            Expect.isTrue(BodyEncoding.convert(m, BodyEncoding.Kind.JSON, "B"), "converted");
            Expect.equal(m.bodyText(), "{\"a\":\"1\",\"b\":\"two &\"}", "json body");
            Expect.equal(m.header("Content-Type"), "application/json", "type");
            Expect.equal(m.header("Content-Length"), String.valueOf(m.body().length), "length");
            Expect.isTrue(BodyEncoding.convert(m, BodyEncoding.Kind.FORM, "B"), "back");
            Expect.equal(m.bodyText(), "a=1&b=two+%26", "form body");
        });

        Harness.test("JSON becomes multipart; the encoding it already has, or a body that cannot be read, changes nothing", () -> {
            HttpMessage m = new HttpMessage(Bytes.bytes("POST /a HTTP/1.1\r\nHost: x\r\nContent-Type: application/json\r\n\r\n{\"n\":5,\"s\":\"v\"}"));
            Expect.isFalse(BodyEncoding.convert(m, BodyEncoding.Kind.JSON, "B"), "already JSON");
            Expect.isTrue(BodyEncoding.convert(m, BodyEncoding.Kind.MULTIPART, "B"), "converted");
            Expect.isTrue(m.bodyText().contains("name=\"n\"\r\n\r\n5\r\n") && m.bodyText().contains("name=\"s\"\r\n\r\nv\r\n"), "parts");
            HttpMessage bad = new HttpMessage(Bytes.bytes("POST /a HTTP/1.1\r\nHost: x\r\nContent-Type: application/json\r\n\r\n[1,2"));
            Expect.isFalse(BodyEncoding.convert(bad, BodyEncoding.Kind.FORM, "B"), "unreadable JSON");
            Expect.equal(bad.bodyText(), "[1,2", "left alone");
        });

        Harness.test("a URL-encoded body becomes multipart with decoded values", () -> {
            HttpMessage m = post("application/x-www-form-urlencoded", "user=a+b&pw=%26x&flag");
            Expect.isTrue(BodyEncoding.toggle(m, "XB"), "changed");
            Expect.equal(m.header("Content-Type"), "multipart/form-data; boundary=XB", "content type");
            Expect.equal(m.bodyText(),
                    "--XB\r\nContent-Disposition: form-data; name=\"user\"\r\n\r\na b\r\n"
                    + "--XB\r\nContent-Disposition: form-data; name=\"pw\"\r\n\r\n&x\r\n"
                    + "--XB\r\nContent-Disposition: form-data; name=\"flag\"\r\n\r\n\r\n"
                    + "--XB--\r\n", "multipart body");
            Expect.equal(m.header("Content-Length"), String.valueOf(m.body().length), "length updated");
        });

        Harness.test("a multipart body becomes URL-encoded, and the round trip is lossless", () -> {
            HttpMessage m = post("application/x-www-form-urlencoded", "user=a+b&pw=%26x%3D1&empty=");
            BodyEncoding.toggle(m, "XB");
            Expect.isTrue(BodyEncoding.isMultipart(m), "multipart after the first change");
            Expect.isTrue(BodyEncoding.toggle(m, "unused"), "changed back");
            Expect.equal(m.header("Content-Type"), "application/x-www-form-urlencoded", "content type");
            Expect.equal(m.bodyText(), "user=a+b&pw=%26x%3D1&empty=", "values re-encoded");
        });

        Harness.test("file parts keep their field name, and a quoted boundary is read", () -> {
            String body = "--b1\r\nContent-Disposition: form-data; name=\"doc\"; filename=\"a.txt\"\r\n"
                    + "Content-Type: text/plain\r\n\r\nhello\r\n--b1--\r\n";
            HttpMessage m = post("multipart/form-data; boundary=\"b1\"", body);
            Expect.isTrue(BodyEncoding.toggle(m, "x"), "changed");
            Expect.equal(m.bodyText(), "doc=hello", "the field name, not the file name");
        });

        Harness.test("requests with nothing to convert are left alone", () -> {
            HttpMessage get = new HttpMessage(Bytes.bytes("GET / HTTP/1.1\r\nHost: a.test\r\n\r\n"));
            Expect.isFalse(BodyEncoding.toggle(get, "x"), "no body");
            HttpMessage bad = post("multipart/form-data", "--b\r\nnot really");
            Expect.isFalse(BodyEncoding.toggle(bad, "x"), "multipart with no boundary");
            Expect.equal(bad.bodyText(), "--b\r\nnot really", "unchanged");
        });
    }
}