Skip to content

fix(headers): use json.dumps for HMAC body serialization (closes #4) - #19

Open
Ccheh wants to merge 1 commit into
Polymarket:mainfrom
Ccheh:fix/json-serialization-headers
Open

fix(headers): use json.dumps for HMAC body serialization (closes #4)#19
Ccheh wants to merge 1 commit into
Polymarket:mainfrom
Ccheh:fix/json-serialization-headers

Conversation

@Ccheh

@Ccheh Ccheh commented May 14, 2026

Copy link
Copy Markdown

Bug

client.py:418 stringifies the request body with str() before HMAC:

python if body is not None: body = str(body) headers = self.builder_config.generate_builder_headers(method, request_path, body) ​

str({"a": "b"}){'a': 'b'} (Python repr, single quotes, not JSON).

Why it breaks

The wire body is sent via requests.request(..., json=body) in
http_helpers/helpers.py, which serializes through json.dumps(...)
(double quotes). HMAC signs one string; the server verifies a different
one.

For BuilderConfig.remote_builder_config, the Node/Express
builder-signing-server fails JSON.parse on the Python repr and
returns 400 — this is what #4 reports.

Fix

​```diff

  •    body = str(body)
    
  •    body = json.dumps(body)
    

​```

json.dumps with default separators matches requests' wire format
exactly, so the signed bytes and transmitted bytes are identical.

Test

Adds tests/test_client_headers.py with two cases:

  • the body handed to BuilderConfig.generate_builder_headers is valid
    JSON, equals json.dumps(input_dict), and contains no single quotes
  • a None body is passed through unchanged

Full suite: 22/22 passing.

Compatibility

No public API change. body was already a dict at the call site
(_post_request); only the local rebinding inside
_generate_builder_headers changes.

Closes #4


Note

Medium Risk
Touches request-signing header generation used for authenticated relayer calls; while the change is small, mismatched serialization could still break signature verification across clients/servers if expectations differ.

Overview
Fixes builder header/HMAC signing to serialize request bodies with json.dumps (instead of Python str(dict)), aligning the signed bytes with the JSON payload sent via requests(..., json=body).

Adds tests/test_client_headers.py to assert the forwarded body is valid JSON, matches json.dumps(...) exactly, and that None bodies are passed through unchanged.

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

_generate_builder_headers stringified the body with Python's built-in
str(), producing Python repr (single quotes) rather than valid JSON.

The body sent on the wire goes through requests(..., json=body) which
serializes via json.dumps (double quotes). The HMAC was signed over one
string while the server received and verified a different one.

Closes Polymarket#4
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.

Invalid JSON Serialization in Relayer Client

1 participant