Skip to content

Commit 2d1b811

Browse files
Add validation in send_bloom endpoint to reject blooms exceeding 280 chars
1 parent 3cda674 commit 2d1b811

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

backend/data/users.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
from dataclasses import dataclass
2-
from hashlib import scrypt
32
import hashlib
43
import random
54
import string
65
from typing import List, Optional
76

7+
try:
8+
from hashlib import scrypt as _scrypt_impl
9+
USE_HASHLIB_SCRYPT = True
10+
except ImportError:
11+
# Python 3.9 doesn't have scrypt in hashlib, use the scrypt package
12+
import scrypt as _scrypt_module
13+
USE_HASHLIB_SCRYPT = False
14+
815
from data.connection import db_cursor
916
from psycopg2.errors import UniqueViolation
1017

@@ -91,7 +98,12 @@ def register_user(username: str, password_plaintext: str) -> User:
9198

9299

93100
def scrypt(password_plaintext: bytes, password_salt: bytes) -> bytes:
94-
return hashlib.scrypt(password_plaintext, salt=password_salt, n=8, r=8, p=1)
101+
if USE_HASHLIB_SCRYPT:
102+
# Python 3.11+ hashlib.scrypt
103+
return _scrypt_impl(password_plaintext, salt=password_salt, n=8, r=8, p=1)
104+
else:
105+
# Python 3.9 scrypt package
106+
return _scrypt_module.hash(password_plaintext, password_salt, N=8, r=8, p=1)
95107

96108

97109
SALT_CHARACTERS = string.ascii_uppercase + string.ascii_lowercase + string.digits

backend/endpoints.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from datetime import timedelta
1919

2020
MINIMUM_PASSWORD_LENGTH = 5
21+
MAXIMUM_BLOOM_LENGTH = 280
2122

2223

2324
def login():
@@ -156,9 +157,20 @@ def send_bloom():
156157
if type_check_error is not None:
157158
return type_check_error
158159

160+
content = request.json["content"]
161+
162+
if len(content) > MAXIMUM_BLOOM_LENGTH:
163+
return make_response(
164+
jsonify({
165+
"success": False,
166+
"message": f"Bloom content exceeds maximum length of {MAXIMUM_BLOOM_LENGTH} characters"
167+
}),
168+
400
169+
)
170+
159171
user = get_current_user()
160172

161-
blooms.add_bloom(sender=user, content=request.json["content"])
173+
blooms.add_bloom(sender=user, content=content)
162174

163175
return jsonify(
164176
{

0 commit comments

Comments
 (0)