Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect against RenderFrameHost swapping out #376

Merged
merged 1 commit into from
Aug 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions browser/ui/webui/basic_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/browser/web_ui_message_handler.h"
#include "content/public/common/bindings_policy.h"

using content::WebUIMessageHandler;

Expand Down Expand Up @@ -78,3 +79,11 @@ content::RenderViewHost* BasicUI::GetRenderViewHost() {
}
return nullptr;
}

bool BasicUI::IsSafeToSetWebUIProperties() const {
// Allow `web_ui()->CanCallJavascript()` to be false.
// Allow `web_ui()->CanCallJavascript()` to be true if `(web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI) != 0`
// Disallow `web_ui()->CanCallJavascript()` to be true if `(web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI) == 0`
DCHECK(!web_ui()->CanCallJavascript() || (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI));
return web_ui()->CanCallJavascript() && (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI);
}
1 change: 1 addition & 0 deletions browser/ui/webui/basic_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class BasicUI : public content::WebUIController {
~BasicUI() override;

protected:
bool IsSafeToSetWebUIProperties() const;
content::RenderViewHost* GetRenderViewHost();

private:
Expand Down
5 changes: 2 additions & 3 deletions browser/ui/webui/brave_adblock_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/browser/web_ui_message_handler.h"
#include "content/public/common/bindings_policy.h"

BraveAdblockUI::BraveAdblockUI(content::WebUI* web_ui, const std::string& name)
: BasicUI(web_ui, name, kAdblockJS,
Expand Down Expand Up @@ -46,13 +45,13 @@ void BraveAdblockUI::CustomizeWebUIProperties(content::RenderViewHost* render_vi
}

void BraveAdblockUI::RenderFrameCreated(content::RenderFrameHost* render_frame_host) {
if (0 != (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI)) {
if (IsSafeToSetWebUIProperties()) {
CustomizeWebUIProperties(render_frame_host->GetRenderViewHost());
}
}

void BraveAdblockUI::OnPreferenceChanged() {
if (0 != (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI)) {
if (IsSafeToSetWebUIProperties()) {
CustomizeWebUIProperties(GetRenderViewHost());
web_ui()->CallJavascriptFunctionUnsafe("brave_adblock.statsUpdated");
}
Expand Down
5 changes: 2 additions & 3 deletions browser/ui/webui/brave_new_tab_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/browser/web_ui_message_handler.h"
#include "content/public/common/bindings_policy.h"

namespace {
class NewTabDOMHandler : public content::WebUIMessageHandler {
Expand Down Expand Up @@ -89,13 +88,13 @@ void BraveNewTabUI::CustomizeNewTabWebUIProperties(content::RenderViewHost* rend
}

void BraveNewTabUI::RenderFrameCreated(content::RenderFrameHost* render_frame_host) {
if (0 != (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI)) {
if (IsSafeToSetWebUIProperties()) {
CustomizeNewTabWebUIProperties(render_frame_host->GetRenderViewHost());
}
}

void BraveNewTabUI::OnPreferenceChanged() {
if (0 != (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI)) {
if (IsSafeToSetWebUIProperties()) {
CustomizeNewTabWebUIProperties(GetRenderViewHost());
web_ui()->CallJavascriptFunctionUnsafe("brave_new_tab.statsUpdated");
}
Expand Down
6 changes: 2 additions & 4 deletions browser/ui/webui/brave_rewards_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include "chrome/browser/profiles/profile.h"
#include "components/grit/brave_components_resources.h"
#include "content/public/browser/web_ui_message_handler.h"
#include "content/public/common/bindings_policy.h"


using content::WebUIMessageHandler;

Expand Down Expand Up @@ -80,13 +78,13 @@ void RewardsDOMHandler::OnWalletCreated(
}

void RewardsDOMHandler::OnWalletCreated() {
if (0 != (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI)) {
if (web_ui()->CanCallJavascript()) {
web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.walletCreated");
}
}

void RewardsDOMHandler::OnWalletCreateFailed() {
if (0 != (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI)) {
if (web_ui()->CanCallJavascript()) {
web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.walletCreateFailed");
}
}
Expand Down