Skip to content

Commit

Permalink
feat: sandbox renderer processes for cross-origin frames
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Jun 5, 2019
1 parent a0b1f4f commit e610c87
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
10 changes: 9 additions & 1 deletion atom/browser/atom_browser_client.cc
Expand Up @@ -325,6 +325,11 @@ void AtomBrowserClient::ConsiderSiteInstanceForAffinity(
}
}

bool AtomBrowserClient::isRendererSubFrame(int process_id) const {
auto it = renderer_is_subframe_.find(process_id);
return it != renderer_is_subframe_.end() && it->second;
}

void AtomBrowserClient::RenderProcessWillLaunch(
content::RenderProcessHost* host,
service_manager::mojom::ServiceRequest* service_request) {
Expand Down Expand Up @@ -461,6 +466,7 @@ void AtomBrowserClient::RegisterPendingSiteInstance(
auto* web_contents = content::WebContents::FromRenderFrameHost(rfh);
auto* pending_process = pending_site_instance->GetProcess();
pending_processes_[pending_process->GetID()] = web_contents;
renderer_is_subframe_[pending_process->GetID()] = rfh->GetParent() != nullptr;
}

void AtomBrowserClient::AppendExtraCommandLineSwitches(
Expand Down Expand Up @@ -511,7 +517,8 @@ void AtomBrowserClient::AppendExtraCommandLineSwitches(
}
auto* web_preferences = WebContentsPreferences::From(web_contents);
if (web_preferences)
web_preferences->AppendCommandLineSwitches(command_line);
web_preferences->AppendCommandLineSwitches(
command_line, isRendererSubFrame(process_id));
SessionPreferences::AppendExtraCommandLineSwitches(
web_contents->GetBrowserContext(), command_line);
if (CanUseCustomSiteInstance()) {
Expand Down Expand Up @@ -757,6 +764,7 @@ void AtomBrowserClient::RenderProcessHostDestroyed(
content::RenderProcessHost* host) {
int process_id = host->GetID();
pending_processes_.erase(process_id);
renderer_is_subframe_.erase(process_id);
RemoveProcessPreferences(process_id);
}

Expand Down
4 changes: 3 additions & 1 deletion atom/browser/atom_browser_client.h
Expand Up @@ -234,9 +234,11 @@ class AtomBrowserClient : public content::ContentBrowserClient,
void ConsiderSiteInstanceForAffinity(content::RenderFrameHost* rfh,
content::SiteInstance* site_instance);

bool isRendererSubFrame(int process_id) const;

// pending_render_process => web contents.
std::map<int, content::WebContents*> pending_processes_;

std::map<int, bool> renderer_is_subframe_;
std::map<int, base::ProcessId> render_process_host_pids_;

// list of site per affinity. weak_ptr to prevent instance locking
Expand Down
10 changes: 7 additions & 3 deletions atom/browser/web_contents_preferences.cc
Expand Up @@ -271,7 +271,8 @@ WebContentsPreferences* WebContentsPreferences::From(
}

void WebContentsPreferences::AppendCommandLineSwitches(
base::CommandLine* command_line) {
base::CommandLine* command_line,
bool is_subframe) {
// Check if plugins are enabled.
if (IsEnabled(options::kPlugins))
command_line->AppendSwitch(switches::kEnablePlugins);
Expand All @@ -293,12 +294,15 @@ void WebContentsPreferences::AppendCommandLineSwitches(
if (IsEnabled(options::kWebviewTag))
command_line->AppendSwitch(switches::kWebviewTag);

bool sandbox_subframes = is_subframe && !IsEnabled(options::kNodeIntegrationInSubFrames));

// If the `sandbox` option was passed to the BrowserWindow's webPreferences,
// pass `--enable-sandbox` to the renderer so it won't have any node.js
// integration.
if (IsEnabled(options::kSandbox)) {
if (IsEnabled(options::kSandbox) || sandbox_subframes) {
command_line->AppendSwitch(switches::kEnableSandbox);
} else if (!command_line->HasSwitch(switches::kEnableSandbox)) {
} else if (!command_line->HasSwitch(switches::kEnableSandbox) &&
!sandbox_subframes) {
command_line->AppendSwitch(service_manager::switches::kNoSandbox);
command_line->AppendSwitch(::switches::kNoZygote);
}
Expand Down
3 changes: 2 additions & 1 deletion atom/browser/web_contents_preferences.h
Expand Up @@ -47,7 +47,8 @@ class WebContentsPreferences
void Merge(const base::DictionaryValue& new_web_preferences);

// Append command paramters according to preferences.
void AppendCommandLineSwitches(base::CommandLine* command_line);
void AppendCommandLineSwitches(base::CommandLine* command_line,
bool is_subframe);

// Modify the WebPreferences according to preferences.
void OverrideWebkitPrefs(content::WebPreferences* prefs);
Expand Down

0 comments on commit e610c87

Please sign in to comment.