-
Notifications
You must be signed in to change notification settings - Fork 7
Semgrep files #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Semgrep files #11
Changes from 4 commits
4ad3152
396d06f
a7c61b8
0927f3e
b47e1bb
82380c1
f1c4f5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| reviews: | ||
| path_filters: ["**/*.yml","**/*.yaml"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # .semgrep.yml | ||
| include: | ||
| - semgrep/example.py | ||
|
|
||
| configs: | ||
| - semgrep/semgrep.yml | ||
|
|
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,32 @@ | ||||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||||
| import sys | ||||||||||||||||||||||||||||||||
| import hashlib | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Hardcoded credentials | ||||||||||||||||||||||||||||||||
| USERNAME = "admin" | ||||||||||||||||||||||||||||||||
| PASSWORD = "secret123" | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def dangerous_eval(): | ||||||||||||||||||||||||||||||||
| user_input = input("Enter a Python expression: ") | ||||||||||||||||||||||||||||||||
| result = eval(user_input) | ||||||||||||||||||||||||||||||||
| print("Evaluated result:", result) | ||||||||||||||||||||||||||||||||
|
Comment on lines
+9
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove eval() on user input - critical code injection vulnerability Using If you need to evaluate mathematical expressions, use a safe alternative like +import ast
+
def dangerous_eval():
- user_input = input("Enter a Python expression: ")
- result = eval(user_input)
- print("Evaluated result:", result)
+ user_input = input("Enter a Python literal: ")
+ try:
+ # Only evaluates Python literals (strings, numbers, tuples, lists, dicts, booleans, None)
+ result = ast.literal_eval(user_input)
+ print("Evaluated result:", result)
+ except (ValueError, SyntaxError) as e:
+ print("Invalid input:", e)For mathematical expressions, consider using a dedicated library like 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def delete_data(path): | ||||||||||||||||||||||||||||||||
| os.system("rm -rf " + path) # Semgrep: shell injection | ||||||||||||||||||||||||||||||||
|
Comment on lines
+14
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix shell injection vulnerability in delete_data Using Use +import shutil
+
def delete_data(path):
- os.system("rm -rf " + path) # Semgrep: shell injection
+ # Validate path to prevent directory traversal
+ if not path or ".." in path:
+ raise ValueError("Invalid path")
+
+ # Use shutil for safe file/directory deletion
+ if os.path.exists(path):
+ if os.path.isdir(path):
+ shutil.rmtree(path)
+ else:
+ os.remove(path)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def hash_password(password): | ||||||||||||||||||||||||||||||||
| hashed = hashlib.md5(password.encode()).hexdigest() # Semgrep: weak hash | ||||||||||||||||||||||||||||||||
| return hashed | ||||||||||||||||||||||||||||||||
|
Comment on lines
+17
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace MD5 with secure password hashing MD5 is cryptographically broken and should never be used for password hashing. It's vulnerable to collision attacks and can be brute-forced easily. Use a proper password hashing library like bcrypt: -import hashlib
+import bcrypt
def hash_password(password):
- hashed = hashlib.md5(password.encode()).hexdigest() # Semgrep: weak hash
- return hashed
+ # Generate a salt and hash the password
+ salt = bcrypt.gensalt()
+ hashed = bcrypt.hashpw(password.encode('utf-8'), salt)
+ return hashed.decode('utf-8')You'll need to install bcrypt: 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| def main(): | ||||||||||||||||||||||||||||||||
| print("Logging in as", USERNAME) | ||||||||||||||||||||||||||||||||
| password_hash = hash_password(PASSWORD) | ||||||||||||||||||||||||||||||||
| print("Password hash:", password_hash) | ||||||||||||||||||||||||||||||||
|
Comment on lines
+21
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't print sensitive credentials Printing credentials to stdout can expose them in logs, terminals, or monitoring systems. Remove or mask credential output: def main():
- print("Logging in as", USERNAME)
+ print("Logging in as user")
password_hash = hash_password(PASSWORD)
- print("Password hash:", password_hash)
+ # Don't print password hashes📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if len(sys.argv) > 1: | ||||||||||||||||||||||||||||||||
| delete_data(sys.argv[1]) | ||||||||||||||||||||||||||||||||
|
Comment on lines
+26
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validate command line arguments before file operations Accepting unvalidated file paths from command line arguments is dangerous and could lead to unintended file deletion. Add proper validation and confirmation: if len(sys.argv) > 1:
- delete_data(sys.argv[1])
+ path = sys.argv[1]
+ # Validate path
+ if not os.path.exists(path):
+ print(f"Path does not exist: {path}")
+ return
+
+ # Require confirmation for destructive operations
+ confirm = input(f"Are you sure you want to delete {path}? (yes/no): ")
+ if confirm.lower() == "yes":
+ delete_data(path)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| dangerous_eval() | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| main() | ||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Use proper script execution guard Direct execution of main() without a guard can cause issues when the module is imported. -main()
+if __name__ == "__main__":
+ main()📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| rules: | ||
| - id: hardcoded-password | ||
| pattern: password = "$SECRET" | ||
| message: "Avoid hardcoded passwords" | ||
| severity: ERROR | ||
| languages: [python] | ||
| metadata: | ||
| category: security | ||
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove hardcoded credentials - critical security vulnerability
Hardcoded credentials in source code pose a severe security risk. These credentials can be exposed through version control, code reviews, or if the source code is compromised.
Replace hardcoded credentials with environment variables or a secure configuration management system:
🤖 Prompt for AI Agents