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

Warn before the user runs a new commandline elevated #11308

Closed
Closed
Show file tree
Hide file tree
Changes from 98 commits
Commits
Show all changes
124 commits
Select commit Hold shift + click to select a range
42c3eea
pull application state members into a base class
zadjii-msft Aug 30, 2021
eb243f5
Split ApplicationState into a Base and add an Elevated version
zadjii-msft Aug 31, 2021
2857324
proof of concept add a dialog to pop when opening new tabs
zadjii-msft Aug 31, 2021
ccbcb42
Merge remote-tracking branch 'origin/main' into dev/migrie/f/elevatio…
zadjii-msft Aug 31, 2021
14d21f4
nits, cleanup
zadjii-msft Sep 1, 2021
c66a566
Allow a Pane to host a UserControl instead of a TermControl
zadjii-msft Sep 1, 2021
ed1cf2a
add the boilerplate for a custom content dialog like thing
zadjii-msft Sep 1, 2021
1ee3522
Allow a TerminalTab to have a UserControl
zadjii-msft Sep 1, 2021
7fb7d64
These are things I might need for #997
zadjii-msft Sep 1, 2021
631cdf7
Who ever said nested lambdas is a bad thing?
zadjii-msft Sep 1, 2021
736a351
This works amazingly well
zadjii-msft Sep 1, 2021
7f29e1e
More things we might need for Non-Terminal content, #997
zadjii-msft Sep 2, 2021
9b32681
This works to prompt before splitting a pane
zadjii-msft Sep 2, 2021
58c6646
Format that dialog with the actual commandline
zadjii-msft Sep 2, 2021
e5dc64e
Hot-reload the elevated state?
zadjii-msft Sep 2, 2021
64898b1
update other panes too
zadjii-msft Sep 2, 2021
b592155
Merge remote-tracking branch 'origin/main' into dev/migrie/f/non-term…
zadjii-msft Sep 8, 2021
447c2f9
I think this creates a test file that's only accessible by SYSTEM. As…
zadjii-msft Sep 8, 2021
880222d
This file isn't even readable by admins, so maybe that's bad
zadjii-msft Sep 9, 2021
5197dc4
this worked, nice
zadjii-msft Sep 9, 2021
721b636
unfortunately, we can't _write_ this file...
zadjii-msft Sep 9, 2021
8569211
Now, we can update the file after it's created
zadjii-msft Sep 9, 2021
aef422d
This is definitely not right.
zadjii-msft Sep 9, 2021
9a1cf5a
bunch of dead ends
zadjii-msft Sep 9, 2021
7854abe
I bet that's what I was doing wrong. The rename is just dandy, it don…
zadjii-msft Sep 9, 2021
6265f4f
this looks like it checks the permissions as we'd expect
zadjii-msft Sep 9, 2021
1111d41
This works to do the 'blow the file away when permissions have change…
zadjii-msft Sep 13, 2021
4f697ec
I thing this was unneeded
zadjii-msft Sep 13, 2021
4da965f
Merge remote-tracking branch 'origin/main' into dev/migrie/f/non-term…
zadjii-msft Sep 13, 2021
1e3a319
more unique_sid's, and m,ore comments
zadjii-msft Sep 13, 2021
d6c2fb5
THis fixes #7754
zadjii-msft Sep 14, 2021
6be6972
This is everything from dev/migrie/f/non-terminal-content-elevation-w…
zadjii-msft Sep 14, 2021
c106f64
:facepalm:
zadjii-msft Sep 14, 2021
51e0473
minor nits
zadjii-msft Sep 14, 2021
da0cc7b
todo! in the code
zadjii-msft Sep 14, 2021
4e69a32
bunch of new allowed words
zadjii-msft Sep 14, 2021
6757452
fix the tests
zadjii-msft Sep 14, 2021
edd7126
Merge branch 'dev/migrie/f/just-elevated-state-2' into dev/migrie/f/n…
zadjii-msft Sep 14, 2021
47d55a8
don't do the lookup for things in system32
zadjii-msft Sep 14, 2021
54a0027
comments
zadjii-msft Sep 14, 2021
723037e
fix the focus thing, frick the env vars thing
zadjii-msft Sep 14, 2021
d944a68
manually allow env var parsing. Remember that wsl needs manual allows…
zadjii-msft Sep 14, 2021
56d5f9e
every day I'm mangling
zadjii-msft Sep 14, 2021
a4acdeb
blindly remove ElevatedState
zadjii-msft Sep 20, 2021
5685063
Merge remote-tracking branch 'origin/main' into dev/migrie/f/just-ele…
zadjii-msft Sep 20, 2021
d0f05f6
Merge branch 'dev/migrie/f/just-elevated-state-2' into dev/migrie/f/n…
zadjii-msft Sep 20, 2021
7f9f75c
gah, I might have broke persisting window state
zadjii-msft Sep 20, 2021
9b3b9e0
remove baseapplicationstate and just merge it back in
zadjii-msft Sep 20, 2021
5a8e27e
Merge branch 'dev/migrie/f/just-elevated-state-2' into dev/migrie/f/n…
zadjii-msft Sep 20, 2021
507a48e
Merge remote-tracking branch 'origin/main' into dev/migrie/f/just-ele…
zadjii-msft Sep 22, 2021
b1b1bef
this is a crazy idea that I hate but gotta start somewhere
zadjii-msft Sep 22, 2021
b755eb0
Merge remote-tracking branch 'origin/main' into dev/migrie/f/non-term…
zadjii-msft Sep 22, 2021
b279601
Merge branch 'dev/migrie/f/just-elevated-state-2' into dev/migrie/f/n…
zadjii-msft Sep 22, 2021
94c4cca
I think this works ALMOST as I want
zadjii-msft Sep 22, 2021
64d02f2
Move window state, approvedCommandlines into `user-state.json`
zadjii-msft Sep 22, 2021
de9dc32
pr nits
zadjii-msft Sep 22, 2021
8635537
fix hot reloading for this file
zadjii-msft Sep 22, 2021
7e2e4ea
I think this works ALMOST as I want
zadjii-msft Sep 22, 2021
f3738f5
Move window state, approvedCommandlines into `user-state.json`
zadjii-msft Sep 22, 2021
a6e044d
pr nits
zadjii-msft Sep 22, 2021
eee657b
fix hot reloading for this file
zadjii-msft Sep 22, 2021
02e9871
Merge remote-tracking branch 'origin/main' into dev/migrie/f/just-ele…
zadjii-msft Sep 22, 2021
7e2b371
Merge branch 'dev/migrie/f/just-elevated-state-2' into dev/migrie/f/n…
zadjii-msft Sep 22, 2021
5ff9a24
okay so I guess that's not a word
zadjii-msft Sep 22, 2021
62fe823
Merge branch 'dev/migrie/f/just-elevated-state-2' into dev/migrie/f/n…
zadjii-msft Sep 22, 2021
5e9d0b8
use the background from the control when we can
zadjii-msft Sep 22, 2021
a3ac32a
derp
zadjii-msft Sep 22, 2021
abb847b
pr nits from miniksa
zadjii-msft Sep 27, 2021
620ee30
Merge remote-tracking branch 'origin/main' into dev/migrie/f/just-ele…
zadjii-msft Sep 28, 2021
a751156
Merge branch 'dev/migrie/f/just-elevated-state-2' into dev/migrie/f/n…
zadjii-msft Sep 28, 2021
d053f6c
Merge remote-tracking branch 'origin/main' into dev/migrie/f/just-ele…
zadjii-msft Sep 30, 2021
5699229
Apply suggestions from code review
zadjii-msft Sep 30, 2021
866832b
This seemingly works the way I'd expect, going to merge into the warn…
zadjii-msft Sep 30, 2021
ff33387
Merge branch 'dev/migrie/f/just-elevated-state-2' into dev/migrie/f/n…
zadjii-msft Sep 30, 2021
b4e0496
cleanup
zadjii-msft Sep 30, 2021
9ff2775
Merge branch 'dev/migrie/f/just-trying-something-on-just-elevated-sta…
zadjii-msft Sep 30, 2021
48b20de
Add some logging. Can't seem to get the crash to repro?
zadjii-msft Sep 30, 2021
3c1866a
Merge remote-tracking branch 'origin/dev/migrie/b/1.12-crash-on-exit'…
zadjii-msft Sep 30, 2021
aea3752
we want this
zadjii-msft Sep 30, 2021
0e7217d
Merge commit 'aea37520b3cdb1c1752a6c8e0ff598991518ce28' into dev/migr…
zadjii-msft Sep 30, 2021
ae99ce9
teh
zadjii-msft Sep 30, 2021
f49c3fc
Merge branch 'dev/migrie/f/just-elevated-state-2' into dev/migrie/f/n…
zadjii-msft Sep 30, 2021
bf3c6e7
spel is hard
zadjii-msft Sep 30, 2021
faa06f8
Merge remote-tracking branch 'origin/main' into dev/migrie/f/just-ele…
zadjii-msft Oct 7, 2021
945c81d
Merge remote-tracking branch 'origin/main' into dev/migrie/f/just-ele…
zadjii-msft Oct 27, 2021
21d6ffe
Merge branch 'dev/migrie/f/just-elevated-state-2' into dev/migrie/f/n…
zadjii-msft Oct 27, 2021
fe5a78c
Fix OpenConsoleProxy for Debug builds
lhecker Oct 27, 2021
90b7962
Merge remote-tracking branch 'origin/dev/lhecker/proxy-no-default-lib…
zadjii-msft Oct 27, 2021
69f1068
update appearance
zadjii-msft Oct 27, 2021
242de14
Incredible that this works at all
zadjii-msft Oct 27, 2021
d6d7087
Cleanup from the experimentation phase
zadjii-msft Oct 27, 2021
7fb4906
More cleanup
zadjii-msft Oct 27, 2021
b9979ff
Esc to dismiss too
zadjii-msft Oct 28, 2021
bdf0816
spel
zadjii-msft Oct 28, 2021
3a8a83a
last pr nits
zadjii-msft Nov 1, 2021
9b4ae9e
Merge remote-tracking branch 'origin/main' into dev/migrie/f/just-ele…
zadjii-msft Nov 1, 2021
25c34df
Merge branch 'dev/migrie/f/just-elevated-state-2' into dev/migrie/f/n…
zadjii-msft Nov 1, 2021
c90eb87
good catch
zadjii-msft Nov 1, 2021
4976a09
this is a collection of the trivial nits while I wait on a reply to t…
zadjii-msft Nov 4, 2021
fd849a5
trying to do the thing eryksun mentioned. This seems to actually work…
zadjii-msft Nov 8, 2021
25b2675
this works really quite well
zadjii-msft Nov 9, 2021
b212871
this is the right way to initialize the unique_hlocal_security_descri…
zadjii-msft Nov 9, 2021
4f16dfb
many comments
zadjii-msft Nov 9, 2021
a93d17e
I believe this is the rest of the comments
zadjii-msft Nov 9, 2021
ce6a9c5
Merge remote-tracking branch 'origin/main' into dev/migrie/f/just-ele…
zadjii-msft Nov 9, 2021
c09bdbd
Merge branch 'dev/migrie/f/just-elevated-state-2' into dev/migrie/f/n…
zadjii-msft Nov 9, 2021
fdc5749
make text selectable
zadjii-msft Nov 9, 2021
5253c11
make sure event handlers get replaced, too
zadjii-msft Nov 9, 2021
1c66877
pwsh core fixed too
zadjii-msft Nov 9, 2021
7024f44
whoops
zadjii-msft Nov 10, 2021
db9cbf3
spell
zadjii-msft Nov 10, 2021
08cbd16
the last of it?
zadjii-msft Nov 10, 2021
999f21f
Merge remote-tracking branch 'origin/main' into dev/migrie/f/just-ele…
zadjii-msft Nov 11, 2021
33e96e7
mitigate a TOCTOU
zadjii-msft Nov 11, 2021
7f03d4d
dustins nits
zadjii-msft Nov 11, 2021
97d11d1
Merge branch 'dev/migrie/f/just-elevated-state-2' into dev/migrie/f/n…
zadjii-msft Nov 11, 2021
bad27a9
THIS NEEDS TO GO TO THE PARENT
zadjii-msft Nov 11, 2021
fd72b79
Merge remote-tracking branch 'origin/main' into dev/migrie/f/non-term…
zadjii-msft Nov 15, 2021
8e43c9d
more tests
zadjii-msft Nov 15, 2021
2445ced
this passes all the tests
zadjii-msft Nov 16, 2021
25947c2
cleanup for the tests"
zadjii-msft Nov 16, 2021
056446c
guard some GetActiveTerminalControl calls
zadjii-msft Nov 16, 2021
841ced7
cant type when it's 26 degrees in here
zadjii-msft Nov 16, 2021
065e5f7
fix unit test build
zadjii-msft Nov 16, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/actions/spelling/allow/allow.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
admins
apc
calt
ccmp
Expand Down
6 changes: 6 additions & 0 deletions .github/actions/spelling/allow/apis.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
ACCEPTFILES
ACCESSDENIED
acl
aclapi
alignas
alignof
APPLYTOSUBMENUS
Expand All @@ -19,6 +21,7 @@ comparand
cstdint
CXICON
CYICON
Dacl
dataobject
dcomp
DERR
Expand Down Expand Up @@ -114,9 +117,12 @@ OSVERSIONINFOEXW
otms
OUTLINETEXTMETRICW
overridable
PACL
PAGESCROLL
PEXPLICIT
PICKFOLDERS
pmr
ptstr
rcx
REGCLS
RETURNCMD
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/LocalTests_SettingsModel/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ Author(s):
// Manually include til after we include Windows.Foundation to give it winrt superpowers
#include "til.h"

