fix(sdk-python): harden cryptographic order salts, preserve zero salts, and enforce cancellation preflight validation - #5
Open
magqqgq wants to merge 1 commit into
Conversation
…s, and enforce cancellation preflight validation ### Description This PR hardens the Python SDK against predictable order identifiers, implicit truthiness bugs on order parameters, and unvalidated cancellation requests[cite: 35, 36, 37]. It replaces weak pseudo-random generators with cryptographic selections, protects explicit zero-salt assignments, and enforces preflight token validation prior to order cancellation submission[cite: 35, 36, 37]. ### Key Changes * **Cryptographic Order Salts (`predict_sdk/_internal/utils.py`):** - Replaced the non-cryptographic `random.randint` with Python's standard `secrets.randbelow(MAX_SALT + 1)` to ensure secure, unbiased order salt generation[cite: 35]. * **Zero-Salt Preservation (`predict_sdk/order_builder.py`):** - Replaced truthiness checks (`data.salt`) with an explicit `None` check (`data.salt is not None`). This ensures that an explicitly provided `salt=0` is correctly preserved and serialized as `"0"` instead of being overwritten by a random value[cite: 36]. * **Cancellation Preflight Validation (`predict_sdk/order_builder.py`):** - Enforced the existing `options.with_validation` contract by calling `validate_token_ids_async` before encoding and dispatching `cancelOrders`, preventing late on-chain failures due to incorrect market configurations[cite: 36]. * **Regression Coverage (`tests/test_order_builder.py`):** - Added unit test coverage verifying zero-salt preservation and the correct rejection behavior when invalid token IDs are passed during cancellations[cite: 37]. ### Validation & Testing * **Syntax & Compiling:** Python source files pass syntax compilation cleanly (`python -m py_compile`).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR hardens the Python SDK against predictable order identifiers, implicit truthiness bugs on order parameters, and unvalidated cancellation requests[cite: 35, 36, 37]. It replaces weak pseudo-random generators with cryptographic selections, protects explicit zero-salt assignments, and enforces preflight token validation prior to order cancellation submission[cite: 35, 36, 37].
Key Changes
predict_sdk/_internal/utils.py):random.randintwith Python's standardsecrets.randbelow(MAX_SALT + 1)to ensure secure, unbiased order salt generation[cite: 35].predict_sdk/order_builder.py):data.salt) with an explicitNonecheck (data.salt is not None). This ensures that an explicitly providedsalt=0is correctly preserved and serialized as"0"instead of being overwritten by a random value[cite: 36].predict_sdk/order_builder.py):options.with_validationcontract by callingvalidate_token_ids_asyncbefore encoding and dispatchingcancelOrders, preventing late on-chain failures due to incorrect market configurations[cite: 36].tests/test_order_builder.py):Validation & Testing
python -m py_compile).Note
Medium Risk
Touches order identity (salts), order serialization, and on-chain cancel behavior; changes are narrow but affect trading and signing paths.
Overview
Hardens order-building and cancellation in the Python SDK with three targeted fixes.
Order salts now use
secrets.randbelowinstead ofrandom.randint, so generated salts are cryptographically unpredictable within[0, MAX_SALT].Explicit
salt=0is preserved inbuild_orderby checkingdata.salt is not Noneinstead of truthiness, so"0"is no longer replaced by a random salt.Cancellation honors
CancelOrdersOptions.with_validation(defaultTrue): when enabled,cancel_orders_asyncrunsvalidate_token_ids_asyncfirst and raisesInvalidNegRiskConfiginstead of sending a doomedcancelOrderstransaction.Reviewed by Cursor Bugbot for commit 3a0ccbd. Bugbot is set up for automated code reviews on this repo. Configure here.