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.

47.6 KB
Raw Blame
package opensuite.proxy;

import opensuite.core.*;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;
import java.net.*;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.*;

/**
 * The interception proxy.
 *
 * One accept loop per listener, one thread per connection. That is what the
 * original did and it stays comprehensible; the thread pool is bounded so a
 * flood of connections cannot exhaust memory.
 */
public class ProxyServer {

    public interface Observer {
        void exchangeLogged(LogEntry entry);
        void statusChanged(String text);
    }

    private final SuiteContext ctx;
    private final CertificateAuthority ca;
    private final InterceptQueue intercept = new InterceptQueue();
    private final ExecutorService pool = new ThreadPoolExecutor(
            4, 400, 60, TimeUnit.SECONDS, new SynchronousQueue<>(),
            r -> { Thread t = new Thread(r, "proxy-conn"); t.setDaemon(true); return t; },
            new ThreadPoolExecutor.CallerRunsPolicy());

    private final List<ProxyListener> listeners = new ArrayList<>();
    private final Map<Integer, ServerSocket> sockets = new ConcurrentHashMap<>();

    // Settings surfaced in the options tab.
    public volatile boolean interceptRequests = true;
    public volatile boolean interceptResponses = false;
    public volatile boolean interceptingEnabled = false;
    /** Tidy a request's trailing new lines when it is forwarded after editing. */
    public volatile boolean fixRequestNewlines = false;
    public volatile boolean updateContentLengthRequests = true;
    public volatile boolean updateContentLengthResponses = true;
    public volatile boolean unpackGzip = true;
    public volatile boolean http10 = false;
    public volatile boolean logOutOfScope = true;
    public volatile boolean stripProxyHeaders = true;
    // Settings > Tools > Proxy > Miscellaneous, each as the reference names it.
    public volatile boolean http10Responses = false;
    public volatile boolean closeResponses = false;
    public volatile boolean closeRequests = false;
    public volatile boolean removeUnsupportedEncodings = true;
    public volatile boolean stripWsExtensions = true;
    public volatile boolean unpackRequests = false;
    public volatile boolean disableWebInterface = false;
    public volatile boolean suppressErrors = false;
    public volatile boolean dontLog = false;
    // Response modification rules beyond the first six.
    public volatile boolean highlightUnhidden = false;
    public volatile boolean httpsLinksToHttp = false;
    public volatile boolean removeSecureFlag = false;

    /** The encodings the suite can unpack; anything else is taken out of Accept-Encoding so bodies stay readable. */
    static String supportedEncodings(String acceptEncoding) {
        if (acceptEncoding == null) return null;
        StringBuilder sb = new StringBuilder();
        for (String part : acceptEncoding.split(",")) {
            String name = part.trim().split(";", 2)[0].trim().toLowerCase(java.util.Locale.ROOT);
            if (name.equals("gzip") || name.equals("deflate") || name.equals("identity") || name.equals("x-gzip")) {
                if (sb.length() > 0) sb.append(", ");
                sb.append(part.trim());
            }
        }
        return sb.toString();
    }

    public final List<ProxyRule> requestRules = new ArrayList<>();
    public final List<ProxyRule> responseRules = new ArrayList<>();
    public final List<MatchReplaceRule> matchReplace = new ArrayList<>(MatchReplaceRule.defaults());
    public final List<MatchReplaceRule> wsMatchReplace = new java.util.concurrent.CopyOnWriteArrayList<>();
    /** Apply the HTTP / WebSocket rules only to traffic that is in the target scope. */
    public volatile boolean matchReplaceInScopeOnly, wsMatchReplaceInScopeOnly;

    /** Whether the rules of one kind apply to a message for this service and path. */
    private boolean rulesApply(boolean inScopeOnly, HttpService svc, HttpMessage request) {
        if (!inScopeOnly) return true;
        try { return ctx.scope().isInScope(svc, request.path()); } catch (RuntimeException e) { return true; }
    }

    // Response modification toggles.
    public volatile boolean unhideHiddenFields = false;
    public volatile boolean enableDisabledFields = false;
    public volatile boolean removeLengthLimits = false;
    public volatile boolean removeJsValidation = false;
    public volatile boolean removeAllJavaScript = false;
    public volatile boolean removeObjectTags = false;
    // Strip the response headers that stop an injected payload running in the
    // browser (CSP, X-Frame-Options, and friends), so a proof of concept
    // actually fires. The scanner still audits the server's original response,
    // so this never produces a false "header missing" finding.
    public volatile boolean stripSecurityHeaders = false;

    private Observer observer;

    // Responses staged for "show in browser". A captured response is parked
    // under a random token; opening the sentinel http://opensuite/show/<token>
    // through the proxy redirects the browser to the genuine URL and arms a
    // one-shot so the very next hit on that URL is answered with the stored
    // bytes instead of the live server. The address bar then shows the real
    // URL while the page is exactly what was captured, and sub-resources load
    // live, which is how the response is meant to be viewed.
    private static final class Staged {
        final String url;        // the genuine absolute URL, or null to serve inline
        final byte[] bytes;      // the full captured response (status line, headers, body)
        Staged(String url, byte[] bytes) { this.url = url; this.bytes = bytes; }
    }
    // Bounded so a long session of "open in browser" clicks cannot pin an
    // unbounded amount of captured response bytes in memory; the oldest staged
    // response falls off once the cap is reached.
    private static final int MAX_STAGED = 16;
    private final Map<String, Staged> showByToken = Collections.synchronizedMap(
            new LinkedHashMap<>(32, 0.75f, false) {
                @Override protected boolean removeEldestEntry(Map.Entry<String, Staged> e) {
                    return size() > MAX_STAGED;
                }
            });
    private final Map<String, byte[]> pendingShowByUrl = new ConcurrentHashMap<>();