#include <til/mutex.h>
#include <til/throttled_func.h>

// Common includes for most tests:
#include "../../inc/argb.h"
#include "../../inc/conattrs.hpp"
Expand Down
74 changes: 74 additions & 0 deletions src/cascadia/TerminalApp/AdminWarningPlaceholder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#pragma once

#include "pch.h"
#include "AdminWarningPlaceholder.h"
#include "AdminWarningPlaceholder.g.cpp"
#include <LibraryResources.h>
using namespace winrt::Windows::UI::Xaml;

namespace winrt::TerminalApp::implementation
{
AdminWarningPlaceholder::AdminWarningPlaceholder(const winrt::Microsoft::Terminal::Control::TermControl& control, const winrt::hstring& cmdline) :
_control{ control },
_Commandline{ cmdline }
{
InitializeComponent();
// If the content we're hosting is a TermControl, then use the control's
// BG as our BG, to give the impression that it's there, under the
// dialog.
if (const auto termControl{ control.try_as<winrt::Microsoft::Terminal::Control::TermControl>() })
{
RootGrid().Background(termControl.BackgroundBrush());
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
}
}
void AdminWarningPlaceholder::_primaryButtonClick(winrt::Windows::Foundation::IInspectable const& /*sender*/,
RoutedEventArgs const& e)
{
_PrimaryButtonClickedHandlers(*this, e);
}
void AdminWarningPlaceholder::_cancelButtonClick(winrt::Windows::Foundation::IInspectable const& /*sender*/,
RoutedEventArgs const& e)
{
_CancelButtonClickedHandlers(*this, e);
}
winrt::Windows::UI::Xaml::Controls::UserControl AdminWarningPlaceholder::Control()
{
return _control;
}

// Method Description:
// - Move the focus to the cancel button by default. This has the LOAD
// BEARING side effect of also triggering Narrator to read out the
// contents of the dialog. It's unclear why doing this works, but it does.
// - Using a LayoutUpdated event to trigger the focus change when we're
// added to the UI tree did not seem to work.
// - Whoever is adding us to the UI tree is responsible for calling this!
// Arguments:
// - <none>
// Return Value:
// - <none>
Comment on lines +42 to +52
Copy link
Member

Choose a reason for hiding this comment

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

Is there any possible way we could write a test to verify that this is happening? Like ensure that the event that's being triggered continues to happen with a UIA test that launches the warning? (Follow on issue?)

void AdminWarningPlaceholder::FocusOnLaunch()
{
CancelButton().Focus(FocusState::Programmatic);
}

winrt::hstring AdminWarningPlaceholder::ControlName()
{
return RS_(L"AdminWarningPlaceholderName");
}

void AdminWarningPlaceholder::_keyUpHandler(IInspectable const& /*sender*/,
Windows::UI::Xaml::Input::KeyRoutedEventArgs const& e)
{
// If the user presses escape, close the dialog (without confirming)
const auto key = e.OriginalKey();
if (key == winrt::Windows::System::VirtualKey::Escape)
{
_CancelButtonClickedHandlers(*this, e);
e.Handled(true);
}
}
}
51 changes: 51 additions & 0 deletions src/cascadia/TerminalApp/AdminWarningPlaceholder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*++
Copyright (c) Microsoft Corporation
Licensed under the MIT license.

