Skip to content

Commit 8cb6bbc

Browse files
committed
python-pg8000-hardcoded-secret-python
1 parent 351d8fc commit 8cb6bbc

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
id: python-pg8000-hardcoded-secret-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+
pg8000.dbapi.connect(..., password="...",...):
20+
# pg8000.dbapi.connect(..., password="...",...)
21+
kind: call
22+
pattern: $CALL
23+
all:
24+
- has:
25+
stopBy: neighbor
26+
pattern: $DB
27+
regex: ^pg8000.dbapi.connect$|^pg8000.native.Connection$
28+
- has:
29+
stopBy: neighbor
30+
kind: argument_list
31+
all:
32+
- has:
33+
stopBy: neighbor
34+
kind: keyword_argument
35+
all:
36+
- has:
37+
stopBy: neighbor
38+
kind: identifier
39+
regex: ^password$
40+
- has:
41+
stopBy: neighbor
42+
kind: string
43+
has:
44+
stopBy: neighbor
45+
kind: string_content
46+
- not:
47+
has:
48+
stopBy: neighbor
49+
kind: keyword_argument
50+
all:
51+
- has:
52+
stopBy: neighbor
53+
kind: identifier
54+
regex: ^password$
55+
- has:
56+
stopBy: neighbor
57+
kind: string
58+
not:
59+
has:
60+
stopBy: end
61+
kind: string_content
62+
rule:
63+
kind: call
64+
matches: pg8000.dbapi.connect(..., password="...",...)
65+
all:
66+
- not:
67+
has:
68+
stopBy: end
69+
kind: ERROR
70+
- not:
71+
inside:
72+
stopBy: end
73+
kind: ERROR
74+
75+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
id: python-pg8000-hardcoded-secret-python
2+
snapshots:
3+
? |
4+
conn = pg8000.dbapi.connect(user="postgres", password="abc")
5+
: labels:
6+
- source: pg8000.dbapi.connect(user="postgres", password="abc")
7+
style: primary
8+
start: 7
9+
end: 60
10+
- source: pg8000.dbapi.connect
11+
style: secondary
12+
start: 7
13+
end: 27
14+
- source: password
15+
style: secondary
16+
start: 45
17+
end: 53
18+
- source: abc
19+
style: secondary
20+
start: 55
21+
end: 58
22+
- source: '"abc"'
23+
style: secondary
24+
start: 54
25+
end: 59
26+
- source: password="abc"
27+
style: secondary
28+
start: 45
29+
end: 59
30+
- source: (user="postgres", password="abc")
31+
style: secondary
32+
start: 27
33+
end: 60
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
id: python-pg8000-hardcoded-secret-python
2+
valid:
3+
- |
4+
conn = pg8000.dbapi.connect(user="postgres", password=get_password())
5+
invalid:
6+
- |
7+
conn = pg8000.dbapi.connect(user="postgres", password="abc")

0 commit comments

Comments
 (0)