Skip to content

Commit 614e14f

Browse files
committed
1 parent 9e48852 commit 614e14f

7 files changed

+1134
-978
lines changed

deps/rabbit/src/rabbit_amqp_filter_sql.erl

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@
1515
-export_type([parsed_expression/0]).
1616

1717
-export([parse/1,
18-
eval/2]).
18+
eval/2,
19+
is_control_char/1]).
1920

2021
%% [filtex-v1.0-wd09 7.1]
2122
-define(MAX_EXPRESSION_LENGTH, 4096).
2223
-define(MAX_TOKENS, 200).
2324

2425
-define(DEFAULT_MSG_PRIORITY, 4).
2526

26-
-define(IS_CONTROL_CHAR(C), C < 32 orelse C =:= 127).
27-
2827
-spec parse(tuple()) ->
2928
{ok, parsed_expression()} | error.
3029
parse({described, Descriptor, {utf8, SQL}}) ->
@@ -332,22 +331,14 @@ parse(Tokens, SQL) ->
332331

333332
transform_ast(Ast0, SQL) ->
334333
try rabbit_amqp_sql_ast:map(
335-
fun({identifier, Ident})
336-
when is_binary(Ident) ->
337-
{identifier, rabbit_amqp_util:section_field_name_to_atom(Ident)};
338-
({'like', _Ident, _Pattern, _Escape} = Node) ->
334+
fun({'like', _Ident, _Pattern, _Escape} = Node) ->
339335
transform_pattern_node(Node);
340336
(Node) ->
341337
Node
342338
end, Ast0) of
343339
Ast ->
344340
{ok, Ast}
345-
catch {unsupported_field_name, Name} ->
346-
rabbit_log:warning(
347-
"field name ~ts in SQL expression ~tp is unsupported",
348-
[Name, SQL]),
349-
error;
350-
{invalid_pattern, Reason} ->
341+
catch {invalid_pattern, Reason} ->
351342
rabbit_log:warning(
352343
"failed to parse LIKE pattern for SQL expression ~tp: ~tp",
353344
[SQL, Reason]),
@@ -456,7 +447,15 @@ escape_regex_char(Char0) ->
456447
end.
457448

458449
%% Let's disallow control characters in the user provided pattern.
459-
check_char(C) when ?IS_CONTROL_CHAR(C) ->
460-
throw({invalid_pattern, {prohibited_control_character, C}});
461450
check_char(C) ->
462-
C.
451+
case is_control_char(C) of
452+
true ->
453+
throw({invalid_pattern, {illegal_control_character, C}});
454+
false ->
455+
C
456+
end.
457+
458+
is_control_char(C) when C < 32 orelse C =:= 127 ->
459+
true;
460+
is_control_char(_) ->
461+
false.

deps/rabbit/src/rabbit_amqp_sql_ast.erl

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -62,54 +62,46 @@ map_2({Op, Arg1, Arg2, Arg3}, Fun) ->
6262
-ifdef(TEST).
6363
-include_lib("eunit/include/eunit.hrl").
6464

