Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsModel/FileUtils.cpp (C++)

Code before:

            return parentDirectoryForSettingsFile;
        }();
        return baseSettingsPath;
    }

    // Returns a path like C:\Users\<username>\AppData\Local\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState
    // to the path of the stable release settings
    std::filesystem::path GetReleaseSettingsPath()
    {
        static std::filesystem::path baseSettingsPath = []() {
            wil::unique_cotaskmem_string localAppDataFolder;
            // KF_FLAG_FORCE_APP_DATA_REDIRECTION, when engaged, causes SHGet... to return
            // the new AppModel paths (Packages/xxx/RoamingState, etc.) for standard path requests.
            // Using this flag allows us to avoid Windows.Storage.ApplicationData completely.
            THROW_IF_FAILED(SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_NO_PACKAGE_REDIRECTION, nullptr, &localAppDataFolder));

            // Returns a path like C:\Users\<username>\AppData\Local
            std::filesystem::path parentDirectoryForSettingsFile{ localAppDataFolder.get() };

            //Appending \Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState to the settings path
            parentDirectoryForSettingsFile /= ReleaseSettingsFolder;

            if (!IsPackaged())
            {
                parentDirectoryForSettingsFile /= UnpackagedSettingsFolderName;
            }

            return parentDirectoryForSettingsFile;
        }();
        return baseSettingsPath;
    }

    // Function Description:
    // - Checks the permissions on this file, to make sure it can only be opened
    //   for writing by admins. We will be checking to see if the file is owned
    //   by the Builtin\Administrators group. If it's not, then it was likely
    //   tampered with.
    // Arguments:
    // - handle: a HANDLE to the file to check
    // Return Value:
    // - true if it had the expected permissions. False otherwise.
    static bool _isOwnedByAdministrators(const HANDLE& handle)
    {
        // If the file is owned by the administrators group, trust the
        // administrators instead of checking the DACL permissions. It's simpler
        // and more flexible.

        wil::unique_hlocal_security_descriptor sd;
        PSID psidOwner{ nullptr };
        // The psidOwner pointer references the security descriptor, so it

Review comment (bug):
I'm not sure this is right for unpackaged versions of the Terminal. I might want to tap in @DHowett for this bit, hes a bit more familiar with running the Terminal unpackaged. 

Just looking at my own files, I've only got a `%localAppData%\Microsoft\Windows Terminal`.

Heck, I'm not sure this is ever relevant for Preview builds running unpackaged - I think they always just use `%localAppData%\Microsoft\Windows Terminal` anyways. I might be wrong here. 

Code after:

            return parentDirectoryForSettingsFile;
        }();
        return baseSettingsPath;
    }

    // Returns a path like C:\Users\<username>\AppData\Local\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState
    // to the path of the stable release settings
    std::filesystem::path GetReleaseSettingsPath()
    {
        static std::filesystem::path baseSettingsPath = []() {
            wil::unique_cotaskmem_string localAppDataFolder;
            // We're using KF_FLAG_NO_PACKAGE_REDIRECTION to ensure that we always get the
            // user's actual local AppData directory.
            THROW_IF_FAILED(SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_NO_PACKAGE_REDIRECTION, nullptr, &localAppDataFolder));

            // Returns a path like C:\Users\<username>\AppData\Local
            std::filesystem::path parentDirectoryForSettingsFile{ localAppDataFolder.get() };

            //Appending \Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState to the settings path
            parentDirectoryForSettingsFile /= ReleaseSettingsFolder;

            if (!IsPackaged())
            {
                parentDirectoryForSettingsFile /= UnpackagedSettingsFolderName;
            }

            return parentDirectoryForSettingsFile;
        }();
        return baseSettingsPath;
    }

    // Function Description:
    // - Checks the permissions on this file, to make sure it can only be opened
    //   for writing by admins. We will be checking to see if the file is owned
    //   by the Builtin\Administrators group. If it's not, then it was likely
    //   tampered with.
    // Arguments:
    // - handle: a HANDLE to the file to check
    // Return Value:
    // - true if it had the expected permissions. False otherwise.
    static bool _isOwnedByAdministrators(const HANDLE& handle)
    {
        // If the file is owned by the administrators group, trust the
        // administrators instead of checking the DACL permissions. It's simpler
        // and more flexible.

        wil::unique_hlocal_security_descriptor sd;
        PSID psidOwner{ nullptr };
        // The psidOwner pointer references the security descriptor, so it
        // doesn't have to be freed separate from sd.