Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

Bound the generate-password length parameter

Juan Denis98bc6946f97a
diff --git a/backend/app/api/databases.py b/backend/app/api/databases.py
index 18a1fe42..14296f68 100644
--- a/backend/app/api/databases.py
+++ b/backend/app/api/databases.py
@@ -1003,6 +1003,8 @@ def enable_pg_stat_statements(container):
@jwt_required()
def generate_password():
"""Generate a secure random password."""
- length = request.args.get('length', 16, type=int)
+ # Bounded: an unchecked ?length=1000000000 made the server build a
+ # gigabyte-long string.
+ length = min(max(request.args.get('length', 16, type=int), 8), 128)
password = DatabaseService.generate_password(length)
return jsonify({'password': password}), 200
diff --git a/backend/tests/test_generate_password_bounds.py b/backend/tests/test_generate_password_bounds.py
new file mode 100644
index 00000000..3fea9353
--- /dev/null
+++ b/backend/tests/test_generate_password_bounds.py
@@ -0,0 +1,13 @@
+"""The password generator endpoint bounds its length parameter."""
+import pytest
+
+from factories import headers_for, make_user
+
+
[email protected]('asked,got', [('1000000000', 128), ('2', 8), ('24', 24), ('', 16)])
+def test_length_is_clamped(app, client, asked, got):
+ from app import db
+ headers = headers_for(make_user(db, f'gp_{asked or "default"}'))
+ resp = client.get(f'/api/v1/databases/generate-password?length={asked}', headers=headers)
+ assert resp.status_code == 200
+ assert len(resp.get_json()['password']) == got