OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CONTENT_CHILD_SITE_ISOLATION_POLICY_H_ | |
6 #define CONTENT_CHILD_SITE_ISOLATION_POLICY_H_ | |
7 | |
8 #include <map> | |
9 #include <utility> | |
10 | |
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" | |
17 | |
18 namespace content { | |
19 | |
20 struct ResourceResponseInfo; | |
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 | |
28 // see the compatibility impact of actual deployment of the policy. The UMA stat | |
29 // categories SiteIsolationStatsGatherer gathers are as follows: | |
30 // | |
31 // SiteIsolation.AllResponses : # of all network responses. | |
32 // SiteIsolation.XSD.DataLength : the length of the first packet of a response. | |
33 // SiteIsolation.XSD.MimeType (enum): | |
34 // # of responses from other sites, tagged with a document mime type. | |
35 // 0:HTML, 1:XML, 2:JSON, 3:Plain, 4:Others | |
36 // SiteIsolation.XSD.[%MIMETYPE].Blocked : | |
37 // blocked # of cross-site document responses grouped by sniffed MIME type. | |
38 // SiteIsolation.XSD.[%MIMETYPE].Blocked.RenderableStatusCode : | |
39 // # of responses with renderable status code, | |
40 // out of SiteIsolation.XSD.[%MIMETYPE].Blocked. | |
41 // SiteIsolation.XSD.[%MIMETYPE].Blocked.NonRenderableStatusCode : | |
42 // # of responses with non-renderable status code, | |
43 // out of SiteIsolation.XSD.[%MIMETYPE].Blocked. | |
44 // SiteIsolation.XSD.[%MIMETYPE].NoSniffBlocked.RenderableStatusCode : | |
45 // # of responses failed to be sniffed for its MIME type, but blocked by | |
46 // "X-Content-Type-Options: nosniff" header, and with renderable status code | |
47 // out of SiteIsolation.XSD.[%MIMETYPE].Blocked. | |
48 // SiteIsolation.XSD.[%MIMETYPE].NoSniffBlocked.NonRenderableStatusCode : | |
49 // # 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 | |
51 // code out of SiteIsolation.XSD.[%MIMETYPE].Blocked. | |
52 // SiteIsolation.XSD.[%MIMETYPE].NotBlocked : | |
53 // # of responses, but not blocked due to failure of mime sniffing. | |
54 // SiteIsolation.XSD.[%MIMETYPE].NotBlocked.MaybeJS : | |
55 // # of responses that are plausibly sniffed to be JavaScript. | |
56 | |
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 { | |
68 SiteIsolationResponseMetaData(); | |
69 | |
70 std::string frame_origin; | |
71 GURL response_url; | |
72 ResourceType resource_type; | |
73 CrossSiteDocumentMimeType canonical_mime_type; | |
74 int http_status_code; | |
75 bool no_sniff; | |
76 }; | |
77 | |
78 // TODO(nick): Move this class into its own file. | |
79 class CONTENT_EXPORT SiteIsolationStatsGatherer { | |
80 public: | |
81 // Set activation flag for the UMA data collection for this renderer process. | |
82 static void SetEnabled(bool enabled); | |
83 | |
84 // Returns any bookkeeping data about the HTTP header information for a | |
85 // request. Any data returned should then be passed to OnReceivedFirstChunk() | |
86 // with the first data chunk. | |
87 static linked_ptr<SiteIsolationResponseMetaData> OnReceivedResponse( | |
88 const GURL& frame_origin, | |
89 const GURL& response_url, | |
90 ResourceType resource_type, | |
91 int origin_pid, | |
92 const ResourceResponseInfo& info); | |
93 | |
94 // Examines the first chunk of network data in case response_url is registered | |
95 // 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 | |
97 // received data is non-zero. | |
98 static bool OnReceivedFirstChunk( | |
99 const linked_ptr<SiteIsolationResponseMetaData>& resp_data, | |
100 const char* payload, | |
101 int length); | |
102 | |
103 private: | |
104 FRIEND_TEST_ALL_PREFIXES(SiteIsolationStatsGathererTest, SniffForJS); | |
105 | |
106 SiteIsolationStatsGatherer(); // Not instantiable. | |
107 | |
108 // Imprecise JS sniffing; only appropriate for collecting UMA stat. | |
109 static bool SniffForJS(base::StringPiece data); | |
110 | |
111 DISALLOW_COPY_AND_ASSIGN(SiteIsolationStatsGatherer); | |
112 }; | |
113 | |
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 | |
153 | |
154 #endif // CONTENT_CHILD_SITE_ISOLATION_POLICY_H_ | |
OLD | NEW |