blob: 1ebc43ca7ab0eac9496c04094150f3dc63ca6a88 [file] [log] [blame]
#pragma once
#include <crow/http_request.h>
#include <crow/http_response.h>
namespace crow
{
struct SecurityHeadersMiddleware
{
struct Context
{
};
void beforeHandle(crow::Request& req, Response& res, Context& ctx)
{
#ifdef BMCWEB_INSECURE_DISABLE_XSS_PREVENTION
if ("OPTIONS"_method == req.method())
{
res.end();
}
#endif
}
void afterHandle(Request& req, Response& res, Context& ctx)
{
/*
TODO(ed) these should really check content types. for example,
X-UA-Compatible header doesn't make sense when retrieving a JSON or
javascript file. It doesn't hurt anything, it's just ugly.
*/
using bf = boost::beast::http::field;
res.addHeader(bf::strict_transport_security, "max-age=31536000; "
"includeSubdomains; "
"preload");
res.addHeader(bf::x_frame_options, "DENY");
res.addHeader(bf::pragma, "no-cache");
res.addHeader(bf::cache_control, "no-Store,no-Cache");
// The KVM currently needs to load images from base64 encoded strings.
// img-src 'self' data: is used to allow that.
// https://stackoverflow.com/questions/18447970/content-security-policy-data-not-working-for-base64-images-in-chrome-28
res.addHeader("Content-Security-Policy",
"default-src 'self'; img-src 'self' data:");
res.addHeader("X-XSS-Protection", "1; "
"mode=block");
res.addHeader("X-Content-Type-Options", "nosniff");
res.addHeader("X-UA-Compatible", "IE=11");
#ifdef BMCWEB_INSECURE_DISABLE_XSS_PREVENTION
res.addHeader(bf::access_control_allow_origin, "http://localhost:8080");
res.addHeader(bf::access_control_allow_methods, "GET, "
"POST, "
"PUT, "
"PATCH, "
"DELETE");
res.addHeader(bf::access_control_allow_credentials, "true");
res.addHeader(bf::access_control_allow_headers, "Origin, "
"Content-Type, "
"Accept, "
"Cookie, "
"X-XSRF-TOKEN");
#endif
}
};
} // namespace crow