Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update(libsinsp/filter): parse wider whitespace combinations in filter expressions #2183

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions userspace/libsinsp/filter/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ static const std::vector<std::string> s_unary_ops = {"exists"};

static const std::vector<std::string> s_binary_num_ops = {"<=", "<", ">=", ">"};

// todo(jasondellaluce): we should accept any blank after these (even line breaks)
// note: by convention, we put a space at the end of operators requiring
// a blank character after them (i.e. whitespace, line break, ...)
static const std::vector<std::string> s_binary_str_ops = {
"==",
"=",
Expand Down Expand Up @@ -592,19 +593,19 @@ inline bool parser::lex_bare_str() {
}

inline bool parser::lex_unary_op() {
return lex_helper_str_list(s_unary_ops);
return lex_helper_operator_list(s_unary_ops);
}

inline bool parser::lex_num_op() {
return lex_helper_str_list(s_binary_num_ops);
return lex_helper_operator_list(s_binary_num_ops);
}

inline bool parser::lex_str_op() {
return lex_helper_str_list(s_binary_str_ops);
return lex_helper_operator_list(s_binary_str_ops);
}

inline bool parser::lex_list_op() {
return lex_helper_str_list(s_binary_list_ops);
return lex_helper_operator_list(s_binary_list_ops);
}

inline bool parser::lex_field_transformer_val() {
Expand Down Expand Up @@ -643,6 +644,25 @@ bool parser::lex_helper_str_list(const std::vector<std::string>& list) {
return false;
}

bool parser::lex_helper_operator_list(const std::vector<std::string>& list) {
for(auto& op : list) {
// if there's no ending whitespace, just parse the operator as-is
if(op.back() != ' ') {
if(lex_helper_str(op)) {
return true;
}
continue;
}

// if there's an ending whitespace, we need to make sure there's
// a blank after the operator (as long as we have an operator lexer match)
if(lex_helper_str(trim_str(op))) {
return lex_blank();
}
}
return false;
}

inline const char* parser::cursor() {
return m_input.c_str() + m_pos.idx;
}
Expand Down
1 change: 1 addition & 0 deletions userspace/libsinsp/filter/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class SINSP_PUBLIC parser {
inline bool lex_helper_rgx(const re2::RE2& rgx);
inline bool lex_helper_str(const std::string& str);
inline bool lex_helper_str_list(const std::vector<std::string>& list);
inline bool lex_helper_operator_list(const std::vector<std::string>& list);
inline const char* cursor();
inline std::string trim_str(std::string str);

Expand Down
74 changes: 51 additions & 23 deletions userspace/libsinsp/test/filter_parser.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
};

static void test_accept(string in, ast::pos_info* out_pos = NULL) {
static void do_test_accept(const std::string& in, ast::pos_info* out_pos = NULL) {
parser parser(in);
try {
parser.parse();
Expand All @@ -33,7 +33,28 @@
}
}

static void test_reject(string in) {
// if out_pos is not set, this will also play with whitespace combinations
// to make sure the parser is resilient to line breaks, tabs, etc...
static void test_accept(const std::string in, ast::pos_info* out_pos = NULL) {
do_test_accept(in, out_pos);
if(out_pos) {
return;

Check warning on line 41 in userspace/libsinsp/test/filter_parser.ut.cpp

View check run for this annotation

Codecov / codecov/patch

userspace/libsinsp/test/filter_parser.ut.cpp#L41

Added line #L41 was not covered by tests
}

// add front and back spaces
auto new_input = " " + in + " ";
do_test_accept(new_input, out_pos);

// change all spaces to line breaks
std::replace(new_input.begin(), new_input.end(), ' ', '\n');
do_test_accept(new_input, out_pos);

// change all spaces to tabs
std::replace(new_input.begin(), new_input.end(), '\n', '\t');
do_test_accept(new_input, out_pos);
}

static void test_reject(const std::string in) {
parser parser(in);
try {
parser.parse();
Expand Down Expand Up @@ -207,6 +228,8 @@
}

TEST(parser, parse_str) {
ast::pos_info tmp_pos{};

// valid bare strings
test_accept("test.str = testval");
test_accept("test.str = 0a!@#456:/\\.;!$%^&*[]{}|");
Expand Down Expand Up @@ -234,8 +257,8 @@
test_accept("test.str = 'multiple escape single quote \\' \\\\''");
test_accept("test.str = 'mixed \"'");
test_accept("test.str = \"mixed '\"");
test_accept("test.str = \"bad escape \\ \" "); // todo(jasondellaluce): reject this case in the
// future
test_accept("test.str = \"bad escape \\ \" ", &tmp_pos); // todo(jasondellaluce): reject this
// case in the future

// invalid bare strings
test_reject("test.str = a,");
Expand Down Expand Up @@ -331,8 +354,11 @@
test_accept("test.op exists and macro");
test_accept("test.op exists");
test_accept("test.op = value");
test_accept("test.op =value");
test_accept("test.op == value");
test_accept("test.op ==value");
test_accept("test.op != value");
test_accept("test.op !=value");
test_accept("test.op glob value");
test_accept("test.op iglob value");
test_accept("test.op contains value");
Expand All @@ -342,20 +368,28 @@
test_accept("test.op bstartswith 12ab001fc5");
test_accept("test.op endswith value");
test_accept("test.op > 1");
test_accept("test.op >1");
test_accept("test.op < 1");
test_accept("test.op <1");
test_accept("test.op >= 1");
test_accept("test.op >=1");
test_accept("test.op <= 1");
test_accept("test.op <=1");
test_accept("test.op in ()");
test_accept("test.op in()");
test_accept("test.op intersects ()");
test_accept("test.op intersects()");
test_accept("test.op pmatch ()");
test_accept("test.op in()");
test_accept("test.op pmatch()");

// invalid operators
test_accept("test.op existsand macro");
test_reject("test.op ExIsTs");
test_reject("test.op exists something");
test_reject("test.op ===");
test_reject("test.op === value");
test_reject("test.op !==");
test_reject("test.op !== value");
test_reject("test.op startswithvalue");
test_reject("test.op bstartswithvalue");
test_reject("test.op endswithvalue");
Expand All @@ -364,6 +398,13 @@
test_reject("test.op bcontainsvalue");
test_reject("test.op globvalue");
test_reject("test.op iglobvalue");
test_reject("test.op >");
test_reject("test.op <");
test_reject("test.op >=");
test_reject("test.op <=");
test_reject("test.op in");
test_reject("test.op intersects");
test_reject("test.op pmatch");
}

TEST(parser, parse_transformers_left_hand) {
Expand Down Expand Up @@ -391,22 +432,15 @@
// valid uses of left-hand transformers (mixed, nested, with spaces)
test_accept("b64(toupper(test.field)) exists");
test_accept("toupper(b64(test.field)) exists");
test_accept(" b64(test.field) exists");
test_accept("\nb64(test.field) exists");
test_accept("b64( test.field) exists");
test_accept("b64(\ntest.field) exists");
test_accept("b64(test.field ) exists");
test_accept("b64(test.field\n) exists");
test_accept("b64(test.field)\n exists");
test_accept("b64(test.field) exists");
test_accept("b64(b64(test.field)) exists");
test_accept("b64( b64(test.field)) exists");
test_accept("b64(\nb64(test.field)) exists");
test_accept("b64(b64( test.field)) exists");
test_accept("b64(b64(\ntest.field)) exists");
test_accept("b64(b64(test.field )) exists");
test_accept("b64(b64(test.field\n)) exists");
test_accept("b64(b64(test.field)\n) exists");
test_accept("b64(b64(test.field))\n exists");
test_accept("b64(b64(test.field) ) exists");
test_accept("b64(b64(test.field)) exists");

// invalid use of "val" left-hand transformers (can't be used in left-hand fields)
test_reject("val(test.field) exists");
Expand Down Expand Up @@ -504,19 +538,13 @@
test_accept("some.field = b64(toupper(test.field))");
test_accept("some.field = toupper(b64(test.field))");
test_accept("some.field = b64( test.field)");
test_accept("some.field = b64(\ntest.field)");
test_accept("some.field = b64(test.field )");
test_accept("some.field = b64(test.field\n)");
test_accept("some.field = b64(test.field)\n");
test_accept("some.field = b64(test.field)");
test_accept("some.field = b64(b64(test.field))");
test_accept("some.field = b64( b64(test.field))");
test_accept("some.field = b64(\nb64(test.field))");
test_accept("some.field = b64(b64( test.field))");
test_accept("some.field = b64(b64(\ntest.field))");
test_accept("some.field = b64(b64(test.field ))");
test_accept("some.field = b64(b64(test.field\n))");
test_accept("some.field = b64(b64(test.field)\n)");
test_accept("some.field = b64(b64(test.field))\n");
test_accept("some.field = b64(b64(test.field) )");

// testing left-hand transformers together with right-hand transformers
test_accept("tolower(some.field) = b64(test.field)");
Expand Down
Loading