-
Notifications
You must be signed in to change notification settings - Fork 896
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add WebUI Untrusted Frame Validation
- Loading branch information
Showing
20 changed files
with
616 additions
and
575 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) 2025 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
#ifndef BRAVE_CHROMIUM_SRC_IOS_WEB_PUBLIC_WEB_STATE_H_ | ||
#define BRAVE_CHROMIUM_SRC_IOS_WEB_PUBLIC_WEB_STATE_H_ | ||
|
||
#define callbacks_ \ | ||
callbacks_; \ | ||
\ | ||
public: \ | ||
template <typename Interface> \ | ||
void AddUntrustedInterface( \ | ||
const GURL& url, \ | ||
base::RepeatingCallback<void(mojo::PendingReceiver<Interface>)> \ | ||
callback) { \ | ||
CHECK(!url.is_empty()); \ | ||
untrusted_callbacks_[url].emplace( \ | ||
std::string(Interface::Name_), \ | ||
base::BindRepeating(&WrapCallback<Interface>, std::move(callback))); \ | ||
} \ | ||
bool HasUntrustedInterface(const GURL& url, \ | ||
const std::string& interface_name); \ | ||
void BindUntrustedInterface(const GURL& url, \ | ||
mojo::GenericPendingReceiver receiver); \ | ||
\ | ||
private: \ | ||
std::map<GURL, std::map<std::string, Callback>> untrusted_callbacks_ | ||
|
||
#include "src/ios/web/public/web_state.h" // IWYU pragma: export | ||
|
||
#undef callbacks_ | ||
|
||
#endif // BRAVE_CHROMIUM_SRC_IOS_WEB_PUBLIC_WEB_STATE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) 2025 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
#include "ios/web/public/web_client.h" | ||
|
||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wnullability-completeness" | ||
|
||
#define IsAppSpecificURL(URL) \ | ||
IsAppSpecificURL(URL) && \ | ||
self.mojoFacade->IsWebUIMessageAllowedForFrame(frame, origin, &prompt) | ||
|
||
#include "src/ios/web/web_state/ui/crw_wk_ui_handler.mm" | ||
|
||
#undef IsAppSpecificURL | ||
|
||
#pragma clang diagnostic pop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2025 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
#include "src/ios/web/web_state/web_state.mm" | ||
|
||
namespace web { | ||
bool WebState::InterfaceBinder::HasUntrustedInterface( | ||
const GURL& url, | ||
const std::string& interface_name) { | ||
DCHECK(!url.is_empty()); | ||
DCHECK(!interface_name.empty()); | ||
if (auto it = untrusted_callbacks_.find(url); | ||
it != untrusted_callbacks_.end()) { | ||
return it->second.find(interface_name) != it->second.end(); | ||
} | ||
return false; | ||
} | ||
|
||
void WebState::InterfaceBinder::BindUntrustedInterface( | ||
const GURL& url, | ||
mojo::GenericPendingReceiver receiver) { | ||
DCHECK(!url.is_empty()); | ||
DCHECK(receiver.is_valid()); | ||
if (auto it = untrusted_callbacks_.find(url); | ||
it != untrusted_callbacks_.end()) { | ||
if (auto jt = it->second.find(*receiver.interface_name()); | ||
jt != it->second.end()) { | ||
jt->second.Run(&receiver); | ||
|
||
GetWebClient()->BindInterfaceReceiverFromMainFrame(web_state_, | ||
std::move(receiver)); | ||
} | ||
} | ||
} | ||
} // namespace web |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
include_rules = [ | ||
"+ios/components/webui/web_ui_url_constants.h", | ||
"+ios/chrome/browser/shared/model/url/chrome_url_constants.h", | ||
"+brave/ios/browser/ui/webui/brave_url_data_source_ios.h", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) 2025 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
#ifndef BRAVE_CHROMIUM_SRC_IOS_WEB_WEBUI_MOJO_FACADE_H_ | ||
#define BRAVE_CHROMIUM_SRC_IOS_WEB_WEBUI_MOJO_FACADE_H_ | ||
|
||
#include <WebKit/WebKit.h> | ||
|
||
#include "url/gurl.h" | ||
|
||
#define HandleMojoMessage \ | ||
Dummy(); \ | ||
bool IsWebUIMessageAllowedForFrame(WKFrameInfo* frame, const GURL& origin, \ | ||
NSString** prompt); \ | ||
std::string HandleMojoMessage | ||
|
||
#include "src/ios/web/webui/mojo_facade.h" // IWYU pragma: export | ||
|
||
#undef HandleMojoMessage | ||
|
||
#endif // BRAVE_CHROMIUM_SRC_IOS_WEB_WEBUI_MOJO_FACADE_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) 2025 The Brave Authors. All rights reserved. | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
// You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
#include "src/ios/web/webui/mojo_facade.mm" | ||
|
||
namespace web { | ||
bool MojoFacade::IsWebUIMessageAllowedForFrame(WKFrameInfo* frame, | ||
const GURL& origin, | ||
NSString** prompt) { | ||
DCHECK_CURRENTLY_ON(web::WebThread::UI); | ||
CHECK(prompt && *prompt); | ||
|
||
auto name_and_args = | ||
GetMessageNameAndArguments(base::SysNSStringToUTF8(*prompt)); | ||
|
||
// If the scheme is untrusted | ||
if (name_and_args.name == "Mojo.bindInterface" && | ||
origin.scheme() == "chrome-untrusted") { | ||
const base::Value::Dict& args = name_and_args.args; | ||
const std::string* interface_name = args.FindString("interfaceName"); | ||
CHECK(interface_name); | ||
|
||
// Check if the requested interface is registered | ||
bool can_bind_interface = | ||
web_state_->GetInterfaceBinderForMainFrame()->HasUntrustedInterface( | ||
origin, *interface_name); | ||
|
||
if (can_bind_interface) { | ||
std::optional<int> pipe_id = args.FindInt("requestHandle"); | ||
CHECK(pipe_id.has_value()); | ||
|
||
mojo::ScopedMessagePipeHandle pipe = TakePipeFromId(*pipe_id); | ||
CHECK(pipe.is_valid()); | ||
web_state_->GetInterfaceBinderForMainFrame()->BindUntrustedInterface( | ||
origin, | ||
mojo::GenericPendingReceiver(*interface_name, std::move(pipe))); | ||
|
||
// Set the prompt to invalid, so that HandleMojoMessage will do nothing. | ||
*prompt = @"{\"name\":\"Mojo.invalidInterface\",\"args\":{}}"; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
std::string MojoFacade::Dummy() { | ||
return ""; | ||
} | ||
} // namespace web |
This file was deleted.
Oops, something went wrong.
118 changes: 0 additions & 118 deletions
118
chromium_src/ios/web/webui/web_ui_ios_data_source_impl.mm
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.