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.

2.9 KB
Raw Blame
package opensuite.core;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputFilter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Base64;
import java.util.List;

/**
 * A scope of its own that a setting keeps: the custom scope of "Drop all
 * out-of-scope requests", the URLs of streaming responses. It lives in the
 * settings under one key, so it travels with the project like any other.
 */
public final class StoredScope {

    private final Settings settings;
    private final String key;
    private final Scope scope = new Scope();
    private volatile String saved = "";

    public StoredScope(Settings settings, String key) {
        this.settings = settings;
        this.key = key;
        load();
        settings.addListener(k -> { if (k == null || key.equals(k)) load(); });
    }

    public Scope scope() { return scope; }
    public String key() { return key; }

    /** Write the rules to the settings; call after editing them. */
    public void save() {
        try {
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            try (ObjectOutputStream out = new ObjectOutputStream(bytes)) { out.writeObject(scope); }
            String text = scope.include().isEmpty() && scope.exclude().isEmpty() ? "" : Base64.getEncoder().encodeToString(bytes.toByteArray());
            if (text.equals(saved)) return;
            saved = text;
            if (text.isEmpty()) settings.remove(key); else settings.set(key, text);
        } catch (java.io.IOException e) {
            Debug.error("could not save the scope stored under " + key, e);
        }
    }

    private void load() {
        String text = settings.get(key, "");
        if (text.equals(saved)) return;
        saved = text;
        scope.include().clear();
        scope.exclude().clear();
        if (text.isEmpty()) return;
        // Only the scope's own classes are read back, whatever the setting holds.
        try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(Base64.getDecoder().decode(text)))) {
            in.setObjectInputFilter(info -> {
                Class<?> c = info.serialClass();
                if (c == null) return ObjectInputFilter.Status.UNDECIDED;
                while (c.isArray()) c = c.getComponentType();
                return c == Scope.class || c == Scope.Rule.class || c.isPrimitive() || c == String.class || c == Object.class
                        || List.class.isAssignableFrom(c) || c == Boolean.class || c == Integer.class || c == Number.class
                        ? ObjectInputFilter.Status.ALLOWED : ObjectInputFilter.Status.REJECTED;
            });
            Scope read = (Scope) in.readObject();
            scope.include().addAll(read.include());
            scope.exclude().addAll(read.exclude());
        } catch (Exception e) {
            Debug.error("could not read the scope stored under " + key, e);
        }
    }
}