Merge in SNORT/snort3 from ~ASERBENI/snort3:arrow_scope to master
Squashed commit of the following:
commit
fa93f3dd0ff971447de8b2d85876b514a33dee85
Author: Andrii Serbeniuk <aserbeni@cisco.com>
Date: Mon Jul 11 15:31:19 2022 +0300
utils: fix arrow functions parsing
enum ScopeMetaType
{
NOT_SET = 0,
- FUNCTION, // function, arrow function
- BLOCK, // if, else, for, while, do, with, switch, try, catch, finally, block of code
- OBJECT, // object definition, class definition
+ ARROW_FUNCTION, // arrow function
+ FUNCTION, // function
+ BLOCK, // if, else, for, while, do, with, switch, try, catch, finally, block of code
+ OBJECT, // object definition, class definition
SCOPE_META_TYPE_MAX
};
enum FuncType
const char* JSTokenizer::p_scope_codes[] =
{
"invalid",
+ "arrow function",
"function",
"block",
"object",
break;
}
+ case ScopeMetaType::ARROW_FUNCTION: break;
case ScopeMetaType::BLOCK: break;
case ScopeMetaType::NOT_SET: break;
default: assert(false); return BAD_TOKEN;
{
switch (mt)
{
+ case ScopeMetaType::ARROW_FUNCTION:
case ScopeMetaType::FUNCTION:
return JSProgramScopeType::FUNCTION;
case ScopeMetaType::BLOCK:
set_ident_norm(true);
if (meta_type() == ScopeMetaType::NOT_SET)
{
- set_meta_type(ScopeMetaType::FUNCTION);
+ set_meta_type(ScopeMetaType::ARROW_FUNCTION);
EXEC(p_scope_push(meta_type()))
}
return EOS;
SECTION("Function scope - arrow function without scope")
test_scope("var f = (a,b)=> a",{GLOBAL,FUNCTION});
+ SECTION("Function scope - function call in an arrow function without scope")
+ test_scope("var f = (a,b)=> call(",{GLOBAL,FUNCTION});
+
SECTION("Function scope - method in object initialization")
test_scope("var o = { f(){",{GLOBAL,BLOCK,BLOCK});
SECTION("Function scope - arrow function without scope")
test_scope("var f = (a,b)=>a;",{GLOBAL});
+ SECTION("Function scope - function call in an arrow function without scope")
+ test_scope("var f = a=>call();",{GLOBAL});
+
SECTION("Function scope - arrow function as a function parameter")
test_scope("console.log(a=>c)",{GLOBAL});
}
}
+TEST_CASE("Function type detection", "[JSNormalizer]")
+{
+ SECTION("in arrow function")
+ {
+ test_normalization(
+ "var func = () => unescape('%62%61%72');"
+ "func = () => String.fromCodePoint(0x0062, 0x0061, 0x0072);"
+ "func = () => String.fromCharCode(0x0062, 0x0061, 0x0072);"
+ "func = () => decodeURIComponent('%62%61%72');"
+ "func = () => decodeURI('%62%61%72');",
+ "var var_0000=()=>'bar';"
+ "var_0000=()=>'bar';"
+ "var_0000=()=>'bar';"
+ "var_0000=()=>'bar';"
+ "var_0000=()=>'bar';"
+ );
+ }
+}
+
#endif // CATCH_TEST_BUILD