blob: b86769a8bca692f3453622b97bc44d0ccc3ac19d [file] [log] [blame]
nicke7cd12a2015-06-17 06:48:381// 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 Anforowicz83e1435d2018-01-08 16:10:508#include <string>
9
avia9aa7a82015-12-25 03:06:3110#include "base/macros.h"
Lukasz Anforowicz83e1435d2018-01-08 16:10:5011#include "base/strings/string_piece_forward.h"
nicke7cd12a2015-06-17 06:48:3812#include "content/common/content_export.h"
13#include "url/gurl.h"
csharrison3b8662f2017-01-06 04:46:5914#include "url/origin.h"
nicke7cd12a2015-06-17 06:48:3815
16namespace 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
23enum 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
33class CONTENT_EXPORT CrossSiteDocumentClassifier {
34 public:
Nick Carter4e4a85fe2017-12-19 07:17:1535 // 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
nicke7cd12a2015-06-17 06:48:3848 // 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 Anforowicz83e1435d2018-01-08 16:10:5052 base::StringPiece mime_type);
nicke7cd12a2015-06-17 06:48:3853
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
nicke7cd12a2015-06-17 06:48:3858 // 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.
csharrison3b8662f2017-01-06 04:46:5966 static bool IsValidCorsHeaderSet(const url::Origin& frame_origin,
nicke7cd12a2015-06-17 06:48:3867 const std::string& access_control_origin);
68
Nick Carter4e4a85fe2017-12-19 07:17:1569 static Result SniffForHTML(base::StringPiece data);
70 static Result SniffForXML(base::StringPiece data);
71 static Result SniffForJSON(base::StringPiece data);
nicke7cd12a2015-06-17 06:48:3872
Nick Carterb569e4a2017-12-21 04:40:3873 // 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
nicke7cd12a2015-06-17 06:48:3879 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_