Skip to content

[BUG] RequestValidator does not accept json string although it says so #885

@lvsun

Description

@lvsun

Describe the bug

Hi dear developers,

regarding the authentication of a websocket API I encountered following issue:

  File "xxx/lib/python3.13/site-packages/twilio/request_validator.py", line 131, in validate
    self.compute_signature(uri_without_port, params), signature
    ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "xxx/lib/python3.13/site-packages/twilio/request_validator.py", line 76, in compute_signature
    values = self.get_values(params, param_name)
  File "xxxlib/python3.13/site-packages/twilio/request_validator.py", line 98, in get_values
    return [param_dict[param_name]]
            ~~~~~~~~~~^^^^^^^^^^^^
TypeError: string indices must be integers, not 'str'

what I did is to verify the websocket request from Twilio upon a call.

The description for the method validate(self, uri, params, signature) from RequestValidator of twilio states as following:

...
:param params: dictionary of POST variables or string of POST body for JSON requests
...

but it seems it does not accept the json string as params.

Code snippet

@app.websocket("/call")
async def websocket_call(ws: WebSocket):
    x_twilio_signature = ws.headers.get("x-twilio-signature", "")
    url = str(ws.url)
    message_body = await ws.receive()
    raw_body = json.dumps(message_body)

    print("x_twilio_signature:", x_twilio_signature)
    print("url:", url)
    is_valid = is_valid_twilio_request(url, raw_body, x_twilio_signature)
    print("is_valid:", is_valid)

def is_valid_twilio_request(url=url, params=params, twilio_signature=twilio_signature):
    load_dotenv()
    auth_token = os.getenv("TWILIO_AUTH_TOKEN")
    # Initialize the request validator
    validator = RequestValidator(auth_token)

    return validator.validate(url, params, twilio_signature)

Actual behavior

validate(self, uri, params, signature) in RequestValidator returned error when the params is a JSON string

Expected behavior

validate(self, uri, params, signature) in RequestValidator also accepts JSON string

twilio-python version

9.8.3

Python version

3.13

Logs or error messages

File "xxx/lib/python3.13/site-packages/twilio/request_validator.py", line 131, in validate
    self.compute_signature(uri_without_port, params), signature
    ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "xxx/lib/python3.13/site-packages/twilio/request_validator.py", line 76, in compute_signature
    values = self.get_values(params, param_name)
  File "xxxlib/python3.13/site-packages/twilio/request_validator.py", line 98, in get_values
    return [param_dict[param_name]]
            ~~~~~~~~~~^^^^^^^^^^^^
TypeError: string indices must be integers, not 'str'

Additional context

I did the json dump

    message_body = await ws.receive()
    raw_body = json.dumps(message_body)

because otherwise the validator will also fail with the message_body, I searched the official documentation and there is no explanation about how to validate the websocket requests, I got it successfully authenticated using HTTP/post.

Could you please help here? Which parameter from the Websocket is the right one to put into the validate() method, when a websocket request needs authentication?

Thanks in advance.

libs:

pydantic==2.11.9
uvicorn==0.35.0
fastapi==0.116.2
starlette==0.48.0
python-dotenv==1.1.1
twilio==9.8.3
python-multipart==0.0.20

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugbug in the library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions