terminal/src/cascadia/TerminalApp/TerminalWarnings.h
Michael Kitzan 77dd51af39 Fix crash related to unparseable/invalid media resource paths (#4194)
WT crashes when an unparseable/invalid `backgroundImage` or `icon`
resource path is provided in `profiles.json`. This PR averts the crash
by the validating and correcting resource paths as a part of the
`_ValidateSettings()` function in `CascadiaSettings`.
`_ValidateSettings()` is run on start up and any time `profiles.json` is
changed, so a user can not change a file path and avoid the validation
step. 

When a bad `backgroundImage` or `icon` resource path is detected, a
warning screen will be presented.

References #4002, which identified a consistent repro for the crash.

To validate the resource, a `Windows::Foundation::Uri` object is
constructed with the path. The ctor will throw if the resource path is
invalid. Whether or not this validation method is robust enough is a
subject worth review. The correction method for when a bad resource path
is detected is to reset the `std::optional<winrt::hstring>` holding the
file path. 

The text in the warning display was cribbed from the text used when an
invalid `colorScheme` is used. Whether or not the case of a bad
background image file path warrants a warning display is a subject worth
review.

Ensured the repro steps in #4002 did not trigger a crash. Additionally,
some potential backdoor paths to a crash were tested: 

- Deleting the file of a validated background image file path
- Changing the actual file name of a validated background image file
  path
- Replacing the file of a validated background image file path with a
  non-image file (of the same name)
- Using a non-image file as a background image

In all the above cases WT does not crash, and instead defaults to the
background color specified in the profile's `colorScheme`. This PR does
not implement this recovery behavior (existing error catching code
does).

Closes #2329
2020-01-16 17:48:37 -08:00

62 lines
1.7 KiB
C++

/*++
Copyright (c) Microsoft Corporation
Licensed under the MIT license.
Module Name:
- TerminalWarnings.h
Abstract:
- This file contains definitions for warnings, errors and exceptions used by the
Windows Terminal
Author(s):
- Mike Griese - August 2019
--*/
#pragma once
namespace TerminalApp
{
// SettingsLoadWarnings are scenarios where the settings contained
// information we knew was invalid, but we could recover from.
enum class SettingsLoadWarnings : uint32_t
{
MissingDefaultProfile = 0,
DuplicateProfile = 1,
UnknownColorScheme = 2,
InvalidBackgroundImage = 3,
InvalidIcon = 4
};
// SettingsLoadWarnings are scenarios where the settings had invalid state
// that we could not recover from.
enum class SettingsLoadErrors : uint32_t
{
NoProfiles = 0,
AllProfilesHidden = 1
};
// This is a helper class to wrap up a SettingsLoadErrors into a proper
// exception type.
class SettingsException : public std::runtime_error
{
public:
SettingsException(const SettingsLoadErrors& error) :
std::runtime_error{ nullptr },
_error{ error } {};
// We don't use the what() method - we want to be able to display
// localizable error messages. Catchers of this exception should use
// _GetErrorText (in App.cpp) to get the localized exception string.
const char* what() const override
{
return "Exception while loading or validating Terminal settings";
};
SettingsLoadErrors Error() const noexcept { return _error; };
private:
const SettingsLoadErrors _error;
};
};