65-
has_binary_identifier_test() ->
66-
false = has_binary_identifier("TRUE"),
67-
true = has_binary_identifier("user_key_1 <> 'fake'"),
68-
false = has_binary_identifier("properties.subject = 'fake'"),
69-
70-
false = has_binary_identifier("NOT properties.group-id = 'test'"),
71-
false = has_binary_identifier("properties.group-sequence IS NULL"),
72-
false = has_binary_identifier("properties.group-sequence IS NOT NULL"),
73-
true = has_binary_identifier("NOT user_key = 'test'"),
74-
true = has_binary_identifier("custom_field IS NULL"),
75-
76-
false = has_binary_identifier("properties.group-id = 'g1' AND header.priority > 5"),
77-
false = has_binary_identifier("properties.group-sequence * 10 < 100"),
78-
false = has_binary_identifier("properties.creation-time >= 12345 OR properties.subject = 'test'"),
79-
true = has_binary_identifier("user_key = 'g1' AND header.priority > 5"),
80-
true = has_binary_identifier("header.priority > 5 AND user_key = 'g1'"),
81-
true = has_binary_identifier("custom_metric * 10 < 100"),
82-
true = has_binary_identifier("properties.creation-time >= 12345 OR user_data = 'test'"),
83-
84-
false = has_binary_identifier("properties.group-id LIKE 'group_%' ESCAPE '!'"),
85-
true = has_binary_identifier("user_tag LIKE 'group_%' ESCAPE '!'"),
86-
87-
true = has_binary_identifier("user_category IN ('g1', 'g2', 'g3')"),
88-
true = has_binary_identifier("p.group-id IN ('g1', user_key, 'g3')"),
89-
true = has_binary_identifier("p.group-id IN ('g1', 'g2', a.user_key)"),
90-
false = has_binary_identifier("p.group-id IN (p.reply-to-group-id, 'g2', 'g3')"),
91-
false = has_binary_identifier("properties.group-id IN ('g1', 'g2', 'g3')"),
92-
93-
false = has_binary_identifier(
94-
"(properties.group-sequence + 1) * 2 <= 100 AND " ++
95-
"(properties.group-id LIKE 'prod_%' OR h.priority > 5)"),
96-
true = has_binary_identifier(
97-
"(properties.group-sequence + 1) * 2 <= 100 AND " ++
98-
"(user_value LIKE 'prod_%' OR p.absolute-expiry-time < 10)"),
65+
search_test() ->
66+
false = search("TRUE"),
67+
true = search("user_key_1 <> 'fake'"),
68+
false = search("properties.subject = 'fake'"),
69+
70+
false = search("NOT properties.group_id = 'test'"),
71+
false = search("properties.group_sequence IS NULL"),
72+
false = search("properties.group_sequence IS NOT NULL"),
73+
true = search("NOT user_key = 'test'"),
74+
true = search("custom_field IS NULL"),
75+
76+
false = search("properties.group_id = 'g1' AND header.priority > 5"),
77+
false = search("properties.group_sequence * 10 < 100"),
78+
false = search("properties.creation_time >= 12345 OR properties.subject = 'test'"),
79+
true = search("user_key = 'g1' AND header.priority > 5"),
80+
true = search("header.priority > 5 AND user_key = 'g1'"),
81+
true = search("custom_metric * 10 < 100"),
82+
true = search("properties.creation_time >= 12345 OR user_data = 'test'"),
83+
84+
false = search("properties.group_id LIKE 'group_%' ESCAPE '!'"),
85+
true = search("user_tag LIKE 'group_%' ESCAPE '!'"),
86+
87+
true = search("user_category IN ('g1', 'g2', 'g3')"),
88+
true = search("p.group_id IN ('g1', user_key, 'g3')"),
89+
true = search("p.group_id IN ('g1', 'g2', a.user_key)"),
90+
false = search("p.group_id IN (p.reply_to_group_id, 'g2', 'g3')"),
91+
false = search("properties.group_id IN ('g1', 'g2', 'g3')"),
92+
93+
false = search("(properties.group_sequence + 1) * 2 <= 100 AND " ++
94+
"(properties.group_id LIKE 'prod_%' OR h.priority > 5)"),
95+
true = search("(properties.group_sequence + 1) * 2 <= 100 AND " ++
96+
"(user_value LIKE 'prod_%' OR p.absolute_expiry_time < 10)"),
9997
ok.
10098

101-
has_binary_identifier(Selector) ->
99+
search(Selector) ->
102100
{ok, Tokens, _EndLocation} = rabbit_amqp_sql_lexer:string(Selector),
103-
{ok, Ast0} = rabbit_amqp_sql_parser:parse(Tokens),
104-
Ast = map(fun({identifier, Ident}) when is_binary(Ident) ->
105-
{identifier, rabbit_amqp_util:section_field_name_to_atom(Ident)};
106-
(Node) ->
107-
Node
108-
end, Ast0),
101+
{ok, Ast} = rabbit_amqp_sql_parser:parse(Tokens),
109102
search(fun({identifier, Val}) ->
110103
is_binary(Val);
111104
(_Node) ->
112105
false
113106
end, Ast).
114-
115107
-endif.

0 commit comments

Comments
 (0)