An open source, non-profit web search engine
Security fixes for auth flow:
Daoud Clarkef882d337dcc4
diff --git a/mwmbl/auth.py b/mwmbl/auth.pyindex 0dd3f6c..18aee75 100644--- a/mwmbl/auth.py+++ b/mwmbl/auth.py@@ -26,13 +26,19 @@ class UsernameOrEmailBackend:if not username or not password:return None+ user = Nonetry:user = UserModel.objects.get(username=username)except UserModel.DoesNotExist:try:user = UserModel.objects.get(email=username)except UserModel.DoesNotExist:- return None+ pass++ if user is None:+ # Run a dummy check to mitigate user-enumeration via timing+ UserModel().check_password(password)+ return Noneif user.check_password(password):return userdiff --git a/mwmbl/migrations/0020_usagebucket_year_month_index.py b/mwmbl/migrations/0020_usagebucket_year_month_index.pynew file mode 100644index 0000000..b0f28fc--- /dev/null+++ b/mwmbl/migrations/0020_usagebucket_year_month_index.py@@ -0,0 +1,15 @@+from django.db import migrations, models+++class Migration(migrations.Migration):++ dependencies = [+ ('mwmbl', '0019_add_userbilling'),+ ]++ operations = [+ migrations.AddIndex(+ model_name='usagebucket',+ index=models.Index(fields=['year', 'month'], name='mwmbl_usage_year_mo_idx'),+ ),+ ]diff --git a/mwmbl/migrations/0021_hash_api_keys.py b/mwmbl/migrations/0021_hash_api_keys.pynew file mode 100644index 0000000..6d4a637--- /dev/null+++ b/mwmbl/migrations/0021_hash_api_keys.py@@ -0,0 +1,26 @@+import hashlib++from django.db import migrations, models+++def hash_existing_keys(apps, schema_editor):+ ApiKey = apps.get_model('mwmbl', 'ApiKey')+ for api_key in ApiKey.objects.all():+ api_key.key = hashlib.sha256(api_key.key.encode()).hexdigest()+ api_key.save(update_fields=['key'])+++class Migration(migrations.Migration):++ dependencies = [+ ('mwmbl', '0020_usagebucket_year_month_index'),+ ]++ operations = [+ migrations.RunPython(hash_existing_keys, migrations.RunPython.noop),+ migrations.AlterField(+ model_name='apikey',+ name='key',+ field=models.CharField(max_length=64, unique=True),+ ),+ ]diff --git a/mwmbl/models.py b/mwmbl/models.pyindex 6e2750b..e744b08 100644--- a/mwmbl/models.py+++ b/mwmbl/models.py@@ -121,8 +121,11 @@ class DomainSubmission(models.Model):rejection_detail = models.CharField(max_length=300, blank=True)-def random_api_key():- return secrets.token_urlsafe(64)+def generate_api_key() -> tuple[str, str]:+ """Return (raw_key, key_hash). Store only the hash; return raw_key to the user once."""+ import hashlib+ raw = secrets.token_urlsafe(64)+ return raw, hashlib.sha256(raw.encode()).hexdigest()class ApiKey(models.Model):@@ -131,7 +134,7 @@ class ApiKey(models.Model):SEARCH = "search", "Search"user = models.ForeignKey(MwmblUser, on_delete=models.CASCADE)- key = models.CharField(max_length=300, unique=True, default=random_api_key)+ key = models.CharField(max_length=64, unique=True) # stores SHA-256 hash of the raw keycreated_on = models.DateTimeField(auto_now_add=True)name = models.CharField(max_length=100, blank=True, default="")scopes = ArrayField(diff --git a/mwmbl/platform/api.py b/mwmbl/platform/api.pyindex aa2b8e2..6265a75 100644--- a/mwmbl/platform/api.py+++ b/mwmbl/platform/api.py@@ -352,14 +352,17 @@ def get_user_vote_history(request) -> list[SearchResultVote]:)def create_api_key(request, body: CreateApiKeyRequest):check_email_verified(request)+ from mwmbl.models import generate_api_key+ raw_key, key_hash = generate_api_key()api_key = ApiKey.objects.create(user=request.user,name=body.name,scopes=[ApiKey.Scope.SEARCH],+ key=key_hash,)return ApiKeyCreatedResponse(id=api_key.id,- key=api_key.key,+ key=raw_key,name=api_key.name,created_on=api_key.created_on,scopes=api_key.scopes,diff --git a/mwmbl/quota.py b/mwmbl/quota.pyindex 994c85c..cf9506f 100644--- a/mwmbl/quota.py+++ b/mwmbl/quota.py@@ -39,10 +39,15 @@ def check_rate_limit(user_id: int) -> bool:Returns True if the request is allowed, False if the limit is exceeded."""key = _rate_key(user_id)- # add() is atomic: sets key=1 with TTL only if absent (first request this second)if cache.add(key, 1, timeout=1):return True- return cache.incr(key) <= RATE_LIMIT+ count = cache.incr(key)+ if count == 1:+ # Key expired between add() and incr(); Redis created it without a TTL.+ # Set the TTL now to prevent the key from leaking indefinitely.+ from django_redis import get_redis_connection+ get_redis_connection("default").expire(key, 1)+ return count <= RATE_LIMIT# ---------------------------------------------------------------------------diff --git a/mwmbl/search_auth.py b/mwmbl/search_auth.pyindex d2c1e12..bc60e8d 100644--- a/mwmbl/search_auth.py+++ b/mwmbl/search_auth.py@@ -1,27 +1,21 @@"""Authentication for the search API using the X-API-Key header."""+import hashlib+from ninja.security import APIKeyHeaderfrom mwmbl.models import ApiKeyclass SearchApiKeyAuth(APIKeyHeader):- """- Ninja authentication class that reads the X-API-Key request header and- validates it against ApiKey rows that have the 'search' scope.-- On success, request.auth is set to the ApiKey instance (giving access to- request.auth.user and request.auth.user.tier).- On failure, returns None which causes Ninja to respond with 401.- """-param_name = "X-API-Key"def authenticate(self, request, key: str):+ key_hash = hashlib.sha256(key.encode()).hexdigest()try:return ApiKey.objects.select_related("user").get(- key=key,+ key=key_hash,scopes__contains=[ApiKey.Scope.SEARCH],)except ApiKey.DoesNotExist:diff --git a/nginx.conf.sigil b/nginx.conf.sigilindex aff1a3a..5a6a194 100644--- a/nginx.conf.sigil+++ b/nginx.conf.sigil@@ -1,3 +1,6 @@+limit_req_zone $binary_remote_addr zone=mwmbl_login:10m rate=10r/m;+limit_req_zone $binary_remote_addr zone=mwmbl_register:10m rate=3r/m;+{{ range $port_map := .PROXY_PORT_MAP | split " " }}{{ $port_map_list := $port_map | split ":" }}{{ $scheme := index $port_map_list 0 }}@@ -36,6 +39,75 @@ server {keepalive_timeout 70;{{ if and (eq $.SPDY_SUPPORTED "true") (ne $.HTTP2_SUPPORTED "true") }}add_header Alternate-Protocol {{ $.PROXY_SSL_PORT }}:npn-spdy/2;{{ end }}+ location = /api/v1/platform/token/pair {+ limit_req zone=mwmbl_login burst=5 nodelay;+ limit_req_status 429;++ if ($request_method = 'OPTIONS') {+ add_header 'Access-Control-Allow-Origin' '*';+ add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';+ add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-API-Key';+ add_header 'Access-Control-Max-Age' 1728000;+ add_header 'Content-Type' 'text/plain; charset=utf-8';+ add_header 'Content-Length' 0;+ return 204;+ }+ add_header 'Access-Control-Allow-Origin' '*' always;+ add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;+ add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-API-Key' always;+ add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;++ proxy_pass http://{{ $.APP }}-{{ $upstream_port }};+ proxy_http_version 1.1;+ proxy_read_timeout {{ $.PROXY_READ_TIMEOUT }};+ proxy_buffer_size {{ $.PROXY_BUFFER_SIZE }};+ proxy_buffering {{ $.PROXY_BUFFERING }};+ proxy_buffers {{ $.PROXY_BUFFERS }};+ proxy_busy_buffers_size {{ $.PROXY_BUSY_BUFFERS_SIZE }};+ proxy_set_header Upgrade $http_upgrade;+ proxy_set_header Connection $http_connection;+ proxy_set_header Host $http_host;+ proxy_set_header X-Forwarded-For {{ $.PROXY_X_FORWARDED_FOR }};+ proxy_set_header X-Forwarded-Port {{ $.PROXY_X_FORWARDED_PORT }};+ proxy_set_header X-Forwarded-Proto {{ $.PROXY_X_FORWARDED_PROTO }};+ proxy_set_header X-Request-Start $msec;+ {{ if $.PROXY_X_FORWARDED_SSL }}proxy_set_header X-Forwarded-Ssl {{ $.PROXY_X_FORWARDED_SSL }};{{ end }}+ }++ location = /api/v1/platform/register {+ limit_req zone=mwmbl_register burst=2 nodelay;+ limit_req_status 429;++ if ($request_method = 'OPTIONS') {+ add_header 'Access-Control-Allow-Origin' '*';+ add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';+ add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-API-Key';+ add_header 'Access-Control-Max-Age' 1728000;+ add_header 'Content-Type' 'text/plain; charset=utf-8';+ add_header 'Content-Length' 0;+ return 204;+ }+ add_header 'Access-Control-Allow-Origin' '*' always;+ add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;+ add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization,X-API-Key' always;+ add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;++ proxy_pass http://{{ $.APP }}-{{ $upstream_port }};+ proxy_http_version 1.1;+ proxy_read_timeout {{ $.PROXY_READ_TIMEOUT }};+ proxy_buffer_size {{ $.PROXY_BUFFER_SIZE }};+ proxy_buffering {{ $.PROXY_BUFFERING }};+ proxy_busy_buffers_size {{ $.PROXY_BUSY_BUFFERS_SIZE }};+ proxy_set_header Upgrade $http_upgrade;+ proxy_set_header Connection $http_connection;+ proxy_set_header Host $http_host;+ proxy_set_header X-Forwarded-For {{ $.PROXY_X_FORWARDED_FOR }};+ proxy_set_header X-Forwarded-Port {{ $.PROXY_X_FORWARDED_PORT }};+ proxy_set_header X-Forwarded-Proto {{ $.PROXY_X_FORWARDED_PROTO }};+ proxy_set_header X-Request-Start $msec;+ {{ if $.PROXY_X_FORWARDED_SSL }}proxy_set_header X-Forwarded-Ssl {{ $.PROXY_X_FORWARDED_SSL }};{{ end }}+ }+location / {