dispatch icon indicating copy to clipboard operation
dispatch copied to clipboard

Not able to report new incident (/incident/report) with basic auth

Open santhoshparepu opened this issue 6 years ago • 6 comments

Describe the bug Issue with basic authentication

To Reproduce Steps to reproduce the behavior:

  1. Install dispatch with basic authentication - dispatch-auth-provider-basic
  2. When you go to dispatch URL, we are not being redirected to register/login endpoint. (This needs to be looked at).
  3. After manually going to /register endpoint and registering yourself, you will be directed to dashboards pane. - This works as expected.
  4. Now, refreshing the same screen (F5), you will get below error.
ERROR:uvicorn.error:Exception in ASGI application
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 390, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/usr/local/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/applications.py", line 111, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc from None
  File "/usr/local/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.8/site-packages/starlette/middleware/base.py", line 26, in __call__
    await response(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/responses.py", line 228, in __call__
    await run_until_first_complete(
  File "/usr/local/lib/python3.8/site-packages/starlette/concurrency.py", line 18, in run_until_first_complete
    [task.result() for task in done]
  File "/usr/local/lib/python3.8/site-packages/starlette/concurrency.py", line 18, in <listcomp>
    [task.result() for task in done]
  File "/usr/local/lib/python3.8/site-packages/starlette/responses.py", line 220, in stream_response
    async for chunk in self.body_iterator:
  File "/usr/local/lib/python3.8/site-packages/starlette/middleware/base.py", line 56, in body_stream
    task.result()
  File "/usr/local/lib/python3.8/site-packages/starlette/middleware/base.py", line 38, in coro
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/sentry_asgi/middleware.py", line 22, in __call__
    raise exc from None
  File "/usr/local/lib/python3.8/site-packages/sentry_asgi/middleware.py", line 19, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/middleware/base.py", line 26, in __call__
    await response(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/responses.py", line 228, in __call__
    await run_until_first_complete(
  File "/usr/local/lib/python3.8/site-packages/starlette/concurrency.py", line 18, in run_until_first_complete
    [task.result() for task in done]
  File "/usr/local/lib/python3.8/site-packages/starlette/concurrency.py", line 18, in <listcomp>
    [task.result() for task in done]
  File "/usr/local/lib/python3.8/site-packages/starlette/responses.py", line 220, in stream_response
    async for chunk in self.body_iterator:
  File "/usr/local/lib/python3.8/site-packages/starlette/middleware/base.py", line 56, in body_stream
    task.result()
  File "/usr/local/lib/python3.8/site-packages/starlette/middleware/base.py", line 38, in coro
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/middleware/base.py", line 26, in __call__
    await response(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/responses.py", line 228, in __call__
    await run_until_first_complete(
  File "/usr/local/lib/python3.8/site-packages/starlette/concurrency.py", line 18, in run_until_first_complete
    [task.result() for task in done]
  File "/usr/local/lib/python3.8/site-packages/starlette/concurrency.py", line 18, in <listcomp>
    [task.result() for task in done]
  File "/usr/local/lib/python3.8/site-packages/starlette/responses.py", line 220, in stream_response
    async for chunk in self.body_iterator:
  File "/usr/local/lib/python3.8/site-packages/starlette/middleware/base.py", line 56, in body_stream
    task.result()
  File "/usr/local/lib/python3.8/site-packages/starlette/middleware/base.py", line 38, in coro
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc from None
  File "/usr/local/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 566, in __call__
    await route.handle(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 376, in handle
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/fastapi/applications.py", line 179, in __call__
    await super().__call__(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/applications.py", line 111, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc from None
  File "/usr/local/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc from None
  File "/usr/local/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 566, in __call__
    await route.handle(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 227, in handle
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 41, in app
    response = await func(request)
  File "/usr/local/lib/python3.8/site-packages/fastapi/routing.py", line 172, in app
    solved_result = await solve_dependencies(
  File "/usr/local/lib/python3.8/site-packages/fastapi/dependencies/utils.py", line 552, in solve_dependencies
    solved = await run_in_threadpool(call, **sub_values)
  File "/usr/local/lib/python3.8/site-packages/starlette/concurrency.py", line 34, in run_in_threadpool
    return await loop.run_in_executor(None, func, *args)
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.8/site-packages/dispatch/auth/service.py", line 82, in get_current_user
    return get_or_create(db_session=db_session, user_in=UserRegister(email=user_email))
  File "pydantic/main.py", line 346, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for UserRegister
email
  none is not an allowed value (type=type_error.none.not_allowed)

Inspecting using Chrome Dev tool shows Bearer token is missing when screen is refreshed or going to other endpoints like /incident/report.

Expected behavior Expect the Bearer token gets passed through for all subsequent calls.

Screenshots If applicable, add screenshots to help explain your problem. Screenshot 2020-09-24 at 8 30 29 PM

santhoshparepu avatar Sep 24 '20 15:09 santhoshparepu

@santhoshparepu thanks for the detailed report. I just ran through your steps on the latest master branch and wasn't able to reproduce, specifically:

  1. When I was unauthenticated I was redirected to /login
  2. Upon registering I was redirected to the dashboard
  3. I was able to navigate to other pages as well
  4. I was still able to use the app have a refresh

Some more questions:

  • Which browser are you using?
  • Is this running locally or deployed behind an ELB/Proxy?
  • Is there any other interesting console errors or request header you might be able to provide?

kevgliss avatar Sep 24 '20 16:09 kevgliss

@kevgliss Here is the .env file I am using. Just to confirm, I am cloning dispatch-docker, modifying .env file and running ./install.sh.

COMPOSE_PROJECT_NAME=dispatch
[email protected]
DISPATCH_HELP_SLACK_CHANNEL=#general
DISPATCH_UI_URL=https://xxx.yyyy.xxx
DISPATCH_AUTHENTICATION_PROVIDER_SLUG=dispatch-auth-provider-basic
SECRET_KEY="xxxxx"
DISPATCH_JWT_SECRET="xxx"
.
.
.
  1. Which browser are you using? I am seeing this in Safari, Chrome and Firefox.
  2. Is this running locally or deployed behind an ELB/Proxy? It is running on ubuntu VM and I am accessing this from my laptop.
  3. Is there any other interesting console errors or request header you might be able to provide? The error I posted in the only interesting thing I see the console logs. Screenshot posted above contains request headers. As I mentioned Authorization header is the only missing piece between successful and unsuccessful attempts.

santhoshparepu avatar Sep 24 '20 18:09 santhoshparepu

@kevgliss Please let me know if you need more information from me.

santhoshparepu avatar Sep 25 '20 11:09 santhoshparepu

Okay, I think I might understand what is happening... talking this through.

It seems that you are running the VM behind a DNS name, e.g. DISPATCH_UI_URL=https://xxx.yyyy.xxx for testing purposes I would access it directly via localhost and see if that resolves the issue. I believe that the frontend code is attempting to make a cross-origin request although we don't allow it (the server doesn't include a header to allow these types of requests).

We don't typically see this in production because we have a load balancer in front of our application, what we probably need to do to enable you to set your own hostname without a proxy is to define a DISPATCH_HOSTNAME that can be used by DISPATCH_UI_URL and be included in a CORS request header sent by the server.

If accessing via localhost works, I will put together a PR for the CORS changes.
`

kevgliss avatar Sep 25 '20 16:09 kevgliss

@kevgliss I switched to pkce and got page redirected to our Auth provider. Now, we are facing CORS issue. Isn't DISPATCH_UI_URL used for notifications only? Is this used as hostname too. And you mentioned about load balancer front-ending application servers. How do you ensure the notifications contains correct URL by setting DISPATH_UI_URL to localhost?

Access to fetch at 'xxx.com/..../token' from origin 'https://yyy.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

santhoshparepu avatar Sep 29 '20 17:09 santhoshparepu

I don't think pkce vs basic-auth is the issue here, it's a CORS issue, essentially you are attempting to access the app from a domain https://yyy.com that loads our app/js. Once loaded, the app attempts to fetch resources from the Dispatch API (auth data in this case), which looks like it's on a different domain than the app itself.

There are two options:

  1. Host the frontend and the API on the same domain (this is what we do in the load balancer case)
  2. Have the server allow the second domain via an Access-Control-Allow-Origin header

You're correct DISPATH_UI_URL is only used for notification. To test my theory, I don't think you should modify this value. Instead, simply access the dispatch app via http://localhost:8000/dashboard or whatever makes sense and ensure that both the frontend and backend are using the same server.

I believe this should be the default when using the docker image (accessing the web service by it's docker IP).

kevgliss avatar Sep 29 '20 17:09 kevgliss