Skip to content

Commit 203e119

Browse files
author
Maciej Szlosarczyk
committed
Introduce error 2000
When a client sends invalid command to proxy, it should be reported as error 2000 and sent to registry. We should sent them there regardless
1 parent 98483da commit 203e119

6 files changed

Lines changed: 86 additions & 24 deletions

File tree

apps/epp_proxy/include/epp_proxy.hrl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,14 @@
99
}).
1010

1111
-type epp_request() :: #epp_request{}.
12+
13+
14+
-define(XMLErrorCode, <<"2001">>).
15+
16+
-define(XMLErrorMessage, <<"Command syntax error.">>).
17+
18+
-define(UnknownCommandErrorCode, <<"2000">>).
19+
20+
-define(UnknownCommandErrorMessage, <<"Unknown command.">>).
21+
22+
-define(DefaultTimeout, 120000).

apps/epp_proxy/src/epp_router.erl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-module(epp_router).
22

3-
-export([request_method/1, route_request/1]).
3+
-export([is_valid_command/1, request_method/1,
4+
route_request/1]).
45

56
-define(validCommands,
67
["hello", "login", "logout", "check", "info", "poll",
@@ -9,14 +10,22 @@
910
%% 47 is the character code
1011
-define(forwardSlash, 47).
1112

12-
%% request method: GET for greeting, POST for everything else.
13+
%% request method: GET for greeting and error, POST for everything else.
1314
request_method("hello") -> get;
1415
request_method(<<"hello">>) -> get;
1516
request_method("error") -> get;
1617
request_method(<<"error">>) -> get;
1718
request_method(_) -> post.
1819

20+
is_valid_command(Command) when is_binary(Command) ->
21+
CommandAsList = binary_to_list(Command),
22+
lists:member(CommandAsList, ?validCommands);
23+
is_valid_command(Command) when is_list(Command) ->
24+
lists:member(Command, ?validCommands);
25+
is_valid_command(_) -> false.
26+
1927
%% Base router
28+
route_request(undefined) -> url_map("error");
2029
route_request(Command) when is_binary(Command) ->
2130
List = binary_to_list(Command), url_map(List);
2231
route_request(Command) when is_list(Command) ->

apps/epp_proxy/src/epp_tcp_worker.erl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@
1818

1919
-record(state, {socket, session_id, headers}).
2020

21-
-define(XMLErrorCode, <<"2001">>).
22-
23-
-define(XMLErrorMessage, <<"Command syntax error.">>).
24-
25-
-define(DefaultTimeout, 120000).
26-
2721
%% Initialize process
2822
%% Assign an unique session id that will be passed on to http server as a cookie
2923
init(Socket) ->
@@ -177,8 +171,15 @@ parse_frame(Frame) ->
177171
case epp_xml:parse(Frame) of
178172
{ok, XMLRecord} ->
179173
Command = epp_xml:get_command(XMLRecord),
180-
#valid_frame{command = Command, cl_trid = ClTRID,
181-
raw_frame = Frame};
174+
case epp_router:is_valid_command(Command) of
175+
true ->
176+
#valid_frame{command = Command, cl_trid = ClTRID,
177+
raw_frame = Frame};
178+
false ->
179+
#invalid_frame{code = ?UnknownCommandErrorCode,
180+
message = ?UnknownCommandErrorMessage,
181+
cl_trid = ClTRID}
182+
end;
182183
{error, _} ->
183184
#invalid_frame{code = ?XMLErrorCode,
184185
message = ?XMLErrorMessage, cl_trid = ClTRID}

apps/epp_proxy/src/epp_tls_worker.erl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@
1818

1919
-record(state, {socket, session_id, headers}).
2020

21-
-define(XMLErrorCode, <<"2001">>).
22-
23-
-define(XMLErrorMessage, <<"Command syntax error.">>).
24-
25-
-define(DefaultTimeout, 120000).
26-
2721
%% Initialize process
2822
%% Assign an unique session id that will be passed on to http server as a cookie
2923
init(Socket) ->
@@ -197,8 +191,15 @@ parse_frame(Frame) ->
197191
case epp_xml:parse(Frame) of
198192
{ok, XMLRecord} ->
199193
Command = epp_xml:get_command(XMLRecord),
200-
#valid_frame{command = Command, cl_trid = ClTRID,
201-
raw_frame = Frame};
194+
case epp_router:is_valid_command(Command) of
195+
true ->
196+
#valid_frame{command = Command, cl_trid = ClTRID,
197+
raw_frame = Frame};
198+
false ->
199+
#invalid_frame{code = ?UnknownCommandErrorCode,
200+
message = ?UnknownCommandErrorMessage,
201+
cl_trid = ClTRID}
202+
end;
202203
{error, _} ->
203204
#invalid_frame{code = ?XMLErrorCode,
204205
message = ?XMLErrorMessage, cl_trid = ClTRID}

