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.7 KB
Raw Blame
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.*;
import java.util.regex.Pattern;
import java.util.zip.*;

/**
 * Removes the AI-assisted part from an engine add-on: the AI-assisted login of
 * the authentication add-on, and the AI import of the API definitions add-on.
 *
 * Open Suite ships no AI features. Each of these add-ons carries an
 * optional one; switching it off is not enough for networks where such code is
 * not allowed at all, so it is taken out of the file: its classes, the window
 * that offers it, its help pages, its messages, and its entry and change notes in
 * the add-on's manifest. The result is checked, and the run fails if anything is left.
 *
 * Run as: java tools/StripAddonAi.java <add-on file>
 */
public class StripAddonAi {
    static final Pattern LEFT = Pattern.compile("authhelper/llm|openapi/llm|addon/llm|AiAssist|AiAuth|[Ll]lm\\b|LLM|\\bAI\\b");

    public static void main(String[] args) throws Exception {
        Path file = Path.of(args[0]);
        Path out = Path.of(args[0] + ".tmp");
        int dropped = 0;
        try (ZipInputStream in = new ZipInputStream(Files.newInputStream(file));
             ZipOutputStream zo = new ZipOutputStream(Files.newOutputStream(out))) {
            for (ZipEntry e; (e = in.getNextEntry()) != null; ) {
                String n = e.getName();
                byte[] data = in.readAllBytes();
                if (drop(n)) { dropped++; continue; }
                if (n.equals("ZapAddOn.xml")) data = manifest(new String(data, StandardCharsets.UTF_8)).getBytes(StandardCharsets.UTF_8);
                else if (n.contains("/Messages") && n.endsWith(".properties")) data = messages(data);
                ZipEntry copy = new ZipEntry(n);
                copy.setTime(e.getTime());
                zo.putNextEntry(copy);
                zo.write(data);
                zo.closeEntry();
            }
        }
        List<String> left = new ArrayList<>();
        try (ZipInputStream in = new ZipInputStream(Files.newInputStream(out))) {
            for (ZipEntry e; (e = in.getNextEntry()) != null; ) {
                // Only the add-on's own files are read: a library it carries has "AI" as a country code.
                if (e.getName().contains("/") && !e.getName().startsWith("org/zaproxy/")) continue;
                String text = new String(in.readAllBytes(), StandardCharsets.ISO_8859_1);
                if (LEFT.matcher(e.getName()).find() || LEFT.matcher(text).find()) left.add(e.getName());
            }
        }
        if (!left.isEmpty()) {
            Files.deleteIfExists(out);
            System.err.println("An AI-assisted part is still referenced in " + file.getFileName() + ": " + left);
            System.exit(1);
        }
        Files.move(out, file, StandardCopyOption.REPLACE_EXISTING);
        System.out.println("Removed the AI-assisted part from " + file.getFileName() + " (" + dropped + " files)");
    }

    static boolean drop(String n) {
        String base = n.substring(n.lastIndexOf('/') + 1);
        return n.contains("/authhelper/llm/")
                || n.contains("/openapi/llm/")
                || base.startsWith("AiAssisted")
                || base.startsWith("AuthTestDialog")      // the window that offers it; never shown, the engine has no windows
                || n.contains("/resources/help");         // help pages for that window
    }

    /** The manifest without the extension that loads the AI-assisted login. */
    static String manifest(String xml) {
        // The change notes name it too; the engine never shows them.
        int c0 = xml.indexOf("<changes>"), c1 = xml.indexOf("</changes>");
        if (c0 >= 0 && c1 > c0) xml = xml.substring(0, c0 + "<changes>".length()) + xml.substring(c1);
        int at = xml.indexOf("authhelper.llm.ExtensionAuthhelperLlm");
        if (at < 0) at = xml.indexOf("openapi.llm.ExtensionOpenApiLlm");
        if (at < 0) return xml;
        int start = xml.lastIndexOf("<extension", at);
        int end = xml.indexOf("</extension>", at) + "</extension>".length();
        return xml.substring(0, start) + xml.substring(end);
    }

    /** The messages without the lines that belong to the AI-assisted login. */
    static byte[] messages(byte[] data) {
        StringBuilder sb = new StringBuilder();
        for (String line : new String(data, StandardCharsets.ISO_8859_1).split("\n", -1)) {
            String key = line.split("=", 2)[0];
            if (key.contains(".ai.") || key.contains(".llm.") || key.strip().endsWith(".ai") || LEFT.matcher(line).find()) continue;
            sb.append(line).append('\n');
        }
        if (sb.length() > 0) sb.setLength(sb.length() - 1);
        return sb.toString().getBytes(StandardCharsets.ISO_8859_1);
    }
}