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

Django 3.1: CORS headers not set on error responses? #578

Open
bjmc opened this issue Sep 24, 2020 · 8 comments
Open

Django 3.1: CORS headers not set on error responses? #578

bjmc opened this issue Sep 24, 2020 · 8 comments

Comments

@bjmc
Copy link

bjmc commented Sep 24, 2020

Hello, I'm not sure if there's something I'm misunderstanding, but from my experiments, it seems like django-cors-headers is not setting CORS headers on HTTP 4XX error responses, when used with Django 3.1. I know there's been some changes to the way middleware are handled over the years. Is it possible that django-cors-headers needs to be updated to be fully compatible with newer versions?

As an experiment, I added a process_exception() method to CorsMiddleware, like this:

def process_exception(self, request, exception):
    return self.process_response(request, exception)

And that seems to have resolved my issue. Is this a sensible way to proceed? If so, I'm happy to put together a formal pull request.

Thanks for a useful library!

@adamchainz
Copy link
Owner

Hi!

Yes we should probably do this.

But does it help you if CORS headers are set on error responses? Was the error expected on the server side, and something you could then process in browser?

*Thanks,
Adam

@bjmc
Copy link
Author

bjmc commented Sep 24, 2020

In our case, yes, I think we definitely want CORS headers set on error responses. We're using graphene-django to build a GraphQL API. If the client sends us a malformed query, we want to be able to return a sensible error without the browser tripping over CORS restrictions.

I can't say this is universal, but I would guess most people building APIs in Django and setting CORS headers would also want those headers on their error responses. Do you think it's correct to treat process_exception() and process_response() equivalently? The only thing that gives me pause is returning an HTTP response from process_exception() will short-circuit other middleware executions. But the CORS middleware needs to be near the outside of the middleware stack anyway, right?

@udemezue01

This comment has been minimized.

@marcosalcazar
Copy link

@bjmc Hello! did you go with that solution in the end? We are facing this same issue here with our graphene-django API endpoing.

@bjmc
Copy link
Author

bjmc commented Aug 20, 2021

Hi @marcosalcazar yes, we just subclassed CorsMiddleware and added the process_exception() method as above. We haven't run into any problems that I'm aware of, but it does feel a little bit hacky. I'd like to see a proper upstream fix for this issue.

@marcosalcazar
Copy link

Cool, thanks @bjmc for the quick response!

@kbakk
Copy link

kbakk commented Apr 12, 2023

A "+ 1" for adding support for process_exception from me. The missing process_exception caused some head scratching and debugging, as I couldn't understand why it would work when I used HttpResponse("Bad...", status=400) but not raise BadRequest("Bad...").

The suggested workaround doesn't seem to work for me – when raising BadRequest, exception is an exception object, so that results in AttributeError: 'BadRequest' object has no attribute 'has_header'.

As an fun fact, this can be raised (with the workaround in place) and will get the correct headers:

class StrangeException(HttpResponse, Exception):
    pass
...
raise StrangeException("Invalid response", status=400)

@merwok
Copy link

merwok commented Apr 12, 2023

But does it help you if CORS headers are set on error responses? Was the error expected on the server side, and something you could then process in browser?

It would help coworkers if the real error messages in the browser console were not hidden by CORS check errors 🙂

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

6 participants