1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.html.csp;
16
17 import static java.nio.charset.StandardCharsets.UTF_8;
18 import static org.htmlunit.BrowserVersionFeatures.CSP_META_SINGLE_POLICY;
19 import static org.htmlunit.BrowserVersionFeatures.CSP_NONCE_HIDING_ENFORCED_ONLY;
20 import static org.htmlunit.BrowserVersionFeatures.CSP_REPORT_FIREFOX_FORMAT;
21 import static org.htmlunit.BrowserVersionFeatures.CSP_UPGRADE_INSECURE_REQUESTS_MATCHES_HTTPS;
22 import static org.htmlunit.html.DomElement.ATTRIBUTE_NOT_DEFINED;
23
24 import java.io.IOException;
25 import java.io.Serializable;
26 import java.net.MalformedURLException;
27 import java.net.URL;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Locale;
31 import java.util.Optional;
32 import java.util.function.Predicate;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.htmlunit.HttpHeader;
37 import org.htmlunit.HttpMethod;
38 import org.htmlunit.TopLevelWindow;
39 import org.htmlunit.WebClient;
40 import org.htmlunit.WebRequest;
41 import org.htmlunit.WebResponse;
42 import org.htmlunit.WebWindow;
43 import org.htmlunit.corejs.javascript.Context;
44 import org.htmlunit.corejs.javascript.EvaluatorException;
45 import org.htmlunit.corejs.javascript.ScriptStackElement;
46 import org.htmlunit.csp.FetchDirectiveKind;
47 import org.htmlunit.csp.Policy;
48 import org.htmlunit.csp.directive.ReportUriDirective;
49 import org.htmlunit.csp.directive.SourceExpressionDirective;
50 import org.htmlunit.csp.url.GUID;
51 import org.htmlunit.csp.url.URI;
52 import org.htmlunit.csp.url.URLWithScheme;
53 import org.htmlunit.html.DomAttr;
54 import org.htmlunit.html.DomElement;
55 import org.htmlunit.html.DomNode;
56 import org.htmlunit.html.FrameWindow;
57 import org.htmlunit.html.HtmlPage;
58 import org.htmlunit.javascript.AbstractJavaScriptEngine;
59 import org.htmlunit.javascript.JavaScriptEngine;
60 import org.htmlunit.javascript.host.event.EventTarget;
61 import org.htmlunit.javascript.host.event.SecurityPolicyViolationEvent;
62 import org.htmlunit.util.NameValuePair;
63 import org.htmlunit.util.StringUtils;
64 import org.htmlunit.util.UrlUtils;
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public final class ContentSecurityPolicy implements Serializable {
81
82 private static final Log LOG = LogFactory.getLog(ContentSecurityPolicy.class);
83
84
85 public static final String HEADER_REPORT_ONLY = "Content-Security-Policy-Report-Only";
86
87
88 private static final int SAMPLE_LENGTH = 40;
89
90 private static final String BLOCKED_INLINE = "inline";
91 private static final String BLOCKED_EVAL = "eval";
92 private static final String FORM_ACTION = "form-action";
93 private static final String BASE_URI = "base-uri";
94 private static final String REPORT_CONTENT_TYPE = "application/csp-report";
95
96
97 private static final ThreadLocal<List<Runnable>> PENDING_VIOLATIONS = new ThreadLocal<>();
98
99 private final List<String> enforced_ = new ArrayList<>();
100 private final List<String> reportOnly_ = new ArrayList<>();
101 private final List<String> meta_ = new ArrayList<>();
102 private boolean metaSinglePolicy_;
103
104 private URL selfUrl_;
105
106 private transient List<Policy> enforcedPolicies_;
107 private transient List<Policy> reportOnlyPolicies_;
108
109
110
111
112 private ContentSecurityPolicy() {
113 super();
114 }
115
116
117
118
119
120
121
122 public static ContentSecurityPolicy fromResponse(final WebResponse webResponse) {
123 if (webResponse == null) {
124 return null;
125 }
126
127 ContentSecurityPolicy result = null;
128 for (final NameValuePair header : webResponse.getResponseHeaders()) {
129 final String value = header.getValue();
130 if (StringUtils.isBlank(value)) {
131 continue;
132 }
133
134 if (HttpHeader.CONTENT_SECURIRY_POLICY.equalsIgnoreCase(header.getName())) {
135 if (result == null) {
136 result = new ContentSecurityPolicy();
137 }
138 result.enforced_.add(value);
139 }
140 else if (HEADER_REPORT_ONLY.equalsIgnoreCase(header.getName())) {
141 if (result == null) {
142 result = new ContentSecurityPolicy();
143 }
144 result.reportOnly_.add(value);
145 }
146 }
147 return result;
148 }
149
150
151
152
153
154
155
156
157
158
159
160
161
162 public static ContentSecurityPolicy inherit(final URL url, final WebWindow window) {
163 if (url == null || window == null) {
164 return null;
165 }
166 final String protocol = url.getProtocol();
167 if (!"about".equals(protocol) && !"data".equals(protocol) && !"blob".equals(protocol)) {
168 return null;
169 }
170
171 WebWindow creator = null;
172 if (window instanceof FrameWindow) {
173 creator = window.getParentWindow();
174 }
175 else if (window instanceof TopLevelWindow topLevelWindow) {
176 creator = topLevelWindow.getOpener();
177 }
178 if (creator == null || creator == window
179 || !(creator.getEnclosedPage() instanceof HtmlPage creatorPage)) {
180 return null;
181 }
182
183 final ContentSecurityPolicy creatorCsp = creatorPage.getContentSecurityPolicy();
184 if (creatorCsp == null) {
185 return null;
186 }
187
188 final ContentSecurityPolicy result = new ContentSecurityPolicy();
189 result.enforced_.addAll(creatorCsp.enforced_);
190 result.reportOnly_.addAll(creatorCsp.reportOnly_);
191 result.meta_.addAll(creatorCsp.meta_);
192 result.metaSinglePolicy_ = creatorCsp.metaSinglePolicy_;
193 result.selfUrl_ = selfUrl(creatorPage);
194 return result;
195 }
196
197
198
199
200
201
202
203
204
205
206 public static void addMetaPolicy(final HtmlPage page, final String serializedPolicy) {
207 if (StringUtils.isBlank(serializedPolicy)) {
208 return;
209 }
210
211 ContentSecurityPolicy csp = page.getContentSecurityPolicy();
212 if (csp == null) {
213 csp = new ContentSecurityPolicy();
214 page.setContentSecurityPolicy(csp);
215 }
216 csp.meta_.add(serializedPolicy);
217 csp.metaSinglePolicy_ = page.getWebClient().getBrowserVersion().hasFeature(CSP_META_SINGLE_POLICY);
218 csp.enforcedPolicies_ = null;
219 }
220
221
222
223
224
225
226
227
228
229 public static boolean allowsInlineScript(final HtmlPage page, final DomElement element, final String source) {
230 final ContentSecurityPolicy csp = page.getContentSecurityPolicy();
231 if (csp == null) {
232 return true;
233 }
234
235 final Optional<String> nonce = nonce(element);
236 final Optional<Boolean> parserInserted = Optional.of(isParserInserted(element));
237 final Violation violation = new Violation(FetchDirectiveKind.ScriptSrcElem, BLOCKED_INLINE, source,
238 element, documentUrl(page), element.getStartLineNumber(), element.getStartColumnNumber());
239 return csp.check(page, violation,
240 policy -> policy.allowsInlineScript(nonce, Optional.of(source), parserInserted));
241 }
242
243
244
245
246
247
248
249
250
251 public static boolean allowsExternalScript(final HtmlPage page, final DomElement element, final URL url) {
252 return allowsExternalScript(page, element, url, url);
253 }
254
255
256
257
258
259
260
261
262
263
264 public static boolean allowsExternalScript(final HtmlPage page, final DomElement element, final URL url,
265 final URL reportedUrl) {
266 final ContentSecurityPolicy csp = page.getContentSecurityPolicy();
267 if (csp == null) {
268 return true;
269 }
270
271 final Optional<String> nonce = nonce(element);
272 final String integrityAttrib = element.getAttributeDirect("integrity");
273 final Optional<String> integrity = ATTRIBUTE_NOT_DEFINED == integrityAttrib
274 ? Optional.empty() : Optional.of(integrityAttrib);
275 final Optional<URI> scriptUrl = URI.parseURI(matchingUrl(page, url).toExternalForm());
276 final Optional<Boolean> parserInserted = Optional.of(isParserInserted(element));
277 final Optional<URI> origin = origin(page);
278 final Violation violation = new Violation(FetchDirectiveKind.ScriptSrcElem, withoutRef(reportedUrl), null,
279 null, "", 0, 0);
280 return csp.check(page, violation,
281 policy -> policy.allowsExternalScript(nonce, integrity, scriptUrl, parserInserted, origin));
282 }
283
284
285
286
287
288
289
290
291
292 public static boolean allowsScriptAsAttribute(final HtmlPage page, final DomElement element,
293 final String source) {
294 final ContentSecurityPolicy csp = page.getContentSecurityPolicy();
295 if (csp == null) {
296 return true;
297 }
298
299 final Violation violation = new Violation(FetchDirectiveKind.ScriptSrcAttr, BLOCKED_INLINE, source,
300 element, "", 0, 0);
301 final boolean allowed = csp.check(page, violation,
302 policy -> policy.allowsScriptAsAttribute(Optional.of(source)));
303 firePendingViolationsIfNoScriptRunning(page);
304 return allowed;
305 }
306
307
308
309
310
311
312
313
314
315 public static boolean allowsJavaScriptUrl(final HtmlPage page, final DomElement element,
316 final String source) {
317 final ContentSecurityPolicy csp = page.getContentSecurityPolicy();
318 if (csp == null) {
319 return true;
320 }
321
322 final Optional<URI> origin = origin(page);
323 final Violation violation = new Violation(FetchDirectiveKind.ScriptSrcElem, BLOCKED_INLINE, source,
324 element, "", 0, 0);
325 final boolean allowed = csp.check(page, violation,
326 policy -> policy.allowsJavascriptUrlNavigation(Optional.of(source), origin));
327 firePendingViolationsIfNoScriptRunning(page);
328 return allowed;
329 }
330
331
332
333
334
335
336
337
338
339
340 public static boolean allowsConnection(final HtmlPage page, final URL url, final URL reportedUrl) {
341 final ContentSecurityPolicy csp = page.getContentSecurityPolicy();
342 if (csp == null) {
343 return true;
344 }
345
346 final Optional<URLWithScheme> target = Optional.of(toUrlWithScheme(matchingUrl(page, url)));
347 final Optional<URI> origin = origin(page);
348 final Violation violation = new Violation(FetchDirectiveKind.ConnectSrc, blockedUri(reportedUrl), null,
349 null, "", 0, 0);
350
351 final boolean allowed = csp.check(page, violation, policy -> policy.allowsConnection(target, origin, false));
352 firePendingViolationsIfNoScriptRunning(page);
353 return allowed;
354 }
355
356
357
358
359
360
361
362
363
364 public static boolean allowsInlineStyle(final HtmlPage page, final DomElement element, final String source) {
365 final ContentSecurityPolicy csp = page.getContentSecurityPolicy();
366 if (csp == null) {
367 return true;
368 }
369
370 final Optional<String> nonce = nonce(element);
371 final Violation violation = inlineViolation(page, FetchDirectiveKind.StyleSrcElem, source, element);
372 final boolean allowed = csp.check(page, violation,
373 policy -> policy.allowsInlineStyle(nonce, Optional.of(source)));
374 firePendingViolationsIfNoScriptRunning(page);
375 return allowed;
376 }
377
378
379
380
381
382
383
384
385
386
387
388 public static boolean allowsStyleAttribute(final HtmlPage page, final DomElement element,
389 final String source) {
390 final ContentSecurityPolicy csp = page.getContentSecurityPolicy();
391 if (csp == null) {
392 return true;
393 }
394
395 final Violation violation = inlineViolation(page, FetchDirectiveKind.StyleSrcAttr, source, element);
396 final boolean allowed = csp.check(page, violation,
397 policy -> policy.allowsStyleAsAttribute(Optional.of(source)));
398 firePendingViolationsIfNoScriptRunning(page);
399 return allowed;
400 }
401
402
403
404
405
406
407
408
409
410 public static boolean allowsExternalStyle(final HtmlPage page, final DomElement element, final URL url) {
411 return allowsExternalStyle(page, element, url, url);
412 }
413
414
415
416
417
418
419
420
421
422
423 public static boolean allowsExternalStyle(final HtmlPage page, final DomElement element, final URL url,
424 final URL reportedUrl) {
425 final ContentSecurityPolicy csp = page.getContentSecurityPolicy();
426 if (csp == null) {
427 return true;
428 }
429
430 final Optional<String> nonce = element == null ? Optional.empty() : nonce(element);
431 final Optional<URI> styleUrl = URI.parseURI(matchingUrl(page, url).toExternalForm());
432 final Optional<URI> origin = origin(page);
433 final Violation violation = new Violation(FetchDirectiveKind.StyleSrcElem, withoutRef(reportedUrl), null,
434 null, "", 0, 0);
435 final boolean allowed = csp.check(page, violation,
436 policy -> policy.allowsExternalStyle(nonce, styleUrl, origin));
437 firePendingViolationsIfNoScriptRunning(page);
438 return allowed;
439 }
440
441
442
443
444
445
446
447
448
449
450
451 public static boolean allowsFrame(final HtmlPage page, final URL url) {
452 final ContentSecurityPolicy csp = page.getContentSecurityPolicy();
453 if (csp == null) {
454 return true;
455 }
456
457 final Optional<URLWithScheme> target = Optional.of(toUrlWithScheme(matchingUrl(page, url)));
458 final Optional<URI> origin = origin(page);
459 final Violation violation = new Violation(FetchDirectiveKind.FrameSrc, navigationBlockedUri(page, url),
460 null, null, "", 0, 0);
461 final boolean allowed = csp.check(page, violation, policy -> policy.allowsFrame(target, origin));
462 firePendingViolationsIfNoScriptRunning(page);
463 return allowed;
464 }
465
466
467
468
469
470
471
472
473
474
475 public static boolean allowsObject(final DomElement element, final String src) {
476 final HtmlPage page = element.getHtmlPageOrNull();
477 if (page == null || page.getContentSecurityPolicy() == null
478 || ATTRIBUTE_NOT_DEFINED == src || StringUtils.isBlank(src)) {
479 return true;
480 }
481
482 final URL url;
483 try {
484 url = page.getFullyQualifiedUrl(src);
485 }
486 catch (final MalformedURLException e) {
487 return true;
488 }
489
490 final Optional<URLWithScheme> target = Optional.of(toUrlWithScheme(matchingUrl(page, url)));
491 final Optional<URI> origin = origin(page);
492 final Violation violation = new Violation(FetchDirectiveKind.ObjectSrc, objectBlockedUri(page, url), null,
493 null, "", 0, 0);
494 final boolean allowed = page.getContentSecurityPolicy()
495 .check(page, violation, policy -> policy.allowsObject(target, origin));
496 firePendingViolationsIfNoScriptRunning(page);
497 return allowed;
498 }
499
500
501
502
503
504
505
506
507
508
509 public static boolean allowsMedia(final DomElement element, final String src) {
510 final HtmlPage page = element.getHtmlPageOrNull();
511 if (page == null || page.getContentSecurityPolicy() == null
512 || ATTRIBUTE_NOT_DEFINED == src || StringUtils.isBlank(src)) {
513 return true;
514 }
515
516 final URL url;
517 try {
518 url = upgradeInsecureRequest(page, page.getFullyQualifiedUrl(src));
519 }
520 catch (final MalformedURLException e) {
521 return true;
522 }
523
524 final Optional<URLWithScheme> target = Optional.of(toUrlWithScheme(matchingUrl(page, url)));
525 final Optional<URI> origin = origin(page);
526 final Violation violation = new Violation(FetchDirectiveKind.MediaSrc, blockedUri(url), null,
527 null, "", 0, 0);
528 final boolean allowed = page.getContentSecurityPolicy()
529 .check(page, violation, policy -> policy.allowsMedia(target, origin));
530 firePendingViolationsIfNoScriptRunning(page);
531 return allowed;
532 }
533
534
535
536
537
538
539
540
541 public static boolean allowsImage(final HtmlPage page, final URL url) {
542 return allowsImage(page, url, url);
543 }
544
545
546
547
548
549
550
551
552
553 public static boolean allowsImage(final HtmlPage page, final URL url, final URL reportedUrl) {
554 final ContentSecurityPolicy csp = page.getContentSecurityPolicy();
555 if (csp == null) {
556 return true;
557 }
558
559 final Optional<URLWithScheme> target = Optional.of(toUrlWithScheme(matchingUrl(page, url)));
560 final Optional<URI> origin = origin(page);
561 final Violation violation = new Violation(FetchDirectiveKind.ImgSrc, blockedUri(reportedUrl), null,
562 null, "", 0, 0);
563 final boolean allowed = csp.check(page, violation, policy -> policy.allowsImage(target, origin));
564 firePendingViolationsIfNoScriptRunning(page);
565 return allowed;
566 }
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582 public static URL upgradeInsecureRequest(final HtmlPage page, final URL url) {
583 if (url == null || !isUpgradable(url) || !upgradesInsecureRequests(page)) {
584 return url;
585 }
586 return upgrade(url);
587 }
588
589
590
591
592
593
594
595
596
597
598 public static URL upgradeInsecureNavigation(final HtmlPage page, final URL url) {
599 if (url == null || !isUpgradable(url)) {
600 return url;
601 }
602
603 HtmlPage current = page;
604 while (current != null) {
605 final ContentSecurityPolicy csp = current.getContentSecurityPolicy();
606 if (csp != null && csp.hasUpgradeInsecureRequests()) {
607 final URL pageUrl = current.getUrl();
608 if (pageUrl != null && pageUrl.getHost().equalsIgnoreCase(url.getHost())
609 && effectivePort(pageUrl) == effectivePort(url)) {
610 return upgrade(url);
611 }
612 }
613 current = parentPage(current);
614 }
615 return url;
616 }
617
618
619
620
621
622
623 private static URL matchingUrl(final HtmlPage page, final URL url) {
624 if (page.getWebClient().getBrowserVersion().hasFeature(CSP_UPGRADE_INSECURE_REQUESTS_MATCHES_HTTPS)) {
625 final String protocol = url.getProtocol();
626 if (("http".equals(protocol) || "ws".equals(protocol)) && upgradesInsecureRequests(page)) {
627 try {
628 return UrlUtils.getUrlWithNewProtocol(url, "http".equals(protocol) ? "https" : "wss");
629 }
630 catch (final MalformedURLException e) {
631 return url;
632 }
633 }
634 }
635 return url;
636 }
637
638
639
640
641
642
643
644
645 public static void upgradeInsecureRedirects(final HtmlPage page, final WebRequest request) {
646 if (upgradesInsecureRequests(page)) {
647 request.setRedirectUrlMapper(url -> upgradeInsecureRequest(page, url));
648 }
649 }
650
651
652
653
654
655
656
657 public static void upgradeInsecureNavigationRedirects(final HtmlPage page, final WebRequest request) {
658 if (upgradesInsecureRequests(page)) {
659 request.setRedirectUrlMapper(url -> upgradeInsecureNavigation(page, url));
660 }
661 }
662
663 private static boolean upgradesInsecureRequests(final HtmlPage page) {
664 HtmlPage current = page;
665 while (current != null) {
666 final ContentSecurityPolicy csp = current.getContentSecurityPolicy();
667 if (csp != null && csp.hasUpgradeInsecureRequests()) {
668 return true;
669 }
670 current = parentPage(current);
671 }
672 return false;
673 }
674
675
676
677
678 private boolean hasUpgradeInsecureRequests() {
679 for (final Policy policy : getEnforcedPolicies()) {
680 if (policy.upgradeInsecureRequests()) {
681 return true;
682 }
683 }
684 return false;
685 }
686
687 private static HtmlPage parentPage(final HtmlPage page) {
688 final WebWindow window = page.getEnclosingWindow();
689 if (window instanceof FrameWindow frameWindow) {
690 return frameWindow.getEnclosingPage();
691 }
692 return null;
693 }
694
695 private static boolean isUpgradable(final URL url) {
696 final String protocol = url.getProtocol();
697 if (!"http".equals(protocol) && !"ws".equals(protocol)) {
698 return false;
699 }
700 return !isLoopback(url.getHost());
701 }
702
703 private static boolean isLoopback(final String host) {
704 final String lower = host.toLowerCase(Locale.ROOT);
705 return "localhost".equals(lower)
706 || lower.endsWith(".localhost")
707 || lower.startsWith("127.")
708 || "[::1]".equals(lower)
709 || "::1".equals(lower);
710 }
711
712 private static int effectivePort(final URL url) {
713 final int port = url.getPort();
714 return port == -1 ? url.getDefaultPort() : port;
715 }
716
717 private static URL upgrade(final URL url) {
718 final String protocol = "ws".equals(url.getProtocol()) ? "wss" : "https";
719 try {
720 URL upgraded = UrlUtils.getUrlWithNewProtocol(url, protocol);
721 if (url.getPort() == 80) {
722 upgraded = UrlUtils.getUrlWithNewPort(upgraded, -1);
723 }
724 return upgraded;
725 }
726 catch (final MalformedURLException e) {
727 return url;
728 }
729 }
730
731
732
733
734
735
736
737
738
739 public static boolean allowsWorker(final HtmlPage page, final URL url) {
740 final ContentSecurityPolicy csp = page.getContentSecurityPolicy();
741 if (csp == null) {
742 return true;
743 }
744
745 final Optional<URLWithScheme> target = Optional.of(toUrlWithScheme(matchingUrl(page, url)));
746 final Optional<URI> origin = origin(page);
747 final Violation violation = new Violation(FetchDirectiveKind.WorkerSrc, blockedUri(url), null,
748 null, "", 0, 0);
749 final boolean allowed = csp.check(page, violation, policy -> policy.allowsWorker(target, origin));
750 firePendingViolationsIfNoScriptRunning(page);
751 return allowed;
752 }
753
754
755
756
757
758
759
760
761
762 public static boolean allowsBaseUri(final HtmlPage page, final URL url) {
763 final ContentSecurityPolicy csp = page.getContentSecurityPolicy();
764 if (csp == null) {
765 return true;
766 }
767
768 final URLWithScheme target = toUrlWithScheme(url);
769 final Optional<URI> origin = origin(page);
770 final Violation violation = new Violation(BASE_URI, withoutRef(url), null, null, "", 0, 0);
771 final boolean allowed = csp.check(page, violation,
772 policy -> policy.baseUri()
773 .map(directive -> Policy.doesUrlMatchSourceListInOrigin(target, directive, origin))
774 .orElse(true));
775 firePendingViolationsIfNoScriptRunning(page);
776 return allowed;
777 }
778
779
780
781
782
783
784
785
786
787
788
789 public static boolean allowsFormAction(final HtmlPage page, final URL url, final URL reportedUrl) {
790 final ContentSecurityPolicy csp = page.getContentSecurityPolicy();
791 if (csp == null) {
792 return true;
793 }
794
795 final Optional<URLWithScheme> target = Optional.of(toUrlWithScheme(matchingUrl(page, url)));
796 final Optional<URI> origin = origin(page);
797 final Violation violation = new Violation(FORM_ACTION, blockedUri(reportedUrl), null, null, "", 0, 0);
798 final boolean allowed = csp.check(page, violation,
799 policy -> policy.allowsFormAction(target, Optional.of(false), Optional.empty(), origin));
800 firePendingViolationsIfNoScriptRunning(page);
801 return allowed;
802 }
803
804
805
806
807
808
809
810
811
812 public static boolean allowsEval(final HtmlPage page, final String source, final int lineNumber) {
813 final ContentSecurityPolicy csp = page.getContentSecurityPolicy();
814 if (csp == null) {
815 return true;
816 }
817
818 final Violation violation = new Violation(FetchDirectiveKind.ScriptSrc, BLOCKED_EVAL, source,
819 null, documentUrl(page), lineNumber, 0);
820 final boolean allowed = csp.check(page, violation, Policy::allowsEval);
821 firePendingViolationsIfNoScriptRunning(page);
822 return allowed;
823 }
824
825
826
827
828
829
830 public static void firePendingViolations() {
831 final List<Runnable> pending = PENDING_VIOLATIONS.get();
832 if (pending == null) {
833 return;
834 }
835
836
837 while (!pending.isEmpty()) {
838 pending.remove(0).run();
839 }
840 PENDING_VIOLATIONS.remove();
841 }
842
843
844
845
846
847 private static void firePendingViolationsIfNoScriptRunning(final HtmlPage page) {
848 final AbstractJavaScriptEngine<?> engine = page.getWebClient().getJavaScriptEngine();
849 if (engine == null || !engine.isScriptRunning()) {
850 firePendingViolations();
851 }
852 }
853
854 private boolean check(final HtmlPage page, final Violation violation, final Predicate<Policy> check) {
855 boolean allowed = true;
856 for (final Policy policy : getEnforcedPolicies()) {
857 if (!check.test(policy)) {
858 allowed = false;
859 report(page, policy, violation, SecurityPolicyViolationEvent.DISPOSITION_ENFORCE);
860 }
861 }
862 for (final Policy policy : getReportOnlyPolicies()) {
863 if (!check.test(policy)) {
864 report(page, policy, violation, SecurityPolicyViolationEvent.DISPOSITION_REPORT);
865 }
866 }
867 return allowed;
868 }
869
870
871
872
873
874 private static void report(final HtmlPage page, final Policy policy, final Violation violation,
875 final String disposition) {
876 final FetchDirectiveKind directive = violation.directive_;
877 final String effectiveDirective = violation.effectiveDirective_;
878 final String blockedUri = violation.blockedUri_;
879 final String source = violation.source_;
880 final DomElement element = violation.target_;
881 if (LOG.isWarnEnabled()) {
882 LOG.warn((SecurityPolicyViolationEvent.DISPOSITION_REPORT.equals(disposition) ? "[Report Only] " : "")
883 + "Content-Security-Policy violation: '" + effectiveDirective
884 + "' blocked '" + blockedUri + "' in '" + page.getUrl() + "' (policy '" + policy + "').");
885 }
886
887 String sample = "";
888 if (source != null && directive != null) {
889 final SourceExpressionDirective governing =
890 policy.getGoverningDirectiveForEffectiveDirective(directive).orElse(null);
891 if (governing != null && governing.reportSample()) {
892 sample = source.length() > SAMPLE_LENGTH ? source.substring(0, SAMPLE_LENGTH) : source;
893 }
894 }
895
896 final DomNode target = element != null && element.isAttachedToPage() ? element : page;
897 final int lineNumber = Math.max(0, violation.lineNumber_);
898 final int columnNumber = Math.max(0, violation.columnNumber_);
899 final String documentUrl = documentUrl(page);
900 final String sourceFile = violation.sourceFile_;
901 final String originalPolicy = policy.toString();
902 final int statusCode = page.getWebResponse() == null ? 0 : page.getWebResponse().getStatusCode();
903 final String finalSample = sample;
904
905 final ContentSecurityPolicyViolationListener listener =
906 page.getWebClient().getContentSecurityPolicyViolationListener();
907 if (listener != null) {
908 listener.violation(page, new ContentSecurityPolicyViolation(documentUrl, blockedUri,
909 effectiveDirective, originalPolicy, sourceFile, finalSample, lineNumber, columnNumber,
910 statusCode, SecurityPolicyViolationEvent.DISPOSITION_REPORT.equals(disposition)));
911 }
912
913
914
915 final ReportUriDirective reportUri = policy.deliveredViaMeta() || policy.reportTo().isPresent()
916 ? null : policy.reportUri().orElse(null);
917 if (reportUri != null && !reportUri.getUris().isEmpty()) {
918 final String report;
919 if (page.getWebClient().getBrowserVersion().hasFeature(CSP_REPORT_FIREFOX_FORMAT)) {
920 report = "{\"csp-report\":{"
921 + jsonMember("blocked-uri", blockedUri) + ","
922 + "\"column-number\":" + Math.max(1, columnNumber) + ","
923 + jsonMember("disposition", disposition) + ","
924 + jsonMember("document-uri", documentUrl) + ","
925 + jsonMember("effective-directive", effectiveDirective) + ","
926 + (lineNumber > 0 ? "\"line-number\":" + lineNumber + "," : "")
927 + jsonMember("original-policy", absoluteReportUris(page, originalPolicy, reportUri)) + ","
928 + jsonMember("referrer", "") + ","
929 + (StringUtils.isEmptyOrNull(finalSample) ? "" : jsonMember("script-sample", finalSample) + ",")
930 + (StringUtils.isEmptyOrNull(sourceFile) ? "" : jsonMember("source-file", sourceFile) + ",")
931 + "\"status-code\":" + statusCode + ","
932 + jsonMember("violated-directive", effectiveDirective)
933 + "}}";
934 }
935 else {
936 report = "{\"csp-report\":{"
937 + jsonMember("document-uri", documentUrl) + ","
938 + jsonMember("referrer", "") + ","
939 + jsonMember("violated-directive", effectiveDirective) + ","
940 + jsonMember("effective-directive", effectiveDirective) + ","
941 + jsonMember("original-policy", originalPolicy) + ","
942 + jsonMember("disposition", disposition) + ","
943 + jsonMember("blocked-uri", blockedUri) + ","
944 + (lineNumber > 0 ? "\"line-number\":" + lineNumber + "," : "")
945 + (columnNumber > 0 ? "\"column-number\":" + columnNumber + "," : "")
946 + (StringUtils.isEmptyOrNull(sourceFile) ? "" : jsonMember("source-file", sourceFile) + ",")
947 + "\"status-code\":" + statusCode + ","
948 + jsonMember("script-sample", finalSample)
949 + "}}";
950 }
951 final List<String> uris = new ArrayList<>(reportUri.getUris());
952 addPending(() -> sendReports(page, uris, report));
953 }
954
955 final WebWindow window = page.getEnclosingWindow();
956 if (window == null || !page.getWebClient().isJavaScriptEnabled()
957 || window.getScriptableObject() == null) {
958 return;
959 }
960
961 final Runnable fireEvent = () -> {
962 final SecurityPolicyViolationEvent event = new SecurityPolicyViolationEvent(target);
963 event.setDocumentURI(documentUrl);
964 event.setBlockedURI(blockedUri);
965 event.setEffectiveDirective(effectiveDirective);
966 event.setViolatedDirective(effectiveDirective);
967 event.setOriginalPolicy(originalPolicy);
968 event.setSourceFile(sourceFile);
969 event.setSample(finalSample);
970 event.setDisposition(disposition);
971 event.setStatusCode(statusCode);
972 event.setLineNumber(lineNumber);
973 event.setColumnNumber(columnNumber);
974
975 final EventTarget jsTarget = target.getScriptableObject();
976 ((JavaScriptEngine) page.getWebClient().getJavaScriptEngine())
977 .callSecured(cx -> jsTarget.fireEvent(event), page);
978 };
979
980
981
982
983 addPending(fireEvent);
984 }
985
986 private static void addPending(final Runnable runnable) {
987 List<Runnable> pending = PENDING_VIOLATIONS.get();
988 if (pending == null) {
989 pending = new ArrayList<>();
990 PENDING_VIOLATIONS.set(pending);
991 }
992 pending.add(runnable);
993 }
994
995
996
997
998
999
1000 private static void sendReports(final HtmlPage page, final List<String> uris, final String report) {
1001 final WebClient webClient = page.getWebClient();
1002 for (final String uri : uris) {
1003 try {
1004 final URL url = page.getFullyQualifiedUrl(uri);
1005 final WebRequest request = new WebRequest(url, HttpMethod.POST);
1006 request.setAdditionalHeader(HttpHeader.CONTENT_TYPE, REPORT_CONTENT_TYPE);
1007 request.setCharset(UTF_8);
1008 request.setRequestBody(report);
1009 request.setFetchDestination(WebRequest.FetchDestination.REPORT);
1010 request.setFetchModeOverride(WebRequest.FetchMode.NO_CORS);
1011 request.setRequestingUrl(page.getUrl());
1012 webClient.loadWebResponse(request);
1013 }
1014 catch (final IOException | RuntimeException e) {
1015 if (LOG.isInfoEnabled()) {
1016 LOG.info("Sending the Content-Security-Policy violation report to '" + uri + "' failed", e);
1017 }
1018 }
1019 }
1020 }
1021
1022
1023
1024
1025 private static String absoluteReportUris(final HtmlPage page, final String policy,
1026 final ReportUriDirective reportUri) {
1027 final StringBuilder uris = new StringBuilder("report-uri");
1028 for (final String uri : reportUri.getUris()) {
1029 String absolute = uri;
1030 try {
1031 absolute = page.getFullyQualifiedUrl(uri).toExternalForm();
1032 }
1033 catch (final MalformedURLException e) {
1034
1035 }
1036 uris.append(' ').append(absolute);
1037 }
1038
1039 final String[] parts = policy.split(";", -1);
1040 for (int i = 0; i < parts.length; i++) {
1041 final String trimmed = parts[i].trim();
1042 if (trimmed.toLowerCase(Locale.ROOT).startsWith("report-uri")) {
1043 final String leading = parts[i].substring(0, parts[i].indexOf(trimmed));
1044 parts[i] = leading + uris;
1045 }
1046 }
1047 return String.join(";", parts);
1048 }
1049
1050 private static String jsonMember(final String name, final String value) {
1051 final StringBuilder json = new StringBuilder();
1052 json.append('"').append(name).append("\":\"");
1053 if (value != null) {
1054 for (int i = 0; i < value.length(); i++) {
1055 final char c = value.charAt(i);
1056 switch (c) {
1057 case '"':
1058 json.append("\\\"");
1059 break;
1060 case '\\':
1061 json.append("\\\\");
1062 break;
1063 case '\n':
1064 json.append("\\n");
1065 break;
1066 case '\r':
1067 json.append("\\r");
1068 break;
1069 case '\t':
1070 json.append("\\t");
1071 break;
1072 default:
1073 if (c < 0x20) {
1074 json.append(String.format("\\u%04x", (int) c));
1075 }
1076 else {
1077 json.append(c);
1078 }
1079 }
1080 }
1081 }
1082 return json.append('"').toString();
1083 }
1084
1085 private List<Policy> getEnforcedPolicies() {
1086 if (enforcedPolicies_ == null) {
1087 final List<Policy> policies = parse(enforced_);
1088 final Policy.MetaCommaHandling commaHandling = metaSinglePolicy_
1089 ? Policy.MetaCommaHandling.SINGLE_POLICY : Policy.MetaCommaHandling.SPLIT;
1090 for (final String serialized : meta_) {
1091 policies.addAll(Policy.parseSerializedCSPList(serialized,
1092 Policy.PolicyListErrorConsumer.ignored, commaHandling).getPolicies());
1093 }
1094 enforcedPolicies_ = policies;
1095 }
1096 return enforcedPolicies_;
1097 }
1098
1099 private List<Policy> getReportOnlyPolicies() {
1100 if (reportOnlyPolicies_ == null) {
1101 reportOnlyPolicies_ = parse(reportOnly_);
1102 }
1103 return reportOnlyPolicies_;
1104 }
1105
1106 private static List<Policy> parse(final List<String> serializedPolicies) {
1107 final List<Policy> policies = new ArrayList<>();
1108 for (final String serialized : serializedPolicies) {
1109 policies.addAll(Policy.parseSerializedCSPList(serialized,
1110 Policy.PolicyListErrorConsumer.ignored).getPolicies());
1111 }
1112 return policies;
1113 }
1114
1115
1116
1117
1118
1119
1120 private static Violation inlineViolation(final HtmlPage page, final FetchDirectiveKind directive,
1121 final String source, final DomElement element) {
1122 if (Context.getCurrentContext() != null) {
1123 final ScriptStackElement[] stack = new EvaluatorException("csp").getScriptStack();
1124 if (stack.length > 0) {
1125 return new Violation(directive, BLOCKED_INLINE, source, element,
1126 stack[0].fileName, Math.max(0, stack[0].lineNumber), 0);
1127 }
1128 }
1129 return new Violation(directive, BLOCKED_INLINE, source, element,
1130 documentUrl(page), element.getStartLineNumber(), element.getStartColumnNumber());
1131 }
1132
1133
1134
1135
1136
1137 private static Optional<String> nonce(final DomElement element) {
1138 final String nonce = element.getNonce();
1139 if (nonce.isEmpty() || !isNonceable(element)) {
1140 return Optional.empty();
1141 }
1142 return Optional.of(nonce);
1143 }
1144
1145
1146
1147
1148
1149 private static boolean isNonceable(final DomElement element) {
1150 for (final DomAttr attr : element.getAttributesMap().values()) {
1151 if (containsScriptOrStyle(attr.getName()) || containsScriptOrStyle(attr.getValue())) {
1152 return false;
1153 }
1154 }
1155 return true;
1156 }
1157
1158 private static boolean containsScriptOrStyle(final String value) {
1159 final String lower = value.toLowerCase(Locale.ROOT);
1160 return lower.contains("<script") || lower.contains("<style");
1161 }
1162
1163
1164
1165
1166
1167
1168
1169
1170 public static void hideNonceIfNeeded(final DomElement element) {
1171 final HtmlPage page = element.getHtmlPageOrNull();
1172 if (page == null) {
1173 return;
1174 }
1175 final ContentSecurityPolicy csp = page.getContentSecurityPolicy();
1176 if (csp == null) {
1177 return;
1178 }
1179 if (csp.enforced_.isEmpty()
1180 && (csp.reportOnly_.isEmpty()
1181 || page.getWebClient().getBrowserVersion().hasFeature(CSP_NONCE_HIDING_ENFORCED_ONLY))) {
1182 return;
1183 }
1184
1185 final String nonce = element.getAttributeDirect(DomElement.NONCE_ATTRIBUTE);
1186 if (ATTRIBUTE_NOT_DEFINED == nonce || nonce.isEmpty()) {
1187 return;
1188 }
1189 element.hideNonce();
1190 }
1191
1192
1193
1194
1195
1196 private static boolean isParserInserted(final DomElement element) {
1197 return element.getStartLineNumber() != -1;
1198 }
1199
1200 private static String documentUrl(final HtmlPage page) {
1201 return withoutRef(page.getUrl());
1202 }
1203
1204 private static String withoutRef(final URL url) {
1205 final String externalForm = url.toExternalForm();
1206 final int hashPos = externalForm.indexOf('#');
1207 if (hashPos == -1) {
1208 return externalForm;
1209 }
1210 return externalForm.substring(0, hashPos);
1211 }
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233 static URLWithScheme toUrlWithScheme(final URL url) {
1234 final String scheme = url.getProtocol().toLowerCase(Locale.ROOT);
1235 final String host = url.getHost();
1236 if (host == null || host.isEmpty()) {
1237 final String externalForm = url.toExternalForm();
1238 return new GUID(scheme, externalForm.substring(externalForm.indexOf(':') + 1));
1239 }
1240
1241 int port = url.getPort();
1242 if (port == -1) {
1243 port = URI.defaultPortForProtocol(scheme);
1244 }
1245 return new URI(scheme, host.toLowerCase(Locale.ROOT), port, url.getPath());
1246 }
1247
1248
1249
1250
1251
1252 private static String blockedUri(final URL url) {
1253 final String host = url.getHost();
1254 if (host == null || host.isEmpty()) {
1255 return url.getProtocol();
1256 }
1257 return withoutRef(url);
1258 }
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268 private static String navigationBlockedUri(final HtmlPage page, final URL url) {
1269 final String host = url.getHost();
1270 if (host == null || host.isEmpty()) {
1271 return "";
1272 }
1273 if (UrlUtils.isSameOrigin(selfUrl(page), url)) {
1274 return withoutRef(url);
1275 }
1276
1277 final String protocol = url.getProtocol();
1278 final int port = url.getPort();
1279 if (port == -1 || port == url.getDefaultPort()) {
1280 return protocol + "://" + host;
1281 }
1282 return protocol + "://" + host + ":" + port;
1283 }
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293 private static String objectBlockedUri(final HtmlPage page, final URL url) {
1294 final String host = url.getHost();
1295 if (host == null || host.isEmpty()) {
1296 return url.getProtocol();
1297 }
1298 return navigationBlockedUri(page, url);
1299 }
1300
1301 private static Optional<URI> origin(final HtmlPage page) {
1302 return URI.parseURI(selfUrl(page).toExternalForm());
1303 }
1304
1305
1306
1307
1308 private static URL selfUrl(final HtmlPage page) {
1309 final ContentSecurityPolicy csp = page.getContentSecurityPolicy();
1310 if (csp != null && csp.selfUrl_ != null) {
1311 return csp.selfUrl_;
1312 }
1313 return page.getUrl();
1314 }
1315
1316
1317
1318
1319 private static final class Violation {
1320 private final FetchDirectiveKind directive_;
1321 private final String effectiveDirective_;
1322 private final String blockedUri_;
1323 private final String source_;
1324 private final DomElement target_;
1325 private final String sourceFile_;
1326 private final int lineNumber_;
1327 private final int columnNumber_;
1328
1329 Violation(final FetchDirectiveKind directive, final String blockedUri, final String source,
1330 final DomElement target, final String sourceFile, final int lineNumber, final int columnNumber) {
1331 directive_ = directive;
1332 effectiveDirective_ = directive.getRepr();
1333 blockedUri_ = blockedUri;
1334 source_ = source;
1335 target_ = target;
1336 sourceFile_ = sourceFile;
1337 lineNumber_ = lineNumber;
1338 columnNumber_ = columnNumber;
1339 }
1340
1341
1342
1343
1344 Violation(final String effectiveDirective, final String blockedUri, final String source,
1345 final DomElement target, final String sourceFile, final int lineNumber, final int columnNumber) {
1346 directive_ = null;
1347 effectiveDirective_ = effectiveDirective;
1348 blockedUri_ = blockedUri;
1349 source_ = source;
1350 target_ = target;
1351 sourceFile_ = sourceFile;
1352 lineNumber_ = lineNumber;
1353 columnNumber_ = columnNumber;
1354 }
1355 }
1356 }