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.

3.9 KB
Raw Blame
package opensuite.extensions;

import opensuite.core.ProjectObjectFilter;
import opensuite.core.SuiteContext;
import opensuite.core.ToolModule;

import javax.swing.JMenu;

/**
 * What the suite sees of a loaded extension. The extension's own object is
 * never handed to the shell or to the context: every method here either
 * returns what was read once at load, or goes through the extension's thread
 * within a time limit.
 */
final class GuardedModule implements ToolModule {

    private final ExtensionLoader loader;
    private final SuiteContext ctx;
    private final LoadedExtension ext;
    private final ToolModule target;
    private final Tab[] tabs;
    private final JMenu[] menus;
    private final boolean alwaysTabbed;
    private boolean refusalReported;

    GuardedModule(ExtensionLoader loader, SuiteContext ctx, LoadedExtension ext, ToolModule target,
                  Tab[] tabs, JMenu[] menus, boolean alwaysTabbed) {
        this.loader = loader;
        this.ctx = ctx;
        this.ext = ext;
        this.target = target;
        this.tabs = tabs;
        this.menus = menus;
        this.alwaysTabbed = alwaysTabbed;
    }

    @Override public String name() { return ext.name; }

    /** Nothing to do: the loader called the extension's init before the suite saw this module. */
    @Override public void init(SuiteContext ctx) { }

    /** Never called: the extension's declared sink is registered by the loader, under the extension's name. */
    @Override public SuiteContext.MessageSink sink() { return null; }

    @Override public Tab[] tabs() { return tabs; }
    @Override public JMenu[] menus() { return menus; }
    @Override public boolean alwaysTabbed() { return alwaysTabbed; }

    @Override public void shutdown() {
        ExtensionThread thread = ext.thread;
        if (thread != null) thread.call("shutdown()", () -> { target.shutdown(); return null; }, loader.timeouts().shutdown().toMillis());
    }

    /**
     * The extension's state, as a copy the project file may hold. It is checked
     * when it is saved, against the list the file is read back through: one
     * class off that list keeps a whole project from opening, so a state the
     * file could not hold is left out instead, with a note on the row and one
     * Alert. So is one that is not ready in time or throws.
     */
    @Override public java.io.Serializable saveState() {
        ExtensionThread thread = ext.thread;
        if (thread == null) return null;
        ExtensionThread.Attempt<java.io.Serializable> made =
                thread.call("saveState()", target::saveState, loader.timeouts().save().toMillis());
        if (!made.ok()) return null;
        try {
            return ProjectObjectFilter.copyForSnapshot(made.value());
        } catch (java.io.IOException | ClassNotFoundException | RuntimeException e) {
            String why = e instanceof java.io.InvalidClassException refused
                    ? "it holds " + refused.classname + ", which a project file may not hold" : String.valueOf(e);
            ext.error("its saved state was left out of the project: " + why, e);
            if (!refusalReported) {
                refusalReported = true;
                ctx.alert("extensions", "Extension " + ext.name + ": its saved state was left out of the project file, because " + why + ".");
            }
            return null;
        }
    }

    /** Restore a state the project file held. One that is not done in time has the extension disabled, and its row says why. */
    @Override public void restoreState(java.io.Serializable state) {
        ExtensionThread thread = ext.thread;
        if (thread == null) return;
        ExtensionThread.Attempt<Void> back = thread.call("restoreState()", () -> { target.restoreState(state); return null; }, loader.timeouts().restore().toMillis());
        if (back.timedOut()) loader.disable(ext, "restoring its saved state did not finish in " + ExtensionLoader.duration(loader.timeouts().restore()));
    }
}