|
| 1 | +package com.stubbornjava.undertow.handlers; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.stream.Collectors; |
| 6 | +import java.util.stream.Stream; |
| 7 | + |
| 8 | +import io.undertow.server.HttpHandler; |
| 9 | +import io.undertow.server.handlers.SetHeaderHandler; |
| 10 | + |
| 11 | +public class ContentSecurityPolicyHandler { |
| 12 | + private static final String CSP_HEADER = "Content-Security-Policy"; |
| 13 | + |
| 14 | + public enum ContentSecurityPolicy { |
| 15 | + NONE("'none'"), // blocks the use of this type of resource. |
| 16 | + SELF("'self'"), // matches the current origin (but not subdomains). |
| 17 | + UNSAFE_INLINE("'unsafe-inline'"), // allows the use of inline JS and CSS. |
| 18 | + UNSAFE_EVAL("'unsafe-eval'"), // allows the use of mechanisms like eval(). |
| 19 | + ; |
| 20 | + |
| 21 | + private final String value; |
| 22 | + ContentSecurityPolicy(String value) { |
| 23 | + this.value = value; |
| 24 | + } |
| 25 | + public String getValue() { |
| 26 | + return value; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + // https://scotthelme.co.uk/content-security-policy-an-introduction/#whatcanweprotect |
| 31 | + public static class Builder { |
| 32 | + private final Map<String, String> policyMap; |
| 33 | + |
| 34 | + public Builder() { |
| 35 | + this.policyMap = new HashMap<>(); |
| 36 | + } |
| 37 | + |
| 38 | + public Builder defaultSrc(ContentSecurityPolicy policy) { |
| 39 | + policyMap.put("default-src", policy.getValue()); |
| 40 | + return this; |
| 41 | + } |
| 42 | + |
| 43 | + public Builder defaultSrc(String... policies) { |
| 44 | + policyMap.put("default-src", join(policies)); |
| 45 | + return this; |
| 46 | + } |
| 47 | + |
| 48 | + public Builder scriptSrc(ContentSecurityPolicy policy) { |
| 49 | + policyMap.put("script-src", policy.getValue()); |
| 50 | + return this; |
| 51 | + } |
| 52 | + |
| 53 | + public Builder scriptSrc(String... policies) { |
| 54 | + policyMap.put("script-src", join(policies)); |
| 55 | + return this; |
| 56 | + } |
| 57 | + |
| 58 | + public Builder objectSrc(ContentSecurityPolicy policy) { |
| 59 | + policyMap.put("object-src", policy.getValue()); |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + public Builder objectSrc(String... policies) { |
| 64 | + policyMap.put("object-src", join(policies)); |
| 65 | + return this; |
| 66 | + } |
| 67 | + |
| 68 | + public Builder styleSrc(ContentSecurityPolicy policy) { |
| 69 | + policyMap.put("style-src", policy.getValue()); |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + public Builder styleSrc(String... policies) { |
| 74 | + policyMap.put("style-src", join(policies)); |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + public Builder imgSrc(ContentSecurityPolicy policy) { |
| 79 | + policyMap.put("img-src", policy.getValue()); |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + public Builder imgSrc(String... policies) { |
| 84 | + policyMap.put("img-src", join(policies)); |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + public Builder mediaSrc(ContentSecurityPolicy policy) { |
| 89 | + policyMap.put("media-src", policy.getValue()); |
| 90 | + return this; |
| 91 | + } |
| 92 | + |
| 93 | + public Builder mediaSrc(String... policies) { |
| 94 | + policyMap.put("media-src", join(policies)); |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + public Builder frameSrc(ContentSecurityPolicy policy) { |
| 99 | + policyMap.put("frame-src", policy.getValue()); |
| 100 | + return this; |
| 101 | + } |
| 102 | + |
| 103 | + public Builder frameSrc(String... policies) { |
| 104 | + policyMap.put("frame-src", join(policies)); |
| 105 | + return this; |
| 106 | + } |
| 107 | + |
| 108 | + public Builder fontSrc(ContentSecurityPolicy policy) { |
| 109 | + policyMap.put("font-src", policy.getValue()); |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + public Builder fontSrc(String... policies) { |
| 114 | + policyMap.put("font-src", join(policies)); |
| 115 | + return this; |
| 116 | + } |
| 117 | + |
| 118 | + public Builder connectSrc(ContentSecurityPolicy policy) { |
| 119 | + policyMap.put("connect-src", policy.getValue()); |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + public Builder connectSrc(String... policies) { |
| 124 | + policyMap.put("connect-src", join(policies)); |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + public Builder formAction(ContentSecurityPolicy policy) { |
| 129 | + policyMap.put("form-action", policy.getValue()); |
| 130 | + return this; |
| 131 | + } |
| 132 | + |
| 133 | + public Builder formAction(String... policies) { |
| 134 | + policyMap.put("form-action", join(policies)); |
| 135 | + return this; |
| 136 | + } |
| 137 | + |
| 138 | + public Builder sandbox(ContentSecurityPolicy policy) { |
| 139 | + policyMap.put("sandbox", policy.getValue()); |
| 140 | + return this; |
| 141 | + } |
| 142 | + |
| 143 | + public Builder sandbox(String... policies) { |
| 144 | + policyMap.put("sandbox", join(policies)); |
| 145 | + return this; |
| 146 | + } |
| 147 | + |
| 148 | + public Builder scriptNonce(ContentSecurityPolicy policy) { |
| 149 | + policyMap.put("script-nonce", policy.getValue()); |
| 150 | + return this; |
| 151 | + } |
| 152 | + |
| 153 | + public Builder scriptNonce(String... policies) { |
| 154 | + policyMap.put("script-nonce", join(policies)); |
| 155 | + return this; |
| 156 | + } |
| 157 | + |
| 158 | + public Builder pluginTypes(ContentSecurityPolicy policy) { |
| 159 | + policyMap.put("plugin-types", policy.getValue()); |
| 160 | + return this; |
| 161 | + } |
| 162 | + |
| 163 | + public Builder pluginTypes(String... policies) { |
| 164 | + policyMap.put("plugin-types", join(policies)); |
| 165 | + return this; |
| 166 | + } |
| 167 | + |
| 168 | + public Builder reflectedXss(ContentSecurityPolicy policy) { |
| 169 | + policyMap.put("reflected-xss", policy.getValue()); |
| 170 | + return this; |
| 171 | + } |
| 172 | + |
| 173 | + public Builder reflectedXss(String... policies) { |
| 174 | + policyMap.put("reflected-xss", join(policies)); |
| 175 | + return this; |
| 176 | + } |
| 177 | + |
| 178 | + public Builder reportUri(String uri) { |
| 179 | + policyMap.put("report-uri", uri); |
| 180 | + return this; |
| 181 | + } |
| 182 | + |
| 183 | + public HttpHandler build(HttpHandler delegate) { |
| 184 | + String policy = policyMap.entrySet() |
| 185 | + .stream() |
| 186 | + .map(entry -> entry.getKey() + " " + entry.getValue()) |
| 187 | + .collect(Collectors.joining("; ")); |
| 188 | + return new SetHeaderHandler(delegate, CSP_HEADER, policy); |
| 189 | + } |
| 190 | + |
| 191 | + private String join(String... strings) { |
| 192 | + return Stream.of(strings).collect(Collectors.joining(" ")); |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments