@@ -512,7 +512,7 @@ static UINT GetSystemSleepIntervalInMilliseconds(UINT defaultValue) {
512
512
}
513
513
514
514
/*
515
- * MSIX implementation o SetDefaultExtensionHandlersUserChoice.
515
+ * MSIX implementation for SetDefaultExtensionHandlersUserChoice.
516
516
*
517
517
* Due to the fact that MSIX builds run in a virtual, walled off environment,
518
518
* calling into the Win32 registry APIs doesn't work to set registry keys.
@@ -530,7 +530,7 @@ static UINT GetSystemSleepIntervalInMilliseconds(UINT defaultValue) {
530
530
* Powershell, to make it as quick as possible.
531
531
*
532
532
*/
533
- nsresult SetDefaultExtensionHandlersUserChoiceImplMsix (
533
+ static nsresult SetDefaultExtensionHandlersUserChoiceImplMsix (
534
534
const wchar_t * aAumi, const wchar_t * const aSid,
535
535
const nsTArray<nsString>& aFileExtensions) {
536
536
mozilla::UniquePtr<wchar_t []> exePath;
@@ -544,24 +544,20 @@ nsresult SetDefaultExtensionHandlersUserChoiceImplMsix(
544
544
// extension, done below.
545
545
nsString startScript (
546
546
uR"(
547
+ # Force exceptions to stop execution
548
+ $ErrorActionPreference = 'Stop'
549
+
547
550
function Set-DefaultHandlerRegistry($Path, $ProgID, $Hash) {
548
551
$Path = "$Path\UserChoice"
549
552
$CurrentUser = [Microsoft.Win32.Registry]::CurrentUser
550
- $ReadWriteSubTreePerm = [Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree
551
- try {
552
- $CurrentUser.DeleteSubKeyTree($Path, $false)
553
- $key = $CurrentUser.CreateSubKey($Path, $ReadWriteSubTreePerm)
554
- } catch {
555
- $key = $CurrentUser.OpenSubKey($Path, $ReadWriteSubTreePerm, [System.Security.AccessControl.RegistryRights]::ChangePermissions)
556
- $acl = $key.GetAccessControl()
557
- $CurrentName = [Security.Principal.WindowsIdentity]::GetCurrent().Name
558
- $rule = New-Object System.Security.AccessControl.RegistryAccessRule($CurrentName, 'SetValue', 'Deny')
559
- $acl.RemoveAccessRule($rule)
560
- $key.SetAccessControl($acl)
561
- $key.Close()
562
-
563
- $key = $CurrentUser.OpenSubKey($Path, $ReadWriteSubTreePerm)
564
- }
553
+
554
+ # DeleteSubKey throws if we don't have sufficient permissions to delete key,
555
+ # signaling failure to launching process.
556
+ #
557
+ # Note: DeleteSubKeyTree fails when DENY permissions are set on key, whereas
558
+ # DeleteSubKey succeeds.
559
+ $CurrentUser.DeleteSubKey($Path, $false)
560
+ $key = $CurrentUser.CreateSubKey($Path)
565
561
566
562
$StringType = [Microsoft.Win32.RegistryValueKind]::String
567
563
$key.SetValue('ProgID', $ProgID, $StringType)
@@ -620,28 +616,20 @@ function Set-DefaultHandlerRegistry($Path, $ProgID, $Hash) {
620
616
for (size_t i = 0 ; i + 1 < aFileExtensions.Length (); i += 2 ) {
621
617
const wchar_t * fileExtension = aFileExtensions[i].get ();
622
618
623
- // Append a line to the script buffer in the form:
624
- // Set-DefaultHandlerRegistry $RegistryKeyPath $ProgID $UserChoiceHash
625
-
626
- // Use Append to minimize string allocation and processing
619
+ nsAutoString keyPath;
620
+ AppendAssociationKeyPath (fileExtension, keyPath);
627
621
628
- scriptBuffer.AppendLiteral (u" Set-DefaultHandlerRegistry " );
629
- rv = AppendAssociationKeyPath (fileExtension, scriptBuffer);
630
- NS_ENSURE_SUCCESS (rv, rv);
631
-
632
- scriptBuffer.AppendLiteral (u" " );
633
-
634
- scriptBuffer.Append (progIDs[i / 2 ].get ());
635
- scriptBuffer.AppendLiteral (u" " );
636
-
637
- auto hash = GenerateUserChoiceHash (fileExtension, aSid,
638
- progIDs[i / 2 ].get (), hashTimestamp);
639
- if (!hash) {
622
+ auto hashWchar = GenerateUserChoiceHash (
623
+ fileExtension, aSid, progIDs[i / 2 ].get (), hashTimestamp);
624
+ if (!hashWchar) {
640
625
return NS_ERROR_FAILURE;
641
626
}
627
+ auto hash = nsDependentString (hashWchar.get ());
642
628
643
- scriptBuffer.Append (hash.get ());
644
- scriptBuffer.AppendLiteral (u" \n " );
629
+ // Append a line to the script buffer in the form:
630
+ // Set-DefaultHandlerRegistry $RegistryKeyPath $ProgID $UserChoiceHash
631
+ scriptBuffer += u" Set-DefaultHandlerRegistry " _ns + keyPath + u" " _ns +
632
+ progIDs[i / 2 ] + u" " _ns + hash + u" \n " _ns;
645
633
}
646
634
647
635
// The hash changes at the end of each minute, so check that the hash should
0 commit comments