Skip to content

Commit 603ab96

Browse files
Semgrep showcase
1 parent f11477f commit 603ab96

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

semgrep/example.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
1-
import os, sys # F401: sys imported but unused
2-
3-
def my_function( x, y ):
4-
print( "Result:",x+y ) # E201, E202, E231, E221
5-
6-
class myclass: # N801: class name should use CapWords convention
7-
def __init__(self):
8-
self.value =42 # E225: missing whitespace around operator
9-
10-
def doSomething(self): # N802: function name should be snake_case
11-
if( self.value>0 ):
12-
print("Positive")
13-
else:
14-
print( "Not positive" )
15-
16-
my_function(1,2)
1+
import os
2+
import sys
3+
import hashlib
4+
5+
# Hardcoded credentials
6+
USERNAME = "admin"
7+
PASSWORD = "secret123"
8+
9+
def dangerous_eval():
10+
user_input = input("Enter a Python expression: ")
11+
result = eval(user_input)
12+
print("Evaluated result:", result)
13+
14+
def delete_data(path):
15+
os.system("rm -rf " + path) # Semgrep: shell injection
16+
17+
def hash_password(password):
18+
hashed = hashlib.md5(password.encode()).hexdigest() # Semgrep: weak hash
19+
return hashed
20+
21+
def main():
22+
print("Logging in as", USERNAME)
23+
password_hash = hash_password(PASSWORD)
24+
print("Password hash:", password_hash)
25+
26+
if len(sys.argv) > 1:
27+
delete_data(sys.argv[1])
28+
29+
dangerous_eval()
30+
31+
main()
32+

0 commit comments

Comments
 (0)