Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Bound the size of a measurement's extra_data

Roland Geider9cae9b025728
diff --git a/wger/measurements/api/serializers.py b/wger/measurements/api/serializers.py
index bb5103455..2941193a0 100644
--- a/wger/measurements/api/serializers.py
+++ b/wger/measurements/api/serializers.py
@@ -14,6 +14,7 @@
# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.
# Standard Library
+import json
from decimal import Decimal
# Third Party
@@ -21,6 +22,7 @@ from rest_framework import serializers
# wger
from wger.measurements.limits import (
+ EXTRA_DATA_MAX_BYTES,
VALUE_DECIMAL_PLACES,
VALUE_MAX_DIGITS,
limits_for,
@@ -249,6 +251,13 @@ class MeasurementSerializer(serializers.ModelSerializer):
"""
if not isinstance(extra_data, dict):
raise serializers.ValidationError('extra_data must be an object')
+
+ # Compact separators: measure what the column stores, not whitespace
+ size = len(json.dumps(extra_data, separators=(',', ':'), ensure_ascii=False).encode())
+ if size > EXTRA_DATA_MAX_BYTES:
+ raise serializers.ValidationError(
+ f'extra_data must be at most {EXTRA_DATA_MAX_BYTES} bytes, got {size}'
+ )
return extra_data
def validate(self, data):
diff --git a/wger/measurements/limits.py b/wger/measurements/limits.py
index e5207cebc..1eea384c6 100644
--- a/wger/measurements/limits.py
+++ b/wger/measurements/limits.py
@@ -30,6 +30,13 @@ Largest value the column can hold. It is what a category without a metric type
is bounded by, since nothing about a free-form category says more.
"""
+EXTRA_DATA_MAX_BYTES = 1000
+"""
+Upper bound for one entry's ``extra_data`` (compact UTF-8 JSON). The column is
+unbounded, so this is what keeps clients from pushing arbitrarily large blobs;
+the largest real payload (the importer's provenance) is ~200 bytes.
+"""
+
class Limits(NamedTuple):
"""
diff --git a/wger/measurements/tests/test_measurements.py b/wger/measurements/tests/test_measurements.py
index 6b8645e6d..56959db21 100644
--- a/wger/measurements/tests/test_measurements.py
+++ b/wger/measurements/tests/test_measurements.py
@@ -364,6 +364,47 @@ class MeasurementExtraDataApiTestCase(WgerTestCase):
self.assertEqual(response.data['extra_data'], {'unit': 'lb'})
self.assertEqual(Measurement.objects.get(pk=response.data['id']).unit, 'lb')
+ def test_extra_data_size_bound(self):
+ """
+ Test that an oversized extra_data blob is refused
+
+ The column is an unbounded JSONField, so this is the only thing keeping
+ a client from pushing arbitrarily large blobs into the table
+ """
+ response = self.client.post(
+ self.url,
+ {
+ 'category': 'cccccccc-cccc-cccc-cccc-0000000000b0',
+ 'date': '2023-05-01T12:00:00Z',
+ 'value': 180,
+ 'extra_data': {'unit': 'lb', 'blob': 'x' * 1000},
+ },
+ content_type='application/json',
+ )
+
+ self.assertEqual(response.status_code, 400)
+ self.assertIn('extra_data', response.data)
+
+ def test_extra_data_size_bound_leaves_room(self):
+ """
+ Test that a large but sane extra_data payload still passes
+
+ The importer's provenance is ~200 bytes; the bound is headroom, not a
+ fitted cap
+ """
+ response = self.client.post(
+ self.url,
+ {
+ 'category': 'cccccccc-cccc-cccc-cccc-0000000000b0',
+ 'date': '2023-05-01T12:00:00Z',
+ 'value': 180,
+ 'extra_data': {'unit': 'lb', 'source_name': 'x' * 900},
+ },
+ content_type='application/json',
+ )
+
+ self.assertEqual(response.status_code, 201)
+
def test_invalid_body_weight_unit(self):
"""
Test that body weight entries only accept kg and lb as unit