Skip to content
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

fix: Enable context isolation #815

Merged
merged 1 commit into from Jul 14, 2022
Merged

Conversation

nicarl
Copy link
Collaborator

@nicarl nicarl commented Jul 11, 2022

Summary of changes

  • Activate context isolation
  • Use a context bridge to expose the ipcRenderer
  • Instead of exposing the full ipcRenderer API a subset of functions is exposed
    • for the ocassions where .invoke is called an API is defined, which describes the actual action, e.g. openLink
    • when listeners are used (.on and .removeListener) the respective part of the API is exposed but an allow list of channels is applied in the backend before passing anything through

Context and reason for change

Deactivating context isolation and exposing ipcRenderer globally is discouraged. An attacker could access it from the frontend with window.ipcRenderer and would have a rather powerful API. Instead it is recommended to use a preload script where a subset of APIs is exposed.
Details can be found here.

How can the changes be tested

  • run all tests
  • more importantly open the app and check the actions invoking the ipcRenderer are working (saving files, opening links, ...)

@nicarl nicarl marked this pull request as ready for review July 11, 2022 20:25
@benedikt-richter benedikt-richter self-assigned this Jul 12, 2022
@leslielazzarino leslielazzarino self-assigned this Jul 12, 2022
Copy link
Member

@benedikt-richter benedikt-richter left a comment

Choose a reason for hiding this comment

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

Great to takle the context isolation! Looks good to me and works.
I added an idea on how to refactor the enums for the IpcChannels.

src/shared/shared-types.ts Outdated Show resolved Hide resolved
@@ -21,3 +22,30 @@ export enum IpcChannel {
ShowProjectStatisticsPopup = 'show-project-statistics-pop-up',
SetBaseURLForRoot = 'set-base-url-for-root',
}

Copy link
Member

Choose a reason for hiding this comment

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

For the channels I'd suggest to split the enum containing all channels into two

  • frontend channels
  • other channels

This could look like (maybe naming might be improved):


export enum IpcChannel {
  ExportFile = 'export-file',
  OpenFile = 'open-file',
  OpenLink = 'open-link',
  SaveFile = 'save-file',
  SendErrorInformation = 'send-error-information',
}

export enum AllowedFrontendChannels {
  ExportFileRequest = 'export-file-request',
  FileLoaded = 'file-loaded',
  Logging = 'logging',
  ResetLoadedFile = 'reset-loaded-file',
  RestoreFrontend = 'restore-frontend',
  SaveFileRequest = 'save-file-request',
  ToggleHighlightForCriticalSignals = 'toggle-highlight-for-critical-signals',
  ShowSearchPopup = 'show-search-pop-up',
  ShowProjectMetadataPopup = 'show-project-metadata-pop-up',
  ShowProjectStatisticsPopup = 'show-project-statistics-pop-up',
  SetBaseURLForRoot = 'set-base-url-for-root',
}


Then the checks in the backend
if (allowedFrontendChannels.includes(channel)) {
could read
if (Object.values(AllowedFrontendChannels).includes(channel)) {
and the constant
export const allowedFrontendChannels
would not be required.

This would avoid to add new channels to two enums in case of "allowed for frontend" and to the constant as well. Further, it would make the distinction between the two enums more explicit.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good idea I changed it according to your suggestion.

@nicarl nicarl force-pushed the fix/enable-context-isolation branch from 319c82c to 6df7e70 Compare July 12, 2022 18:50
It is discouraged to disable context isolation and expose ipcRendeder to the main window as it allows access from the frontend to an extremly powerful API. Instead it is recommended to use a contextBridge and expose only subset of APIs to the frontend.

Signed-off-by: Nico Carl <nicocarl@protonmail.com>
@leslielazzarino leslielazzarino merged commit f52a84f into main Jul 14, 2022
@leslielazzarino leslielazzarino deleted the fix/enable-context-isolation branch July 14, 2022 14:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants