View Javadoc
1   /*
2    * Copyright (c) 2002-2026 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
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   * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
68   *
69   * The Content-Security-Policy of a page. Contains the enforced policies (from the
70   * Content-Security-Policy headers and meta elements) and the report only policies
71   * (from the Content-Security-Policy-Report-Only headers).
72   * <p>
73   * All the check methods return {@code false} if one of the enforced policies does not allow
74   * the action. For every policy (enforced or report only) that does not allow the action a
75   * securitypolicyviolation event is fired.
76   * </p>
77   *
78   * @author Ronald Brill
79   */
80  public final class ContentSecurityPolicy implements Serializable {
81  
82      private static final Log LOG = LogFactory.getLog(ContentSecurityPolicy.class);
83  
84      /** The name of the report only header. */
85      public static final String HEADER_REPORT_ONLY = "Content-Security-Policy-Report-Only";
86  
87      /** The maximum length of the sample reported for 'report-sample'. */
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      /** The violation events to be fired after the current script has finished. */
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     // for inherited policies the url of the creator document ('self')
104     private URL selfUrl_;
105 
106     private transient List<Policy> enforcedPolicies_;
107     private transient List<Policy> reportOnlyPolicies_;
108 
109     /**
110      * Ctor.
111      */
112     private ContentSecurityPolicy() {
113         super();
114     }
115 
116     /**
117      * Creates the policy for the given response.
118      *
119      * @param webResponse the response
120      * @return the policy or {@code null} if the response has no policy header
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      * Returns a copy of the policy of the creator for documents with a local scheme url
152      * (about:, data:, blob:). Like the browsers, these documents inherit the policy of the
153      * document creating them; for frames this is the parent document, for windows opened
154      * by window.open() the opener. The copy is independent from the policy of the creator
155      * (policies added later by meta elements are not shared) and 'self' refers to the
156      * origin of the creator.
157      *
158      * @param url the url of the new document
159      * @param window the window the new document is loaded into
160      * @return the inherited policy or {@code null}
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      * Adds a policy defined by a meta element to the given page.
199      * The policy is parsed with the meta restrictions (frame-ancestors, sandbox and
200      * report-uri are ignored). Depending on the browser, commas split the content into
201      * several policies (Chrome) or are part of the single policy (Firefox).
202      *
203      * @param page the page
204      * @param serializedPolicy the policy
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      * Checks if the given inline script is allowed.
223      *
224      * @param page the page
225      * @param element the script element
226      * @param source the script source
227      * @return {@code true} if the script is allowed
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      * Checks if the given external script is allowed.
245      *
246      * @param page the page
247      * @param element the script element
248      * @param url the url of the script
249      * @return {@code true} if the script is allowed
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      * Checks if the given external script is allowed.
257      *
258      * @param page the page
259      * @param element the script element
260      * @param url the url of the script (or the target of a redirect)
261      * @param reportedUrl the url reported as blocked uri (for redirects the url of the script)
262      * @return {@code true} if the script is allowed
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      * Checks if the given event handler attribute is allowed.
286      *
287      * @param page the page
288      * @param element the element
289      * @param source the source of the event handler
290      * @return {@code true} if the event handler is allowed
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      * Checks if the given javascript: url is allowed.
309      *
310      * @param page the page
311      * @param element the element (link, form) or {@code null}
312      * @param source the source of the javascript url (including the 'javascript:' prefix)
313      * @return {@code true} if the javascript url is allowed
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      * Checks if a connection (XMLHttpRequest, fetch(), ...) to the given url is allowed (connect-src).
333      *
334      * @param page the page
335      * @param url the url to connect to
336      * @param reportedUrl the url reported in case of a violation; for redirects this is the
337      *        url of the initial request
338      * @return {@code true} if the connection is allowed
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         // like browsers, ws: and wss: urls are not mapped to http: and https: before matching
351         final boolean allowed = csp.check(page, violation, policy -> policy.allowsConnection(target, origin, false));
352         firePendingViolationsIfNoScriptRunning(page);
353         return allowed;
354     }
355 
356     /**
357      * Checks if the inline style element (style-src-elem) is allowed.
358      *
359      * @param page the page
360      * @param element the style element
361      * @param source the content of the style element
362      * @return {@code true} if the style is allowed
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      * Checks if the style attribute (style-src-attr) is allowed.
380      * Like the browsers, style changes done by the CSSOM (e.g. element.style.color = 'red')
381      * are not checked.
382      *
383      * @param page the page
384      * @param element the element with the style attribute
385      * @param source the value of the style attribute
386      * @return {@code true} if the style attribute is allowed
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      * Checks if loading the external stylesheet (link element or @import) is allowed (style-src-elem).
404      *
405      * @param page the page
406      * @param element the link element (the nonce is taken from this element) or {@code null} for @import
407      * @param url the url of the stylesheet
408      * @return {@code true} if the stylesheet is allowed
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      * Checks if loading the external stylesheet (link element or @import) is allowed (style-src-elem).
416      *
417      * @param page the page
418      * @param element the link element (the nonce is taken from this element) or {@code null} for @import
419      * @param url the url of the stylesheet (or the target of a redirect)
420      * @param reportedUrl the url reported as blocked uri (for redirects the url of the stylesheet)
421      * @return {@code true} if the stylesheet is allowed
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      * Checks if loading the given url into a frame or iframe is allowed
443      * (frame-src, with fallback to child-src and default-src).
444      * Like the browsers, the violation is reported for the document and the blocked url
445      * is reported without path if it is cross origin (navigations may leak information).
446      *
447      * @param page the page containing the frame
448      * @param url the url to load into the frame (for redirects the target of the redirect)
449      * @return {@code true} if loading is allowed
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      * Checks if the url of an object or embed element is allowed (object-src, with fallback
468      * to default-src). HtmlUnit does not load the content of these elements; like the browsers,
469      * the url is checked anyway to report the violation.
470      *
471      * @param element the object or embed element
472      * @param src the value of the data (object) or src (embed) attribute
473      * @return {@code true} if the url is allowed (or the attribute is empty or invalid)
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      * Checks if the url of a media element (audio, video), a source element or a track element
502      * is allowed (media-src, with fallback to default-src). HtmlUnit does not load media; like the
503      * browsers, the url is checked anyway to report the violation.
504      *
505      * @param element the element
506      * @param src the value of the src attribute
507      * @return {@code true} if the url is allowed (or the attribute is empty or invalid)
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      * Checks if loading the given image url is allowed (img-src, with fallback to default-src).
536      *
537      * @param page the page containing the image
538      * @param url the url of the image
539      * @return {@code true} if the image is allowed
540      */
541     public static boolean allowsImage(final HtmlPage page, final URL url) {
542         return allowsImage(page, url, url);
543     }
544 
545     /**
546      * Checks if loading the given image url is allowed (img-src, with fallback to default-src).
547      *
548      * @param page the page containing the image
549      * @param url the url of the image (or the target of a redirect)
550      * @param reportedUrl the url reported as blocked uri (for redirects the url of the image)
551      * @return {@code true} if the image is allowed
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      * Upgrade Insecure Requests (upgrade-insecure-requests directive): returns the url to be used for
570      * a request of the page; used for subresources, nested navigations (frames) and form submissions.
571      * <p>
572      * If the page (or one of its ancestors) has an enforced policy containing
573      * upgrade-insecure-requests, http urls are upgraded to https and ws urls to wss (port 80 is
574      * replaced by the default port). Like the browsers, loopback hosts (localhost, 127.0.0.0/8, ::1)
575      * are not upgraded.
576      * </p>
577      *
578      * @param page the page doing the request
579      * @param url the url of the request
580      * @return the upgraded url or the url itself
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      * Upgrade Insecure Requests for top level navigations: the url is upgraded only if the host and port
591      * are the same as the host and port of the page (or of one of its ancestors) having the
592      * upgrade-insecure-requests directive.
593      *
594      * @param page the page doing the navigation
595      * @param url the url of the navigation
596      * @return the upgraded url or the url itself
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      * Returns the url used for matching the source expressions. Firefox treats http (ws) urls
620      * like https (wss) urls if the page upgrades insecure requests; this is relevant for
621      * urls that are not upgraded (loopback hosts).
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      * Upgrade Insecure Requests for the redirects of a request (subresources, nested navigations
640      * and form submissions): the redirect targets are upgraded like the request itself.
641      *
642      * @param page the page doing the request
643      * @param request the request
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      * Upgrade Insecure Requests for the redirects of a top level navigation.
653      *
654      * @param page the page doing the navigation
655      * @param request the request
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      * The directive is ignored for report only policies.
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      * Checks if creating a worker with the given script url is allowed
733      * (worker-src, with fallback to child-src, script-src and default-src).
734      *
735      * @param page the page creating the worker
736      * @param url the url of the worker script
737      * @return {@code true} if the worker is allowed
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      * Checks if the given url is allowed as base url of the document (base-uri).
756      * There is no fallback to default-src for this directive.
757      *
758      * @param page the page containing the base element
759      * @param url the resolved url of the base element
760      * @return {@code true} if the base url is allowed
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      * Checks if the submission of a form to the given url is allowed (form-action).
781      * There is no fallback to default-src for this directive.
782      *
783      * @param page the page containing the form
784      * @param url the url the form is submitted to (for javascript: urls the whole url)
785      * @param reportedUrl the url reported in case of a violation; for redirects this is the
786      *        url of the form submission
787      * @return {@code true} if the submission is allowed
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      * Checks if the evaluation of strings as code (eval(), new Function(), setTimeout(string)) is allowed.
806      *
807      * @param page the page
808      * @param source the source to evaluate
809      * @param lineNumber the line number of the code calling eval or {@code 0} if not known
810      * @return {@code true} if the evaluation is allowed
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      * Fires the securitypolicyviolation events of the violations detected by
827      * the current thread so far. Called after a script element was processed and
828      * when the outermost script execution has finished.
829      */
830     public static void firePendingViolations() {
831         final List<Runnable> pending = PENDING_VIOLATIONS.get();
832         if (pending == null) {
833             return;
834         }
835 
836         // firing an event may add new violations
837         while (!pending.isEmpty()) {
838             pending.remove(0).run();
839         }
840         PENDING_VIOLATIONS.remove();
841     }
842 
843     /**
844      * Fires the pending violations if no script is running; otherwise this is done
845      * at the end of the script execution.
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      * Logs the violation and fires the securitypolicyviolation event (async)
872      * at the target element (if connected) or the document.
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         // like the browsers, report-uri is ignored if the policy is delivered by a meta element
914         // or if the policy contains a report-to directive
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         // browsers queue a task to fire the event; to get a deterministic order
981         // we fire the event after the current script has finished
982         // (see firePendingViolations())
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      * Sends the violation report (report-uri) as POST request with content type
997      * application/csp-report to all the given uris (relative uris are resolved against the
998      * url of the document). Errors are only logged.
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      * Returns the policy with absolute report uris (like Firefox).
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                 // keep the uri as it is
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      * Creates the violation for inline content (style element or style attribute). Like the browsers,
1117      * the location is the location of the running script, if the element was created or changed
1118      * by a script; otherwise the location of the element in the document.
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      * Returns the nonce of the element (the nonce slot, not the maybe hidden attribute);
1135      * empty if the element has no nonce or is not nonceable.
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      * Spec 'is element nonceable': elements with an attribute name or value containing
1147      * '&lt;script' or '&lt;style' are not nonceable (protection against dangling markup).
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      * Nonce hiding: if the page has a policy delivered by a header, the nonce attribute of
1165      * an element added to the page is replaced by an empty string (the nonce IDL attribute
1166      * still returns the nonce). Firefox does this only for enforced policies.
1167      *
1168      * @param element the element added to the page
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      * Elements created by the parser have a line number; elements created by
1194      * JavaScript (e.g. document.createElement()) have no line number.
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      * Converts the given url into the url representation used by the csp library
1215      * (htmlunit-csp) to match the url against the source expressions of a policy.
1216      * <ul>
1217      *   <li>Urls with a host (e.g. http, https, ws, wss) are converted into a
1218      *       {@link URI} made of the scheme and the host (both lower case), the port
1219      *       (the default port of the scheme if the url has no explicit port) and the path; the query
1220      *       and the fragment are not part of the result. Host sources (e.g. {@code https://example.com/api/})
1221      *       and 'self' are matched against these parts.</li>
1222      *   <li>Urls without a host (e.g. data:, blob:, about:, file:///) are converted into a
1223      *       {@link GUID} made of the scheme (lower case) and the remaining part
1224      *       of the url; such urls are only matched by a scheme source (e.g. {@code data:}),
1225      *       never by 'self', a host source or {@code *}.</li>
1226      * </ul>
1227      * The result is the same as the one of {@link URI#parseURI(String)} or
1228      * {@link GUID#parseGUID(String)}, but without parsing the url again.
1229      *
1230      * @param url the url to convert
1231      * @return the converted url
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      * Returns the blockedURI reported for the given url; like in browsers only the scheme
1250      * is reported for urls without host (e.g. data:).
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      * The blocked uri reported for navigations (e.g. frames); like Chrome
1262      * <ul>
1263      *   <li>urls without host (e.g. data:) are reported as empty string</li>
1264      *   <li>cross origin urls are reported as origin (without path)</li>
1265      *   <li>same origin urls are reported without the fragment</li>
1266      * </ul>
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      * The blocked uri reported for objects (object-src); like the browsers.
1287      * <ul>
1288      *   <li>urls without host (e.g. data:) are reported as scheme</li>
1289      *   <li>cross origin urls are reported as origin (without path)</li>
1290      *   <li>same origin urls are reported without the fragment</li>
1291      * </ul>
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      * Returns the url used for 'self'; for inherited policies the url of the creator.
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      * The details of a violation.
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          * Ctor for violations of directives that are no fetch directives (e.g. form-action).
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 }