nick | e7cd12a | 2015-06-17 06:48:38 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CONTENT_COMMON_CROSS_SITE_DOCUMENT_CLASSIFIER_H_ |
| 6 | #define CONTENT_COMMON_CROSS_SITE_DOCUMENT_CLASSIFIER_H_ |
| 7 | |
Lukasz Anforowicz | 83e1435d | 2018-01-08 16:10:50 | [diff] [blame] | 8 | #include <string> |
| 9 | |
avi | a9aa7a8 | 2015-12-25 03:06:31 | [diff] [blame] | 10 | #include "base/macros.h" |
Lukasz Anforowicz | 83e1435d | 2018-01-08 16:10:50 | [diff] [blame] | 11 | #include "base/strings/string_piece_forward.h" |
nick | e7cd12a | 2015-06-17 06:48:38 | [diff] [blame] | 12 | #include "content/common/content_export.h" |
| 13 | #include "url/gurl.h" |
csharrison | 3b8662f | 2017-01-06 04:46:59 | [diff] [blame] | 14 | #include "url/origin.h" |
nick | e7cd12a | 2015-06-17 06:48:38 | [diff] [blame] | 15 | |
| 16 | namespace content { |
| 17 | |
| 18 | // CrossSiteDocumentClassifier implements the cross-site document blocking |
| 19 | // policy (XSDP) for Site Isolation. XSDP will monitor network responses to a |
| 20 | // renderer and block illegal responses so that a compromised renderer cannot |
| 21 | // steal private information from other sites. |
| 22 | |
| 23 | enum CrossSiteDocumentMimeType { |
| 24 | // Note that these values are used in histograms, and must not change. |
| 25 | CROSS_SITE_DOCUMENT_MIME_TYPE_HTML = 0, |
| 26 | CROSS_SITE_DOCUMENT_MIME_TYPE_XML = 1, |
| 27 | CROSS_SITE_DOCUMENT_MIME_TYPE_JSON = 2, |
| 28 | CROSS_SITE_DOCUMENT_MIME_TYPE_PLAIN = 3, |
| 29 | CROSS_SITE_DOCUMENT_MIME_TYPE_OTHERS = 4, |
| 30 | CROSS_SITE_DOCUMENT_MIME_TYPE_MAX, |
| 31 | }; |
| 32 | |
| 33 | class CONTENT_EXPORT CrossSiteDocumentClassifier { |
| 34 | public: |
Nick Carter | 4e4a85fe | 2017-12-19 07:17:15 | [diff] [blame] | 35 | // Three conclusions are possible from sniffing a byte sequence: |
| 36 | // - No: meaning that the data definitively doesn't match the indicated type. |
| 37 | // - Yes: meaning that the data definitive does match the indicated type. |
| 38 | // - Maybe: meaning that if more bytes are appended to the stream, it's |
| 39 | // possible to get a Yes result. For example, if we are sniffing for a tag |
| 40 | // like "<html", a kMaybe result would occur if the data contains just |
| 41 | // "<ht". |
| 42 | enum Result { |
| 43 | kNo, |
| 44 | kMaybe, |
| 45 | kYes, |
| 46 | }; |
| 47 | |
nick | e7cd12a | 2015-06-17 06:48:38 | [diff] [blame] | 48 | // Returns the representative mime type enum value of the mime type of |
| 49 | // response. For example, this returns the same value for all text/xml mime |
| 50 | // type families such as application/xml, application/rss+xml. |
| 51 | static CrossSiteDocumentMimeType GetCanonicalMimeType( |
Lukasz Anforowicz | 83e1435d | 2018-01-08 16:10:50 | [diff] [blame] | 52 | base::StringPiece mime_type); |
nick | e7cd12a | 2015-06-17 06:48:38 | [diff] [blame] | 53 | |
| 54 | // Returns whether this scheme is a target of cross-site document |
| 55 | // policy(XSDP). This returns true only for http://* and https://* urls. |
| 56 | static bool IsBlockableScheme(const GURL& frame_origin); |
| 57 | |
nick | e7cd12a | 2015-06-17 06:48:38 | [diff] [blame] | 58 | // Returns whether there's a valid CORS header for frame_origin. This is |
| 59 | // simliar to CrossOriginAccessControl::passesAccessControlCheck(), but we use |
| 60 | // sites as our security domain, not origins. |
| 61 | // TODO(dsjang): this must be improved to be more accurate to the actual CORS |
| 62 | // specification. For now, this works conservatively, allowing XSDs that are |
| 63 | // not allowed by actual CORS rules by ignoring 1) credentials and 2) |
| 64 | // methods. Preflight requests don't matter here since they are not used to |
| 65 | // decide whether to block a document or not on the client side. |
csharrison | 3b8662f | 2017-01-06 04:46:59 | [diff] [blame] | 66 | static bool IsValidCorsHeaderSet(const url::Origin& frame_origin, |
nick | e7cd12a | 2015-06-17 06:48:38 | [diff] [blame] | 67 | const std::string& access_control_origin); |
| 68 | |
Nick Carter | 4e4a85fe | 2017-12-19 07:17:15 | [diff] [blame] | 69 | static Result SniffForHTML(base::StringPiece data); |
| 70 | static Result SniffForXML(base::StringPiece data); |
| 71 | static Result SniffForJSON(base::StringPiece data); |
nick | e7cd12a | 2015-06-17 06:48:38 | [diff] [blame] | 72 | |
Nick Carter | b569e4a | 2017-12-21 04:40:38 | [diff] [blame] | 73 | // Sniff for patterns that indicate |data| only ought to be consumed by XHR() |
| 74 | // or fetch(). This detects Javascript parser-breaker and particular JS |
| 75 | // infinite-loop patterns, which are used conventionally as a defense against |
| 76 | // JSON data exfiltration by means of a <script> tag. |
| 77 | static Result SniffForFetchOnlyResource(base::StringPiece data); |
| 78 | |
nick | e7cd12a | 2015-06-17 06:48:38 | [diff] [blame] | 79 | private: |
| 80 | CrossSiteDocumentClassifier(); // Not instantiable. |
| 81 | |
| 82 | DISALLOW_COPY_AND_ASSIGN(CrossSiteDocumentClassifier); |
| 83 | }; |
| 84 | |
| 85 | } // namespace content |
| 86 | |
| 87 | #endif // CONTENT_COMMON_CROSS_SITE_DOCUMENT_CLASSIFIER_H_ |