blob: e12395a5530b1a9f09eb0f1d982e1ad4b2100ae8 [file] [log] [blame]
Ed Tanous8041f312017-04-03 09:47:01 -07001#pragma once
2
3#include <crow/http_request.h>
4#include <crow/http_response.h>
5
6namespace crow {
Ed Tanous911ac312017-08-15 09:37:42 -07007static const char* strict_transport_security_key = "Strict-Transport-Security";
8static const char* strict_transport_security_value =
Ed Tanousf3d847c2017-06-12 16:01:42 -07009 "max-age=31536000; includeSubdomains; preload";
10
Ed Tanous911ac312017-08-15 09:37:42 -070011static const char* ua_compatability_key = "X-UA-Compatible";
12static const char* ua_compatability_value = "IE=11";
Ed Tanousf3d847c2017-06-12 16:01:42 -070013
Ed Tanous911ac312017-08-15 09:37:42 -070014static const char* xframe_key = "X-Frame-Options";
15static const char* xframe_value = "DENY";
Ed Tanousf3d847c2017-06-12 16:01:42 -070016
Ed Tanous911ac312017-08-15 09:37:42 -070017static const char* xss_key = "X-XSS-Protection";
18static const char* xss_value = "1; mode=block";
Ed Tanousf3d847c2017-06-12 16:01:42 -070019
Ed Tanous911ac312017-08-15 09:37:42 -070020static const char* content_security_key = "X-Content-Security-Policy";
21static const char* content_security_value = "default-src 'self'";
Ed Tanous8041f312017-04-03 09:47:01 -070022
23struct SecurityHeadersMiddleware {
24 struct context {};
25
Ed Tanous911ac312017-08-15 09:37:42 -070026 void before_handle(crow::request& req, response& res, context& ctx) {}
Ed Tanous8041f312017-04-03 09:47:01 -070027
Ed Tanous911ac312017-08-15 09:37:42 -070028 void after_handle(request& req, response& res, context& ctx) {
Ed Tanousf3d847c2017-06-12 16:01:42 -070029 /*
30 TODO(ed) these should really check content types. for example,
31 X-UA-Compatible header doesn't make sense when retrieving a JSON or
32 javascript file. It doesn't hurt anything, it's just ugly.
33 */
34 res.add_header(strict_transport_security_key,
35 strict_transport_security_value);
36 res.add_header(ua_compatability_key, ua_compatability_value);
37 res.add_header(xframe_key, xframe_value);
38 res.add_header(xss_key, xss_value);
39 res.add_header(content_security_key, content_security_value);
Ed Tanous911ac312017-08-15 09:37:42 -070040 res.add_header("Access-Control-Allow-Origin", "http://localhost:8085");
41 res.add_header("Access-Control-Allow-Credentials", "true");
Ed Tanousf3d847c2017-06-12 16:01:42 -070042 }
Ed Tanous8041f312017-04-03 09:47:01 -070043};
Ed Tanous911ac312017-08-15 09:37:42 -070044} // namespace crow