Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / child / site_isolation_policy.h
blobf0842889b1b2d47fb971c2b7e576ce5c8a040b17
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_CHILD_SITE_ISOLATION_POLICY_H_
6 #define CONTENT_CHILD_SITE_ISOLATION_POLICY_H_
8 #include <map>
9 #include <utility>
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/linked_ptr.h"
13 #include "base/strings/string_piece.h"
14 #include "content/common/content_export.h"
15 #include "content/public/common/resource_type.h"
16 #include "url/gurl.h"
18 namespace content {
20 struct ResourceResponseInfo;
22 // SiteIsolationPolicy implements the cross-site document blocking policy (XSDP)
23 // for Site Isolation. XSDP will monitor network responses to a renderer and
24 // block illegal responses so that a compromised renderer cannot steal private
25 // information from other sites. For now SiteIsolationPolicy monitors responses
26 // to gather various UMA stats to see the compatibility impact of actual
27 // deployment of the policy. The UMA stat categories SiteIsolationPolicy gathers
28 // are as follows:
30 // SiteIsolation.AllResponses : # of all network responses.
31 // SiteIsolation.XSD.DataLength : the length of the first packet of a response.
32 // SiteIsolation.XSD.MimeType (enum):
33 // # of responses from other sites, tagged with a document mime type.
34 // 0:HTML, 1:XML, 2:JSON, 3:Plain, 4:Others
35 // SiteIsolation.XSD.[%MIMETYPE].Blocked :
36 // blocked # of cross-site document responses grouped by sniffed MIME type.
37 // SiteIsolation.XSD.[%MIMETYPE].Blocked.RenderableStatusCode :
38 // # of responses with renderable status code,
39 // out of SiteIsolation.XSD.[%MIMETYPE].Blocked.
40 // SiteIsolation.XSD.[%MIMETYPE].Blocked.NonRenderableStatusCode :
41 // # of responses with non-renderable status code,
42 // out of SiteIsolation.XSD.[%MIMETYPE].Blocked.
43 // SiteIsolation.XSD.[%MIMETYPE].NoSniffBlocked.RenderableStatusCode :
44 // # of responses failed to be sniffed for its MIME type, but blocked by
45 // "X-Content-Type-Options: nosniff" header, and with renderable status code
46 // out of SiteIsolation.XSD.[%MIMETYPE].Blocked.
47 // SiteIsolation.XSD.[%MIMETYPE].NoSniffBlocked.NonRenderableStatusCode :
48 // # of responses failed to be sniffed for its MIME type, but blocked by
49 // "X-Content-Type-Options: nosniff" header, and with non-renderable status
50 // code out of SiteIsolation.XSD.[%MIMETYPE].Blocked.
51 // SiteIsolation.XSD.[%MIMETYPE].NotBlocked :
52 // # of responses, but not blocked due to failure of mime sniffing.
53 // SiteIsolation.XSD.[%MIMETYPE].NotBlocked.MaybeJS :
54 // # of responses that are plausibly sniffed to be JavaScript.
56 struct SiteIsolationResponseMetaData {
57 enum CanonicalMimeType {
58 HTML = 0,
59 XML = 1,
60 JSON = 2,
61 Plain = 3,
62 Others = 4,
63 MaxCanonicalMimeType,
66 SiteIsolationResponseMetaData();
68 std::string frame_origin;
69 GURL response_url;
70 ResourceType resource_type;
71 CanonicalMimeType canonical_mime_type;
72 int http_status_code;
73 bool no_sniff;
76 class CONTENT_EXPORT SiteIsolationPolicy {
77 public:
78 // Set activation flag for the UMA data collection for this renderer process.
79 static void SetPolicyEnabled(bool enabled);
81 // Returns any bookkeeping data about the HTTP header information for the
82 // request identified by |request_id|. Any data returned should then be
83 // passed to ShouldBlockResponse with the first packet.
84 static linked_ptr<SiteIsolationResponseMetaData> OnReceivedResponse(
85 const GURL& frame_origin,
86 const GURL& response_url,
87 ResourceType resource_type,
88 int origin_pid,
89 const ResourceResponseInfo& info);
91 // Examines the first network packet in case response_url is registered as a
92 // cross-site document by DidReceiveResponse(). In case that this response is
93 // blocked, it returns an alternative data to be sent to the renderer in
94 // |alternative_data|. This records various kinds of UMA data stats. This
95 // function is called only if the length of received data is non-zero.
96 static bool ShouldBlockResponse(
97 linked_ptr<SiteIsolationResponseMetaData>& resp_data, const char* payload,
98 int length, std::string* alternative_data);
100 private:
101 FRIEND_TEST_ALL_PREFIXES(SiteIsolationPolicyTest, IsBlockableScheme);
102 FRIEND_TEST_ALL_PREFIXES(SiteIsolationPolicyTest, IsSameSite);
103 FRIEND_TEST_ALL_PREFIXES(SiteIsolationPolicyTest, IsValidCorsHeaderSet);
104 FRIEND_TEST_ALL_PREFIXES(SiteIsolationPolicyTest, SniffForHTML);
105 FRIEND_TEST_ALL_PREFIXES(SiteIsolationPolicyTest, SniffForXML);
106 FRIEND_TEST_ALL_PREFIXES(SiteIsolationPolicyTest, SniffForJSON);
107 FRIEND_TEST_ALL_PREFIXES(SiteIsolationPolicyTest, SniffForJS);
109 // Returns the representative mime type enum value of the mime type of
110 // response. For example, this returns the same value for all text/xml mime
111 // type families such as application/xml, application/rss+xml.
112 static SiteIsolationResponseMetaData::CanonicalMimeType GetCanonicalMimeType(
113 const std::string& mime_type);
115 // Returns whether this scheme is a target of cross-site document
116 // policy(XSDP). This returns true only for http://* and https://* urls.
117 static bool IsBlockableScheme(const GURL& frame_origin);
119 // Returns whether the two urls belong to the same sites.
120 static bool IsSameSite(const GURL& frame_origin, const GURL& response_url);
122 // Returns whether there's a valid CORS header for frame_origin. This is
123 // simliar to CrossOriginAccessControl::passesAccessControlCheck(), but we use
124 // sites as our security domain, not origins.
125 // TODO(dsjang): this must be improved to be more accurate to the actual CORS
126 // specification. For now, this works conservatively, allowing XSDs that are
127 // not allowed by actual CORS rules by ignoring 1) credentials and 2)
128 // methods. Preflight requests don't matter here since they are not used to
129 // decide whether to block a document or not on the client side.
130 static bool IsValidCorsHeaderSet(const GURL& frame_origin,
131 const GURL& website_origin,
132 const std::string& access_control_origin);
134 static bool SniffForHTML(base::StringPiece data);
135 static bool SniffForXML(base::StringPiece data);
136 static bool SniffForJSON(base::StringPiece data);
138 // TODO(dsjang): this is only needed for collecting UMA stat. Will be deleted
139 // when this class is used for actual blocking.
140 static bool SniffForJS(base::StringPiece data);
142 // Never needs to be constructed/destructed.
143 SiteIsolationPolicy() {}
144 ~SiteIsolationPolicy() {}
146 DISALLOW_COPY_AND_ASSIGN(SiteIsolationPolicy);
149 } // namespace content
151 #endif // CONTENT_CHILD_SITE_ISOLATION_POLICY_H_