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 4341891
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
13 changes: 12 additions & 1 deletion atom/browser/atom_browser_client.cc
Expand Up @@ -325,6 +325,10 @@ void AtomBrowserClient::ConsiderSiteInstanceForAffinity(
}
}

bool AtomBrowserClient::isRendererSubFrame(int process_id) const {
return base::ContainsKey(renderer_is_subframe_, process_id);
}

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

if (rfh->GetParent())
renderer_is_subframe_.insert(pending_process->GetID());
else
renderer_is_subframe_.erase(pending_process->GetID());
}

void AtomBrowserClient::AppendExtraCommandLineSwitches(
Expand Down Expand Up @@ -511,7 +520,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 +767,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: 4 additions & 0 deletions atom/browser/atom_browser_client.h
Expand Up @@ -234,11 +234,15 @@ 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, base::ProcessId> render_process_host_pids_;

std::set<int> renderer_is_subframe_;

// list of site per affinity. weak_ptr to prevent instance locking
std::map<std::string, content::SiteInstance*> site_per_affinities_;

Expand Down
9 changes: 6 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,14 @@ void WebContentsPreferences::AppendCommandLineSwitches(
if (IsEnabled(options::kWebviewTag))
command_line->AppendSwitch(switches::kWebviewTag);

bool sandbox = 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) {
command_line->AppendSwitch(switches::kEnableSandbox);
} else if (!command_line->HasSwitch(switches::kEnableSandbox)) {
} else if (!command_line->HasSwitch(switches::kEnableSandbox) && !sandbox) {
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 4341891

Please sign in to comment.