diff --git a/karton/core/task.py b/karton/core/task.py index 4d8c34b..251e931 100644 --- a/karton/core/task.py +++ b/karton/core/task.py @@ -408,7 +408,12 @@ def unserialize_resources(value: Any) -> Any: if parse_resources: task_data = json.loads(data, object_hook=unserialize_resources) else: - task_data = orjson.loads(data) + try: + task_data = orjson.loads(data) + except orjson.JSONDecodeError: + # Fallback, in case orjson raises exception during loading + # This may happen for large numbers (too large for float) + task_data = json.loads(data, object_hook=unserialize_resources) # Compatibility with Karton <5.2.0 headers_persistent_fallback = task_data["payload_persistent"].get( diff --git a/tests/test_core.py b/tests/test_core.py index 831c534..9ebb811 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -55,3 +55,18 @@ def test_matching_filters(self): self.assertTrue(task.matches_filters([{"A": "a", "B": "b"}])) self.assertFalse(task.matches_filters([{"Z": "a"}])) self.assertFalse(task.matches_filters([{"A": "a", "Z": "a"}])) + + +class TestTaskLargeNumber(unittest.TestCase): + def test_large_number(self): + # Case from https://github.com/CERT-Polska/karton/issues/224 + huge_n = 16500472521988697010663112438705807640072764589479002554256260695717317064262484691290598356970646828426660349407212809681822203919126081272297252018228030950728477136113932493730960235450313211028445802933417736042246471737169968152424614291341808025585752848637795661794780420312612645575283548111399362423653839834760368929525863676430601132093478720051122016787902563729760403548823660511230280753799912283221065068155277355752466002679387284450151073598015621110653783630539129630982849849675891985711599794713831157549822527748863844615219682824485519877354977586980738215172053213147055330238573803265248619061 + task = Task( + headers={}, + payload={ + "e": 65537, + "n": huge_n, + } + ) + task = Task.unserialize(task.serialize(), parse_resources=False) + self.assertTrue(task.payload["n"] == huge_n)