Module Name:
- AdminWarningPlaceholder

Abstract:
- The AdminWarningPlaceholder is a control used to fill space in a pane and
present a warning to the user. It holds on to a real control that it should be
replaced with. It looks just like a ContentDialog, except it exists per-pane,
whereas a ContentDialog can only be added to take up the whole window.
- The caller should make sure to bind our PrimaryButtonClicked and
CancelButtonClicked events, to be informed as to which was pressed.

Author(s):
- Mike Griese - September 2021

--*/

#pragma once

#include "AdminWarningPlaceholder.g.h"
#include "../../cascadia/inc/cppwinrt_utils.h"

namespace winrt::TerminalApp::implementation
{
struct AdminWarningPlaceholder : AdminWarningPlaceholderT<AdminWarningPlaceholder>
{
AdminWarningPlaceholder(const winrt::Microsoft::Terminal::Control::TermControl& control, const winrt::hstring& cmdline);
void FocusOnLaunch();
winrt::Windows::UI::Xaml::Controls::UserControl Control();
winrt::hstring ControlName();
WINRT_CALLBACK(PropertyChanged, Windows::UI::Xaml::Data::PropertyChangedEventHandler);
WINRT_OBSERVABLE_PROPERTY(winrt::hstring, Commandline, _PropertyChangedHandlers);
TYPED_EVENT(PrimaryButtonClicked, TerminalApp::AdminWarningPlaceholder, winrt::Windows::UI::Xaml::RoutedEventArgs);
TYPED_EVENT(CancelButtonClicked, TerminalApp::AdminWarningPlaceholder, winrt::Windows::UI::Xaml::RoutedEventArgs);

private:
friend struct AdminWarningPlaceholderT<AdminWarningPlaceholder>; // friend our parent so it can bind private event handlers

winrt::Microsoft::Terminal::Control::TermControl _control{ nullptr };

void _primaryButtonClick(winrt::Windows::Foundation::IInspectable const& sender,
winrt::Windows::UI::Xaml::RoutedEventArgs const& e);
void _cancelButtonClick(winrt::Windows::Foundation::IInspectable const& sender,
winrt::Windows::UI::Xaml::RoutedEventArgs const& e);
void _keyUpHandler(Windows::Foundation::IInspectable const& sender,
Windows::UI::Xaml::Input::KeyRoutedEventArgs const& e);
};
}
12 changes: 12 additions & 0 deletions src/cascadia/TerminalApp/AdminWarningPlaceholder.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

