#include "third_party/blink/renderer/core/frame/csp/csp_directive_list.h"
#include <memory>
#include <utility>
#include "base/notreached.h"
#include "services/network/public/cpp/content_security_policy/content_security_policy.h"
#include "services/network/public/mojom/content_security_policy.mojom-blink.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/space_split_string.h"
#include "third_party/blink/renderer/core/execution_context/security_context.h"
#include "third_party/blink/renderer/core/frame/csp/content_security_policy_violation_type.h"
#include "third_party/blink/renderer/core/frame/csp/source_list_directive.h"
#include "third_party/blink/renderer/core/frame/csp/trusted_types_directive.h"
#include "third_party/blink/renderer/core/frame/deprecation/deprecation.h"
#include "third_party/blink/renderer/core/html/html_script_element.h"
#include "third_party/blink/renderer/core/inspector/console_message.h"
#include "third_party/blink/renderer/core/loader/mixed_content_checker.h"
#include "third_party/blink/renderer/platform/bindings/source_location.h"
#include "third_party/blink/renderer/platform/crypto.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
#include "third_party/blink/renderer/platform/weborigin/known_ports.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/weborigin/reporting_disposition.h"
#include "third_party/blink/renderer/platform/wtf/text/base64.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#include "third_party/blink/renderer/platform/wtf/text/string_utf8_adaptor.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace blink {
ContentSecurityPolicySource;
ContentSecurityPolicyType;
namespace {
String GetRawDirectiveForMessage(
const HashMap<CSPDirectiveName, String> raw_directives,
CSPDirectiveName directive_name) { … }
String GetSha256String(const String& content) { … }
network::mojom::blink::CSPHashAlgorithm ConvertHashAlgorithmToCSPHashAlgorithm(
IntegrityAlgorithm algorithm) { … }
bool ParseBase64Digest(String base64, Vector<uint8_t>& hash) { … }
CSPDirectiveName EffectiveDirectiveForInlineCheck(
ContentSecurityPolicy::InlineType inline_type) { … }
CSPOperativeDirective OperativeDirective(
const network::mojom::blink::ContentSecurityPolicy& csp,
CSPDirectiveName type,
CSPDirectiveName original_type = CSPDirectiveName::Unknown) { … }
void ReportViolation(
const network::mojom::blink::ContentSecurityPolicy& csp,
ContentSecurityPolicy* policy,
const String& directive_text,
CSPDirectiveName effective_type,
const String& console_message,
const KURL& blocked_url,
ContentSecurityPolicyViolationType violation_type =
ContentSecurityPolicyViolationType::kURLViolation,
const String& sample = String(),
const String& sample_prefix = String(),
std::optional<base::UnguessableToken> issue_id = std::nullopt) { … }
void ReportViolationWithLocation(
const network::mojom::blink::ContentSecurityPolicy& csp,
ContentSecurityPolicy* policy,
const String& directive_text,
CSPDirectiveName effective_type,
const String& console_message,
const KURL& blocked_url,
const String& context_url,
const WTF::OrdinalNumber& context_line,
Element* element,
const String& source) { … }
void ReportEvalViolation(
const network::mojom::blink::ContentSecurityPolicy& csp,
ContentSecurityPolicy* policy,
const String& directive_text,
CSPDirectiveName effective_type,
const String& message,
const KURL& blocked_url,
const ContentSecurityPolicy::ExceptionStatus exception_status,
const String& content) { … }
void ReportWasmEvalViolation(
const network::mojom::blink::ContentSecurityPolicy& csp,
ContentSecurityPolicy* policy,
const String& directive_text,
CSPDirectiveName effective_type,
const String& message,
const KURL& blocked_url,
const ContentSecurityPolicy::ExceptionStatus exception_status,
const String& content) { … }
bool CheckEval(const network::mojom::blink::CSPSourceList* directive) { … }
bool SupportsWasmEval(const network::mojom::blink::ContentSecurityPolicy& csp,
const ContentSecurityPolicy* policy) { … }
bool CheckWasmEval(const network::mojom::blink::ContentSecurityPolicy& csp,
const ContentSecurityPolicy* policy) { … }
bool CheckHash(const network::mojom::blink::CSPSourceList* directive,
const network::mojom::blink::CSPHashSource& hash_value) { … }
bool CheckUnsafeHashesAllowed(
const network::mojom::blink::CSPSourceList* directive) { … }
bool CheckUnsafeHashesAllowed(
ContentSecurityPolicy::InlineType inline_type,
const network::mojom::blink::CSPSourceList* directive) { … }
bool CheckDynamic(const network::mojom::blink::CSPSourceList* directive,
CSPDirectiveName effective_type) { … }
bool IsMatchingNoncePresent(
const network::mojom::blink::CSPSourceList* directive,
const String& nonce) { … }
bool AreAllMatchingHashesPresent(
const network::mojom::blink::CSPSourceList* directive,
const IntegrityMetadataSet& hashes) { … }
bool CheckEvalAndReportViolation(
const network::mojom::blink::ContentSecurityPolicy& csp,
ContentSecurityPolicy* policy,
const String& console_message,
ContentSecurityPolicy::ExceptionStatus exception_status,
const String& content) { … }
bool CheckWasmEvalAndReportViolation(
const network::mojom::blink::ContentSecurityPolicy& csp,
ContentSecurityPolicy* policy,
const String& console_message,
ContentSecurityPolicy::ExceptionStatus exception_status,
const String& content) { … }
bool CheckInlineAndReportViolation(
const network::mojom::blink::ContentSecurityPolicy& csp,
ContentSecurityPolicy* policy,
CSPOperativeDirective directive,
const String& console_message,
Element* element,
const String& source,
const String& context_url,
const WTF::OrdinalNumber& context_line,
ContentSecurityPolicy::InlineType inline_type,
const String& hash_value,
CSPDirectiveName effective_type) { … }
void ReportViolationForCheckSource(
const network::mojom::blink::ContentSecurityPolicy& csp,
ContentSecurityPolicy* policy,
CSPOperativeDirective directive,
const KURL& url,
CSPDirectiveName effective_type,
const KURL& url_before_redirects,
String suffix) { … }
CSPCheckResult CheckSource(
const network::mojom::blink::ContentSecurityPolicy& csp,
ContentSecurityPolicy* policy,
CSPOperativeDirective directive,
const KURL& url,
CSPDirectiveName effective_type,
const KURL& url_before_redirects,
ResourceRequest::RedirectStatus redirect_status,
ReportingDisposition reporting_disposition) { … }
bool AllowDynamicWorker(
const network::mojom::blink::ContentSecurityPolicy& csp) { … }
}
bool CSPDirectiveListIsReportOnly(
const network::mojom::blink::ContentSecurityPolicy& csp) { … }
bool CSPDirectiveListAllowTrustedTypeAssignmentFailure(
const network::mojom::blink::ContentSecurityPolicy& csp,
ContentSecurityPolicy* policy,
const String& message,
const String& sample,
const String& sample_prefix,
std::optional<base::UnguessableToken> issue_id) { … }
bool CSPDirectiveListAllowInline(
const network::mojom::blink::ContentSecurityPolicy& csp,
ContentSecurityPolicy* policy,
ContentSecurityPolicy::InlineType inline_type,
Element* element,
const String& content,
const String& nonce,
const String& context_url,
const WTF::OrdinalNumber& context_line,
ReportingDisposition reporting_disposition) { … }
bool CSPDirectiveListShouldCheckEval(
const network::mojom::blink::ContentSecurityPolicy& csp) { … }
bool CSPDirectiveListAllowEval(
const network::mojom::blink::ContentSecurityPolicy& csp,
ContentSecurityPolicy* policy,
ReportingDisposition reporting_disposition,
ContentSecurityPolicy::ExceptionStatus exception_status,
const String& content) { … }
bool CSPDirectiveListAllowWasmCodeGeneration(
const network::mojom::blink::ContentSecurityPolicy& csp,
ContentSecurityPolicy* policy,
ReportingDisposition reporting_disposition,
ContentSecurityPolicy::ExceptionStatus exception_status,
const String& content) { … }
bool CSPDirectiveListShouldDisableEval(
const network::mojom::blink::ContentSecurityPolicy& csp,
String& error_message) { … }
bool CSPDirectiveListShouldDisableWasmEval(
const network::mojom::blink::ContentSecurityPolicy& csp,
const ContentSecurityPolicy* policy,
String& error_message) { … }
CSPCheckResult CSPDirectiveListAllowFromSource(
const network::mojom::blink::ContentSecurityPolicy& csp,
ContentSecurityPolicy* policy,
CSPDirectiveName type,
const KURL& url,
const KURL& url_before_redirects,
ResourceRequest::RedirectStatus redirect_status,
ReportingDisposition reporting_disposition,
const String& nonce,
const IntegrityMetadataSet& hashes,
ParserDisposition parser_disposition) { … }
bool CSPDirectiveListAllowTrustedTypePolicy(
const network::mojom::blink::ContentSecurityPolicy& csp,
ContentSecurityPolicy* policy,
const String& policy_name,
bool is_duplicate,
ContentSecurityPolicy::AllowTrustedTypePolicyDetails& violation_details,
std::optional<base::UnguessableToken> issue_id) { … }
bool CSPDirectiveListRequiresTrustedTypes(
const network::mojom::blink::ContentSecurityPolicy& csp) { … }
bool CSPDirectiveListAllowHash(
const network::mojom::blink::ContentSecurityPolicy& csp,
const network::mojom::blink::CSPHashSource& hash_value,
const ContentSecurityPolicy::InlineType inline_type) { … }
bool CSPDirectiveListAllowDynamic(
const network::mojom::blink::ContentSecurityPolicy& csp,
CSPDirectiveName directive_type) { … }
bool CSPDirectiveListIsObjectRestrictionReasonable(
const network::mojom::blink::ContentSecurityPolicy& csp) { … }
bool CSPDirectiveListIsBaseRestrictionReasonable(
const network::mojom::blink::ContentSecurityPolicy& csp) { … }
bool CSPDirectiveListIsScriptRestrictionReasonable(
const network::mojom::blink::ContentSecurityPolicy& csp) { … }
bool CSPDirectiveListIsActiveForConnections(
const network::mojom::blink::ContentSecurityPolicy& csp) { … }
CSPOperativeDirective CSPDirectiveListOperativeDirective(
const network::mojom::blink::ContentSecurityPolicy& csp,
CSPDirectiveName type) { … }
}