Skip to content

Commit 0e41477

Browse files
committed
Guardrails off on localhost
1 parent a5b20e7 commit 0e41477

1 file changed

Lines changed: 38 additions & 10 deletions

File tree

ArchipelagoDebugClient/ViewModels/MainViewModel.cs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using ReactiveUI;
55
using System;
66
using System.Linq;
7+
using System.Net;
78
using System.Reactive;
89
using System.Reactive.Linq;
910
using System.Threading.Tasks;
@@ -91,20 +92,13 @@ private async Task ConnectAsync()
9192

9293
if (infoLogin is LoginSuccessful success)
9394
{
94-
int playerCount = session.Players.AllPlayers.Count(p => !p.IsGroup);
9595
string game = session.Players.ActivePlayer.Game;
96-
bool isRaceMode = session.DataStorage.GetRaceMode();
96+
string? guardRailError = await GetGuardRailErrorMessage(session);
9797
await session.Socket.DisconnectAsync();
9898

99-
// always includes player for server
100-
if (playerCount > 3)
99+
if (guardRailError != null)
101100
{
102-
ErrorMessage = "Debug client only supports connecting to games with 2 or fewer players to prevent abuse";
103-
return;
104-
}
105-
if (isRaceMode)
106-
{
107-
ErrorMessage = "Debug client cannot be used in race mode to prevent abuse";
101+
ErrorMessage = guardRailError;
108102
return;
109103
}
110104

@@ -148,6 +142,40 @@ private async Task<LoginResult> TryConnectAndLoginAsync(ArchipelagoSession sessi
148142
new Version(0, 5, 0), tags: tags, password: password, requestSlotData: false);
149143
}
150144

145+
private async Task<string?> GetGuardRailErrorMessage(IArchipelagoSession session)
146+
{
147+
// games on localhost are always safe - malevolent hosts can already use admin console here
148+
// if they want to grief
149+
string trimmedAddress = Address;
150+
if (trimmedAddress.Split("://") is [_, string hostPort])
151+
{
152+
trimmedAddress = hostPort;
153+
}
154+
if (trimmedAddress.Split(":") is [string hostname, ..])
155+
{
156+
trimmedAddress = hostname;
157+
}
158+
IPHostEntry hostEntry = await Dns.GetHostEntryAsync(trimmedAddress);
159+
if (hostEntry.AddressList.Any(addr => addr.Equals(IPAddress.Loopback) || addr.Equals(IPAddress.IPv6Loopback)))
160+
{
161+
return null;
162+
}
163+
164+
int playerCount = session.Players.AllPlayers.Count(p => !p.IsGroup);
165+
bool isRaceMode = session.DataStorage.GetRaceMode();
166+
167+
// always includes player for server
168+
if (playerCount > 3)
169+
{
170+
return "Debug client only supports connecting to non-local games with 2 or fewer players to prevent abuse";
171+
}
172+
if (isRaceMode)
173+
{
174+
return "Debug client cannot be used in race mode to prevent abuse";
175+
}
176+
return null;
177+
}
178+
151179
private string BuildErrorForFailedLogin(LoginFailure failure)
152180
{
153181
string errors = string.Join(", ", failure.Errors);

0 commit comments

Comments
 (0)