namespace TerminalApp
{
[default_interface] runtimeclass AdminWarningPlaceholder : Windows.UI.Xaml.Controls.UserControl,
Windows.UI.Xaml.Data.INotifyPropertyChanged
{
String Commandline { get; };
String ControlName { get; };
}
}
93 changes: 93 additions & 0 deletions src/cascadia/TerminalApp/AdminWarningPlaceholder.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<!--
Copyright (c) Microsoft Corporation. All rights reserved. Licensed under
the MIT License. See LICENSE in the project root for license information.
-->
<UserControl x:Class="TerminalApp.AdminWarningPlaceholder"
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:TerminalApp"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mux="using:Microsoft.UI.Xaml.Controls"
AutomationProperties.AccessibilityView="Content"
AutomationProperties.IsDialog="True"
AutomationProperties.Name="{x:Bind ControlName, Mode=OneWay}"
PreviewKeyUp="_keyUpHandler"
mc:Ignorable="d">

<!--
We have to use two grids to be tricky here:
- The outer grid will get its background from the control it hosts, and
expands to fit its container. This will make it look like the background
fills the space available.
- The inner grid is set to Center,Center, so that the "dialog" appears
centered
-->

<Grid x:Name="RootGrid"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<Grid HorizontalAlignment="Center"
VerticalAlignment="Center">
<Border Margin="8,8,8,8"
Padding="16,8,16,8"
AllowFocusOnInteraction="True"
AutomationProperties.AccessibilityView="Raw"
AutomationProperties.IsDialog="True"
Background="{ThemeResource SystemControlBackgroundAltHighBrush}"
BorderBrush="{ThemeResource SystemAccentColor}"
BorderThickness="2,2,2,2"
CornerRadius="{ThemeResource OverlayCornerRadius}">
<StackPanel Orientation="Vertical">
<TextBlock x:Name="ApproveCommandlineWarningTitle"
x:Uid="ApproveCommandlineWarningTitle"
Padding="0,0,0,16"
HorizontalAlignment="Left"
FontSize="20"
FontWeight="Normal"
TextWrapping="WrapWholeWords" />

