Skip to content

Commit 99312bf

Browse files
committed
prevent running application as admin
1 parent d366c04 commit 99312bf

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

Gui/PlatformSpecific.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,42 @@
77
using System.Diagnostics;
88
using System.IO;
99
using System.Runtime.InteropServices;
10+
using System.Security.Principal;
1011
using System.Threading.Tasks;
1112

1213
namespace OpenLoco.Gui
1314
{
15+
// For Unix-like systems (Linux, macOS)
16+
static class UnixUserChecker
17+
{
18+
// Imports the geteuid() function from libc (the standard C library)
19+
// geteuid() returns the effective user ID of the calling process.
20+
// A value of 0 typically indicates the root user.
21+
[DllImport("libc", EntryPoint = "geteuid")]
22+
internal static extern uint geteuid();
23+
24+
public static bool IsRoot()
25+
=> geteuid() == 0;
26+
}
27+
1428
public static class PlatformSpecific
1529
{
30+
public static bool RunningAsAdmin()
31+
{
32+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
33+
{
34+
var identity = WindowsIdentity.GetCurrent();
35+
var principal = new WindowsPrincipal(identity);
36+
return principal.IsInRole(WindowsBuiltInRole.Administrator);
37+
}
38+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
39+
{
40+
return UnixUserChecker.IsRoot();
41+
}
42+
43+
return false;
44+
}
45+
1646
public static void FolderOpenInDesktop(string directory, ILogger logger)
1747
{
1848
try

Gui/Program.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,26 @@ class Program
1111
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
1212
// yet and stuff might break.
1313
[STAThread]
14-
public static void Main(string[] args) => BuildAvaloniaApp()
15-
.StartWithClassicDesktopLifetime(args);
14+
public static void Main(string[] args)
15+
{
16+
PreventRunningAsAdmin();
17+
_ = BuildAvaloniaApp()
18+
.StartWithClassicDesktopLifetime(args);
19+
}
20+
21+
static void PreventRunningAsAdmin()
22+
{
23+
if (PlatformSpecific.RunningAsAdmin())
24+
{
25+
const string errorMessage = "This application should not be run with elevated privileges. Please run it as a regular user.";
26+
27+
// show user a message. must be OS-independent
28+
Console.Error.WriteLine(errorMessage);
29+
30+
// terminate current program
31+
throw new UnauthorizedAccessException(errorMessage);
32+
}
33+
}
1634

1735
// Avalonia configuration, don't remove; also used by visual designer.
1836
public static AppBuilder BuildAvaloniaApp()

Gui/app.manifest

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
33
<!-- This manifest is used on Windows only.
4-
Don't remove it as it might cause problems with window transparency and embeded controls.
5-
For more details visit https://learn.microsoft.com/en-us/windows/win32/sbscs/application-manifests -->
4+
Don't remove it as it might cause problems with window transparency and embedded controls.
5+
For more details visit https://learn.microsoft.com/en-us/windows/win32/sbscs/application-manifests -->
66
<assemblyIdentity version="1.0.0.0" name="OpenLoco.Gui.Desktop"/>
77

88
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
99
<application>
1010
<!-- A list of the Windows versions that this application has been tested on
11-
and is designed to work with. Uncomment the appropriate elements
12-
and Windows will automatically select the most compatible environment. -->
11+
and is designed to work with. Uncomment the appropriate elements
12+
and Windows will automatically select the most compatible environment. -->
1313

1414
<!-- Windows 10 -->
1515
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />

0 commit comments

Comments
 (0)