-
Notifications
You must be signed in to change notification settings - Fork 1
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
Allow using POST for the api because GET made problems in Java #2
Conversation
WalkthroughThe changes in this pull request primarily involve modifications to the Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 2
🧹 Outside diff range and nitpick comments (5)
server.py (5)
Line range hint
98-102
: Add proper input validation and error handlingThe endpoint assumes all required fields exist in the request body without validation. This could lead to 500 errors if fields are missing.
data = await request.get_json() + required_fields = ['key', 'message'] + if not all(field in data for field in required_fields): + return {'error': 'Missing required fields'}, 400 + badwords = load_data("json/badwords.json") goodwords = load_data("json/goodwords.json") key_hash_list = load_data("json/key_hash.json") if hash_string(data["key"]) not in key_hash_list: - return {"error": "access denied"} + return {"error": "Access denied"}, 401
Line range hint
108-113
: Improve error handling specificityThe bare except clause masks specific errors, making debugging difficult.
try: cid = data["cid"] gid = data["gid"] - except: + except KeyError: cid = None gid = None
Line range hint
149-164
: Fix critical data access bugs and improve securityThe function contains several critical bugs in data access and security concerns:
- Incorrect dictionary access pattern causing KeyError
- Insufficient admin validation
- No input validation
@server.route("/flagg_user", methods=["POST"]) async def add_flagged_user(): data = await request.get_json() + required_fields = ['key', 'user_id', 'user_name', 'reason'] + if not all(field in data for field in required_fields): + return {'error': 'Missing required fields'}, 400 + admin_key_hash_list = load_data("json/admin_key_hash.json") ids_list = load_data("json/ids.json") if hash_string(data["key"]) not in admin_key_hash_list: - return {"error": "access denied"} + return {"error": "Access denied"}, 401 - user_id = data[str(user_id)]["id"] - user_name = data[str(user_id)]["name"] - reason = data[str(user_id)]["reason"] + user_id = data["user_id"] + user_name = data["user_name"] + reason = data["reason"]
Line range hint
174-185
: Fix critical data access bugs and improve securityThe function contains similar critical bugs as the /flagg_user endpoint:
- Incorrect dictionary access pattern causing KeyError
- Insufficient admin validation
- No input validation
@server.route("/deflag_user", methods=["POST"]) async def remove_flagged_user(): data = await request.get_json() + required_fields = ['key', 'user_id'] + if not all(field in data for field in required_fields): + return {'error': 'Missing required fields'}, 400 + admin_key_hash_list = load_data("json/admin_key_hash.json") ids_list = load_data("json/ids.json") if hash_string(data["key"]) not in admin_key_hash_list: - return {"error": "access denied"} + return {"error": "Access denied"}, 401 - user_id = data[str(user_id)]["id"] + user_id = data["user_id"]
Line range hint
93-190
: Consider implementing security best practicesThe current implementation could benefit from several security enhancements:
- Add rate limiting to prevent abuse
- Implement proper logging for security events (failed auth attempts, flagging operations)
- Use environment variables for sensitive configurations
- Consider implementing request validation middleware
- Add proper HTTP status codes for different error scenarios
Would you like me to provide example implementations for any of these security enhancements?
@@ -121,7 +121,7 @@ async def check_message(): | |||
return results | |||
|
|||
|
|||
@server.route("/user") | |||
@server.route("/user", methods=["GET", "POST"]) |
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.
Add input validation and type checking
The endpoint needs proper input validation and type checking for the user_id field.
-@server.route("/user", methods=["GET", "POST"])
+@server.route("/user", methods=["POST"])
async def check_user():
# ... existing code ...
data = await request.get_json()
+ required_fields = ['key', 'id']
+ if not all(field in data for field in required_fields):
+ return {'error': 'Missing required fields'}, 400
+
+ try:
+ user_id = int(data["id"])
+ except (ValueError, TypeError):
+ return {'error': 'Invalid user ID format'}, 400
Also applies to: 134-137
@@ -90,7 +90,7 @@ def hash_string(string: str): | |||
return hashed | |||
|
|||
|
|||
@server.route("/chatfilter") | |||
@server.route("/chatfilter", methods=["GET", "POST"]) |
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 GET method for endpoints accepting request body
The route accepts both GET and POST methods, but GET requests with a request body are not recommended as they may be:
- Stripped by some proxies
- Not properly handled by some HTTP clients
- Not compliant with HTTP/1.1 semantics
-@server.route("/chatfilter", methods=["GET", "POST"])
+@server.route("/chatfilter", methods=["POST"])
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
@server.route("/chatfilter", methods=["GET", "POST"]) | |
@server.route("/chatfilter", methods=["POST"]) |
Current code shouldn't break :)
Summary by CodeRabbit
New Features
/chatfilter
,/user
,/flagg_user
, and/deflag_user
endpoints.Bug Fixes