<TextBlock x:Name="PrefixTextBlock"
x:Uid="ApproveCommandlineWarningPrefixTextBlock"
HorizontalAlignment="Left"
TextWrapping="WrapWholeWords" />

<TextBlock x:Name="CommandlineText"
Margin="0,16,0,16"
HorizontalAlignment="Left"
FontFamily="Cascadia Mono"
Text="{x:Bind Commandline, Mode=OneWay}"
TextWrapping="WrapWholeWords" />

<TextBlock x:Name="SuffixTextBlock"
x:Uid="ApproveCommandlineWarningSuffixTextBlock"
HorizontalAlignment="Left"
TextWrapping="WrapWholeWords" />
<Grid Margin="0,8,0,8"
HorizontalAlignment="Stretch"
XYFocusKeyboardNavigation="Enabled">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button x:Name="PrimaryButton"
x:Uid="ApproveCommandlineWarning_PrimaryButton"
Grid.Column="0"
Margin="0,0,8,0"
HorizontalAlignment="Stretch"
Click="_primaryButtonClick"
IsTabStop="True"
Style="{StaticResource AccentButtonStyle}" />
<Button x:Name="CancelButton"
x:Uid="ApproveCommandlineWarning_CancelButton"
Grid.Column="1"
HorizontalAlignment="Stretch"
Click="_cancelButtonClick"
IsTabStop="True" />
</Grid>
</StackPanel>
</Border>
</Grid>
</Grid>

