/**
 * Registry allowlist for `OLAM_DEVBOX_IMAGE` overrides.
 *
 * Closes T9 — unconstrained env-var overrides redirecting docker pulls
 * to attacker-controlled images. The default allowlist is `ghcr.io/pleri/*`
 * (the namespace olam itself publishes to). Out-of-allowlist refs require
 * the explicit `--allow-custom-registry` CLI flag.
 *
 * Decision #19 dropped the env-var bypass form (`OLAM_ALLOW_CUSTOM_REGISTRY=1`)
 * to avoid ambient bypass via shell inheritance / CI-secret leakage. Flag-only.
 *
 * Atlas-parity (Decision #15) lives at `ghcr.io/pleri/olam-devbox-atlas:<v>`
 * — same `pleri` namespace, so the allowlist accepts it without the flag.
 * Other extenders publishing under their own org (e.g. `ghcr.io/atlas/atlas-devbox`)
 * must pass `--allow-custom-registry` per invocation.
 */
const DEFAULT_ALLOWLIST_PATTERNS = [
    // ghcr.io/pleri/<anything>:<tag> or ghcr.io/pleri/<anything>@sha256:<digest>
    /^ghcr\.io\/pleri\/[^/\s]+(?::[^\s]+|@sha256:[a-f0-9]+)?$/,
];
export function decideAllowlist(input) {
    const { imageRef, allowCustomRegistry } = input;
    const allowedByDefault = DEFAULT_ALLOWLIST_PATTERNS.some((re) => re.test(imageRef));
    if (allowedByDefault) {
        return {
            imageRef,
            allowedByDefault: true,
            accepted: true,
            stderrLine: '',
        };
    }
    if (allowCustomRegistry) {
        return {
            imageRef,
            allowedByDefault: false,
            accepted: true,
            stderrLine: `Warning: using custom devbox image '${imageRef}'. ` +
                `(--allow-custom-registry was specified.) ` +
                `Verify the source and digest before proceeding.`,
        };
    }
    return {
        imageRef,
        allowedByDefault: false,
        accepted: false,
        stderrLine: `Error: image '${imageRef}' is outside allowed registries (ghcr.io/pleri/*).\n` +
            `  To override: re-run with --allow-custom-registry\n` +
            `  Verify the source and digest before doing so.`,
    };
}
/**
 * Resolve the devbox-image reference from the operator's input, in
 * order of precedence: explicit CLI flag > env var > default. Returns
 * `undefined` when neither flag nor env var is set — caller falls back
 * to the digest-pinned default from `image-digests.json` (Phase B).
 */
export function resolveDevboxImageOverride(flagValue, env = process.env) {
    if (flagValue && flagValue.trim().length > 0) {
        return flagValue.trim();
    }
    const envValue = env.OLAM_DEVBOX_IMAGE;
    if (envValue && envValue.trim().length > 0) {
        return envValue.trim();
    }
    return undefined;
}
//# sourceMappingURL=registry-allowlist.js.map