-
Notifications
You must be signed in to change notification settings - Fork 62
fix: complete untrusted certificate support #1308
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b46665b
chore(deps): add vue-frag
ShGKme 0fc3a4f
fix(certificate): improve custom showCertificateTrust dialog
ShGKme f30fdda
fix(certificate): untrusted cert prompt not shown for requests
ShGKme bfaa5ed
fix(certificate): untrusted cert prompt not shown on macOS and Windows
ShGKme 86b0e7f
fix(certificate): Electron rejects cert 30s timeout during user prompt
ShGKme 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,98 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| import type { BrowserWindow, Request } from 'electron' | ||
|
|
||
| import { session } from 'electron' | ||
| import { showCertificateTrustDialog } from '../certificate/certificate.window.ts' | ||
| import { getAppConfig, setAppConfig } from './AppConfig.ts' | ||
|
|
||
| export type UntrustedCertificateDetails = Pick<Request, 'hostname' | 'certificate' | 'verificationResult'> | ||
|
|
||
| /** | ||
| * Pending showCertificateTrustDialog prompts per fingerprint to prevent duplicated dialogs | ||
| */ | ||
| const pendingCertificateTrustPrompts: Map<string, Promise<boolean>> = new Map() | ||
|
|
||
| /** | ||
| * Handle request for untrusted certificate acceptance. | ||
| * For unknown certificates, show custom certificate trust dialog. | ||
| * Note: dialog.showCertificateTrustDialog is not used | ||
| * since application level trusted dialog is needed, not system-level | ||
| * | ||
| * @param window - Parent window | ||
| * @param details - Error details | ||
| * @return Whether the certificate is accepted as trusted | ||
| */ | ||
| export async function promptCertificateTrust(window: BrowserWindow, details: UntrustedCertificateDetails): Promise<boolean> { | ||
| const fingerprint = details.certificate.fingerprint | ||
| const trustedFingerprints = getAppConfig('trustedFingerprints') | ||
|
|
||
| // Already accepted | ||
| if (trustedFingerprints.includes(fingerprint)) { | ||
| return true | ||
| } | ||
|
|
||
| // Already in prompt in parallel | ||
| const existingPrompt = pendingCertificateTrustPrompts.get(fingerprint) | ||
| if (existingPrompt) { | ||
| return existingPrompt | ||
| } | ||
|
|
||
| // Prompt user acceptance | ||
| const pendingDialog = showCertificateTrustDialog(window, details) | ||
| pendingCertificateTrustPrompts.set(fingerprint, pendingDialog) | ||
| const isAccepted = await pendingDialog | ||
| pendingCertificateTrustPrompts.delete(fingerprint) | ||
|
|
||
| if (isAccepted) { | ||
| setAppConfig('trustedFingerprints', [...trustedFingerprints, fingerprint]) | ||
| } | ||
|
|
||
| return isAccepted | ||
| } | ||
|
|
||
| /** | ||
| * Verify certificate on a URL. | ||
| * Note: this function only exists due to Electron limitations. | ||
| * If a user accepts the certificate later than the request is rejected by timeout, | ||
| * Electron considers it rejected for 30 minutes or until the app restart. | ||
| * Issue: https://github.com/electron/electron/issues/47267 | ||
| * And there is no way to reset the cache. | ||
| * Issue: https://github.com/electron/electron/issues/41448 | ||
| * Thus the verification on the defaultSession cannot be used (at least for login). | ||
| * This function makes a single request in a new random session, to avoid verification caching. | ||
| * The actual result is stored in the application config. | ||
| * | ||
| * @param window - Parent browser window | ||
| * @param url - URL | ||
| */ | ||
| export async function verifyCertificate(window: BrowserWindow, url: string): Promise<boolean> { | ||
| const certificateVerifySession = session.fromPartition(`certificate:verify:${Math.random().toString(36).slice(2, 9)}`) | ||
|
|
||
| let verificationResolvers: PromiseWithResolvers<boolean> | undefined | ||
|
|
||
| certificateVerifySession.setCertificateVerifyProc(async (request, callback) => { | ||
| verificationResolvers = Promise.withResolvers() | ||
| // Use original result, failing the request | ||
| callback(-3) | ||
|
|
||
| const isAccepted = request.errorCode === 0 || await promptCertificateTrust(window, request) | ||
| verificationResolvers.resolve(isAccepted) | ||
| }) | ||
|
|
||
| try { | ||
| await certificateVerifySession.fetch(url, { bypassCustomProtocolHandlers: true }) | ||
| // Successful request - no SSL errors | ||
| return true | ||
| } catch { | ||
| // SSL Error - handled by user prompt | ||
| if (verificationResolvers) { | ||
| return verificationResolvers.promise | ||
| } | ||
| // Some unexpected network error - not a certificate error | ||
| return true | ||
| } | ||
| } |
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,72 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| import type { IpcMainEvent } from 'electron' | ||
| import type { UntrustedCertificateDetails } from '../app/certificate.service.ts' | ||
|
|
||
| import { BrowserWindow, ipcMain } from 'electron' | ||
| import { applyContextMenu } from '../app/applyContextMenu.js' | ||
| import { applyZoom, buildTitle, getScaledWindowMinSize, getScaledWindowSize, getWindowUrl } from '../app/utils.ts' | ||
| import { getBrowserWindowIcon } from '../shared/icons.utils.js' | ||
|
|
||
| /** | ||
| * Show untrusted certificate dialog window | ||
| * | ||
| * @param parentWindow - Parent browser window | ||
| * @param details - Error details | ||
| * @return Whether user accept the certificate | ||
| */ | ||
| export function showCertificateTrustDialog(parentWindow: BrowserWindow, details: UntrustedCertificateDetails) { | ||
| const TITLE = buildTitle('Security warning') | ||
| const window = new BrowserWindow({ | ||
| title: TITLE, | ||
| ...getScaledWindowSize({ | ||
| width: 600, | ||
| height: 600, | ||
| }), | ||
| ...getScaledWindowMinSize({ | ||
| minWidth: 320, | ||
| minHeight: 256, | ||
| }), | ||
| parent: parentWindow, | ||
| modal: true, | ||
| show: false, | ||
| maximizable: false, | ||
| minimizable: false, | ||
| center: true, | ||
| fullscreenable: false, | ||
| autoHideMenuBar: true, | ||
| webPreferences: { | ||
| preload: TALK_DESKTOP__WINDOW_CERTIFICATE_PRELOAD_WEBPACK_ENTRY, | ||
| }, | ||
| icon: getBrowserWindowIcon(), | ||
| }) | ||
|
|
||
| applyContextMenu(window) | ||
| applyZoom(window) | ||
| window.removeMenu() | ||
| window.on('ready-to-show', () => window.show()) | ||
|
|
||
| window.loadURL(getWindowUrl('certificate') + '#' + encodeURIComponent(JSON.stringify(details))) | ||
|
|
||
| return new Promise<boolean>((resolve) => { | ||
| let isAccepted = false | ||
|
|
||
| const onCertificateAccept = (event: IpcMainEvent, accepted: boolean) => { | ||
| if (event.sender !== window.webContents) { | ||
| return | ||
| } | ||
| isAccepted = accepted | ||
| window.close() | ||
| } | ||
|
|
||
| ipcMain.once('certificate:accept', onCertificateAccept) | ||
|
|
||
| window.on('closed', () => { | ||
| ipcMain.off('certificate:accept', onCertificateAccept) | ||
| resolve(isAccepted) | ||
| }) | ||
| }) | ||
| } |
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.