forta-network / forta-bot-sdk-v2

1 stars 0 forks source link

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

Open benefacto opened 4 months ago

benefacto commented 4 months ago

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:

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-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:

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.