Files
CommandForge/src/CommandForge.Wpf/Services/ShellService.cs
T
poli0981andClaude Opus 5.5 2741691c81 fix(security): open only https/mailto addresses and existing folders through the shell
ShellService.TryOpen handed any string to ShellExecute, which runs whatever it is given: a file:
address or an unknown protocol handler is a program. Its twelve callers only ever pass the
project's links, mailto addresses and the app's own folders, so anything else is now refused
and logged (IsAllowedTarget, tested).

ProcessStartSitesTests lists every file that starts a process, with what it starts, and fails
on a new one until it has been read - the tripwire beside Qodana's security rule, which leaves
those read files out (qodana.yaml).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:19:19 +07:00

71 lines
2.5 KiB
C#

using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using Serilog;
namespace CommandForge.Desktop.Services;
/// <summary>
/// <see cref="IShellService"/> backed by ShellExecute — for the project's own web pages and mail
/// addresses and the app's own folders only. ShellExecute runs whatever it is handed: a <c>file:</c>
/// address or an unknown protocol handler is a program, so anything else is refused.
/// </summary>
public sealed class ShellService : IShellService
{
/// <inheritdoc />
public bool TryOpen(string target)
{
ArgumentException.ThrowIfNullOrWhiteSpace(target);
if (!IsAllowedTarget(target, Directory.Exists))
{
Log.Warning("Refused to open {Target}: only https and mailto addresses and existing folders are opened.", target);
return false;
}
try
{
using var process = Process.Start(new ProcessStartInfo(target) { UseShellExecute = true });
return true;
}
catch (Exception ex) when (ex is Win32Exception or InvalidOperationException or FileNotFoundException)
{
Log.Warning(ex, "Could not open {Target}.", target);
return false;
}
}
/// <inheritdoc />
public bool TryRestartWindows()
{
try
{
var shutdown = Path.Combine(Environment.SystemDirectory, "shutdown.exe");
using var process = Process.Start(new ProcessStartInfo(shutdown, "/r /t 0") { UseShellExecute = false, CreateNoWindow = true });
return true;
}
catch (Exception ex) when (ex is Win32Exception or InvalidOperationException)
{
Log.Warning(ex, "Could not start the restart.");
return false;
}
}
/// <summary>
/// Pure: whether <paramref name="target"/> is a web page (<c>https:</c>), a mail address
/// (<c>mailto:</c>) or an existing folder given by its full path.
/// </summary>
public static bool IsAllowedTarget(string target, Func<string, bool> folderExists)
{
ArgumentNullException.ThrowIfNull(target);
ArgumentNullException.ThrowIfNull(folderExists);
// "C:\Users\…" parses as a file: URI too, so a path is checked as a path.
if (Uri.TryCreate(target, UriKind.Absolute, out var uri) && !uri.IsFile && !uri.IsUnc)
{
return uri.Scheme == Uri.UriSchemeHttps || uri.Scheme == Uri.UriSchemeMailto;
}
return Path.IsPathFullyQualified(target) && folderExists(target);
}
}