|
| 1 | +id: hardcoded-secret-rsa-passphrase-ruby |
| 2 | +language: ruby |
| 3 | +severity: warning |
| 4 | +message: >- |
| 5 | + Found the use of an hardcoded passphrase for RSA. The passphrase can be |
| 6 | + easily discovered, and therefore should not be stored in source-code. It |
| 7 | + is recommended to remove the passphrase from source-code, and use system |
| 8 | + environment variables or a restricted configuration file. |
| 9 | +note: >- |
| 10 | + [CWE-798]: Use of Hard-coded Credentials |
| 11 | + [OWASP A07:2021]: Identification and Authentication Failures |
| 12 | + [REFERENCES] |
| 13 | + https://cwe.mitre.org/data/definitions/522.html |
| 14 | +
|
| 15 | +ast-grep-essentials: true |
| 16 | + |
| 17 | +utils: |
| 18 | + OpenSSL::PKey::RSA.new(..., '...'): |
| 19 | + kind: call |
| 20 | + all: |
| 21 | + - has: |
| 22 | + stopBy: neighbor |
| 23 | + kind: scope_resolution |
| 24 | + regex: ^OpenSSL::PKey::RSA$ |
| 25 | + - has: |
| 26 | + stopBy: neighbor |
| 27 | + regex: ^.$ |
| 28 | + - has: |
| 29 | + stopBy: neighbor |
| 30 | + kind: identifier |
| 31 | + regex: ^new$ |
| 32 | + - has: |
| 33 | + stopBy: neighbor |
| 34 | + kind: argument_list |
| 35 | + all: |
| 36 | + - has: |
| 37 | + stopBy: neighbor |
| 38 | + kind: string |
| 39 | + nthChild: 2 |
| 40 | + has: |
| 41 | + stopBy: neighbor |
| 42 | + kind: string_content |
| 43 | + |
| 44 | + OpenSSL::PKey::RSA.new(...).to_pem(..., '...'): |
| 45 | + kind: call |
| 46 | + all: |
| 47 | + - has: |
| 48 | + stopBy: neighbor |
| 49 | + kind: call |
| 50 | + pattern: OpenSSL::PKey::RSA.new($$$) |
| 51 | + - has: |
| 52 | + stopBy: neighbor |
| 53 | + regex: ^.$ |
| 54 | + - has: |
| 55 | + stopBy: neighbor |
| 56 | + kind: identifier |
| 57 | + regex: ^to_pem|export$ |
| 58 | + - has: |
| 59 | + stopBy: neighbor |
| 60 | + kind: argument_list |
| 61 | + has: |
| 62 | + stopBy: neighbor |
| 63 | + kind: string |
| 64 | + nthChild: |
| 65 | + position: 2 |
| 66 | + ofRule: |
| 67 | + not: |
| 68 | + kind: comment |
| 69 | + not: |
| 70 | + precedes: |
| 71 | + stopBy: end |
| 72 | + nthChild: 3 |
| 73 | + |
| 74 | + OpenSSL::PKey::RSA.new(..., '...')_with_instance: |
| 75 | + kind: call |
| 76 | + all: |
| 77 | + - has: |
| 78 | + stopBy: neighbor |
| 79 | + kind: scope_resolution |
| 80 | + regex: ^OpenSSL::PKey::RSA$ |
| 81 | + - has: |
| 82 | + stopBy: neighbor |
| 83 | + regex: ^.$ |
| 84 | + - has: |
| 85 | + stopBy: neighbor |
| 86 | + kind: identifier |
| 87 | + regex: ^new$ |
| 88 | + - has: |
| 89 | + stopBy: neighbor |
| 90 | + kind: argument_list |
| 91 | + all: |
| 92 | + - has: |
| 93 | + stopBy: neighbor |
| 94 | + pattern: $SECRET |
| 95 | + nthChild: 2 |
| 96 | + |
| 97 | + - inside: |
| 98 | + stopBy: end |
| 99 | + kind: class |
| 100 | + has: |
| 101 | + stopBy: end |
| 102 | + kind: assignment |
| 103 | + pattern: $SECRET = '$SECRET_VALUE' |
| 104 | + |
| 105 | + OpenSSL::PKey::RSA.new(...).to_pem(..., '...')_with_instance: |
| 106 | + kind: call |
| 107 | + all: |
| 108 | + - has: |
| 109 | + stopBy: neighbor |
| 110 | + kind: call |
| 111 | + pattern: OpenSSL::PKey::RSA.new($$$) |
| 112 | + - has: |
| 113 | + stopBy: neighbor |
| 114 | + regex: ^.$ |
| 115 | + - has: |
| 116 | + stopBy: neighbor |
| 117 | + kind: identifier |
| 118 | + regex: ^to_pem|export$ |
| 119 | + - has: |
| 120 | + stopBy: neighbor |
| 121 | + kind: argument_list |
| 122 | + all: |
| 123 | + - has: |
| 124 | + stopBy: neighbor |
| 125 | + pattern: $SECRET |
| 126 | + nthChild: 2 |
| 127 | + |
| 128 | + - inside: |
| 129 | + stopBy: end |
| 130 | + kind: class |
| 131 | + has: |
| 132 | + stopBy: end |
| 133 | + kind: assignment |
| 134 | + pattern: $SECRET = '$SECRET_VALUE' |
| 135 | + |
| 136 | + $OPENSSL.export(...,'...'): |
| 137 | + kind: call |
| 138 | + all: |
| 139 | + - has: |
| 140 | + stopBy: neighbor |
| 141 | + pattern: $OPENSSL |
| 142 | + - has: |
| 143 | + stopBy: neighbor |
| 144 | + regex: ^.$ |
| 145 | + - has: |
| 146 | + stopBy: neighbor |
| 147 | + kind: identifier |
| 148 | + regex: ^export|to_pem$ |
| 149 | + - has: |
| 150 | + stopBy: neighbor |
| 151 | + kind: argument_list |
| 152 | + all: |
| 153 | + - has: |
| 154 | + stopBy: neighbor |
| 155 | + kind: string |
| 156 | + nthChild: 2 |
| 157 | + has: |
| 158 | + stopBy: neighbor |
| 159 | + kind: string_content |
| 160 | + |
| 161 | + - inside: |
| 162 | + stopBy: end |
| 163 | + kind: class |
| 164 | + has: |
| 165 | + stopBy: end |
| 166 | + kind: assignment |
| 167 | + pattern: $OPENSSL = OpenSSL::PKey::RSA.new |
| 168 | + |
| 169 | + $OPENSSL.to_pem(...,$ASSIGN): |
| 170 | + kind: call |
| 171 | + all: |
| 172 | + - has: |
| 173 | + stopBy: neighbor |
| 174 | + pattern: $OPENSSL |
| 175 | + - has: |
| 176 | + stopBy: neighbor |
| 177 | + regex: ^.$ |
| 178 | + - has: |
| 179 | + stopBy: neighbor |
| 180 | + kind: identifier |
| 181 | + regex: ^export|to_pem$ |
| 182 | + - has: |
| 183 | + stopBy: neighbor |
| 184 | + kind: argument_list |
| 185 | + all: |
| 186 | + - has: |
| 187 | + stopBy: neighbor |
| 188 | + pattern: $SECRET |
| 189 | + nthChild: 2 |
| 190 | + - inside: |
| 191 | + stopBy: end |
| 192 | + kind: class |
| 193 | + all: |
| 194 | + - has: |
| 195 | + stopBy: end |
| 196 | + kind: assignment |
| 197 | + pattern: $OPENSSL = OpenSSL::PKey::RSA.new |
| 198 | + - has: |
| 199 | + stopBy: end |
| 200 | + kind: assignment |
| 201 | + pattern: $SECRET = '$SECRET_STRING' |
| 202 | + |
| 203 | + match_call: |
| 204 | + kind: call |
| 205 | + all: |
| 206 | + - has: |
| 207 | + stopBy: end |
| 208 | + kind: identifier |
| 209 | + field: receiver |
| 210 | + - has: |
| 211 | + stopBy: end |
| 212 | + kind: identifier |
| 213 | + field: method |
| 214 | + - has: |
| 215 | + stopBy: end |
| 216 | + kind: argument_list |
| 217 | + field: arguments |
| 218 | + all: |
| 219 | + - has: |
| 220 | + kind: call |
| 221 | + - has: |
| 222 | + kind: string |
| 223 | +rule: |
| 224 | + kind: call |
| 225 | + any: |
| 226 | + - matches: OpenSSL::PKey::RSA.new(..., '...') |
| 227 | + - matches: OpenSSL::PKey::RSA.new(...).to_pem(..., '...') |
| 228 | + - matches: OpenSSL::PKey::RSA.new(..., '...')_with_instance |
| 229 | + - matches: OpenSSL::PKey::RSA.new(...).to_pem(..., '...')_with_instance |
| 230 | + - matches: $OPENSSL.export(...,'...') |
| 231 | + - matches: $OPENSSL.to_pem(...,$ASSIGN) |
| 232 | + - matches: match_call |
0 commit comments