Skip to content

Commit b3af8ad

Browse files
committed
Add rules
1 parent 1d128e4 commit b3af8ad

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

fenjing/const.py

+2
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ class DetectWafKeywords(Enum):
267267
(STRING, "__add__"),
268268
(STRING, "__globals__"),
269269
(STRING, "__builtins__"),
270+
(STRING, "__getitem__"),
270271
(STRING, "eval"),
272+
(STRING, "bytes"),
271273
]
272274

273275
RENDER_ERROR_KEYWORDS = [

fenjing/rules/string.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,6 @@ def gen_string_lipsumtobytes4(context: dict, value: str):
529529
ints: List[Target] = join_target(
530530
sep=(LITERAL, ","), targets=[(INTEGER, ord(c)) for c in value]
531531
)
532-
# TODO: rewrite it with GENERATED_EXPR
533532
bytes_targets = targets_from_pattern(
534533
"lipsum[GLOBALS][BUILTINS][BYTES]( ( INTS ) MAYBE_COMMA)[DECODE]( )",
535534
{
@@ -579,6 +578,40 @@ def gen_string_lipsumtobytes5(context: dict, value: str):
579578
] + [(REQUIRE_PYTHON3,)]
580579

581580

581+
@expression_gen
582+
def gen_string_undefinedtobytes(context: dict, value: str):
583+
584+
bytes_targets = targets_from_pattern(
585+
"UNDEFINED|attr( ADD )|attr( GLOBALS )|attr( GETITEM )( BUILTINS )"
586+
"|attr( GETITEM )( BYTES )( ( INTS ) MAYBE_COMMA)|attr(DECODE)( )",
587+
{
588+
"UNDEFINED": (ONEOF, [
589+
[(LITERAL, "a")],
590+
[(LITERAL, "t")],
591+
[(LITERAL, "r")],
592+
[(LITERAL, "x")],
593+
]),
594+
"ADD": (GENERATED_EXPR, (STRING, "__add__")),
595+
"GLOBALS": (GENERATED_EXPR, (STRING, "__globals__")),
596+
"GETITEM": (GENERATED_EXPR, (STRING, "__getitem__")),
597+
"BUILTINS": (GENERATED_EXPR, (STRING, "__builtins__")),
598+
"BYTES": (GENERATED_EXPR, (STRING, "bytes")),
599+
" ": (WHITESPACE,),
600+
"INTS": join_target(
601+
sep=(LITERAL, ","), targets=[(INTEGER, ord(c)) for c in value]
602+
),
603+
"MAYBE_COMMA": (ONEOF, [[(LITERAL, ",")], [(LITERAL, "")]]),
604+
"DECODE": (GENERATED_EXPR, (STRING, "decode")),
605+
},
606+
)
607+
return [
608+
(
609+
EXPRESSION,
610+
precedence["function_call"],
611+
bytes_targets,
612+
)
613+
] + [(REQUIRE_PYTHON3,)]
614+
582615
@expression_gen
583616
def gen_string_formatpercent1(context: dict, value: str):
584617
# (('%c'*n)%(97,98,99))

0 commit comments

Comments
 (0)