Skip to content

Commit e55eb51

Browse files
committed
Deny unauthenticated commands
1 parent aa233ac commit e55eb51

1 file changed

Lines changed: 24 additions & 11 deletions

File tree

lib/teiserver/protocols/spring/spring_in.ex

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ defmodule Teiserver.Protocols.SpringIn do
3434

3535
@action_commands ~w(SAY SAYEX SAYPRIVATE SAYBATTLE SAYBATTLEPRIVATEEX JOINBATTLE LEAVEBATTLE)
3636

37+
# Commands that don't require the user to be logged in
38+
@unauthenticated_commands ~w(
39+
PING STLS LOGIN REGISTER CONFIRMAGREEMENT RESETPASSWORDREQUEST EXIT CHANGEPASSWORD
40+
)
41+
3742
@spec data_in(String.t(), map()) :: map()
3843
def data_in(data, state) do
3944
if Config.get_site_config_cache("debug.Print incoming messages") or
@@ -81,22 +86,30 @@ defmodule Teiserver.Protocols.SpringIn do
8186
state =
8287
case tuple do
8388
{command, data, msg_id} ->
84-
start = :erlang.monotonic_time(:millisecond)
89+
if command not in @unauthenticated_commands and state.userid == nil do
90+
Logger.info("Unauthenticated command '#{command} #{data}' from #{state.ip}")
8591

86-
state = do_handle(command, data, msg_id, state)
92+
reply(:denied, "Unauthenticated", msg_id, state)
8793

88-
elapsed = :erlang.monotonic_time(:millisecond) - start
94+
state
95+
else
96+
start = :erlang.monotonic_time(:millisecond)
8997

90-
command = if state.last_message_invalid, do: "INVALID", else: command
98+
state = do_handle(command, data, msg_id, state)
9199

92-
:telemetry.execute([:spring, :in], %{duration: elapsed, count: 1}, %{
93-
command: command
94-
})
100+
elapsed = :erlang.monotonic_time(:millisecond) - start
95101

96-
if Enum.member?(@action_commands, command) do
97-
Map.put(state, :last_action_timestamp, System.system_time(:second))
98-
else
99-
state
102+
command = if state.last_message_invalid, do: "INVALID", else: command
103+
104+
:telemetry.execute([:spring, :in], %{duration: elapsed, count: 1}, %{
105+
command: command
106+
})
107+
108+
if Enum.member?(@action_commands, command) do
109+
Map.put(state, :last_action_timestamp, System.system_time(:second))
110+
else
111+
state
112+
end
100113
end
101114

102115
nil ->

0 commit comments

Comments
 (0)