| 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 the | |
| 85 // request identified by |request_id|. Any data returned should then be | |
| 86 // passed to OnReceivedFirstChunk() 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 private: | |
| 103 FRIEND_TEST_ALL_PREFIXES(SiteIsolationStatsGathererTest, SniffForJS); | |
| 104 | |
| 105 SiteIsolationStatsGatherer(); // Not instantiable. | |
| 106 | |
| 107 // Imprecise JS sniffing; only appropriate for collecting UMA stat. | |
| 108 static bool SniffForJS(base::StringPiece data); | |
| 109 | |
| 110 DISALLOW_COPY_AND_ASSIGN(SiteIsolationStatsGatherer); | |
| 111 }; | |
| 112 | |
| 113 // TODO(nick): Move this class into its own file. | |
| 114 class CONTENT_EXPORT CrossSiteDocumentClassifier { | |
| 115 public: | |
| 116 // Returns the representative mime type enum value of the mime type of | |
| 117 // response. For example, this returns the same value for all text/xml mime | |
| 118 // type families such as application/xml, application/rss+xml. | |
| 119 static CrossSiteDocumentMimeType GetCanonicalMimeType( | |
| 120 const std::string& mime_type); | |
| 121 | |
| 122 // Returns whether this scheme is a target of cross-site document | |
| 123 // policy(XSDP). This returns true only for http://* and https://* urls. | |
| 124 static bool IsBlockableScheme(const GURL& frame_origin); | |
| 125 | |
| 126 // Returns whether the two urls belong to the same sites. | |
| 127 static bool IsSameSite(const GURL& frame_origin, const GURL& response_url); | |
| 128 | |
| 129 // Returns whether there's a valid CORS header for frame_origin. This is | |
| 130 // simliar to CrossOriginAccessControl::passesAccessControlCheck(), but we use | |
| 131 // sites as our security domain, not origins. | |
| 132 // TODO(dsjang): this must be improved to be more accurate to the actual CORS | |
| 133 // specification. For now, this works conservatively, allowing XSDs that are | |
| 134 // not allowed by actual CORS rules by ignoring 1) credentials and 2) | |
| 135 // methods. Preflight requests don't matter here since they are not used to | |
| 136 // decide whether to block a document or not on the client side. | |
| 137 static bool IsValidCorsHeaderSet(const GURL& frame_origin, | |
| 138 const GURL& website_origin, | |
| 139 const std::string& access_control_origin); | |
| 140 | |
| 141 static bool SniffForHTML(base::StringPiece data); | |
| 142 static bool SniffForXML(base::StringPiece data); | |
| 143 static bool SniffForJSON(base::StringPiece data); | |
| 144 | |
| 145 private: | |
| 146 CrossSiteDocumentClassifier(); // Not instantiable. | |
| 147 | |
| 148 DISALLOW_COPY_AND_ASSIGN(CrossSiteDocumentClassifier); | |
| 149 }; | |
| 150 | |
| 151 } // namespace content | |
| 152 | |
| 153 #endif // CONTENT_CHILD_SITE_ISOLATION_POLICY_H_ | |
| OLD | NEW |