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

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

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/** The first-use browser download: pinned, verified, and unpacked safely. */
public final class BrowserInstallerTest {

    private static Path zip(Path dir, String name, Object... entries) throws Exception {
        Path z = dir.resolve(name);
        try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(z))) {
            for (int i = 0; i < entries.length; i += 2) {
                out.putNextEntry(new ZipEntry((String) entries[i]));
                out.write((byte[]) entries[i + 1]);
                out.closeEntry();
            }
        }
        return z;
    }

    private interface Body { void run() throws Exception; }
    private interface Throwing<T> { T get() throws Exception; }

    private static String failure(Body b) {
        try { b.run(); return null; } catch (Exception e) { return String.valueOf(e.getMessage()); }
    }

    private static <T> T unchecked(Throwing<T> t) {
        try { return t.get(); } catch (Exception e) { throw new IllegalStateException(e); }
    }

    public void run() {
        Harness.suite("browser installer");

        Harness.test("a browser fetched by an earlier version is moved out of plain sight, once", () -> { try {
            java.io.File home = java.nio.file.Files.createTempDirectory("oshome").toFile();
            java.io.File old = new java.io.File(home, "opensuite/browser/chrome-linux");
            old.mkdirs();
            java.nio.file.Files.writeString(new java.io.File(old, "chrome").toPath(), "x");
            EmbeddedBrowser.tidyOldInstall(home);
            Expect.isTrue(new java.io.File(home, ".opensuite/bundled/browser/chrome-linux/chrome").isFile(), "moved into the application's own folder");
            Expect.isTrue(!new java.io.File(home, "opensuite").exists(), "the empty old folder is gone");
            EmbeddedBrowser.tidyOldInstall(home);
            Expect.isTrue(new java.io.File(home, ".opensuite/bundled/browser/chrome-linux/chrome").isFile(), "a second run changes nothing");
        } catch (java.io.IOException e) { throw new RuntimeException(e); } });

        Harness.test("an old folder holding anything else is kept", () -> { try {
            java.io.File home = java.nio.file.Files.createTempDirectory("oshome").toFile();
            new java.io.File(home, "opensuite/browser/chrome-linux").mkdirs();
            java.nio.file.Files.writeString(new java.io.File(home, "opensuite/notes.txt").toPath(), "mine");
            new java.io.File(home, "opensuite/other/browser").mkdirs();
            EmbeddedBrowser.tidyOldInstall(home);
            Expect.isTrue(new java.io.File(home, "opensuite/notes.txt").isFile(), "the user's own file is untouched");
            Expect.isTrue(new java.io.File(home, ".opensuite/bundled/browser/chrome-linux").isDirectory(), "the browser is still moved");
        } catch (java.io.IOException e) { throw new RuntimeException(e); } });

        Harness.test("a browser that gives up as it starts is explained", () -> {
            Expect.isTrue(EmbeddedBrowser.startFailure(0, "anything") == null, "a clean exit is no failure");
            String sandbox = EmbeddedBrowser.startFailure(134,
                    "[1:1:FATAL:zygote_host_impl_linux.cc:144] No usable sandbox! If you are running on Ubuntu 23.10+\n#0 0x1 base::debug");
            Expect.isTrue(sandbox != null && sandbox.contains("without a sandbox") && sandbox.contains("Settings > Tools > Browser"),
                    "a refused sandbox points at the setting");
            String other = EmbeddedBrowser.startFailure(1, "\n#0 trace\nerror while loading shared libraries: libnss3.so\n");
            Expect.isTrue(other != null && other.contains("exit code 1") && other.contains("libnss3.so"), "any other end quotes its first line");
            Expect.isTrue(EmbeddedBrowser.startFailure(2, null) != null, "silence is still reported");
        });

        Harness.test("the pinned build is the one tools/versions names", () -> {
            String v = unchecked(() -> Files.readString(Path.of("tools/versions")));
            Expect.isTrue(v.contains("CHROMIUM_REVISION=" + BrowserInstaller.REVISION), "revision");
            Expect.isTrue(v.contains("CHROMIUM_CHROME_SHA256=" + BrowserInstaller.CHROME_SHA256), "browser checksum");
            Expect.isTrue(v.contains("CHROMIUM_DRIVER_SHA256=" + BrowserInstaller.DRIVER_SHA256), "driver checksum");
            Expect.isTrue(v.contains("CHROMIUM_REVISION_MAC_ARM=" + BrowserInstaller.MAC_REVISION), "mac revision");
            Expect.isTrue(v.contains("CHROMIUM_CHROME_SHA256_MAC_ARM=" + BrowserInstaller.MAC_CHROME_SHA256), "mac browser checksum");
            Expect.isTrue(v.contains("CHROMIUM_DRIVER_SHA256_MAC_ARM=" + BrowserInstaller.MAC_DRIVER_SHA256), "mac driver checksum");
            Expect.isTrue(v.contains("CHROMIUM_REVISION_WINDOWS=" + BrowserInstaller.WIN_REVISION), "windows revision");
            Expect.isTrue(v.contains("CHROMIUM_CHROME_SHA256_WINDOWS=" + BrowserInstaller.WIN_CHROME_SHA256), "windows browser checksum");
            Expect.isTrue(v.contains("CHROMIUM_DRIVER_SHA256_WINDOWS=" + BrowserInstaller.WIN_DRIVER_SHA256), "windows driver checksum");
        });

        Harness.test("a download that does not match its checksum is refused", () -> {
            Path dir = unchecked(() -> Files.createTempDirectory("otbi"));
            Path f = unchecked(() -> Files.writeString(dir.resolve("f"), "abc"));
            Expect.isNull(failure(() -> BrowserInstaller.verify(f, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")), "the right sum passes");
            Expect.isTrue(failure(() -> BrowserInstaller.verify(f, "00" + "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015")) != null, "a wrong sum does not");
        });

        Harness.test("programs come out runnable and nothing lands outside the folder", () -> {
            Path dir = unchecked(() -> Files.createTempDirectory("otbi"));
            byte[] elf = { 0x7F, 'E', 'L', 'F', 1, 2, 3 };
            Path good = unchecked(() -> zip(dir, "good.zip", "app/chrome", elf, "app/run.sh", "#!/bin/sh\n".getBytes(), "app/data.pak", "data".getBytes()));
            Path out = dir.resolve("out");
            Expect.isNull(failure(() -> BrowserInstaller.unzip(good, out, () -> false)), "unpacks");
            // On a Mac the archive's own file modes are kept rather than guessed from the content.
            if (!opensuite.core.Os.MAC) {
                Expect.isTrue(Files.isExecutable(out.resolve("app/chrome")), "the program runs");
                Expect.isTrue(Files.isExecutable(out.resolve("app/run.sh")), "the script runs");
            }
            // Windows has no runnable mark: every file there counts as runnable.
            if (!opensuite.core.Os.WINDOWS) Expect.isFalse(Files.isExecutable(out.resolve("app/data.pak")), "data is left as data");
            Path bad = unchecked(() -> zip(dir, "bad.zip", "../escaped", "x".getBytes()));
            Expect.isTrue(failure(() -> BrowserInstaller.unzip(bad, dir.resolve("out2"), () -> false)) != null, "a path outside is refused");
            Expect.isFalse(Files.exists(dir.resolve("escaped")), "and nothing was written there");
            Expect.isTrue(failure(() -> BrowserInstaller.unzip(good, dir.resolve("out3"), () -> true)) != null, "cancelling stops it");
        });

        if (opensuite.core.Os.MAC) Harness.test("a macOS app bundle keeps its links and modes", () -> {
            Path dir = unchecked(() -> Files.createTempDirectory("otbi"));
            Path app = dir.resolve("src/A.app/Contents");
            unchecked(() -> Files.createDirectories(app.resolve("Versions/1")));
            unchecked(() -> Files.writeString(app.resolve("Versions/1/run"), "#!/bin/sh\n"));
            app.resolve("Versions/1/run").toFile().setExecutable(true);
            unchecked(() -> Files.createSymbolicLink(app.resolve("Versions/Current"), Path.of("1")));
            Path z = dir.resolve("a.zip");
            unchecked(() -> new ProcessBuilder("/usr/bin/zip", "-qry", z.toString(), "A.app").directory(dir.resolve("src").toFile()).start().waitFor());
            Path out = dir.resolve("out");
            Expect.isNull(failure(() -> BrowserInstaller.unzip(z, out, () -> false)), "unpacks");
            Expect.isTrue(Files.isSymbolicLink(out.resolve("A.app/Contents/Versions/Current")), "the link is a link");
            Expect.isTrue(Files.isExecutable(out.resolve("A.app/Contents/Versions/Current/run")), "the program runs through it");
        });
    }
}