-
Notifications
You must be signed in to change notification settings - Fork 395
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: restrict the Windows Hello-related libraries from running in ch…
…ild processes. (#6108)
- Loading branch information
1 parent
8e67746
commit f3c6680
Showing
5 changed files
with
193 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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 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 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,4 @@ | ||
export enum EWindowHelloEventType { | ||
CheckAvailabilityAsync = 'checkAvailabilityAsync', | ||
RequestVerificationAsync = 'requestVerificationAsync', | ||
} |
This file contains 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,79 @@ | ||
import path from 'path'; | ||
|
||
import { utilityProcess } from 'electron/main'; | ||
import Logger from 'electron-log/main'; | ||
|
||
import { EWindowHelloEventType } from './enum'; | ||
|
||
import type { UtilityProcess } from 'electron/main'; | ||
|
||
let windowsHelloChild: UtilityProcess | null = null; | ||
let windowsHelloCallbacks: { | ||
type: string; | ||
callback: (e: any) => void; | ||
timestamp: number; | ||
}[] = []; | ||
export const startServices = () => { | ||
windowsHelloChild = utilityProcess.fork( | ||
// After build, the directory is 'dist' and WindowsHello file is located in 'dist/service' | ||
path.join(__dirname, './service/windowsHello.js'), | ||
); | ||
windowsHelloChild.on('message', (e: { type: string; result: boolean }) => { | ||
Logger.info('parent process--onMessage', e); | ||
const callbacks = windowsHelloCallbacks.filter( | ||
(callbackItem) => callbackItem.type === e.type, | ||
); | ||
if (callbacks.length) { | ||
callbacks.forEach((callbackItem) => { | ||
// Callbacks older than 1 minute will not be executed | ||
if (Date.now() - callbackItem.timestamp < 60 * 1000) { | ||
callbackItem.callback(e.result); | ||
} | ||
}); | ||
windowsHelloCallbacks = windowsHelloCallbacks.filter( | ||
(callbackItem) => !callbacks.includes(callbackItem), | ||
); | ||
} | ||
}); | ||
}; | ||
|
||
let cacheWindowsHelloSupported: boolean | null = null; | ||
export const checkAvailabilityAsync = async () => { | ||
if (cacheWindowsHelloSupported === null) { | ||
cacheWindowsHelloSupported = await Promise.race<boolean>([ | ||
new Promise<boolean>((resolve) => { | ||
windowsHelloCallbacks.push({ | ||
type: EWindowHelloEventType.CheckAvailabilityAsync, | ||
callback: resolve, | ||
timestamp: Date.now(), | ||
}); | ||
windowsHelloChild?.postMessage({ | ||
type: EWindowHelloEventType.CheckAvailabilityAsync, | ||
}); | ||
}), | ||
new Promise((resolve) => | ||
setTimeout(() => { | ||
cacheWindowsHelloSupported = false; | ||
resolve(cacheWindowsHelloSupported); | ||
}, 500), | ||
), | ||
]); | ||
} | ||
return cacheWindowsHelloSupported; | ||
}; | ||
|
||
export const requestVerificationAsync = (message: string) => | ||
new Promise<{ | ||
success: boolean; | ||
error?: string; | ||
}>((resolve) => { | ||
windowsHelloCallbacks.push({ | ||
type: EWindowHelloEventType.RequestVerificationAsync, | ||
callback: resolve, | ||
timestamp: Date.now(), | ||
}); | ||
windowsHelloChild?.postMessage({ | ||
type: EWindowHelloEventType.RequestVerificationAsync, | ||
params: message, | ||
}); | ||
}); |
This file contains 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,70 @@ | ||
import windowsSecurityCredentialsUiModule, { | ||
UserConsentVerificationResult, | ||
UserConsentVerifierAvailability, | ||
} from 'electron-windows-security'; | ||
|
||
import { EWindowHelloEventType } from './enum'; | ||
|
||
function checkWindowsHelloAvailability(callback: (result: boolean) => void) { | ||
try { | ||
windowsSecurityCredentialsUiModule.UserConsentVerifier.checkAvailabilityAsync( | ||
(error, status) => { | ||
if (error) { | ||
callback(false); | ||
} else { | ||
callback(status === UserConsentVerifierAvailability.available); | ||
} | ||
}, | ||
); | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
|
||
function requestVerificationAsync( | ||
message: string, | ||
callback: (params: { success: boolean; error?: string }) => void, | ||
) { | ||
windowsSecurityCredentialsUiModule.UserConsentVerifier.requestVerificationAsync( | ||
message, | ||
(error, status) => { | ||
if (error) { | ||
callback({ | ||
success: false, | ||
error: error.message, | ||
}); | ||
} else { | ||
callback({ | ||
success: status === UserConsentVerificationResult.verified, | ||
}); | ||
} | ||
}, | ||
); | ||
} | ||
|
||
// Child process | ||
process.parentPort.on( | ||
'message', | ||
(e: { data: { type: string; params: unknown } }) => { | ||
switch (e.data.type) { | ||
case 'checkAvailabilityAsync': | ||
checkWindowsHelloAvailability((result) => { | ||
process.parentPort.postMessage({ | ||
type: EWindowHelloEventType.CheckAvailabilityAsync, | ||
result, | ||
}); | ||
}); | ||
break; | ||
case 'requestVerificationAsync': | ||
requestVerificationAsync(e.data.params as string, (result) => { | ||
process.parentPort.postMessage({ | ||
type: EWindowHelloEventType.RequestVerificationAsync, | ||
result, | ||
}); | ||
}); | ||
break; | ||
default: | ||
break; | ||
} | ||
}, | ||
); |