OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_CHILD_SITE_ISOLATION_POLICY_H_ | 5 #ifndef CONTENT_CHILD_SITE_ISOLATION_STATS_GATHERER_H_ |
6 #define CONTENT_CHILD_SITE_ISOLATION_POLICY_H_ | 6 #define CONTENT_CHILD_SITE_ISOLATION_STATS_GATHERER_H_ |
7 | 7 |
8 #include <map> | 8 #include <string> |
9 #include <utility> | |
10 | 9 |
11 #include "base/gtest_prod_util.h" | 10 #include "base/gtest_prod_util.h" |
12 #include "base/memory/linked_ptr.h" | 11 #include "base/memory/linked_ptr.h" |
13 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
14 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
| 14 #include "content/common/cross_site_document_classifier.h" |
15 #include "content/public/common/resource_type.h" | 15 #include "content/public/common/resource_type.h" |
16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
17 | 17 |
18 namespace content { | 18 namespace content { |
19 | 19 |
20 struct ResourceResponseInfo; | 20 struct ResourceResponseInfo; |
21 | 21 |
22 // CrossSiteDocumentClassifier implements the cross-site document blocking | |
23 // policy (XSDP) for Site Isolation. XSDP will monitor network responses to a | |
24 // renderer and block illegal responses so that a compromised renderer cannot | |
25 // steal private information from other sites. | |
26 // | |
27 // SiteIsolationStatsGatherer monitors responses to gather various UMA stats to | 22 // SiteIsolationStatsGatherer monitors responses to gather various UMA stats to |
28 // see the compatibility impact of actual deployment of the policy. The UMA stat | 23 // see the compatibility impact of actual deployment of the policy. The UMA stat |
29 // categories SiteIsolationStatsGatherer gathers are as follows: | 24 // categories SiteIsolationStatsGatherer gathers are as follows: |
30 // | 25 // |
31 // SiteIsolation.AllResponses : # of all network responses. | 26 // SiteIsolation.AllResponses : # of all network responses. |
32 // SiteIsolation.XSD.DataLength : the length of the first packet of a response. | 27 // SiteIsolation.XSD.DataLength : the length of the first packet of a response. |
33 // SiteIsolation.XSD.MimeType (enum): | 28 // SiteIsolation.XSD.MimeType (enum): |
34 // # of responses from other sites, tagged with a document mime type. | 29 // # of responses from other sites, tagged with a document mime type. |
35 // 0:HTML, 1:XML, 2:JSON, 3:Plain, 4:Others | 30 // 0:HTML, 1:XML, 2:JSON, 3:Plain, 4:Others |
36 // SiteIsolation.XSD.[%MIMETYPE].Blocked : | 31 // SiteIsolation.XSD.[%MIMETYPE].Blocked : |
(...skipping 10 matching lines...) Expand all Loading... |
47 // out of SiteIsolation.XSD.[%MIMETYPE].Blocked. | 42 // out of SiteIsolation.XSD.[%MIMETYPE].Blocked. |
48 // SiteIsolation.XSD.[%MIMETYPE].NoSniffBlocked.NonRenderableStatusCode : | 43 // SiteIsolation.XSD.[%MIMETYPE].NoSniffBlocked.NonRenderableStatusCode : |
49 // # of responses failed to be sniffed for its MIME type, but blocked by | 44 // # of responses failed to be sniffed for its MIME type, but blocked by |
50 // "X-Content-Type-Options: nosniff" header, and with non-renderable status | 45 // "X-Content-Type-Options: nosniff" header, and with non-renderable status |
51 // code out of SiteIsolation.XSD.[%MIMETYPE].Blocked. | 46 // code out of SiteIsolation.XSD.[%MIMETYPE].Blocked. |
52 // SiteIsolation.XSD.[%MIMETYPE].NotBlocked : | 47 // SiteIsolation.XSD.[%MIMETYPE].NotBlocked : |
53 // # of responses, but not blocked due to failure of mime sniffing. | 48 // # of responses, but not blocked due to failure of mime sniffing. |
54 // SiteIsolation.XSD.[%MIMETYPE].NotBlocked.MaybeJS : | 49 // SiteIsolation.XSD.[%MIMETYPE].NotBlocked.MaybeJS : |
55 // # of responses that are plausibly sniffed to be JavaScript. | 50 // # of responses that are plausibly sniffed to be JavaScript. |
56 | 51 |
57 enum CrossSiteDocumentMimeType { | |
58 // Note that these values are used in histograms, and must not change. | |
59 CROSS_SITE_DOCUMENT_MIME_TYPE_HTML = 0, | |
60 CROSS_SITE_DOCUMENT_MIME_TYPE_XML = 1, | |
61 CROSS_SITE_DOCUMENT_MIME_TYPE_JSON = 2, | |
62 CROSS_SITE_DOCUMENT_MIME_TYPE_PLAIN = 3, | |
63 CROSS_SITE_DOCUMENT_MIME_TYPE_OTHERS = 4, | |
64 CROSS_SITE_DOCUMENT_MIME_TYPE_MAX, | |
65 }; | |
66 | |
67 struct SiteIsolationResponseMetaData { | 52 struct SiteIsolationResponseMetaData { |
68 SiteIsolationResponseMetaData(); | 53 SiteIsolationResponseMetaData(); |
69 | 54 |
70 std::string frame_origin; | 55 std::string frame_origin; |
71 GURL response_url; | 56 GURL response_url; |
72 ResourceType resource_type; | 57 ResourceType resource_type; |
73 CrossSiteDocumentMimeType canonical_mime_type; | 58 CrossSiteDocumentMimeType canonical_mime_type; |
74 int http_status_code; | 59 int http_status_code; |
75 bool no_sniff; | 60 bool no_sniff; |
76 }; | 61 }; |
77 | 62 |
78 // TODO(nick): Move this class into its own file. | |
79 class CONTENT_EXPORT SiteIsolationStatsGatherer { | 63 class CONTENT_EXPORT SiteIsolationStatsGatherer { |
80 public: | 64 public: |
81 // Set activation flag for the UMA data collection for this renderer process. | 65 // Set activation flag for the UMA data collection for this renderer process. |
82 static void SetEnabled(bool enabled); | 66 static void SetEnabled(bool enabled); |
83 | 67 |
84 // Returns any bookkeeping data about the HTTP header information for a | 68 // Returns any bookkeeping data about the HTTP header information for the |
85 // request. Any data returned should then be passed to OnReceivedFirstChunk() | 69 // request identified by |request_id|. Any data returned should then be |
86 // with the first data chunk. | 70 // passed to OnReceivedFirstChunk() with the first data chunk. |
87 static linked_ptr<SiteIsolationResponseMetaData> OnReceivedResponse( | 71 static linked_ptr<SiteIsolationResponseMetaData> OnReceivedResponse( |
88 const GURL& frame_origin, | 72 const GURL& frame_origin, |
89 const GURL& response_url, | 73 const GURL& response_url, |
90 ResourceType resource_type, | 74 ResourceType resource_type, |
91 int origin_pid, | 75 int origin_pid, |
92 const ResourceResponseInfo& info); | 76 const ResourceResponseInfo& info); |
93 | 77 |
94 // Examines the first chunk of network data in case response_url is registered | 78 // Examines the first chunk of network data in case response_url is registered |
95 // as a cross-site document by OnReceivedResponse(). This records various | 79 // as a cross-site document by OnReceivedResponse(). This records various |
96 // kinds of UMA data stats. This function is called only if the length of | 80 // kinds of UMA data stats. This function is called only if the length of |
97 // received data is non-zero. | 81 // received data is non-zero. |
98 static bool OnReceivedFirstChunk( | 82 static bool OnReceivedFirstChunk( |
99 const linked_ptr<SiteIsolationResponseMetaData>& resp_data, | 83 const linked_ptr<SiteIsolationResponseMetaData>& resp_data, |
100 const char* payload, | 84 const char* payload, |
101 int length); | 85 int length); |
102 | 86 |
103 private: | 87 private: |
104 FRIEND_TEST_ALL_PREFIXES(SiteIsolationStatsGathererTest, SniffForJS); | 88 FRIEND_TEST_ALL_PREFIXES(SiteIsolationStatsGathererTest, SniffForJS); |
105 | 89 |
106 SiteIsolationStatsGatherer(); // Not instantiable. | 90 SiteIsolationStatsGatherer(); // Not instantiable. |
107 | 91 |
108 // Imprecise JS sniffing; only appropriate for collecting UMA stat. | 92 // Imprecise JS sniffing; only appropriate for collecting UMA stat. |
109 static bool SniffForJS(base::StringPiece data); | 93 static bool SniffForJS(base::StringPiece data); |
110 | 94 |
111 DISALLOW_COPY_AND_ASSIGN(SiteIsolationStatsGatherer); | 95 DISALLOW_COPY_AND_ASSIGN(SiteIsolationStatsGatherer); |
112 }; | 96 }; |
113 | 97 |
114 // TODO(nick): Move this class into its own file. | |
115 class CONTENT_EXPORT CrossSiteDocumentClassifier { | |
116 public: | |
117 // Returns the representative mime type enum value of the mime type of | |
118 // response. For example, this returns the same value for all text/xml mime | |
119 // type families such as application/xml, application/rss+xml. | |
120 static CrossSiteDocumentMimeType GetCanonicalMimeType( | |
121 const std::string& mime_type); | |
122 | |
123 // Returns whether this scheme is a target of cross-site document | |
124 // policy(XSDP). This returns true only for http://* and https://* urls. | |
125 static bool IsBlockableScheme(const GURL& frame_origin); | |
126 | |
127 // Returns whether the two urls belong to the same sites. | |
128 static bool IsSameSite(const GURL& frame_origin, const GURL& response_url); | |
129 | |
130 // Returns whether there's a valid CORS header for frame_origin. This is | |
131 // simliar to CrossOriginAccessControl::passesAccessControlCheck(), but we use | |
132 // sites as our security domain, not origins. | |
133 // TODO(dsjang): this must be improved to be more accurate to the actual CORS | |
134 // specification. For now, this works conservatively, allowing XSDs that are | |
135 // not allowed by actual CORS rules by ignoring 1) credentials and 2) | |
136 // methods. Preflight requests don't matter here since they are not used to | |
137 // decide whether to block a document or not on the client side. | |
138 static bool IsValidCorsHeaderSet(const GURL& frame_origin, | |
139 const GURL& website_origin, | |
140 const std::string& access_control_origin); | |
141 | |
142 static bool SniffForHTML(base::StringPiece data); | |
143 static bool SniffForXML(base::StringPiece data); | |
144 static bool SniffForJSON(base::StringPiece data); | |
145 | |
146 private: | |
147 CrossSiteDocumentClassifier(); // Not instantiable. | |
148 | |
149 DISALLOW_COPY_AND_ASSIGN(CrossSiteDocumentClassifier); | |
150 }; | |
151 | |
152 } // namespace content | 98 } // namespace content |
153 | 99 |
154 #endif // CONTENT_CHILD_SITE_ISOLATION_POLICY_H_ | 100 #endif // CONTENT_CHILD_SITE_ISOLATION_STATS_GATHERER_H_ |
OLD | NEW |