Django XFrameOptionsMiddleware (X-Frame-Options) - allow iframe by client IP
Asked Answered
H

1

4

I'm using Django XFrameOptionsMiddleware to control clickjacking, but I have a customer that needs to be able to browse the app in an iframe from within their network. I want to be able to apply (or remove) the xframe_options_exempt decorator from within the view method.

Hypochondrium answered 10/9, 2014 at 18:56 Comment(0)
H
9

Best approach is to override get_xframe_options_value. XFRAME_EXEMPT_IPS is a glob_list in my case to detect allowable networks using fnmatch (192.168.*).

class TFXFrameOptionsMiddleware(XFrameOptionsMiddleware):
    def get_xframe_options_value(self, request, response):
        if request.META['REMOTE_ADDR'] in settings.XFRAME_EXEMPT_IPS:
            return 'ALLOWALL' # non standard, equivalent to omitting
        return getattr(settings, 'X_FRAME_OPTIONS', 'SAMEORIGIN').upper()
Hypochondrium answered 10/9, 2014 at 20:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.