(self, context)
| 15 | """Various checks mostly for best-practices""" |
| 16 | |
| 17 | def node_String(self, context): |
| 18 | val = str(context.node) |
| 19 | entropy = calculate_entropy(val) |
| 20 | |
| 21 | if ENTROPY_THRESHOLD > 0 and entropy >= ENTROPY_THRESHOLD: |
| 22 | hit = Detection( |
| 23 | detection_type="HighEntropyString", |
| 24 | message="A string with high shanon entropy was found", |
| 25 | extra={ |
| 26 | "type": "high_entropy_string", |
| 27 | "entropy": entropy, |
| 28 | "string": val, |
| 29 | }, |
| 30 | signature=f"misc#high_entropy#{fast_checksum(val)}#{context.signature}", |
| 31 | node=context.node |
| 32 | ) |
| 33 | hit.line_no = context.node.line_no |
| 34 | yield hit |
| 35 | |
| 36 | # ReDoS Detection |
| 37 | # DoS attack via catastrophic regex backtracking |
| 38 | # Inspired by: |
| 39 | # https://github.com/dlint-py/dlint/blob/master/docs/linters/DUO138.md |
| 40 | # https://github.com/dlint-py/dlint/blob/master/dlint/linters/bad_re_catastrophic_use.py |
| 41 | |
| 42 | try: |
| 43 | if detect_redos.catastrophic(val): |
| 44 | hit = Detection( |
| 45 | detection_type="ReDoS", |
| 46 | message = "Possible catastrophic ReDoS", |
| 47 | extra = { |
| 48 | "type": "redos", |
| 49 | "regex": val, |
| 50 | }, |
| 51 | signature = f"misc#redos#{fast_checksum(val)}#{context.signature}", |
| 52 | node=context.node, |
| 53 | tags={"redos"} |
| 54 | ) |
| 55 | yield hit |
| 56 | except RecursionError: |
| 57 | yield Detection( |
| 58 | detection_type="Misc", |
| 59 | message="Recursion limit exceeded when scanning regex pattern for ReDoS", |
| 60 | extra={ |
| 61 | "regex": val |
| 62 | }, |
| 63 | signature=f"misc#redos_recursion_error#{fast_checksum(val)}#{context.signature}", |
| 64 | node=context.node |
| 65 | ) |
| 66 | |
| 67 | def node_FunctionDef(self, context): |
| 68 | if type(context.node.name) != str: |
nothing calls this directly
no test coverage detected