OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 #include "content/browser/frame_host/ancestor_throttle.h" |
| 6 |
| 7 #include "base/strings/string_util.h" |
| 8 #include "base/strings/stringprintf.h" |
| 9 #include "content/browser/frame_host/frame_tree.h" |
| 10 #include "content/browser/frame_host/frame_tree_node.h" |
| 11 #include "content/browser/frame_host/navigation_handle_impl.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/navigation_handle.h" |
| 14 #include "content/public/browser/navigation_throttle.h" |
| 15 #include "content/public/common/console_message_level.h" |
| 16 #include "net/http/http_response_headers.h" |
| 17 #include "url/origin.h" |
| 18 |
| 19 namespace content { |
| 20 |
| 21 // static |
| 22 std::unique_ptr<NavigationThrottle> AncestorThrottle::MaybeCreateThrottleFor( |
| 23 NavigationHandle* handle) { |
| 24 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 25 |
| 26 if (handle->IsInMainFrame()) |
| 27 return nullptr; |
| 28 |
| 29 return std::unique_ptr<NavigationThrottle>(new AncestorThrottle(handle)); |
| 30 } |
| 31 |
| 32 AncestorThrottle::AncestorThrottle(NavigationHandle* handle) |
| 33 : NavigationThrottle(handle) {} |
| 34 |
| 35 AncestorThrottle::~AncestorThrottle() {} |
| 36 |
| 37 NavigationThrottle::ThrottleCheckResult |
| 38 AncestorThrottle::WillProcessResponse() { |
| 39 DCHECK(!navigation_handle()->IsInMainFrame()); |
| 40 |
| 41 NavigationHandleImpl* handle = |
| 42 static_cast<NavigationHandleImpl*>(navigation_handle()); |
| 43 |
| 44 std::string header_value; |
| 45 HeaderDisposition disposition = |
| 46 ParseHeader(handle->GetResponseHeaders(), &header_value); |
| 47 switch (disposition) { |
| 48 case CONFLICT: |
| 49 ParseError(header_value, disposition); |
| 50 return NavigationThrottle::BLOCK_RESPONSE; |
| 51 |
| 52 case INVALID: |
| 53 ParseError(header_value, disposition); |
| 54 // TODO(mkwst): Consider failing here. |
| 55 return NavigationThrottle::PROCEED; |
| 56 |
| 57 case DENY: |
| 58 ConsoleError(disposition); |
| 59 return NavigationThrottle::BLOCK_RESPONSE; |
| 60 |
| 61 case SAMEORIGIN: { |
| 62 url::Origin current_origin(navigation_handle()->GetURL()); |
| 63 url::Origin top_origin = |
| 64 handle->frame_tree_node()->frame_tree()->root()->current_origin(); |
| 65 if (top_origin.IsSameOriginWith(current_origin)) |
| 66 return NavigationThrottle::PROCEED; |
| 67 ConsoleError(disposition); |
| 68 return NavigationThrottle::BLOCK_RESPONSE; |
| 69 } |
| 70 |
| 71 case NONE: |
| 72 case ALLOWALL: |
| 73 return NavigationThrottle::PROCEED; |
| 74 } |
| 75 NOTREACHED(); |
| 76 return NavigationThrottle::BLOCK_RESPONSE; |
| 77 } |
| 78 |
| 79 void AncestorThrottle::ParseError(const std::string& value, |
| 80 HeaderDisposition disposition) { |
| 81 DCHECK(disposition == CONFLICT || disposition == INVALID); |
| 82 |
| 83 std::string message; |
| 84 if (disposition == CONFLICT) { |
| 85 message = base::StringPrintf( |
| 86 "Refused to display '%s' in a frame because it set multiple " |
| 87 "'X-Frame-Options' headers with conflicting values " |
| 88 "('%s'). Falling back to 'deny'.", |
| 89 navigation_handle()->GetURL().spec().c_str(), value.c_str()); |
| 90 } else { |
| 91 message = base::StringPrintf( |
| 92 "Invalid 'X-Frame-Options' header encountered when loading '%s': " |
| 93 "'%s' is not a recognized directive. The header will be ignored.", |
| 94 navigation_handle()->GetURL().spec().c_str(), value.c_str()); |
| 95 } |
| 96 |
| 97 // Log a console error in the parent of the current RenderFrameHost (as |
| 98 // the current RenderFrameHost itself doesn't yet have a document). |
| 99 navigation_handle()->GetRenderFrameHost()->GetParent()->AddMessageToConsole( |
| 100 CONSOLE_MESSAGE_LEVEL_ERROR, message); |
| 101 } |
| 102 |
| 103 void AncestorThrottle::ConsoleError(HeaderDisposition disposition) { |
| 104 DCHECK(disposition == DENY || disposition == SAMEORIGIN); |
| 105 std::string message = base::StringPrintf( |
| 106 "Refused to display '%s' in a frame because it set 'X-Frame-Options' " |
| 107 "to '%s'.", |
| 108 navigation_handle()->GetURL().spec().c_str(), |
| 109 disposition == DENY ? "deny" : "sameorigin"); |
| 110 |
| 111 // Log a console error in the parent of the current RenderFrameHost (as |
| 112 // the current RenderFrameHost itself doesn't yet have a document). |
| 113 navigation_handle()->GetRenderFrameHost()->GetParent()->AddMessageToConsole( |
| 114 CONSOLE_MESSAGE_LEVEL_ERROR, message); |
| 115 } |
| 116 |
| 117 AncestorThrottle::HeaderDisposition AncestorThrottle::ParseHeader( |
| 118 const net::HttpResponseHeaders* headers, |
| 119 std::string* header_value) { |
| 120 DCHECK(header_value); |
| 121 if (!headers) |
| 122 return NONE; |
| 123 |
| 124 size_t iter = 0; |
| 125 std::string value; |
| 126 HeaderDisposition result = NONE; |
| 127 while (headers->EnumerateHeader(&iter, "x-frame-options", &value)) { |
| 128 HeaderDisposition current = INVALID; |
| 129 |
| 130 base::StringPiece trimmed = |
| 131 base::TrimWhitespaceASCII(value, base::TRIM_ALL); |
| 132 if (!header_value->empty()) |
| 133 header_value->append(", "); |
| 134 header_value->append(trimmed.as_string()); |
| 135 |
| 136 if (base::LowerCaseEqualsASCII(trimmed, "deny")) |
| 137 current = DENY; |
| 138 else if (base::LowerCaseEqualsASCII(trimmed, "allowall")) |
| 139 current = ALLOWALL; |
| 140 else if (base::LowerCaseEqualsASCII(trimmed, "sameorigin")) |
| 141 current = SAMEORIGIN; |
| 142 else |
| 143 current = INVALID; |
| 144 |
| 145 if (result == NONE) |
| 146 result = current; |
| 147 else if (result != current) |
| 148 result = CONFLICT; |
| 149 } |
| 150 return result; |
| 151 } |
| 152 |
| 153 } // namespace content |
OLD | NEW |