django-debug-toolbar icon indicating copy to clipboard operation
django-debug-toolbar copied to clipboard

Original response is lost when redirection is intercepted

Open interDist opened this issue 3 years ago • 5 comments

The RedirectsPanel creates a new response, preserving only the cookies of the original response. Then any additional information in the original response is lost and cannot be made visible in the toolbar’s panels. In our case, each response has authorization information and I would like to be able to verify that this information is captured correctly also when the user is ultimately redirected. The fix looks quite straightforward, within the if: https://github.com/jazzband/django-debug-toolbar/blob/f1387801e84a343fbce5c7955423f87589790a26/debug_toolbar/panels/redirects.py#L20 modify the code creating the new response to

    original_response = response
    response = SimpleTemplateResponse( ... )
    response.original_response = original_response

Everything else will be taken care of by custom code in the relevant panels...

interDist avatar Sep 26 '22 08:09 interDist

@interDist sorry I never responded to this. Do you remember why this worked for you? I don't see original_response used anywhere, so I'm not sure how that helps you.

tim-schilling avatar Oct 23 '23 00:10 tim-schilling

I had to remember what this is about and my idea for a solution, this was one year ago... 😏 Basically we have custom panels or customizations of the existing panels, that would need the extra information from the original response. My idea was that this custom code would do getattr(response, 'original_response', response).extra_data. I have not included this code in the end, because I didn’t want to reimplement the RedirectsPanel. So at this moment we cannot inspect this extra information via the Debug Toolbar when the request is redirected.

interDist avatar Oct 24 '23 18:10 interDist

Understood. Unless we use that original response somewhere, I'm inclined to not collect it in the first place. If someone does need it, it's relatively straightforward to subclass RedirectsPanel and capture it.

tim-schilling avatar Oct 27 '23 14:10 tim-schilling

Would it be possible then to add a callback point which subclasses can use? It would be called after the new response is created and before it is rendered, with both the original response and the redirect capture response.

interDist avatar Oct 27 '23 18:10 interDist

I think I'd be on board with a PR that changes RedirectsPanels to use a new method called def get_interception_response(self, response) -> SimpleTemplateResponse or something similar.

From what I understand that should provide you with the hook to do what you need.

tim-schilling avatar Oct 27 '23 18:10 tim-schilling