Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added sanitization of input string from external socket

  • Loading branch information...
commit 974ba8d8205d6d1887ab9835441ea695cafa1979 1 parent e1df506
Gianfranco Alongi authored
View
18 Two/Solution/src/command.erl
@@ -3,13 +3,17 @@
-include("command.hrl").
-spec(parse(string()) -> {error,empty_command} | #binding_command{} | #port_command{}).
-parse("") ->
+parse(Input) ->
+ Sanitized = re:replace(Input,"\r|\n","",[{return,list},global]),
+ parse_sanitized(Sanitized).
+
+parse_sanitized("") ->
{error,empty_command};
-parse("list bind") ->
+parse_sanitized("list bind") ->
#binding_command{type = list};
-parse("bind "++Rest) ->
+parse_sanitized("bind "++Rest) ->
[Type,ParseFile] = string:tokens(Rest," "),
File = case ParseFile of
"echo" -> echo;
@@ -19,21 +23,21 @@ parse("bind "++Rest) ->
arguments = [{type,Type},
{file,File}]};
-parse("unbind "++Type) ->
+parse_sanitized("unbind "++Type) ->
#binding_command{type = unbind,
arguments = [{type,Type}]};
-parse("open "++Rest) ->
+parse_sanitized("open "++Rest) ->
[Port,Type] = string:tokens(Rest," "),
#port_command{type = open,
arguments = [{port,list_to_integer(Port)},
{type,Type}]};
-parse("close "++Rest) ->
+parse_sanitized("close "++Rest) ->
#port_command{type = close,
arguments = [{port,list_to_integer(Rest)}]};
-parse("list port") ->
+parse_sanitized("list port") ->
#port_command{type = list}.
View
6 Two/Solution/test/command_tests.erl
@@ -41,5 +41,11 @@ list_port_test() ->
Command = "list port",
?assertMatch(#port_command{type = list},
command:parse(Command)).
+
+handle_input_with_rn_test() ->
+ Command = "list port\r\n",
+ ?assertMatch(#port_command{type = list},
+ command:parse(Command)).
+
Please sign in to comment.
Something went wrong with that request. Please try again.