</UserControl>
38 changes: 12 additions & 26 deletions src/cascadia/TerminalApp/AppLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <WtExeUtils.h>
#include <wil/token_helpers.h >

#include "../../types/inc/utils.hpp"

using namespace winrt::Windows::ApplicationModel;
using namespace winrt::Windows::ApplicationModel::DataTransfer;
using namespace winrt::Windows::UI::Xaml;
Expand Down Expand Up @@ -139,28 +141,8 @@ static Documents::Run _BuildErrorRun(const winrt::hstring& text, const ResourceD
// Return Value:
// - true if the user is an administrator
static bool _isUserAdmin() noexcept
try
{
wil::unique_handle processToken{ GetCurrentProcessToken() };
const auto elevationType = wil::get_token_information<TOKEN_ELEVATION_TYPE>(processToken.get());
const auto elevationState = wil::get_token_information<TOKEN_ELEVATION>(processToken.get());
if (elevationType == TokenElevationTypeDefault && elevationState.TokenIsElevated)
{
// In this case, the user has UAC entirely disabled. This is sort of
// weird, we treat this like the user isn't an admin at all. There's no
// separation of powers, so the things we normally want to gate on
// "having special powers" doesn't apply.
//
// See GH#7754, GH#11096
return false;
}

return wil::test_token_membership(nullptr, SECURITY_NT_AUTHORITY, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS);
}
catch (...)
{
LOG_CAUGHT_EXCEPTION();
return false;
return Microsoft::Console::Utils::IsElevated();
}

namespace winrt::TerminalApp::implementation
Expand Down Expand Up @@ -907,8 +889,6 @@ namespace winrt::TerminalApp::implementation
void AppLogic::_RegisterSettingsChange()
{
const std::filesystem::path settingsPath{ std::wstring_view{ CascadiaSettings::SettingsPath() } };
const std::filesystem::path statePath{ std::wstring_view{ ApplicationState::SharedInstance().FilePath() } };

_reader.create(
settingsPath.parent_path().c_str(),
false,
Expand All @@ -917,14 +897,20 @@ namespace winrt::TerminalApp::implementation
// editors, who will write a temp file, then rename it to be the
// actual file you wrote. So listen for that too.
wil::FolderChangeEvents::FileName | wil::FolderChangeEvents::LastWriteTime,
[this, settingsBasename = settingsPath.filename(), stateBasename = statePath.filename()](wil::FolderChangeEvent, PCWSTR fileModified) {
const auto modifiedBasename = std::filesystem::path{ fileModified }.filename();
[this, settingsBasename = settingsPath.filename()](wil::FolderChangeEvent, PCWSTR fileModified) {
// DO NOT create a static reference to
// ApplicationState::SharedInstance here. See
// https://github.com/microsoft/terminal/pull/11222/files/9ff2775122a496fb8b1bcc7a0b83a64ce5b26c5f#r719627541
// for why. ApplicationState::SharedInstance already caches it's
// own static ref.

const winrt::hstring modifiedBasename{ std::filesystem::path{ fileModified }.filename().c_str() };

if (modifiedBasename == settingsBasename)
{
_reloadSettings->Run();
}
else if (modifiedBasename == stateBasename)
else if (ApplicationState::SharedInstance().IsStatePath(modifiedBasename))
{
_reloadState();
}
Expand Down
Loading