    /**
     * Park a captured response for viewing in a proxied browser and return the
     * sentinel URL to open. When {@code realUrl} is non-null the sentinel
     * redirects to it and the stored response is served for that one hit, so
     * the browser shows the genuine address; when null the bytes are served at
     * the sentinel itself.
     */
    public String stageShow(String realUrl, byte[] responseBytes) {
        String token = UUID.randomUUID().toString().replace("-", "");
        showByToken.put(token, new Staged(realUrl, responseBytes));
        return "http://opensuite/show/" + token;
    }

    /** The suite's settings, for what is launched on the proxy's behalf. */
    public opensuite.core.Settings settings() { return ctx.settings(); }

    public ProxyServer(SuiteContext ctx, Path caDir) throws Exception {
        this.ctx = ctx;
        this.ca = new CertificateAuthority(caDir);
        if (ca.setAside() != null) {
            ctx.alert("proxy", "The CA store could not be read, so a new CA certificate was generated. Install the new"
                    + " certificate in any browser or tool that trusted the old one. The unreadable store was kept as "
                    + ca.setAside());
        }
        requestRules.add(new ProxyRule(true, "or", "file extension", "does not match",
                "(^gif$|^jpg$|^png$|^css$|^js$|^ico$|^svg$|^eot$|^woff$|^woff2$|^ttf$)"));
        requestRules.add(new ProxyRule(false, "or", "request", "contains parameters", ""));
        requestRules.add(new ProxyRule(false, "or", "HTTP method", "does not match", "(get|post)"));
        requestRules.add(new ProxyRule(false, "and", "URL", "is in target scope", ""));
        responseRules.add(new ProxyRule(true, "or", "content type", "matches", "text"));
        responseRules.add(new ProxyRule(false, "or", "request", "was modified", ""));
        responseRules.add(new ProxyRule(false, "or", "request", "was intercepted", ""));
        responseRules.add(new ProxyRule(false, "and", "response code", "does not match", "^304$"));
        responseRules.add(new ProxyRule(false, "and", "URL", "is in target scope", ""));
        listeners.add(new ProxyListener(defaultPort()));
    }

    /**
     * The port of the listener created at startup: {@link ProxyListener#DEFAULT_PORT}
     * unless the {@code opensuite.port} system property names a valid port.
     */
    public static int defaultPort() {
        try {
            int p = Integer.parseInt(System.getProperty("opensuite.port", "").trim());
            if (p >= 1 && p <= 65535) return p;
        } catch (Exception ignored) {}
        return ProxyListener.DEFAULT_PORT;
    }

    public CertificateAuthority ca()       { return ca; }
    public InterceptQueue interceptQueue() { return intercept; }
    public List<ProxyListener> listeners() { return listeners; }
    public void setObserver(Observer o)    { this.observer = o; }

    private void status(String s) { if (observer != null) observer.statusChanged(s); }

    // ---------------- lifecycle ----------------

    /**
     * Starts every listener that is switched on, as the suite opens. A
     * listener whose port something else already holds moves to the next
     * free port by itself and says so, so the proxy is working from the
     * start instead of waiting for the user to pick another port.
     */
    public void startAll() {
        for (ProxyListener l : listeners) {
            if (!l.running) continue;
            if (!sockets.containsKey(l.port) && !free(l, l.port)) {
                int wanted = l.port;
                int next = nextFree(l);
                if (next > 0) {
                    l.port = next;
                    ctx.alert("proxy", "port " + wanted + " is in use by another service, so the proxy listener moved to port " + next);
                }
            }
            start(l);
        }
    }

    /** The first port above the listener's own that nothing holds, or -1 when the next fifty are all taken. */
    private int nextFree(ProxyListener l) {
        for (int p = l.port + 1; p <= Math.min(65535, l.port + 50); p++) {
            boolean ours = false;
            for (ProxyListener o : listeners) if (o != l && o.port == p) ours = true;
            if (!ours && !sockets.containsKey(p) && free(l, p)) return p;
        }
        return -1;
    }

