Skip to content
Permalink
Browse files
Fix regex patterns in keys
Make sure to escape regex patterns in config keys
  • Loading branch information
nickva committed Jan 24, 2019
1 parent 0565fed commit 2734c76ee9b01848f893a06f467bfbfb88c22c37
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
@@ -31,7 +31,7 @@ save_to_file({{Section, Key}, Value}, File) ->
Lines = re:split(OldFileContents, "\r\n|\n|\r|\032", [{return, list}]),

SectionLine = "[" ++ Section ++ "]",
{ok, Pattern} = re:compile(["^(", Key, "\\s*=)|\\[[a-zA-Z0-9\.\_-]*\\]"]),
{ok, Pattern} = re:compile(["^(\\Q", Key, "\\E\\s*=)|\\[[a-zA-Z0-9\.\_-]*\\]"]),

NewLines = process_file_lines(Lines, [], SectionLine, Pattern, Key, Value),
NewFileContents = reverse_and_add_newline(strip_empty_lines(NewLines), []),
@@ -297,6 +297,20 @@ config_notifier_behaviour_test_() ->
}.


config_key_has_regex_test_() ->
{
"Test key with regex can be compiled and written to file",
{
foreach,
fun setup/0,
fun teardown/1,
[
fun should_handle_regex_patterns_in_key/0
]
}
}.


config_access_right_test_() ->
{
"Test config file access right",
@@ -318,6 +332,11 @@ should_write_config_to_file() ->
?assertEqual(ok, config:set("admins", "foo", "500", true)).


should_handle_regex_patterns_in_key() ->
?assertEqual(ok, config:set("sect1", "pat||*", "true", true)),
?assertEqual([{"pat||*", "true"}], config:get("sect1")).


should_delete_config_from_file() ->
?assertEqual(ok, config:delete("admins", "foo", true)).

0 comments on commit 2734c76

Please sign in to comment.