Skip to content

403 when using special characters #902

Description

@Alw3ys

Issue: When using special characters like "ö" app_handler.handle(request) will return Unauthorized.

Tools: I'm using FastAPI AsyncSlackRequestHandler adapter. I don't know if this happends in without adapters on others, this is my use case.

With this code can be reproduced: https://github.com/slackapi/bolt-python/blob/main/examples/fastapi/async_app.py

Simply using the mention listener and as a user on slack send a special character, 403 will be returned.

Hypothesis: I believe is the way the raw body is handled, that when using special characters is not well decoded or something, I'll keep looking to see if I find a solution, in the meantime reporting this as issue since seems pretty clear is one

Activity

  1. Alw3ys commented on May 26, 2023

    @Alw3ys
    Author

    More support of evidence:

    I've tried creating the app like this:

    slack_app = AsyncApp(
        authorize=authorize,
        signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
        request_verification_enabled=False # Switching this off
    )
    

    and it works, so in deed there's something with the auth.

    I've also tried to implement my own checks while request_verification_enabled=False and still fails with special characters:

    async def verify_request_signature(request: Request) -> bool:
        raw_body = await request.body()
        body = raw_body.decode("utf-8")
        signature_verifier = SignatureVerifier(os.environ.get("SLACK_SIGNING_SECRET"))
        timestamp = str(request.headers.get("X-Slack-Request-Timestamp"))
        signature = str(request.headers.get("X-Slack-Signature"))
        if not timestamp or not signature:
            return False
        return signature_verifier.is_valid(body, timestamp, signature)
    

    Keeps happening, maybe something off with FastAPI raw body or simply slack, I don't know but I hope this helps

  2. added this to the 1.18.1 milestone on May 27, 2023
  3. seratch commented on May 27, 2023

    @seratch
    Contributor

    @Alw3ys Thanks for sharing this. We will look into it next week. If you find the cause of the issue, sharing it with us would be greatly appericated.

  4. Alw3ys commented on May 27, 2023

    @Alw3ys
    Author

    Thanks! I will if I do!

  5. Alw3ys commented on Jul 4, 2023

    @Alw3ys
    Author

    Any updates?

  6. stasfilin commented on Jul 4, 2023

    @stasfilin

    This one is really strange,
    I try to investigate this. I found that we do decoding (https://github.com/slackapi/bolt-python/blob/main/slack_bolt/adapter/starlette/async_handler.py#L17) for body.

    I guess we can change it here (

    if isinstance(body, str):
    self.body = parse_body(self.raw_body, self.content_type)
    elif isinstance(body, dict):
    self.body = body
    else:
    self.body = {}
    )

    to

    try:
        if isinstance(body, str):
            self.body = parse_body(self.raw_body, self.content_type)
        elif isinstance(body, dict):
            self.body = body
        else:
            self.body = {}
    except Exception as e:
        print(f"Error parsing the body: {e}")
        self.body = {}

    Also, for headers, we can try to change to

    resp.headers['Content-Type'] = 'text/html; charset=utf-8'

    @seratch, any thoughts about this?

  7. added
    questionFurther information is requested
    and removed
    bugSomething isn't working
    on Jul 4, 2023
  8. seratch commented on Jul 4, 2023

    @seratch
    Contributor

    @Alw3ys Sorry, I had been busy for other tasks. I just quickly tried to reproduce your issue but I was not able to manage to see the same situation. I simply sent a message like @my-app can you parse ö correctly? and my example app handled the request payload without any issues.

    Here is my code. I ran the app by uvicorn app:api --reload --port 3000 --log-level debug.

    from slack_bolt.async_app import AsyncApp
    from slack_bolt.adapter.fastapi.async_handler import AsyncSlackRequestHandler
    
    app = AsyncApp()
    app_handler = AsyncSlackRequestHandler(app)
    
    @app.event("app_mention")
    async def handle_app_mentions(body, say, logger):
        await say("What's up?")
    
    from fastapi import FastAPI, Request
    
    api = FastAPI()
    
    @api.post("/slack/events")
    async def endpoint(req: Request):
        return await app_handler.handle(req)

    My app responded as expected:

    Could you provide the steps to reproduce the issue? If the above bot mentioning string does not work for you, the cause of the issue might not be the FastAPI adapter code. This means that something in your environment, such as proxy servers, may prevent delivering the raw payload string to your FastAPI app.

    Once again, I am sorry for my slow response here. I look forward to hearing from you.

  9. Alw3ys commented on Jul 4, 2023

    @Alw3ys
    Author

    Hi there,

    Ok, that's def weird. No worries, first of all thanks for taking the time, to try it out, I'll give it another go this week and provide you with an example if the issue still persist

  10. modified the milestones: 1.18.1, 1.x on Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions