|
| 1 | +id: python-mysql-empty-password-python |
| 2 | +severity: warning |
| 3 | +language: python |
| 4 | +message: >- |
| 5 | + The application creates a database connection with an empty password. |
| 6 | + This can lead to unauthorized access by either an internal or external |
| 7 | + malicious actor. To prevent this vulnerability, enforce authentication |
| 8 | + when connecting to a database by using environment variables to securely |
| 9 | + provide credentials or retrieving them from a secure vault or HSM |
| 10 | + (Hardware Security Module). |
| 11 | +note: >- |
| 12 | + [CWE-287] Improper Authentication. |
| 13 | + [REFERENCES] |
| 14 | + - https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html |
| 15 | +
|
| 16 | +ast-grep-essentials: true |
| 17 | + |
| 18 | +utils: |
| 19 | + define_string: |
| 20 | + kind: string |
| 21 | + all: |
| 22 | + - has: |
| 23 | + kind: string_start |
| 24 | + nthChild: 1 |
| 25 | + # - has: |
| 26 | + # kind: string_content |
| 27 | + # nthChild: 2 |
| 28 | + - has: |
| 29 | + kind: string_end |
| 30 | + nthChild: 2 |
| 31 | + |
| 32 | + define_password: |
| 33 | + any: |
| 34 | + - matches: define_string |
| 35 | + - kind: identifier |
| 36 | + pattern: $PWD_IDENTIFIER |
| 37 | + inside: |
| 38 | + stopBy: end |
| 39 | + follows: |
| 40 | + stopBy: end |
| 41 | + kind: expression_statement |
| 42 | + has: |
| 43 | + stopBy: end |
| 44 | + kind: assignment |
| 45 | + nthChild: 1 |
| 46 | + all: |
| 47 | + - has: |
| 48 | + nthChild: 1 |
| 49 | + kind: identifier |
| 50 | + field: left |
| 51 | + pattern: $PWD_IDENTIFIER |
| 52 | + - has: |
| 53 | + nthChild: 2 |
| 54 | + matches: define_string |
| 55 | + |
| 56 | +rule: |
| 57 | + any: |
| 58 | + - kind: call |
| 59 | + any: |
| 60 | + - kind: call |
| 61 | + has: |
| 62 | + kind: identifier |
| 63 | + regex: ^connect$ |
| 64 | + precedes: |
| 65 | + kind: argument_list |
| 66 | + has: |
| 67 | + stopBy: end |
| 68 | + kind: keyword_argument |
| 69 | + all: |
| 70 | + - has: |
| 71 | + nthChild: 1 |
| 72 | + kind: identifier |
| 73 | + regex: ^(password|passwd)$ |
| 74 | + - has: |
| 75 | + nthChild: 2 |
| 76 | + matches: define_password |
| 77 | + inside: |
| 78 | + stopBy: end |
| 79 | + follows: |
| 80 | + stopBy: end |
| 81 | + kind: import_from_statement |
| 82 | + all: |
| 83 | + - has: |
| 84 | + nthChild: 1 |
| 85 | + kind: dotted_name |
| 86 | + field: module_name |
| 87 | + regex: ^mysql.connector$ |
| 88 | + precedes: |
| 89 | + stopBy: end |
| 90 | + kind: dotted_name |
| 91 | + regex: ^connect$ |
| 92 | + - kind: call |
| 93 | + any: |
| 94 | + - kind: call |
| 95 | + has: |
| 96 | + kind: identifier |
| 97 | + pattern: $SASL_ALIAS |
| 98 | + precedes: |
| 99 | + kind: argument_list |
| 100 | + has: |
| 101 | + stopBy: end |
| 102 | + kind: keyword_argument |
| 103 | + all: |
| 104 | + - has: |
| 105 | + nthChild: 1 |
| 106 | + kind: identifier |
| 107 | + regex: ^(password|passwd)$ |
| 108 | + - has: |
| 109 | + nthChild: 2 |
| 110 | + matches: define_password |
| 111 | + inside: |
| 112 | + stopBy: end |
| 113 | + follows: |
| 114 | + stopBy: end |
| 115 | + kind: import_from_statement |
| 116 | + all: |
| 117 | + - has: |
| 118 | + nthChild: 1 |
| 119 | + kind: dotted_name |
| 120 | + field: module_name |
| 121 | + regex: ^mysql.connector$ |
| 122 | + precedes: |
| 123 | + stopBy: end |
| 124 | + kind: aliased_import |
| 125 | + all: |
| 126 | + - has: |
| 127 | + kind: dotted_name |
| 128 | + nthChild: 1 |
| 129 | + regex: ^connect$ |
| 130 | + - has: |
| 131 | + kind: identifier |
| 132 | + field: alias |
| 133 | + nthChild: 2 |
| 134 | + pattern: $SASL_ALIAS |
| 135 | + - kind: call |
| 136 | + any: |
| 137 | + - kind: call |
| 138 | + has: |
| 139 | + kind: attribute |
| 140 | + all: |
| 141 | + - has: |
| 142 | + kind: identifier |
| 143 | + field: object |
| 144 | + nthChild: 1 |
| 145 | + pattern: $MYSQL_ALIAS |
| 146 | + - has: |
| 147 | + kind: identifier |
| 148 | + field: attribute |
| 149 | + nthChild: 2 |
| 150 | + regex: ^connect$ |
| 151 | + precedes: |
| 152 | + kind: argument_list |
| 153 | + has: |
| 154 | + stopBy: end |
| 155 | + kind: keyword_argument |
| 156 | + all: |
| 157 | + - has: |
| 158 | + nthChild: 1 |
| 159 | + kind: identifier |
| 160 | + regex: ^(password|passwd)$ |
| 161 | + - has: |
| 162 | + nthChild: 2 |
| 163 | + matches: define_password |
| 164 | + inside: |
| 165 | + stopBy: end |
| 166 | + follows: |
| 167 | + stopBy: end |
| 168 | + kind: import_statement |
| 169 | + has: |
| 170 | + nthChild: 1 |
| 171 | + kind: aliased_import |
| 172 | + all: |
| 173 | + - has: |
| 174 | + nthChild: 1 |
| 175 | + kind: dotted_name |
| 176 | + field: name |
| 177 | + regex: ^mysql.connector$ |
| 178 | + precedes: |
| 179 | + stopBy: end |
| 180 | + kind: identifier |
| 181 | + pattern: $MYSQL_ALIAS |
| 182 | + - kind: call |
| 183 | + any: |
| 184 | + - kind: call |
| 185 | + has: |
| 186 | + kind: attribute |
| 187 | + field: function |
| 188 | + nthChild: 1 |
| 189 | + regex: ^mysql.connector.connect$ |
| 190 | + precedes: |
| 191 | + kind: argument_list |
| 192 | + has: |
| 193 | + stopBy: end |
| 194 | + kind: keyword_argument |
| 195 | + all: |
| 196 | + - has: |
| 197 | + nthChild: 1 |
| 198 | + kind: identifier |
| 199 | + regex: ^(password|passwd)$ |
| 200 | + - has: |
| 201 | + nthChild: 2 |
| 202 | + matches: define_password |
0 commit comments