Skip to content

fix(sdk-python): harden cryptographic order salts, preserve zero salts, and enforce cancellation preflight validation - #5

Open
magqqgq wants to merge 1 commit into
PredictDotFun:mainfrom
magqqgq:magqqgq-patch-1
Open

magqqgq wants to merge 1 commit into
PredictDotFun:mainfrom
magqqgq:magqqgq-patch-1

Conversation

@magqqgq

@magqqgq magqqgq commented Aug 27, 2026

Copy link
Copy Markdown

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).

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.randbelow instead of random.randint, so generated salts are cryptographically unpredictable within [0, MAX_SALT].

Explicit salt=0 is preserved in build_order by checking data.salt is not None instead of truthiness, so "0" is no longer replaced by a random salt.

Cancellation honors CancelOrdersOptions.with_validation (default True): when enabled, cancel_orders_async runs validate_token_ids_async first and raises InvalidNegRiskConfig instead of sending a doomed cancelOrders transaction.

Reviewed by Cursor Bugbot for commit 3a0ccbd. Bugbot is set up for automated code reviews on this repo. Configure here.

…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`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant