Skip to content

Commit

Permalink
Fallback JSON loading during Task unserializing (#261)
Browse files Browse the repository at this point in the history
* Fallback JSON loading during Task unserializing (#225)

* Explain the reasoning

* Add test case for issue #224

---------

Co-authored-by: @Antelox <[email protected]>
Co-authored-by: msm <[email protected]>
Co-authored-by: psrok1 <[email protected]>
  • Loading branch information
4 people authored Oct 18, 2024
1 parent a80712c commit e14b017
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
7 changes: 6 additions & 1 deletion karton/core/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
15 changes: 15 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit e14b017

Please sign in to comment.