apps/epp_proxy/test/tcp_client_SUITE.erl

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
valid_command_test_case/1,
1212
long_message_test_case/1,
1313
invalid_command_test_case/1,
14+
missing_command_test_case/1,
1415
error_test_case/1]).
1516

1617
all() ->
@@ -20,6 +21,7 @@ all() ->
2021
valid_command_test_case,
2122
long_message_test_case,
2223
invalid_command_test_case,
24+
missing_command_test_case,
2325
error_test_case].
2426

2527
init_per_suite(Config) ->
@@ -133,8 +135,7 @@ long_message_test_case(Config) ->
133135
"Command completed successfully; no messages"),
134136
ok.
135137

136-
%% Sending an invalid command frame should close the connection.
137-
%% It also crashes the process.
138+
%% Sending an invalid command frame should return a canned response.
138139
invalid_command_test_case(Config) ->
139140
Options = proplists:get_value(tcp_options, Config),
140141
{ok, Socket} = gen_tcp:connect("localhost", 1180, Options, 2000),
@@ -150,7 +151,26 @@ invalid_command_test_case(Config) ->
150151
"</command>\n"
151152
"</epp>\n">>,
152153
ok = send_data(InvalidCommand, Socket),
153-
{error, closed} = receive_data(Socket),
154+
ErrorResponse = receive_data(Socket),
155+
match_data(ErrorResponse,
156+
"Unknown command."),
157+
ok.
158+
159+
%% Sending a valid XML without command frame should return a canned response.
160+
missing_command_test_case(Config) ->
161+
Options = proplists:get_value(tcp_options, Config),
162+
{ok, Socket} = gen_tcp:connect("localhost", 1180, Options, 2000),
163+
_Data = receive_data(Socket),
164+
ok = send_data(login_command(), Socket),
165+
_LoginResponse = receive_data(Socket),
166+
InvalidCommand =
167+
<<"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
168+
"<epp xmlns=\"https://epp.tld.ee/schema/epp-ee-1.0.xsd\">\n"
169+
"</epp>\n">>,
170+
ok = send_data(InvalidCommand, Socket),
171+
ErrorResponse = receive_data(Socket),
172+
match_data(ErrorResponse,
173+
"Unknown command."),
154174
ok.
155175

156176
error_test_case(Config) ->

apps/epp_proxy/test/tls_client_SUITE.erl

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
valid_command_test_case/1,
1212
long_message_test_case/1,
1313
invalid_command_test_case/1,
14+
missing_command_test_case/1,
1415
error_test_case/1,
1516
revoked_cert_test_case/1]).
1617

@@ -21,6 +22,7 @@ all() ->
2122
valid_command_test_case,
2223
long_message_test_case,
2324
invalid_command_test_case,
25+
missing_command_test_case,
2426
error_test_case,
2527
revoked_cert_test_case].
2628

@@ -142,8 +144,7 @@ long_message_test_case(Config) ->
142144
"Command completed successfully; no messages"),
143145
ok.
144146

145-
%% Sending an invalid command frame should close the connection.
146-
%% It also crashes the process.
147+
%% Sending an invalid command frame returns a canned response.
147148
invalid_command_test_case(Config) ->
148149
Options = proplists:get_value(ssl_options, Config),
149150
{ok, Socket} = ssl:connect("localhost", 1443, Options, 2000),
@@ -159,7 +160,26 @@ invalid_command_test_case(Config) ->
159160
"</command>\n"
160161
"</epp>\n">>,
161162
ok = send_data(InvalidCommand, Socket),
162-
{error, closed} = receive_data(Socket),
163+
ErrorResponse = receive_data(Socket),
164+
match_data(ErrorResponse,
165+
"Unknown command."),
166+
ok.
167+
168+
%% Sending a missing command frame should return a canned response.
169+
missing_command_test_case(Config) ->
170+
Options = proplists:get_value(ssl_options, Config),
171+
{ok, Socket} = ssl:connect("localhost", 1443, Options, 2000),
172+
_Data = receive_data(Socket),
173+
ok = send_data(login_command(), Socket),
174+
_LoginResponse = receive_data(Socket),
175+
InvalidCommand =
176+
<<"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
177+
"<epp xmlns=\"https://epp.tld.ee/schema/epp-ee-1.0.xsd\">\n"
178+
"</epp>\n">>,
179+
ok = send_data(InvalidCommand, Socket),
180+
ErrorResponse = receive_data(Socket),
181+
match_data(ErrorResponse,
182+
"Unknown command."),
163183
ok.
164184

165185
error_test_case(Config) ->

0 commit comments

Comments
 (0)