This source file includes following definitions.
- IsRenderableStatusCode
- MatchesSignature
- IncrementHistogramCount
- IncrementHistogramEnum
- HistogramCountBlockedResponse
- HistogramCountNotBlockedResponse
- SetPolicyEnabled
- OnReceivedResponse
- ShouldBlockResponse
- GetCanonicalMimeType
- IsBlockableScheme
- IsSameSite
- IsValidCorsHeaderSet
- SniffForHTML
- SniffForXML
- SniffForJSON
- SniffForJS
#include "content/child/site_isolation_policy.h"
#include "base/basictypes.h"
#include "base/command_line.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/strings/string_util.h"
#include "content/public/common/content_switches.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "net/http/http_response_headers.h"
#include "webkit/common/resource_response_info.h"
using base::StringPiece;
namespace content {
namespace {
static bool g_policy_enabled = false;
const char kTextHtml[] = "text/html";
const char kTextXml[] = "text/xml";
const char xAppRssXml[] = "application/rss+xml";
const char kAppXml[] = "application/xml";
const char kAppJson[] = "application/json";
const char kTextJson[] = "text/json";
const char kTextXjson[] = "text/x-json";
const char kTextPlain[] = "text/plain";
bool IsRenderableStatusCode(int status_code) {
const int renderable_status_code[] = {200, 201, 202, 203, 206, 300,
301, 302, 303, 305, 306, 307};
for (size_t i = 0; i < arraysize(renderable_status_code); ++i) {
if (renderable_status_code[i] == status_code)
return true;
}
return false;
}
bool MatchesSignature(StringPiece data,
const StringPiece signatures[],
size_t arr_size) {
size_t offset = data.find_first_not_of(" \t\r\n");
if (offset == base::StringPiece::npos)
return false;
data.remove_prefix(offset);
size_t length = data.length();
for (size_t sig_index = 0; sig_index < arr_size; ++sig_index) {
const StringPiece& signature = signatures[sig_index];
size_t signature_length = signature.length();
if (length < signature_length)
continue;
if (LowerCaseEqualsASCII(
data.begin(), data.begin() + signature_length, signature.data()))
return true;
}
return false;
}
void IncrementHistogramCount(const std::string& name) {
base::HistogramBase* histogram_pointer = base::Histogram::FactoryGet(
name, 1, 100000, 50, base::HistogramBase::kUmaTargetedHistogramFlag);
histogram_pointer->Add(1);
}
void IncrementHistogramEnum(const std::string& name,
uint32 sample,
uint32 boundary_value) {
base::HistogramBase* histogram_pointer = base::LinearHistogram::FactoryGet(
name,
1,
boundary_value,
boundary_value + 1,
base::HistogramBase::kUmaTargetedHistogramFlag);
histogram_pointer->Add(sample);
}
void HistogramCountBlockedResponse(
const std::string& bucket_prefix,
linked_ptr<SiteIsolationResponseMetaData>& resp_data,
bool nosniff_block) {
std::string block_label(nosniff_block ? ".NoSniffBlocked" : ".Blocked");
IncrementHistogramCount(bucket_prefix + block_label);
bool renderable_status_code =
IsRenderableStatusCode(resp_data->http_status_code);
if (renderable_status_code) {
IncrementHistogramEnum(
bucket_prefix + block_label + ".RenderableStatusCode",
resp_data->resource_type,
ResourceType::LAST_TYPE);
} else {
IncrementHistogramCount(bucket_prefix + block_label +
".NonRenderableStatusCode");
}
}
void HistogramCountNotBlockedResponse(const std::string& bucket_prefix,
bool sniffed_as_js) {
IncrementHistogramCount(bucket_prefix + ".NotBlocked");
if (sniffed_as_js)
IncrementHistogramCount(bucket_prefix + ".NotBlocked.MaybeJS");
}
}
SiteIsolationResponseMetaData::SiteIsolationResponseMetaData() {}
void SiteIsolationPolicy::SetPolicyEnabled(bool enabled) {
g_policy_enabled = enabled;
}
linked_ptr<SiteIsolationResponseMetaData>
SiteIsolationPolicy::OnReceivedResponse(
const GURL& frame_origin,
const GURL& response_url,
ResourceType::Type resource_type,
int origin_pid,
const webkit_glue::ResourceResponseInfo& info) {
if (!g_policy_enabled)
return linked_ptr<SiteIsolationResponseMetaData>();
if (origin_pid)
return linked_ptr<SiteIsolationResponseMetaData>();
UMA_HISTOGRAM_COUNTS("SiteIsolation.AllResponses", 1);
if (ResourceType::IsFrame(resource_type))
return linked_ptr<SiteIsolationResponseMetaData>();
if (!IsBlockableScheme(response_url))
return linked_ptr<SiteIsolationResponseMetaData>();
if (IsSameSite(frame_origin, response_url))
return linked_ptr<SiteIsolationResponseMetaData>();
SiteIsolationResponseMetaData::CanonicalMimeType canonical_mime_type =
GetCanonicalMimeType(info.mime_type);
if (canonical_mime_type == SiteIsolationResponseMetaData::Others)
return linked_ptr<SiteIsolationResponseMetaData>();
std::string access_control_origin;
info.headers->EnumerateHeader(
NULL, "access-control-allow-origin", &access_control_origin);
if (IsValidCorsHeaderSet(frame_origin, response_url, access_control_origin))
return linked_ptr<SiteIsolationResponseMetaData>();
std::string no_sniff;
info.headers->EnumerateHeader(NULL, "x-content-type-options", &no_sniff);
linked_ptr<SiteIsolationResponseMetaData> resp_data(
new SiteIsolationResponseMetaData);
resp_data->frame_origin = frame_origin.spec();
resp_data->response_url = response_url;
resp_data->resource_type = resource_type;
resp_data->canonical_mime_type = canonical_mime_type;
resp_data->http_status_code = info.headers->response_code();
resp_data->no_sniff = LowerCaseEqualsASCII(no_sniff, "nosniff");
return resp_data;
}
bool SiteIsolationPolicy::ShouldBlockResponse(
linked_ptr<SiteIsolationResponseMetaData>& resp_data,
const char* raw_data,
int raw_length,
std::string* alternative_data) {
if (!g_policy_enabled)
return false;
DCHECK(resp_data.get());
StringPiece data(raw_data, raw_length);
UMA_HISTOGRAM_COUNTS("SiteIsolation.XSD.DataLength", raw_length);
UMA_HISTOGRAM_ENUMERATION(
"SiteIsolation.XSD.MimeType",
resp_data->canonical_mime_type,
SiteIsolationResponseMetaData::MaxCanonicalMimeType);
bool is_blocked = false;
bool sniffed_as_js = SniffForJS(data);
if (resp_data->canonical_mime_type !=
SiteIsolationResponseMetaData::Plain) {
std::string bucket_prefix;
bool sniffed_as_target_document = false;
if (resp_data->canonical_mime_type ==
SiteIsolationResponseMetaData::HTML) {
bucket_prefix = "SiteIsolation.XSD.HTML";
sniffed_as_target_document = SniffForHTML(data);
} else if (resp_data->canonical_mime_type ==
SiteIsolationResponseMetaData::XML) {
bucket_prefix = "SiteIsolation.XSD.XML";
sniffed_as_target_document = SniffForXML(data);
} else if (resp_data->canonical_mime_type ==
SiteIsolationResponseMetaData::JSON) {
bucket_prefix = "SiteIsolation.XSD.JSON";
sniffed_as_target_document = SniffForJSON(data);
} else {
NOTREACHED() << "Not a blockable mime type: "
<< resp_data->canonical_mime_type;
}
if (sniffed_as_target_document) {
is_blocked = true;
HistogramCountBlockedResponse(bucket_prefix, resp_data, false);
} else {
if (resp_data->no_sniff) {
is_blocked = true;
HistogramCountBlockedResponse(bucket_prefix, resp_data, true);
} else {
HistogramCountNotBlockedResponse(bucket_prefix, sniffed_as_js);
}
}
} else {
std::string bucket_prefix;
if (SniffForHTML(data))
bucket_prefix = "SiteIsolation.XSD.Plain.HTML";
else if (SniffForXML(data))
bucket_prefix = "SiteIsolation.XSD.Plain.XML";
else if (SniffForJSON(data))
bucket_prefix = "SiteIsolation.XSD.Plain.JSON";
if (bucket_prefix.size() > 0) {
is_blocked = true;
HistogramCountBlockedResponse(bucket_prefix, resp_data, false);
} else if (resp_data->no_sniff) {
is_blocked = true;
HistogramCountBlockedResponse("SiteIsolation.XSD.Plain", resp_data, true);
} else {
HistogramCountNotBlockedResponse("SiteIsolation.XSD.Plain",
sniffed_as_js);
}
}
if (!CommandLine::ForCurrentProcess()->HasSwitch(
switches::kBlockCrossSiteDocuments))
is_blocked = false;
if (is_blocked) {
alternative_data->erase();
alternative_data->insert(0, " ");
LOG(ERROR) << resp_data->response_url
<< " is blocked as an illegal cross-site document from "
<< resp_data->frame_origin;
}
return is_blocked;
}
SiteIsolationResponseMetaData::CanonicalMimeType
SiteIsolationPolicy::GetCanonicalMimeType(const std::string& mime_type) {
if (LowerCaseEqualsASCII(mime_type, kTextHtml)) {
return SiteIsolationResponseMetaData::HTML;
}
if (LowerCaseEqualsASCII(mime_type, kTextPlain)) {
return SiteIsolationResponseMetaData::Plain;
}
if (LowerCaseEqualsASCII(mime_type, kAppJson) ||
LowerCaseEqualsASCII(mime_type, kTextJson) ||
LowerCaseEqualsASCII(mime_type, kTextXjson)) {
return SiteIsolationResponseMetaData::JSON;
}
if (LowerCaseEqualsASCII(mime_type, kTextXml) ||
LowerCaseEqualsASCII(mime_type, xAppRssXml) ||
LowerCaseEqualsASCII(mime_type, kAppXml)) {
return SiteIsolationResponseMetaData::XML;
}
return SiteIsolationResponseMetaData::Others;
}
bool SiteIsolationPolicy::IsBlockableScheme(const GURL& url) {
return url.SchemeIs("http") || url.SchemeIs("https");
}
bool SiteIsolationPolicy::IsSameSite(const GURL& frame_origin,
const GURL& response_url) {
if (!frame_origin.is_valid() || !response_url.is_valid())
return false;
if (frame_origin.scheme() != response_url.scheme())
return false;
return net::registry_controlled_domains::SameDomainOrHost(
frame_origin,
response_url,
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
}
bool SiteIsolationPolicy::IsValidCorsHeaderSet(
const GURL& frame_origin,
const GURL& website_origin,
const std::string& access_control_origin) {
if (access_control_origin == "*")
return true;
GURL cors_origin(access_control_origin);
return IsSameSite(frame_origin, cors_origin);
}
bool SiteIsolationPolicy::SniffForHTML(StringPiece data) {
static const StringPiece kHtmlSignatures[] = {
StringPiece("<!DOCTYPE html"),
StringPiece("<script"),
StringPiece("<html"),
StringPiece("<head"),
StringPiece("<iframe"),
StringPiece("<h1"),
StringPiece("<div"),
StringPiece("<font"),
StringPiece("<table"),
StringPiece("<a"),
StringPiece("<style"),
StringPiece("<title"),
StringPiece("<b"),
StringPiece("<body"),
StringPiece("<br"),
StringPiece("<p"),
StringPiece("<?xml")
};
while (data.length() > 0) {
if (MatchesSignature(
data, kHtmlSignatures, arraysize(kHtmlSignatures)))
return true;
static const StringPiece kCommentBegins[] = { StringPiece("<!--") };
if (!MatchesSignature(data, kCommentBegins, arraysize(kCommentBegins)))
break;
static const char kEndComment[] = "-->";
size_t offset = data.find(kEndComment);
if (offset == base::StringPiece::npos)
break;
data.remove_prefix(offset + strlen(kEndComment));
}
return false;
}
bool SiteIsolationPolicy::SniffForXML(base::StringPiece data) {
static const StringPiece kXmlSignatures[] = { StringPiece("<?xml") };
return MatchesSignature(data, kXmlSignatures, arraysize(kXmlSignatures));
}
bool SiteIsolationPolicy::SniffForJSON(base::StringPiece data) {
enum {
kStartState,
kLeftBraceState,
kLeftQuoteState,
kColonState,
kTerminalState,
} state = kStartState;
size_t length = data.length();
for (size_t i = 0; i < length && state < kColonState; ++i) {
const char c = data[i];
if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
continue;
switch (state) {
case kStartState:
if (c == '{')
state = kLeftBraceState;
else
state = kTerminalState;
break;
case kLeftBraceState:
if (c == '\"' || c == '\'')
state = kLeftQuoteState;
else
state = kTerminalState;
break;
case kLeftQuoteState:
if (c == ':')
state = kColonState;
break;
case kColonState:
case kTerminalState:
NOTREACHED();
break;
}
}
return state == kColonState;
}
bool SiteIsolationPolicy::SniffForJS(StringPiece data) {
return data.find("var ") != base::StringPiece::npos;
}
}