Skip to content

feat: add app.enableSecureMode() API - #17902

Closed
miniak wants to merge 1 commit into
masterfrom
miniak/secure-mode
Closed

miniak wants to merge 1 commit into
masterfrom
miniak/secure-mode

Conversation

@miniak

@miniak miniak commented Apr 22, 2019 •

Copy link
Copy Markdown
Contributor

Description of Change

This is an opt-in locked down mode designed for apps loading remote content.

Check docs/api/secure-mode.md for details.

Checklist

Release Notes

Notes: Added app.enableSecureMode() API to opt-in into more secure mode designed for apps running remote content.

/cc @electron/wg-security

@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Apr 22, 2019
@miniak miniak self-assigned this Apr 22, 2019

@MarshallOfSound MarshallOfSound left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good initial change, I like that the diff is minimal

Comment thread docs/api/app.md Outdated

When no handler is set using `session.setPermissionRequestHandler()`, it behaves as if `callback(false)` was called.

This method can only be called before app is ready.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will involve a race condition, is there any technical reason why this is the case. I would imagine it doesn't matter when this is called?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it probably does not matter, but it seems more future-proof to make this limitation? it should be enabled before any windows are created.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make that the restriction then instead of before app ready. Enforce that it must be called before any webcontents are made.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my concern is that people are used to the limitation that APIs must / cannot be called before / after app.isReady. do we have any API which can only be used before webcontents are created?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dumb question, would it make sense to make this a webPreferences argument instead? That would prevent the race condition and also resolve the question of when the API call was allowed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw where is the race condition?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@miniak It is very easy to delay a task (run something after an event)
There is no easy way to guarantee to do something before an event.

If there is not a technical limitation here, I would suggest we tie this to the actual thing we care about not some other arbitrary event.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will change the condition. Is checking if there are 0 webcontents instances alive at the time the API is called good enough check?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

webContents().getAll().length === 0 sounds like it will suffice for the actual condition you are trying to enforce here. (That this API is called before any windows / web contents are made)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarshallOfSound I've implemented the check in native code, can you check that? if you don't like the approach, I can do it in JS instead

Comment thread typings/internal-electron.d.ts Resolved
Comment thread docs/api/app.md Resolved
Comment thread atom/browser/browser.h Resolved
Comment thread atom/browser/browser.h Resolved
Comment thread atom/browser/browser.cc Resolved
Comment thread atom/browser/atom_permission_manager.cc Resolved
@miniak
miniak force-pushed the miniak/secure-mode branch 4 times, most recently from 917105a to 7b73779 Compare April 23, 2019 13:48
@miniak

miniak commented Apr 23, 2019

Copy link
Copy Markdown
Contributor Author

@MarshallOfSound should I mark it _Experimental_?

@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Apr 23, 2019
@miniak
miniak force-pushed the miniak/secure-mode branch 3 times, most recently from f2cbd00 to 106aa5d Compare April 23, 2019 14:28
@miniak
miniak requested review from a team, deepak1556, nornagon and ppontes April 23, 2019 14:32
@miniak

miniak commented Apr 23, 2019 •

Copy link
Copy Markdown
Contributor Author

@MarshallOfSound I've added remote and desktopCapturer filtering events as well.

@miniak
miniak force-pushed the miniak/secure-mode branch 8 times, most recently from f982023 to 1b5053d Compare April 23, 2019 15:52
Comment thread atom/browser/atom_permission_manager.cc Resolved
Comment thread docs/api/secure-mode.md Resolved
Comment thread docs/api/app.md Outdated

When no handler is set using `session.setPermissionRequestHandler()`, it behaves as if `callback(false)` was called.

This method can only be called before app is ready.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dumb question, would it make sense to make this a webPreferences argument instead? That would prevent the race condition and also resolve the question of when the API call was allowed.

@miniak
miniak force-pushed the miniak/secure-mode branch 2 times, most recently from 8f5b266 to 5bdef48 Compare April 23, 2019 17:06
#endif
SetDefaultBoolIfUndefined(options::kOffscreen, false);

if (Browser::Get()->secure_mode_enabled()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might have gotten lost in the original comment but I meant that we should always do the SetDefaultBoolIfUndefined calls for the normal defaults. And if secure_mode_enabled() then override the values regardless of whether they are undefined. E.g.

SetBoolIfUndefined(optionA, true);
if (is_secure_mode) {
  SetBool(optionA, false);
}

Then we can remove the if (!secure_mode_enabled && IsEnabled(options::kNodeIntegration)) style code below (we won't need to check secure_mode_enabled)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some options need to stay overridable. I only want to kill nodeIntegration and nodeIntegrationInWorker as those are not necessary to keep functionality as you can use preload script instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or maybe it should be configurable, which options are going to be disabled completely.

@miniak miniak Apr 24, 2019 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something like?

interface OverridableOptions {
  sandbox?: boolean;
  contextIsolation?: boolean;
  nativeWindowOpen?: boolean;
  enableRemoteModule?: boolean;
  nodeIntegration?: boolean;
  nodeIntegrationInWorker?: boolean;
}

app.enableSecureMode(overridableOptions: OverridableOptions): void;

@miniak miniak Apr 24, 2019 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options will always default to the more secure value and will only be overridable if specified in this list

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g.

app.enableSecureMode({
    contextIsolation: true // allow disabling contextIsolation
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or some other variant of this, to make the API as least confusing as possible

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some options need to stay overridable.

I disagree, if the options can be overridable then any of the previous vulns we've had where people have been able to set nodeIntegration: true (somehow) would still work making this option not very helpful.

I think this option should give no options and violently enforce the security guidelines (disable node, enable isolation, reject permission requests if not already handled, etc..)

That's just IMO though, would like some weigh in from @electron/wg-security

@miniak miniak May 1, 2019 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, contextIsolation for example is not an option for us for now (we'll need to do a lot of refactoring to get there). We need it to be optionally overridable. The default can be that nothing is overridable and you have to opt-in explicitly into being able to override some options.

@miniak
miniak force-pushed the miniak/secure-mode branch 7 times, most recently from 5263bc3 to e537a89 Compare April 24, 2019 22:35
@miniak miniak mentioned this pull request Apr 24, 2019
3 tasks done
@miniak
miniak force-pushed the miniak/secure-mode branch 2 times, most recently from a8d8f10 to 70eb367 Compare April 29, 2019 17:46
@miniak
miniak requested a review from MarshallOfSound April 29, 2019 17:47
@miniak
miniak force-pushed the miniak/secure-mode branch 8 times, most recently from 486ebaf to 256fe3e Compare May 1, 2019 18:49
@miniak miniak added the wip ⚒ label May 1, 2019
@miniak
miniak force-pushed the miniak/secure-mode branch 2 times, most recently from 29daf8d to 6b6c80a Compare May 4, 2019 09:28
@miniak
miniak force-pushed the miniak/secure-mode branch from 6b6c80a to 4285156 Compare May 15, 2019 16:54
@nornagon

nornagon commented Jun 3, 2019

Copy link
Copy Markdown
Contributor

Closing this due to discussion in @electron/wg-security meeting. This should be implemented as granular, descriptive APIs, not as one "secure mode" flag.

@nornagon nornagon closed this Jun 3, 2019
@zcbenz
zcbenz deleted the miniak/secure-mode branch June 1, 2020 10:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants