fix(headers): use json.dumps for HMAC body serialization (closes #4) - #19
Open
Ccheh wants to merge 1 commit into
Open
fix(headers): use json.dumps for HMAC body serialization (closes #4)#19Ccheh wants to merge 1 commit into
Ccheh wants to merge 1 commit into
Conversation
_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
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.
Bug
client.py:418stringifies the request body withstr()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)inhttp_helpers/helpers.py, which serializes throughjson.dumps(...)(double quotes). HMAC signs one string; the server verifies a different
one.
For
BuilderConfig.remote_builder_config, the Node/Expressbuilder-signing-serverfailsJSON.parseon the Python repr andreturns 400 — this is what #4 reports.
Fix
```diff
```
json.dumpswith default separators matchesrequests' wire formatexactly, so the signed bytes and transmitted bytes are identical.
Test
Adds
tests/test_client_headers.pywith two cases:BuilderConfig.generate_builder_headersis validJSON, equals
json.dumps(input_dict), and contains no single quotesNonebody is passed through unchangedFull suite: 22/22 passing.
Compatibility
No public API change.
bodywas already adictat the call site(
_post_request); only the local rebinding inside_generate_builder_headerschanges.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 Pythonstr(dict)), aligning the signed bytes with the JSON payload sent viarequests(..., json=body).Adds
tests/test_client_headers.pyto assert the forwarded body is valid JSON, matchesjson.dumps(...)exactly, and thatNonebodies are passed through unchanged.Reviewed by Cursor Bugbot for commit f2bbdd8. Bugbot is set up for automated code reviews on this repo. Configure here.