-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Desktop: Block network requests in CEF that are not explicitly allowlisted #4225
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
Merged
+155
−1
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,84 @@ | ||
| use cef::rc::{Rc, RcImpl}; | ||
| use cef::sys::{_cef_request_handler_t, cef_base_ref_counted_t}; | ||
| use cef::{AuthCallback, Browser, CefString, Frame, ImplRequest, ImplRequestHandler, Request, ResourceRequestHandler, WrapRequestHandler}; | ||
| use std::ffi::c_int; | ||
|
|
||
| use super::resource_request_handler::ResourceRequestHandlerImpl; | ||
| use crate::cef::consts::{RESOURCE_DOMAIN, RESOURCE_SCHEME}; | ||
|
|
||
| pub(crate) struct RequestHandlerImpl { | ||
| object: *mut RcImpl<_cef_request_handler_t, Self>, | ||
| } | ||
|
|
||
| impl RequestHandlerImpl { | ||
| pub(crate) fn new() -> Self { | ||
| Self { object: std::ptr::null_mut() } | ||
| } | ||
| } | ||
|
|
||
| impl ImplRequestHandler for RequestHandlerImpl { | ||
| fn on_before_browse(&self, _browser: Option<&mut Browser>, _frame: Option<&mut Frame>, request: Option<&mut Request>, _user_gesture: c_int, _is_redirect: c_int) -> c_int { | ||
| let Some(request) = request else { return 1 }; | ||
| let url = CefString::from(&request.url()).to_string(); | ||
| if url.starts_with(&format!("{RESOURCE_SCHEME}://{RESOURCE_DOMAIN}/")) { | ||
| 0 | ||
|
timon-schelling marked this conversation as resolved.
|
||
| } else { | ||
| tracing::warn!("Blocked navigation to: {}", url); | ||
| 1 | ||
| } | ||
| } | ||
|
timon-schelling marked this conversation as resolved.
|
||
|
|
||
| fn resource_request_handler( | ||
| &self, | ||
| _browser: Option<&mut Browser>, | ||
| _frame: Option<&mut Frame>, | ||
| _request: Option<&mut Request>, | ||
| _is_navigation: c_int, | ||
| _is_download: c_int, | ||
| _request_initiator: Option<&CefString>, | ||
| _disable_default_handling: Option<&mut c_int>, | ||
| ) -> Option<ResourceRequestHandler> { | ||
| Some(ResourceRequestHandler::new(ResourceRequestHandlerImpl::new())) | ||
| } | ||
|
|
||
| fn auth_credentials( | ||
| &self, | ||
| _browser: Option<&mut Browser>, | ||
| _origin_url: Option<&CefString>, | ||
| _is_proxy: c_int, | ||
| _host: Option<&CefString>, | ||
| _port: c_int, | ||
| _realm: Option<&CefString>, | ||
| _scheme: Option<&CefString>, | ||
| _callback: Option<&mut AuthCallback>, | ||
| ) -> c_int { | ||
| 0 | ||
| } | ||
|
|
||
| fn get_raw(&self) -> *mut _cef_request_handler_t { | ||
| self.object.cast() | ||
| } | ||
| } | ||
|
|
||
| impl Clone for RequestHandlerImpl { | ||
| fn clone(&self) -> Self { | ||
| unsafe { | ||
| let rc_impl = &mut *self.object; | ||
| rc_impl.interface.add_ref(); | ||
| } | ||
| Self { object: self.object } | ||
| } | ||
| } | ||
|
timon-schelling marked this conversation as resolved.
|
||
| impl Rc for RequestHandlerImpl { | ||
| fn as_base(&self) -> &cef_base_ref_counted_t { | ||
| unsafe { | ||
| let base = &*self.object; | ||
| std::mem::transmute(&base.cef_object) | ||
| } | ||
| } | ||
| } | ||
| impl WrapRequestHandler for RequestHandlerImpl { | ||
| fn wrap_rc(&mut self, object: *mut RcImpl<_cef_request_handler_t, Self>) { | ||
| self.object = object; | ||
| } | ||
| } | ||
This file contains hidden or 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,60 @@ | ||
| use cef::rc::{Rc, RcImpl}; | ||
| use cef::sys::{_cef_resource_request_handler_t, cef_base_ref_counted_t}; | ||
| use cef::{Browser, Callback, CefString, Frame, ImplRequest, ImplResourceRequestHandler, Request, ReturnValue, WrapResourceRequestHandler}; | ||
|
|
||
| use crate::cef::consts::{RESOURCE_DOMAIN, RESOURCE_SCHEME}; | ||
|
|
||
| // TODO: Deny all external requests once we stop relying on google fonts for font preview | ||
| fn is_allowed_url(url: &str) -> bool { | ||
| url.starts_with(&format!("{RESOURCE_SCHEME}://{RESOURCE_DOMAIN}/")) || url.starts_with("https://fonts.googleapis.com/css2") || url.starts_with("https://fonts.gstatic.com/") | ||
| } | ||
|
timon-schelling marked this conversation as resolved.
|
||
|
|
||
| pub(crate) struct ResourceRequestHandlerImpl { | ||
| object: *mut RcImpl<_cef_resource_request_handler_t, Self>, | ||
| } | ||
|
|
||
| impl ResourceRequestHandlerImpl { | ||
| pub(crate) fn new() -> Self { | ||
| Self { object: std::ptr::null_mut() } | ||
| } | ||
| } | ||
|
|
||
| impl ImplResourceRequestHandler for ResourceRequestHandlerImpl { | ||
| fn on_before_resource_load(&self, _browser: Option<&mut Browser>, _frame: Option<&mut Frame>, request: Option<&mut Request>, _callback: Option<&mut Callback>) -> ReturnValue { | ||
| let Some(request) = request else { return ReturnValue::CANCEL }; | ||
| let url = CefString::from(&request.url()).to_string(); | ||
| if is_allowed_url(&url) { | ||
| ReturnValue::CONTINUE | ||
| } else { | ||
| tracing::error!("Blocked resource load: {}", url); | ||
|
timon-schelling marked this conversation as resolved.
|
||
| ReturnValue::CANCEL | ||
| } | ||
| } | ||
|
|
||
| fn get_raw(&self) -> *mut _cef_resource_request_handler_t { | ||
| self.object.cast() | ||
| } | ||
| } | ||
|
|
||
| impl Clone for ResourceRequestHandlerImpl { | ||
| fn clone(&self) -> Self { | ||
| unsafe { | ||
| let rc_impl = &mut *self.object; | ||
| rc_impl.interface.add_ref(); | ||
| } | ||
| Self { object: self.object } | ||
| } | ||
| } | ||
|
timon-schelling marked this conversation as resolved.
|
||
| impl Rc for ResourceRequestHandlerImpl { | ||
| fn as_base(&self) -> &cef_base_ref_counted_t { | ||
| unsafe { | ||
| let base = &*self.object; | ||
| std::mem::transmute(&base.cef_object) | ||
| } | ||
| } | ||
| } | ||
| impl WrapResourceRequestHandler for ResourceRequestHandlerImpl { | ||
| fn wrap_rc(&mut self, object: *mut RcImpl<_cef_resource_request_handler_t, Self>) { | ||
| self.object = object; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.