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 java.io.Serializable;
18
19 /**
20 * The details of a violation of the Content-Security-Policy of a page.
21 * The properties correspond to the ones of the securitypolicyviolation event
22 * (see <a href="https://w3c.github.io/webappsec-csp/#violation-events">CSP - Violation Events</a>).
23 * <ul>
24 * <li>blockedUri: the blocked url; 'inline' for inline scripts, event handler attributes and
25 * javascript: urls, 'eval' for the evaluation of strings; only the scheme for urls
26 * without host (e.g. 'data'); for frame-ancestors the ancestor not allowed to embed
27 * the document</li>
28 * <li>effectiveDirective: the directive whose enforcement caused the violation
29 * (e.g. 'script-src-elem', 'script-src-attr', 'script-src', 'connect-src', 'frame-ancestors')</li>
30 * <li>sourceFile, lineNumber, columnNumber: the location of the code causing the violation;
31 * an empty string or 0 if not known</li>
32 * <li>sample: the first 40 characters of the blocked code if the directive contains
33 * 'report-sample', an empty string otherwise</li>
34 * </ul>
35 *
36 * @author Ronald Brill
37 * @see ContentSecurityPolicyViolationListener
38 */
39 public final class ContentSecurityPolicyViolation implements Serializable {
40
41 private final String documentUri_;
42 private final String blockedUri_;
43 private final String effectiveDirective_;
44 private final String originalPolicy_;
45 private final String sourceFile_;
46 private final String sample_;
47 private final int lineNumber_;
48 private final int columnNumber_;
49 private final int statusCode_;
50 private final boolean reportOnly_;
51
52 /**
53 * Ctor.
54 *
55 * @param documentUri the url of the document
56 * @param blockedUri the blocked url
57 * @param effectiveDirective the directive whose enforcement caused the violation
58 * @param originalPolicy the policy (serialized) that was violated
59 * @param sourceFile the url of the source causing the violation
60 * @param sample the sample of the blocked code
61 * @param lineNumber the line number in the source file
62 * @param columnNumber the column number in the source file
63 * @param statusCode the http status code of the document
64 * @param reportOnly whether the violated policy is a report only policy
65 */
66 public ContentSecurityPolicyViolation(final String documentUri,
67 final String blockedUri,
68 final String effectiveDirective,
69 final String originalPolicy,
70 final String sourceFile,
71 final String sample,
72 final int lineNumber,
73 final int columnNumber,
74 final int statusCode,
75 final boolean reportOnly) {
76 documentUri_ = documentUri;
77 blockedUri_ = blockedUri;
78 effectiveDirective_ = effectiveDirective;
79 originalPolicy_ = originalPolicy;
80 sourceFile_ = sourceFile;
81 sample_ = sample;
82 lineNumber_ = lineNumber;
83 columnNumber_ = columnNumber;
84 statusCode_ = statusCode;
85 reportOnly_ = reportOnly;
86 }
87
88 /**
89 * Returns the url of the document.
90 *
91 * @return the url of the document
92 */
93 public String getDocumentUri() {
94 return documentUri_;
95 }
96
97 /**
98 * Returns the blocked url.
99 *
100 * @return the blocked url
101 */
102 public String getBlockedUri() {
103 return blockedUri_;
104 }
105
106 /**
107 * Returns the directive whose enforcement caused the violation.
108 *
109 * @return the directive whose enforcement caused the violation
110 */
111 public String getEffectiveDirective() {
112 return effectiveDirective_;
113 }
114
115 /**
116 * Returns the policy (serialized) that was violated.
117 *
118 * @return the policy (serialized) that was violated
119 */
120 public String getOriginalPolicy() {
121 return originalPolicy_;
122 }
123
124 /**
125 * Returns the url of the source causing the violation.
126 *
127 * @return the url of the source causing the violation
128 */
129 public String getSourceFile() {
130 return sourceFile_;
131 }
132
133 /**
134 * Returns the sample of the blocked code.
135 *
136 * @return the sample of the blocked code
137 */
138 public String getSample() {
139 return sample_;
140 }
141
142 /**
143 * Returns the line number in the source file.
144 *
145 * @return the line number in the source file
146 */
147 public int getLineNumber() {
148 return lineNumber_;
149 }
150
151 /**
152 * Returns the column number in the source file.
153 *
154 * @return the column number in the source file
155 */
156 public int getColumnNumber() {
157 return columnNumber_;
158 }
159
160 /**
161 * Returns the http status code of the document.
162 *
163 * @return the http status code of the document
164 */
165 public int getStatusCode() {
166 return statusCode_;
167 }
168
169 /**
170 * Returns the violated directive; same as {@link #getEffectiveDirective()}.
171 *
172 * @return the violated directive
173 */
174 public String getViolatedDirective() {
175 return effectiveDirective_;
176 }
177
178 /**
179 * Returns whether the violated policy is a report only policy
180 * (Content-Security-Policy-Report-Only header); in this case the action was not blocked.
181 *
182 * @return {@code true} if the violated policy is a report only policy
183 */
184 public boolean isReportOnly() {
185 return reportOnly_;
186 }
187
188 /**
189 * Returns the disposition of the violated policy ('enforce' or 'report').
190 *
191 * @return the disposition
192 */
193 public String getDisposition() {
194 return reportOnly_ ? "report" : "enforce";
195 }
196
197 /**
198 * {@inheritDoc}
199 */
200 @Override
201 public String toString() {
202 return (reportOnly_ ? "[Report Only] " : "")
203 + "'" + effectiveDirective_ + "' blocked '" + blockedUri_ + "' in '" + documentUri_
204 + "' (policy '" + originalPolicy_ + "')";
205 }
206 }