    /** Whether the listener's address can be taken on this port right now. */
    private static boolean free(ProxyListener l, int port) {
        try (ServerSocket probe = l.bind == ProxyListener.Bind.ALL
                ? new ServerSocket(port, 1)
                : new ServerSocket(port, 1, InetAddress.getByName(l.bindHost()))) {
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    /** Starts the listener; false when its port or address could not be taken. */
    public synchronized boolean start(ProxyListener cfg) {
        if (sockets.containsKey(cfg.port)) return true;
        boolean ok = true;
        try {
            // ALL binds every interface, which is what a null bind address
            // means to ServerSocket; the other two name one address.
            ServerSocket ss = cfg.bind == ProxyListener.Bind.ALL
                    ? new ServerSocket(cfg.port, 100)
                    : new ServerSocket(cfg.port, 100, InetAddress.getByName(cfg.bindHost()));
            ss.setReuseAddress(true);
            sockets.put(cfg.port, ss);
            cfg.running = true;
            Thread t = new Thread(() -> acceptLoop(cfg, ss), "proxy-accept-" + cfg.port);
            t.setDaemon(true);
            t.start();
            ctx.alert("proxy", "proxy service started on port " + cfg.port);
        } catch (IOException e) {
            cfg.running = false;
            ok = false;
            ctx.alert("proxy", "failed to start proxy service on port " + cfg.port
                    + ". Check whether another service is already using this port.");
        }
        listenersChanged();
        return ok;
    }

    public synchronized void stop(ProxyListener cfg) {
        ServerSocket ss = sockets.remove(cfg.port);
        cfg.running = false;
        if (ss != null) {
            try { ss.close(); } catch (IOException ignored) {}
            ctx.alert("proxy", "proxy service stopped on port " + cfg.port);
        }
        listenersChanged();
    }

    public void stopAll() {
        for (ProxyListener l : new ArrayList<>(listeners)) stop(l);
        intercept.releaseAll();
    }

    public boolean anyRunning() { return !sockets.isEmpty(); }

    private final List<Runnable> listenerHooks = new java.util.concurrent.CopyOnWriteArrayList<>();

    /** Run {@code r} on the event thread whenever a listener starts or stops. */
    public void onListenersChanged(Runnable r) { listenerHooks.add(r); }

    private void listenersChanged() {
        for (Runnable r : listenerHooks) javax.swing.SwingUtilities.invokeLater(r);
    }

    /** What the proxy is listening on right now, e.g. "listening on 127.0.0.1:8080". */
    public String listeningSummary() { return summary(listeners); }

    /**
     * Restrict the handshake to the protocols a listener names.
     *
     * <p>Names the runtime does not know are dropped rather than refused: a
     * configuration mentioning a protocol this Java has no support for should
     * narrow the handshake, not break every connection through the listener.
     * Naming nothing usable leaves the runtime's own defaults alone.
     */
    static void applyProtocols(SSLSocket socket, ProxyListener cfg) {
        if (cfg == null || cfg.tlsProtocols == null || cfg.tlsProtocols.isBlank()) return;
        java.util.Set<String> supported =
                new java.util.HashSet<>(java.util.Arrays.asList(socket.getSupportedProtocols()));
        java.util.List<String> wanted = new ArrayList<>();
        for (String p : cfg.tlsProtocols.split("[,\\s]+")) {
            String name = p.trim();
            if (!name.isEmpty() && supported.contains(name) && !wanted.contains(name)) wanted.add(name);
        }
        if (!wanted.isEmpty()) socket.setEnabledProtocols(wanted.toArray(new String[0]));
    }

    /**
     * Give an edited request exactly one blank line between its headers and its
     * body, and no stray new lines after it. Hand-editing a request in the
     * intercept editor is where these creep in, and a server is entitled to
     * reject the result.
     */
    static byte[] tidyTrailingNewlines(byte[] raw) {
        String s = new String(raw, java.nio.charset.StandardCharsets.ISO_8859_1);
        int sep = s.indexOf("\r\n\r\n");
        if (sep < 0) return raw;
        String head = s.substring(0, sep);
        String body = s.substring(sep + 4);
        // Trailing new lines belong to the body only when it has content.
        while (body.endsWith("\r\n")) body = body.substring(0, body.length() - 2);
        while (body.endsWith("\n") || body.endsWith("\r")) body = body.substring(0, body.length() - 1);
        return (head + "\r\n\r\n" + body).getBytes(java.nio.charset.StandardCharsets.ISO_8859_1);
    }

    /**
     * Describe the running listeners in {@code list}: every address and port, or
     * a clear statement that none is running. Shown on the intercept and options
     * tabs so the port is never in doubt.
     */
    public static String summary(List<ProxyListener> list) {
        StringBuilder sb = new StringBuilder();
        for (ProxyListener l : list) {
            if (!l.running) continue;
            if (sb.length() > 0) sb.append(", ");
            sb.append(l.interfaceLabel());
        }
        return sb.length() == 0
                ? "no proxy listeners are running - add one on the options tab"
                : "listening on " + sb;
    }

    private void acceptLoop(ProxyListener cfg, ServerSocket ss) {
        while (!ss.isClosed()) {
            try {
                Socket client = ss.accept();
                pool.execute(() -> {
                    try { new Connection(cfg, client).run(); }
                    // Per-connection isolation: one broken exchange must not
                    // affect the listener. Anything reaching here is a bug or
                    // resource exhaustion, so it is logged, never swallowed.
                    catch (Throwable t) {
                        Debug.error("proxy connection handling failed for "
                                + client.getRemoteSocketAddress(), t);
                    }
                });
            } catch (IOException e) {
                if (ss.isClosed()) return;
            } catch (OutOfMemoryError e) {
                System.gc();
                ctx.alert("proxy", "failed to allocate memory - consider saving your work");
            }
        }
    }

    // ---------------- per-connection handling ----------------

    private class Connection {
        private final ProxyListener cfg;
        private final Socket client;
        private HttpService tunnelService;

        Connection(ProxyListener cfg, Socket client) {
            this.cfg = cfg; this.client = client;
        }

        void run() {
            try {
                client.setSoTimeout(120000);
                // No read-ahead buffer here: for CONNECT we hand the raw socket to
                // TLS, and a buffer would swallow the ClientHello bytes.
                InputStream in = client.getInputStream();
                OutputStream out = client.getOutputStream();

                byte[] head = readHead(in);
                if (head.length == 0) return;
                HttpMessage first = new HttpMessage(head);

                if ("CONNECT".equalsIgnoreCase(first.method())) {
                    handleConnect(first, in, out);
                    return;
                }
                handlePlain(first, in, out, false, null);
            } catch (Exception e) {
                // A protocol-level failure ends this exchange; the client sees a
                // dropped connection. Routine client-side garbage (truncated
                // requests, bogus headers) lands here too, so trace it at debug
                // level rather than reporting every malformed request.
                Debug.log("proxy exchange failed: " + e);
            } finally {
                try { client.close(); } catch (IOException ignored) {}
            }
        }

        /** Establish a TLS tunnel, then read decrypted requests from inside it. */
        private void handleConnect(HttpMessage req, InputStream in, OutputStream out) throws Exception {
            String target = req.requestTarget();
            String host = target;
            int port = 443;
            int c = target.lastIndexOf(':');
            if (c > 0) {
                host = target.substring(0, c);
                try { port = Integer.parseInt(target.substring(c + 1)); } catch (Exception ignored) {}
            }
            tunnelService = new HttpService(host, port, true);
            if (TlsPassThrough.matches(TlsPassThrough.entries(ctx.settings()), host, port)) {
                passThrough(host, port, in, out);
                return;
            }
            out.write(Bytes.bytes("HTTP/1.1 200 Connection established\r\n\r\n"));
            out.flush();

            SSLContext sslCtx;
            try {
                sslCtx = ca.contextFor(host, cfg);
            } catch (Exception e) {
                // A listener pointed at a certificate that cannot be loaded
                // would otherwise fail every connection silently.
                ctx.alert("proxy", "listener on port " + cfg.port
                        + " could not load its certificate: " + e.getMessage());
                return;
            }
            SSLSocketFactory f = sslCtx.getSocketFactory();
            SSLSocket tls;
            try {
                tls = (SSLSocket) f.createSocket(client, host, port, true);
                tls.setUseClientMode(false);
                applyProtocols(tls, cfg);
                tls.setSoTimeout(120000);
                tls.startHandshake();
            } catch (Exception e) {
                // The client rejected our certificate, or pins its own. With
                // auto-add on, its next connection to this host is relayed
                // untouched instead of failing again.
                if (TlsPassThrough.autoAdd(ctx.settings())
                        && TlsPassThrough.add(ctx.settings(), host)) {
                    ctx.alert("proxy", "TLS negotiation with the client failed for " + host
                            + "; added it to the TLS pass-through list");
                }
                return;
            }

            InputStream tin = new BufferedInputStream(tls.getInputStream(), 32768);
            OutputStream tout = new BufferedOutputStream(tls.getOutputStream(), 32768);
            tls.setSoTimeout(120000);
            while (true) {
                byte[] head = readHead(tin);
                if (head.length == 0) break;
                HttpMessage m = new HttpMessage(head);
                if (!handlePlain(m, tin, tout, true, tunnelService)) break;
            }
            try { tls.close(); } catch (IOException ignored) {}
        }

        /**
         * Relay a CONNECT tunnel byte for byte without intercepting its TLS.
         * The upstream leg honours the upstream proxy settings.
         */
        private void passThrough(String host, int port, InputStream in, OutputStream out) throws Exception {
            Socket upstream;
            try {
                upstream = HttpClient.openSocket(new HttpService(host, port, false), 15000, 0);
            } catch (Exception e) {
                out.write(Bytes.bytes("HTTP/1.1 502 Bad Gateway\r\nContent-Length: 0\r\n\r\n"));
                out.flush();
                return;
            }
            try (Socket up = upstream) {
                out.write(Bytes.bytes("HTTP/1.1 200 Connection established\r\n\r\n"));
                out.flush();
                client.setSoTimeout(0);
                InputStream uin = up.getInputStream();
                OutputStream uout = up.getOutputStream();
                Thread back = new Thread(() -> {
                    pump(uin, out);
                    try { client.close(); } catch (IOException ignored) {}
                }, "proxy-passthrough");
                back.setDaemon(true);
                back.start();
                pump(in, uout);
                try { up.close(); } catch (IOException ignored) {}
                back.join(2000);
            }
        }

        private void pump(InputStream from, OutputStream to) {
            byte[] buf = new byte[16384];
            try {
                int n;
                while ((n = from.read(buf)) >= 0) { to.write(buf, 0, n); to.flush(); }
            } catch (IOException ignored) {
                // Either side closing ends the relay.
            }
        }

        /** Returns true when the connection may be reused for another request. */
        private boolean handlePlain(HttpMessage headMsg, InputStream in, OutputStream out,
                                    boolean secure, HttpService known) throws Exception {
            HttpMessage request = new HttpMessage(Bytes.concat(headMsg.raw(), readBody(in, headMsg)));

            HttpService svc = known;
            if (svc == null) {
                String t = request.requestTarget();
                if (t.startsWith("http://") || t.startsWith("https://")) {
                    svc = HttpService.parse(t);
                    // An absolute-form target becomes origin-form upstream.
                    try {
                        URI u = URI.create(t);
                        String p = (u.getRawPath() == null || u.getRawPath().isEmpty()) ? "/" : u.getRawPath();
                        if (u.getRawQuery() != null) p += "?" + u.getRawQuery();
                        request.setRequestTarget(p);
                    } catch (Exception ignored) {}
                } else {
                    String hostHeader = request.header("Host");
                    if (hostHeader == null) { writeError(out, "no Host header in request"); return false; }
                    svc = parseHostHeader(hostHeader, secure);
                }
            }
            if (svc == null) { writeError(out, "could not determine target host"); return false; }

            if (cfg.redirectHost != null && !cfg.redirectHost.isBlank()) {
                svc = new HttpService(cfg.redirectHost,
                        cfg.redirectPort > 0 ? cfg.redirectPort : svc.port(), svc.secure());
            }

            // Serve our own CA so the user can install it without a side
            // channel, and the "show in browser" sentinel.
            if (isLocalRequest(request, svc)) {
                if (disableWebInterface) {
                    out.write(Bytes.bytes("HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"));
                    out.flush();
                    return false;
                }
                String p = request.path();
                if (p.startsWith("/show/")) serveShow(out, p);
                else serveCertificate(out, p);
                return false;
            }

            // A response staged for viewing: answer this one hit from the store
            // (consuming the one-shot) so the browser renders exactly what was
            // captured while showing the genuine URL.
            byte[] staged = pendingShowByUrl.remove(svc.url() + request.requestTarget());
            if (staged != null) {
                out.write(staged);
                out.flush();
                return false;
            }

            if (stripProxyHeaders) stripProxyHeaders(request);
            if (closeRequests) request.setHeader("Connection", "close");
            if (removeUnsupportedEncodings && request.header("Accept-Encoding") != null) {
                String kept = supportedEncodings(request.header("Accept-Encoding"));
                if (kept.isEmpty()) request.removeHeader("Accept-Encoding"); else request.setHeader("Accept-Encoding", kept);
            }
            if (stripWsExtensions && isWebSocketUpgrade(request)) request.removeHeader("Sec-WebSocket-Extensions");
            if (unpackRequests) unpackRequestBody(request);
            if (http10) {
                String sl = request.startLine().replace("HTTP/1.1", "HTTP/1.0");
                byte[] rest = Bytes.slice(request.raw(),
                        Bytes.indexOf(request.raw(), Bytes.bytes("\n")) + 1, request.raw().length);
                request.setRaw(Bytes.concat(Bytes.bytes(sl + "\r\n"), rest));
            }

            HttpMessage original = request.copy();
            final boolean rulesOn = rulesApply(matchReplaceInScopeOnly, svc, request);
            if (rulesOn) MatchReplaceRule.applyAll(matchReplace, request, true);

            boolean wasIntercepted = false;
            boolean wasModified = !Arrays.equals(original.raw(), request.raw());

            if (interceptingEnabled && interceptRequests
                    && ProxyRule.evaluateChain(requestRules, svc, request, null, "", false, wasModified, ctx.scope())) {
                wasIntercepted = true;
                InterceptQueue.Pending p = new InterceptQueue.Pending(svc, request, true, null);
                InterceptQueue.Action a = intercept.submit(p);
                if (a == InterceptQueue.Action.DROP) {
                    intercept.completed(p);
                    return false;
                }
                if (p.editedBytes() != null) {
                    request.setRaw(p.editedBytes());
                    wasModified = true;
                    if (fixRequestNewlines) request.setRaw(tidyTrailingNewlines(request.raw()));
                    if (updateContentLengthRequests) request.updateContentLength();
                }
                intercept.completed(p);
            }

            // A WebSocket upgrade leaves HTTP behind: hand the connection to the
            // relay, which streams and records frames both ways until it closes.
            if (isWebSocketUpgrade(request)) {
                handleWebSocket(request, in, out, svc, client.getLocalPort());
                return false;
            }

            LogEntry entry = new LogEntry("proxy", svc, request);
            entry.setListenerPort(client.getLocalPort());
            // The pre-edit request is kept only when something changed it, as
            // the response side does below: an unmodified original would just
            // duplicate the bytes the entry already holds, for every entry.
            if (wasModified) entry.setOriginalRequest(original);
            entry.setEdited(wasModified);

            HttpClient.Options opt = new HttpClient.Options();
            opt.updateContentLength = false;
            opt.unpackGzip = unpackGzip;
            opt.followRedirects = false;
            opt.session = ctx.sessions();
            opt.sessionTool = "proxy";
            // Live passive scanning: route in-scope traffic through the scan engine.
            if (ctx.liveScanRouter() != null) opt.upstream = ctx.liveScanRouter().apply(svc);
            long t0 = System.currentTimeMillis();
            HttpClient.Result res = new HttpClient().send(svc, request, opt);
            entry.setDurationMillis(System.currentTimeMillis() - t0);
            entry.setRemoteIp(res.remoteIp);

            if (!res.ok()) {
                entry.setError(res.error);
                logEntry(entry, svc, request);
                writeError(out, res.error);
                return false;
            }

            HttpMessage response = res.response;
            // Keep the server's response as it arrived, so the scanner audits
            // the real headers even when response modification or security-header
            // stripping changes what the browser is handed.
            HttpMessage serverResponse = response.copy();
            if (rulesOn) MatchReplaceRule.applyAll(matchReplace, response, false);
            applyResponseModification(response);
            stripSecurityResponseHeaders(response);
            if (!Arrays.equals(serverResponse.raw(), response.raw())) {
                entry.setOriginalResponse(serverResponse);
            }

            if (interceptingEnabled && interceptResponses
                    && ProxyRule.evaluateChain(responseRules, svc, request, response,
                                               res.remoteIp, wasIntercepted, wasModified, ctx.scope())) {
                InterceptQueue.Pending p = new InterceptQueue.Pending(svc, response, false, request);
                InterceptQueue.Action a = intercept.submit(p);
                if (a == InterceptQueue.Action.DROP) {
                    intercept.completed(p);
                    return false;
                }
                if (p.editedBytes() != null) {
                    response.setRaw(p.editedBytes());
                    if (updateContentLengthResponses) response.updateContentLength();
                }
                intercept.completed(p);
            }

            entry.setResponse(response);
            logEntry(entry, svc, request);

            // What the browser is handed may differ in protocol and connection handling from what is recorded.
            if (http10Responses || closeResponses) {
                response = response.copy();
                if (closeResponses) response.setHeader("Connection", "close");
                if (http10Responses && response.startLine().startsWith("HTTP/1.1")) {
                    byte[] rawResponse = response.raw();
                    int eol = Bytes.indexOf(rawResponse, Bytes.bytes("\n"));
                    if (eol > 0) response.setRaw(Bytes.concat(Bytes.bytes(response.startLine().replaceFirst("HTTP/1\\.1", "HTTP/1.0") + "\r\n"),
                            Bytes.slice(rawResponse, eol + 1, rawResponse.length)));
                }
            }
            out.write(response.raw());
            out.flush();

            String conn = response.header("Connection");
            return conn == null || !conn.toLowerCase(Locale.ROOT).contains("close");
        }

        private boolean isLocalRequest(HttpMessage req, HttpService svc) {
            String h = svc.host();
            return h.equals("opensuite") || h.equals("opensuite.proxy")
                || ((h.equals("localhost") || h.equals("127.0.0.1")) && svc.port() == cfg.port);
        }

        private void serveShow(OutputStream out, String path) throws Exception {
            String token = path.substring("/show/".length());
            int slash = token.indexOf('/');
            if (slash >= 0) token = token.substring(0, slash);
            Staged s = showByToken.get(token);
            if (s == null) {
                byte[] b = Bytes.bytes("<html><body><h2>Open Suite</h2>"
                        + "<p>This response is no longer staged for viewing.</p></body></html>");
                out.write(Bytes.bytes("HTTP/1.1 404 Not Found\r\nContent-Type: text/html; charset=utf-8\r\n"
                        + "Content-Length: " + b.length + "\r\nConnection: close\r\n\r\n"));
                out.write(b);
                out.flush();
                return;
            }
            if (s.url == null) {
                // No genuine URL to show; serve the captured bytes directly.
                out.write(s.bytes);
                out.flush();
                return;
            }
            // Arm the one-shot and send the browser to the genuine URL.
            pendingShowByUrl.put(s.url, s.bytes);
            String head = "HTTP/1.1 302 Found\r\nLocation: " + s.url
                    + "\r\nContent-Length: 0\r\nConnection: close\r\n\r\n";
            out.write(Bytes.bytes(head));
            out.flush();
        }

        private void serveCertificate(OutputStream out, String path) throws Exception {
            if (path.startsWith("/cert/der") || path.startsWith("/cert/download")) {
                byte[] der = ca.caCertificateDer();
                String head = "HTTP/1.1 200 OK\r\n"
                        + "Content-Type: application/x-x509-ca-cert\r\n"
                        + "Content-Disposition: attachment; filename=\"opensuite-ca.der\"\r\n"
                        + "Content-Length: " + der.length + "\r\n"
                        + "Connection: close\r\n\r\n";
                out.write(Bytes.bytes(head));
                out.write(der);
            } else {
                String body = StartPage.html(cfg.port, ca.caCertificatePem());
                byte[] b = Bytes.bytes(body);
                String head = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n"
                        + "Content-Length: " + b.length + "\r\nConnection: close\r\n\r\n";
                out.write(Bytes.bytes(head));
                out.write(b);
            }
            out.flush();
        }

        private void writeError(OutputStream out, String message) {
            // "Suppress error messages in browser": the connection is closed with nothing said.
            if (suppressErrors) return;
            try {
                String body = "<html><body><h2>Open Suite proxy error</h2><p>"
                        + message.replace("<", "&lt;") + "</p></body></html>";
                byte[] b = Bytes.bytes(body);
                out.write(Bytes.bytes("HTTP/1.1 502 Bad Gateway\r\nContent-Type: text/html\r\n"
                        + "Content-Length: " + b.length + "\r\nConnection: close\r\n\r\n"));
                out.write(b);
                out.flush();
            } catch (IOException ignored) {}
        }
    }

    void applyResponseModification(HttpMessage response) {
        if (removeSecureFlag) {
            // Cookies the server marks Secure are sent over plain HTTP too once the flag is gone.
            java.util.List<String> cookies = response.headers("Set-Cookie");
            boolean changed = false;
            java.util.List<String> rewritten = new java.util.ArrayList<>();
            for (String c : cookies) {
                String r = c.replaceAll("(?i);\\s*secure(?=\\s*(;|$))", "");
                changed |= !r.equals(c);
                rewritten.add(r);
            }
            if (changed) {
                response.removeHeader("Set-Cookie");
                StringBuilder head = new StringBuilder(response.headerText().replaceAll("(\r?\n)+$", ""));
                for (String c : rewritten) head.append("\r\nSet-Cookie: ").append(c);
                response.setRaw(Bytes.concat(Bytes.bytes(head + "\r\n\r\n"), response.body()));
            }
        }
        if (!unhideHiddenFields && !enableDisabledFields && !removeLengthLimits
                && !removeJsValidation && !removeAllJavaScript && !removeObjectTags && !httpsLinksToHttp) return;
        if (!"HTML".equals(response.mimeType())) return;
        String b = response.bodyText();
        if (httpsLinksToHttp)
            b = b.replaceAll("(?i)((?:href|src|action)\\s*=\\s*[\"']?)https://", "$1http://");
        if (unhideHiddenFields && highlightUnhidden)
            b = b.replaceAll("(?i)type\\s*=\\s*([\"']?)hidden\\1", "type=$1text$1 style=$1border:2px solid #e8590c;background:#fff4e6$1 title=$1hidden field$1");
        else if (unhideHiddenFields)
            b = b.replaceAll("(?i)type\\s*=\\s*([\"']?)hidden\\1", "type=$1text$1");
        if (enableDisabledFields)
            b = b.replaceAll("(?i)\\s+disabled(\\s*=\\s*[\"']?[^\"'>\\s]*[\"']?)?", "");
        if (removeLengthLimits)
            b = b.replaceAll("(?i)\\s+maxlength\\s*=\\s*[\"']?\\d+[\"']?", "");
        if (removeJsValidation)
            b = b.replaceAll("(?i)\\s+onsubmit\\s*=\\s*\"[^\"]*\"", "");
        if (removeObjectTags)
            b = b.replaceAll("(?is)<object.*?</object>", "").replaceAll("(?i)<embed[^>]*>", "");
        if (removeAllJavaScript) {
            b = b.replaceAll("(?is)<script.*?</script>", "");
            b = b.replaceAll("(?i)\\s+on[a-z]+\\s*=\\s*\"[^\"]*\"", "");
        }
        response.setBody(Bytes.bytes(b));
        response.setHeader("Content-Length", String.valueOf(response.body().length));
    }

    /**
     * Response headers whose only purpose is to make the browser refuse to run
     * or display attacker-influenced content. Stripping them lets a proof of
     * concept fire in a current browser, which is how you demonstrate a
     * reflected cross-site scripting or a clickjacking flaw to the team that
     * has to fix it on the server.
     *
     * <p>The browser's own cross-site-scripting auditor was removed from
     * Chromium in 2019, so for a reflected payload it is the target's
     * Content-Security-Policy that stands in the way, not the browser; removing
     * the header here is what reproduces how a legacy browser, or any browser
     * before the policy was added, would have run the payload.
     *
     * <p>This changes only what the browser receives. The scanner audits the
     * server's original response, so a header removed here is still reported as
     * present, and a header the server never sent is still reported as missing.
     */
    private static final String[] SECURITY_RESPONSE_HEADERS = {
        "Content-Security-Policy",
        "Content-Security-Policy-Report-Only",
        "X-Frame-Options",
        "X-XSS-Protection",
        "Cross-Origin-Opener-Policy",
        "Cross-Origin-Embedder-Policy",
        "Cross-Origin-Resource-Policy",
        "X-WebKit-CSP",
    };

    private void stripSecurityResponseHeaders(HttpMessage response) {
        if (!stripSecurityHeaders) return;
        for (String h : SECURITY_RESPONSE_HEADERS) response.removeHeader(h);
        // Deliberately no change to the CORS headers. A genuine cross-origin
        // misconfiguration is exploitable in a normal browser against the
        // server's real Access-Control-Allow-Origin and -Allow-Credentials
        // headers, so forging those here would only manufacture a finding the
        // target does not actually have.
    }

    private static HttpService parseHostHeader(String hostHeader, boolean secure) {
        String h = hostHeader.trim();
        int port = secure ? 443 : 80;
        int c = h.lastIndexOf(':');
        if (c > 0 && c > h.lastIndexOf(']')) {
            try { port = Integer.parseInt(h.substring(c + 1).trim()); h = h.substring(0, c); }
            catch (Exception ignored) {}
        }
        return new HttpService(h, port, secure);
    }

    /** Read bytes up to and including the header terminator. */
    /**
     * Record an exchange, HTTP or WebSocket handshake alike. With no scope
     * defined nothing is out of scope, so everything is recorded whatever the
     * out-of-scope setting says.
     */
    private void logEntry(LogEntry entry, HttpService svc, HttpMessage request) {
        if (dontLog) return;
        boolean inScope = ctx.scope().isEmpty() || ctx.scope().isInScope(svc, request.path());
        if (!inScope && !logOutOfScope) return;
        ctx.store().add(entry);
        ctx.siteMapChanged(entry);
        if (observer != null) observer.exchangeLogged(entry);
    }

    /** A request body sent gzip or deflate encoded, unpacked so it can be read and edited. */
    static void unpackRequestBody(HttpMessage request) {
        String enc = request.header("Content-Encoding");
        if (enc == null || request.body().length == 0) return;
        String e = enc.trim().toLowerCase(java.util.Locale.ROOT);
        try {
            java.io.InputStream in;
            if (e.equals("gzip") || e.equals("x-gzip")) in = new java.util.zip.GZIPInputStream(new java.io.ByteArrayInputStream(request.body()));
            else if (e.equals("deflate")) in = new java.util.zip.InflaterInputStream(new java.io.ByteArrayInputStream(request.body()));
            else return;
            // Bounded, so a small packed body cannot be made to fill the memory.
            byte[] plain = in.readNBytes(64 * 1024 * 1024);
            request.setBody(plain);
            request.removeHeader("Content-Encoding");
            request.setHeader("Content-Length", String.valueOf(plain.length));
        } catch (java.io.IOException ignored) {
            // Not what it claimed to be: left as it arrived.
        }
    }

    /**
     * Take out the headers meant for the proxy itself. A client that asked the
     * proxy to keep its connection open, and said nothing of the connection to
     * the server, is taken to want the same there: the request goes on with
     * "Connection: keep-alive", as the reference sends it.
     */
    static void stripProxyHeaders(HttpMessage request) {
        String kept = request.header("Proxy-Connection");
        request.removeHeader("Proxy-Connection");
        request.removeHeader("Proxy-Authorization");
        if (kept != null && kept.toLowerCase(java.util.Locale.ROOT).contains("keep-alive")
                && request.header("Connection") == null) {
            request.setHeader("Connection", "keep-alive");
        }
    }

    private static boolean isWebSocketUpgrade(HttpMessage request) {
        String up = request.header("Upgrade");
        String con = request.header("Connection");
        return up != null && up.toLowerCase(java.util.Locale.ROOT).contains("websocket")
                && con != null && con.toLowerCase(java.util.Locale.ROOT).contains("upgrade");
    }

    /**
     * Relay an upgraded WebSocket: forward the handshake to the upstream server,
     * return its 101 to the browser, then stream frames both ways through the
     * {@link WebSocketConnection} relay until either side closes.
     */
    private void handleWebSocket(HttpMessage request, InputStream clientIn, OutputStream clientOut, HttpService svc, int listenerPort) {
        java.net.Socket up = null;
        try {
            up = HttpClient.openSocket(svc, 15000, 0); // no read timeout: WebSockets idle between frames
            OutputStream upOut = up.getOutputStream();
            InputStream upIn = new BufferedInputStream(up.getInputStream(), 32768);
            upOut.write(request.raw());
            upOut.flush();

            byte[] respHead = readHead(upIn);
            if (respHead.length == 0) return;
            HttpMessage resp = new HttpMessage(respHead);
            clientOut.write(resp.raw());
            clientOut.flush();

            LogEntry entry = new LogEntry("proxy", svc, request);
            entry.setListenerPort(listenerPort);
            entry.setResponse(resp);
            logEntry(entry, svc, request);   // same scope rule as every other exchange

            if (resp.statusCode() != 101) return; // server declined the upgrade
            ctx.alert("proxy", "WebSocket connected to " + svc.host());
            new WebSocketConnection(ctx, svc, clientOut, upOut,
                    rulesApply(wsMatchReplaceInScopeOnly, svc, request) ? wsMatchReplace : List.of()).run(clientIn, upIn);
        } catch (Exception e) {
            ctx.alert("proxy", "WebSocket relay error: " + e.getMessage());
        } finally {
            if (up != null) try { up.close(); } catch (Exception ignored) {}
        }
    }

    private static byte[] readHead(InputStream in) throws IOException {
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        int consecutive = 0;
        while (true) {
            int b;
            try { b = in.read(); } catch (SocketTimeoutException e) { return new byte[0]; }
            if (b < 0) break;
            buf.write(b);
            if (b == 10) {
                consecutive++;
                if (consecutive == 2) break;
            } else if (b != 13) {
                consecutive = 0;
            }
            if (buf.size() > 1024 * 1024) break;
        }
        return buf.toByteArray();
    }

    /** Read a request body framed by Content-Length or chunked encoding.
     *  A size claimed in the framing is never trusted for allocation: the
     *  body is streamed through a small buffer, so a bogus Content-Length or
     *  chunk header from a client costs nothing beyond the bytes that
     *  actually arrive. */
    private static byte[] readBody(InputStream in, HttpMessage head) throws IOException {
        String te = head.header("Transfer-Encoding");
        if (te != null && te.toLowerCase(Locale.ROOT).contains("chunked")) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buf = new byte[8192];
            while (true) {
                ByteArrayOutputStream line = new ByteArrayOutputStream();
                int c;
                while ((c = in.read()) >= 0 && c != 10) line.write(c);
                String sz = Bytes.str(line.toByteArray()).trim();
                int semi = sz.indexOf(';');
                if (semi >= 0) sz = sz.substring(0, semi);
                int n;
                try { n = Integer.parseInt(sz.trim(), 16); } catch (Exception e) { break; }
                if (n < 0) break;
                out.write(Bytes.bytes(Integer.toHexString(n) + "\r\n"));
                if (n == 0) { out.write(Bytes.bytes("\r\n")); break; }
                int remaining = n;
                while (remaining > 0) {
                    int k = in.read(buf, 0, Math.min(buf.length, remaining));
                    if (k < 0) break;
                    out.write(buf, 0, k);
                    remaining -= k;
                }
                out.write(Bytes.bytes("\r\n"));
                in.read(); in.read();
            }
            return out.toByteArray();
        }
        String cl = head.header("Content-Length");
        if (cl == null) return new byte[0];
        int n;
        try { n = Integer.parseInt(cl.trim()); } catch (Exception e) { return new byte[0]; }
        if (n <= 0) return new byte[0];
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buf = new byte[8192];
        int remaining = n;
        while (remaining > 0) {
            int k = in.read(buf, 0, Math.min(buf.length, remaining));
            if (k < 0) break;
            out.write(buf, 0, k);
            remaining -= k;
        }
        return out.toByteArray();
    }
}