Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback JSON loading during Task unserializing #261

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading