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

Issue with Default Headers in web3py Due to JWT Token Support (Python) #6

Open
benefacto opened this issue Jun 6, 2024 · 0 comments

Comments

@benefacto
Copy link

Added support for JWT tokens in the headers within the Forta SDK causes the default headers in web3py to be overridden since custom headers, such as the Authorization header, are provided. This results in a bug where the Content-Type header is not set to application/json, leading to issues in RPC calls. The Forta SDK should ensure that the default headers are still passed along with any custom headers.

Steps to Reproduce:

  1. Point the rpc_url to a local EVM-compatible node such as Frontier.
  2. Observe that the default headers in web3py are overridden.
  3. Notice that the Content-Type header is missing, leading to issues in RPC calls.

Expected Behavior:
The SDK should retain the default headers in web3py while adding the JWT token headers to avoid any issues.

Actual Behavior:
The SDK overrides the default headers in web3py, causing issues in RPC calls that require the Content-Type header.

Workaround:
To resolve this issue temporarily, include the Content-Type header explicitly in the rpc_headers parameter as shown below:

async def main():
    """This function is the entry point"""
    initialize_response = await initialize()

    await asyncio.gather(
        scan_ethereum({
            'rpc_url': EVM_RPC,
            'handle_transaction': handle_transaction,
            'rpc_headers': {
                "Content-Type": "application/json"
            }
        }),
        run_health_check()
    )

Debugging Details:

  • The issue appears as a 415 Unsupported Media Type error when using an EVM-compatible node such as Frontier, which does not occur when using Cloudflare's public ETH node.
  • The error traceback is as follows:
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/forta_bot_sdk/utils/with_retry.py", line 35, in with_retry
    response = await fn(*args)
  File "/usr/local/lib/python3.10/site-packages/web3/providers/async_rpc.py", line 91, in make_request
    raw_response = await async_make_post_request(
  File "/usr/local/lib/python3.10/site-packages/web3/_utils/request.py", line 239, in async_make_post_request
    response = await async_get_response_from_post_request(
  File "/usr/local/lib/python3.10/site-packages/web3/_utils/request.py", line 232, in async_get_response_from_post_request
    response = await session.post(endpoint_uri, *args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/aiohttp/client.py", line 693, in _request
    resp.raise_for_status()
  File "/usr/local/lib/python3.10/site-packages/aiohttp/client_reqrep.py", line 1058, in raise_for_status
    raise ClientResponseError(
aiohttp.client_exceptions.ClientResponseError: 415, message='Unsupported Media Type', url=URL('http://alice:9944')
  • Mitmproxy request logs show that the content-type header is being set to application/octet-stream instead of application/json:
mitmproxy-1  | === Request ===
mitmproxy-1  | URL: http://alice:9944/
mitmproxy-1  | Headers: Headers[(b'Host', b'alice:9944'), (b'Accept', b'*/*'), (b'Accept-Encoding', b'gzip, deflate'), (b'User-Agent', b'Python/3.10 aiohttp/3.9.2'), (b'Content-Length', b'66'), (b'Content-Type', b'application/octet-stream')]
mitmproxy-1  | Content: {"jsonrpc": "2.0", "method": "eth_chainId", "params": [], "id": 0}

Relevant Code: forta-bot-sdk-v2/py-sdk/src/forta_bot_sdk/scanning/evm/get_provider.py

Environment:

  • Library Version: forta_bot_sdk 0.2.3
  • Python Version: 3.10.14
  • OS: 5.15.160-1-MANJARO

Additional Context:
This issue causes significant inconvenience as it requires users to manually set the Content-Type header, which is not intuitive. A fix to retain default headers while supporting JWT would greatly improve the developer experience.

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

No branches or pull requests

1 participant