#include "services/network/public/cpp/content_security_policy/content_security_policy.h"
#include <sstream>
#include <string>
#include <string_view>
#include "base/base64url.h"
#include "base/containers/contains.h"
#include "base/containers/flat_set.h"
#include "base/feature_list.h"
#include "base/ranges/algorithm.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "net/http/http_response_headers.h"
#include "services/network/public/cpp/content_security_policy/csp_context.h"
#include "services/network/public/cpp/content_security_policy/csp_source.h"
#include "services/network/public/cpp/content_security_policy/csp_source_list.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/cpp/is_potentially_trustworthy.h"
#include "services/network/public/cpp/web_sandbox_flags.h"
#include "services/network/public/mojom/content_security_policy.mojom.h"
#include "url/gurl.h"
#include "url/origin.h"
#include "url/url_canon.h"
#include "url/url_util.h"
namespace network {
CSPDirectiveName;
DirectivesMap;
namespace features {
BASE_FEATURE(…);
}
namespace {
bool IsDirectiveNameCharacter(char c) { … }
bool IsDirectiveValueCharacter(char c) { … }
std::string ElideURLForReportViolation(const GURL& url) { … }
CSPDirectiveName ToCSPDirectiveName(std::string_view name) { … }
bool SupportedInReportOnly(CSPDirectiveName directive) { … }
bool SupportedInMeta(CSPDirectiveName directive) { … }
const char* ErrorMessage(CSPDirectiveName directive) { … }
void ReportViolation(CSPContext* context,
const mojom::ContentSecurityPolicyPtr& policy,
const CSPDirectiveName effective_directive_name,
const CSPDirectiveName directive_name,
const GURL& url,
const mojom::SourceLocationPtr& source_location) { … }
const GURL ExtractInnerURL(const GURL& url) { … }
std::string InnermostScheme(const GURL& url) { … }
bool ShouldBypassContentSecurityPolicy(CSPContext* context,
CSPDirectiveName directive,
const GURL& url) { … }
DirectivesMap ParseHeaderValue(std::string_view header) { … }
bool ParseScheme(std::string_view scheme, mojom::CSPSource* csp_source) { … }
bool ParseHost(std::string_view host, mojom::CSPSource* csp_source) { … }
bool ParsePort(std::string_view port, mojom::CSPSource* csp_source) { … }
bool ParsePath(std::string_view path, mojom::CSPSource* csp_source) { … }
bool IsBase64Char(char c) { … }
int EatChar(std::string_view::const_iterator* it,
std::string_view::const_iterator end,
bool (*predicate)(char)) { … }
bool IsBase64(std::string_view expression) { … }
bool ParseNonce(std::string_view expression, std::string* nonce) { … }
struct SupportedPrefixesStruct { … };
bool ParseHash(std::string_view expression, mojom::CSPHashSource* hash) { … }
mojom::CSPSourceListPtr ParseSourceList(
CSPDirectiveName directive_name,
std::string_view directive_value,
std::vector<std::string>& parsing_errors) { … }
network::mojom::CSPRequireTrustedTypesFor ParseRequireTrustedTypesFor(
std::string_view value,
std::vector<std::string>& parsing_errors) { … }
bool IsValidTrustedTypesPolicyName(std::string_view value) { … }
network::mojom::CSPTrustedTypesPtr ParseTrustedTypes(
std::string_view value,
std::vector<std::string>& parsing_errors) { … }
void ParseReportDirective(const GURL& request_url,
std::string_view value,
bool using_reporting_api,
std::vector<std::string>* report_endpoints,
std::vector<std::string>& parsing_errors) { … }
void WarnIfDirectiveValueNotEmpty(
const std::pair<std::string_view, std::string_view>& directive,
std::vector<std::string>& parsing_errors) { … }
mojom::CSPSourcePtr ComputeSelfOrigin(const GURL& url) { … }
std::string UnrecognizedDirectiveErrorMessage(
const std::string& directive_name) { … }
void AddContentSecurityPolicyFromHeader(
std::string_view header,
mojom::ContentSecurityPolicyType type,
mojom::ContentSecurityPolicySource source,
const GURL& base_url,
mojom::ContentSecurityPolicyPtr& out) { … }
std::pair<CSPDirectiveName, const mojom::CSPSourceList*> GetSourceList(
CSPDirectiveName directive,
const mojom::ContentSecurityPolicy& policy) { … }
}
CSPCheckResult::CSPCheckResult(bool allowed)
: … { … }
CSPCheckResult& CSPCheckResult::operator&=(const CSPCheckResult& other) { … }
bool CSPCheckResult::operator==(const CSPCheckResult& other) const { … }
operator bool()
CSPCheckResult CSPCheckResult::Allowed() { … }
CSPCheckResult CSPCheckResult::Blocked() { … }
CSPCheckResult CSPCheckResult::AllowedOnlyIfWildcardMatchesWs() { … }
CSPCheckResult CSPCheckResult::AllowedOnlyIfWildcardMatchesFtp() { … }
bool CSPCheckResult::WouldBlockIfWildcardDoesNotMatchWs() const { … }
bool CSPCheckResult::WouldBlockIfWildcardDoesNotMatchFtp() const { … }
bool CSPCheckResult::IsAllowed() const { … }
CSPCheckResult::CSPCheckResult(bool allowed,
bool allowed_if_wildcard_does_not_match_ws,
bool allowed_if_wildcard_does_not_match_ftp)
: … { … }
CSPDirectiveName CSPFallbackDirective(CSPDirectiveName directive,
CSPDirectiveName original_directive) { … }
void AddContentSecurityPolicyFromHeaders(
const net::HttpResponseHeaders& headers,
const GURL& base_url,
std::vector<mojom::ContentSecurityPolicyPtr>* out) { … }
std::vector<mojom::ContentSecurityPolicyPtr> ParseContentSecurityPolicies(
std::string_view header_value,
mojom::ContentSecurityPolicyType type,
mojom::ContentSecurityPolicySource source,
const GURL& base_url) { … }
mojom::AllowCSPFromHeaderValuePtr ParseAllowCSPFromHeader(
const net::HttpResponseHeaders& headers) { … }
bool ParseSource(CSPDirectiveName directive_name,
std::string_view expression,
mojom::CSPSource* csp_source,
std::vector<std::string>& parsing_errors) { … }
CSPCheckResult CheckContentSecurityPolicy(
const mojom::ContentSecurityPolicyPtr& policy,
CSPDirectiveName directive_name,
const GURL& url,
const GURL& url_before_redirects,
bool has_followed_redirect,
bool is_response_check,
CSPContext* context,
const mojom::SourceLocationPtr& source_location,
bool is_form_submission,
bool is_opaque_fenced_frame) { … }
bool ShouldUpgradeInsecureRequest(
const std::vector<mojom::ContentSecurityPolicyPtr>& policies) { … }
bool ShouldTreatAsPublicAddress(
const std::vector<mojom::ContentSecurityPolicyPtr>& policies) { … }
void UpgradeInsecureRequest(GURL* url) { … }
bool IsValidRequiredCSPAttr(
const std::vector<mojom::ContentSecurityPolicyPtr>& policy,
const mojom::ContentSecurityPolicy* context,
std::string& error_message) { … }
bool Subsumes(const mojom::ContentSecurityPolicy& policy_a,
const std::vector<mojom::ContentSecurityPolicyPtr>& policies_b) { … }
std::string ToString(CSPDirectiveName name) { … }
bool AllowCspFromAllowOrigin(
const url::Origin& request_origin,
const network::mojom::AllowCSPFromHeaderValue* allow_csp_from) { … }
bool AllowsBlanketEnforcementOfRequiredCSP(
const url::Origin& request_origin,
const GURL& response_url,
const network::mojom::AllowCSPFromHeaderValue* allow_csp_from,
network::mojom::ContentSecurityPolicyPtr& required_csp) { … }
}