New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CSP headers #63

Merged
merged 2 commits into from Jan 9, 2018
File filter...
Filter file types
Jump to file or symbol
Failed to load files and symbols.
+217 −12
Diff settings

Always

Just for now

@@ -0,0 +1,195 @@
package com.stubbornjava.undertow.handlers;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.undertow.server.HttpHandler;
import io.undertow.server.handlers.SetHeaderHandler;

public class ContentSecurityPolicyHandler {
private static final String CSP_HEADER = "Content-Security-Policy";

public enum ContentSecurityPolicy {
NONE("'none'"), // blocks the use of this type of resource.
SELF("'self'"), // matches the current origin (but not subdomains).
UNSAFE_INLINE("'unsafe-inline'"), // allows the use of inline JS and CSS.
UNSAFE_EVAL("'unsafe-eval'"), // allows the use of mechanisms like eval().
;

private final String value;
ContentSecurityPolicy(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}

// https://scotthelme.co.uk/content-security-policy-an-introduction/#whatcanweprotect
public static class Builder {
private final Map<String, String> policyMap;

public Builder() {
this.policyMap = new HashMap<>();
}

public Builder defaultSrc(ContentSecurityPolicy policy) {
policyMap.put("default-src", policy.getValue());
return this;
}

public Builder defaultSrc(String... policies) {
policyMap.put("default-src", join(policies));
return this;
}

public Builder scriptSrc(ContentSecurityPolicy policy) {
policyMap.put("script-src", policy.getValue());
return this;
}

public Builder scriptSrc(String... policies) {
policyMap.put("script-src", join(policies));
return this;
}

public Builder objectSrc(ContentSecurityPolicy policy) {
policyMap.put("object-src", policy.getValue());
return this;
}

public Builder objectSrc(String... policies) {
policyMap.put("object-src", join(policies));
return this;
}

public Builder styleSrc(ContentSecurityPolicy policy) {
policyMap.put("style-src", policy.getValue());
return this;
}

public Builder styleSrc(String... policies) {
policyMap.put("style-src", join(policies));
return this;
}

public Builder imgSrc(ContentSecurityPolicy policy) {
policyMap.put("img-src", policy.getValue());
return this;
}

public Builder imgSrc(String... policies) {
policyMap.put("img-src", join(policies));
return this;
}

public Builder mediaSrc(ContentSecurityPolicy policy) {
policyMap.put("media-src", policy.getValue());
return this;
}

public Builder mediaSrc(String... policies) {
policyMap.put("media-src", join(policies));
return this;
}

public Builder frameSrc(ContentSecurityPolicy policy) {
policyMap.put("frame-src", policy.getValue());
return this;
}

public Builder frameSrc(String... policies) {
policyMap.put("frame-src", join(policies));
return this;
}

public Builder fontSrc(ContentSecurityPolicy policy) {
policyMap.put("font-src", policy.getValue());
return this;
}

public Builder fontSrc(String... policies) {
policyMap.put("font-src", join(policies));
return this;
}

public Builder connectSrc(ContentSecurityPolicy policy) {
policyMap.put("connect-src", policy.getValue());
return this;
}

public Builder connectSrc(String... policies) {
policyMap.put("connect-src", join(policies));
return this;
}

public Builder formAction(ContentSecurityPolicy policy) {
policyMap.put("form-action", policy.getValue());
return this;
}

public Builder formAction(String... policies) {
policyMap.put("form-action", join(policies));
return this;
}

public Builder sandbox(ContentSecurityPolicy policy) {
policyMap.put("sandbox", policy.getValue());
return this;
}

public Builder sandbox(String... policies) {
policyMap.put("sandbox", join(policies));
return this;
}

public Builder scriptNonce(ContentSecurityPolicy policy) {
policyMap.put("script-nonce", policy.getValue());
return this;
}

public Builder scriptNonce(String... policies) {
policyMap.put("script-nonce", join(policies));
return this;
}

public Builder pluginTypes(ContentSecurityPolicy policy) {
policyMap.put("plugin-types", policy.getValue());
return this;
}

public Builder pluginTypes(String... policies) {
policyMap.put("plugin-types", join(policies));
return this;
}

public Builder reflectedXss(ContentSecurityPolicy policy) {
policyMap.put("reflected-xss", policy.getValue());
return this;
}

public Builder reflectedXss(String... policies) {
policyMap.put("reflected-xss", join(policies));
return this;
}

public Builder reportUri(String uri) {
policyMap.put("report-uri", uri);
return this;
}

public HttpHandler build(HttpHandler delegate) {
String policy = policyMap.entrySet()
.stream()
.map(entry -> entry.getKey() + " " + entry.getValue())
.collect(Collectors.joining("; "));
return new SetHeaderHandler(delegate, CSP_HEADER, policy);
}

private String join(String... strings) {
return Stream.of(strings).collect(Collectors.joining(" "));
}
}
}
@@ -10,6 +10,8 @@
import com.stubbornjava.common.seo.SitemapRoutes;
import com.stubbornjava.common.undertow.SimpleServer;
import com.stubbornjava.common.undertow.handlers.CustomHandlers;
import com.stubbornjava.undertow.handlers.ContentSecurityPolicyHandler;
import com.stubbornjava.undertow.handlers.ContentSecurityPolicyHandler.ContentSecurityPolicy;
import com.stubbornjava.undertow.handlers.MiddlewareBuilder;
import com.stubbornjava.undertow.handlers.ReferrerPolicyHandlers.ReferrerPolicy;
import com.stubbornjava.webapp.guide.GuideRoutes;
@@ -31,9 +33,20 @@ private static HttpHandler exceptionHandler(HttpHandler next) {
.addExceptionHandler(Throwable.class, PageRoutes::error);
}

private static HttpHandler contentSecurityPolicy(HttpHandler delegate) {
return new ContentSecurityPolicyHandler.Builder()
.defaultSrc(ContentSecurityPolicy.SELF)
.scriptSrc("'self'", "https://www.google-analytics.com")
.imgSrc("'self'", "https://www.google-analytics.com")
.connectSrc("'self'", "https://www.google-analytics.com")
.styleSrc(ContentSecurityPolicy.SELF.getValue(), ContentSecurityPolicy.UNSAFE_INLINE.getValue())
.build(delegate);
}

private static HttpHandler wrapWithMiddleware(HttpHandler next) {
return MiddlewareBuilder.begin(PageRoutes::redirector)
.next(handler -> CustomHandlers.securityHeaders(handler, ReferrerPolicy.STRICT_ORIGIN_WHEN_CROSS_ORIGIN))
.next(StubbornJavaWebApp::contentSecurityPolicy)
.next(CustomHandlers::gzip)
.next(BlockingHandler::new)
.next(ex -> CustomHandlers.accessLog(ex, logger))
@@ -10,7 +10,6 @@
{{> content}}
{{> templates/src/widgets/footer/footer}}
{{> templates/src/common/scripts}}
{{> templates/src/common/google-analytics}}
{{#> extra-scripts}}{{/extra-scripts}}
</body>
</html>
@@ -7,6 +7,5 @@
<body class="error-bg">
{{> content}}
{{> templates/src/common/scripts}}
{{> templates/src/common/google-analytics}}
</body>
</html>
@@ -1,5 +1,7 @@
import $ from 'jquery';

import './google-analytics.js';

import './3rdparty.js';

import './common.scss';

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,7 @@
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-89603048-1', 'auto');
ga('send', 'pageview');
ProTip! Use n and p to navigate between commits in a pull request.