Skip to content

Commit

Permalink
Prevent tab reordering while elevated
Browse files Browse the repository at this point in the history
There's a platform limitation that causes us to crash when we rearrange
tabs. Xaml tries to send a drag visual (to wit: a screenshot) to the
drag hosting process, but that process is running at a different IL than
us.

For now, we're disabling elevated drag.

Fixes #3581
  • Loading branch information
DHowett committed Mar 10, 2020
1 parent d954ad6 commit f86bff2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/cascadia/TerminalApp/AppLogic.cpp
Expand Up @@ -115,6 +115,27 @@ static Documents::Run _BuildErrorRun(const winrt::hstring& text, const ResourceD
return textRun;
}

// Method Description:
// - Returns whether the user is either a member of the Administrators group or
// is currently elevated.
// Return Value:
// - true if the user is an administrator
static bool _isUserAdmin() noexcept
try
{
SID_IDENTIFIER_AUTHORITY ntAuthority{ SECURITY_NT_AUTHORITY };
wil::unique_sid adminGroupSid{};
THROW_IF_WIN32_BOOL_FALSE(AllocateAndInitializeSid(&ntAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &adminGroupSid));
BOOL b;
THROW_IF_WIN32_BOOL_FALSE(CheckTokenMembership(NULL, adminGroupSid.get(), &b));
return !!b;
}
catch (...)
{
LOG_CAUGHT_EXCEPTION();
return false;
}

namespace winrt::TerminalApp::implementation
{
AppLogic::AppLogic() :
Expand All @@ -131,6 +152,7 @@ namespace winrt::TerminalApp::implementation
// The TerminalPage has to be constructed during our construction, to
// make sure that there's a terminal page for callers of
// SetTitleBarContent
_isElevated = _isUserAdmin();
_root = winrt::make_self<TerminalPage>();
}

Expand All @@ -145,6 +167,17 @@ namespace winrt::TerminalApp::implementation
return _isUwp;
}

// Method Description:
// - Called around the codebase to discover if Terminal is running elevated
// Arguments:
// - <none> - reports internal state
// Return Value:
// - True if elevated, false otherwise.
bool AppLogic::IsElevated() const noexcept
{
return _isElevated;
}

// Method Description:
// - Called by UWP context invoker to let us know that we may have to change some of our behaviors
// for being a UWP
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/AppLogic.h
Expand Up @@ -21,6 +21,7 @@ namespace winrt::TerminalApp::implementation
void Create();
bool IsUwp() const noexcept;
void RunAsUwp();
bool IsElevated() const noexcept;
void LoadSettings();
[[nodiscard]] std::shared_ptr<::TerminalApp::CascadiaSettings> GetSettings() const noexcept;

Expand All @@ -47,6 +48,7 @@ namespace winrt::TerminalApp::implementation

private:
bool _isUwp{ false };
bool _isElevated{ false };

// If you add controls here, but forget to null them either here or in
// the ctor, you're going to have a bad time. It'll mysteriously fail to
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/AppLogic.idl
Expand Up @@ -25,6 +25,7 @@ namespace TerminalApp

Boolean IsUwp();
void RunAsUwp();
Boolean IsElevated();

Int32 SetStartupCommandline(String[] commands);
String EarlyExitMessage { get; };
Expand Down
8 changes: 8 additions & 0 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Expand Up @@ -63,6 +63,14 @@ namespace winrt::TerminalApp::implementation
_tabView = _tabRow.TabView();
_rearranging = false;

// GH#3581 - There's a platform limitation that causes us to crash when we rearrange tabs.
// Xaml tries to send a drag visual (to wit: a screenshot) to the drag hosting process,
// but that process is running at a different IL than us.
// For now, we're disabling elevated drag.
const auto isElevated = ::winrt::Windows::UI::Xaml::Application::Current().as<::winrt::TerminalApp::App>().Logic().IsElevated();
_tabView.CanReorderTabs(!isElevated);
_tabView.CanDragTabs(!isElevated);

_tabView.TabDragStarting([weakThis{ get_weak() }](auto&& /*o*/, auto&& /*a*/) {
if (auto page{ weakThis.get() })
{
Expand Down

0 comments on commit f86bff2

Please sign in to comment.