"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_CACHE_CONTROL = exports.DEFAULT_PERMISSIONS_POLICY = void 0;
exports.buildDefaultSecurityHeaderOptions = buildDefaultSecurityHeaderOptions;
exports.buildPermissionsPolicyHeader = buildPermissionsPolicyHeader;
exports.setDefaultCacheControl = setDefaultCacheControl;
const disallow_in_production_decorator_1 = require("../decorators/disallow-in-production.decorator");
function buildDefaultSecurityHeaderOptions() {
    const isProd = process.env.ENV === disallow_in_production_decorator_1.Environment.Production;
    return {
        contentSecurityPolicy: {
            useDefaults: true,
            directives: {
                "frame-ancestors": ["'none'"],
                "style-src": ["'self'"],
            },
        },
        frameguard: { action: "deny" },
        referrerPolicy: { policy: "strict-origin-when-cross-origin" },
        crossOriginEmbedderPolicy: false,
        crossOriginResourcePolicy: { policy: "same-site" },
        hsts: isProd
            ? { maxAge: 31536000, includeSubDomains: true, preload: true }
            : false,
    };
}
exports.DEFAULT_PERMISSIONS_POLICY = {
    camera: "none",
    microphone: "none",
    geolocation: "none",
    fullscreen: "self",
    payment: "none",
    accelerometer: "none",
    autoplay: "none",
    "clipboard-read": "none",
    "clipboard-write": "none",
    gyroscope: "none",
    magnetometer: "none",
    usb: "none",
};
function buildPermissionsPolicyHeader(overrides = {}) {
    const merged = { ...exports.DEFAULT_PERMISSIONS_POLICY, ...overrides };
    return Object.entries(merged)
        .map(([feature, value]) => `${feature}=${serializeValue(value)}`)
        .join(", ");
}
function serializeValue(v) {
    if (v === "none")
        return "()";
    if (v === "self")
        return "(self)";
    const parts = v.map((src) => (src === "self" ? "self" : src)).join(" ");
    return `(${parts})`;
}
exports.DEFAULT_CACHE_CONTROL = "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0, s-maxage=0";
function setDefaultCacheControl() {
    return function cacheControlMiddleware(_req, _res, next) {
        _res.setHeader("Cache-Control", exports.DEFAULT_CACHE_CONTROL);
        next();
    };
}
//# sourceMappingURL=security.helper.js.map