
    ]j
                         d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	 	 d dl
mZ 	 d dlmZ d d
lmZ  G d de      Zy# e$ r	 d dlmZ Y &w xY w# e$ r  G d d	e      ZY 5w xY w)    )absolute_importN)partial)settings)SimpleLazyObject)http_client)MiddlewareMixinc                       e Zd ZdZy)r   zt
        If this middleware doesn't exist, this is an older version of django
        and we don't need it.
        N)__name__
__module____qualname____doc__     8/root/env/lib/python3.12/site-packages/csp/middleware.pyr   r      s    	 	r   r   )build_policyc                   (    e Zd ZdZd Zd Zd Zd Zy)CSPMiddlewarez
    Implements the Content-Security-Policy response header, which
    conforming user-agents can use to restrict the permitted sources
    of various content.

    See http://www.w3.org/TR/CSP/

    c                     t        |dd       s<t        j                  t        j                  d            j                  d      |_        |j                  S )N
_csp_nonce   ascii)getattrbase64	b64encodeosurandomdecoder   )selfrequests     r   _make_noncezCSPMiddleware._make_nonce&   sF     wd32::b>+ 
 !!!r   c                 P    t        | j                  |      }t        |      |_        y )N)r   r    r   	csp_nonce)r   r   nonces      r   process_requestzCSPMiddleware.process_request1   s!    (('2,U3r   c                 j   t        |dd      r|S t        t        dd      }|j                  j                  |      r|S |j                  }t
        j                  t
        j                  f}||v rt        j                  r|S d}t        t        dd      r|dz  }||v r|S | j                  ||      ||<   |S )N_csp_exemptFCSP_EXCLUDE_URL_PREFIXESr   zContent-Security-PolicyCSP_REPORT_ONLYz-Report-Only)
r   r   	path_info
startswithstatus_coder   INTERNAL_SERVER_ERROR	NOT_FOUNDDEBUGr   )r   r   responseprefixesr+   exempted_debug_codesheaders          r   process_responsezCSPMiddleware.process_response5   s    8]E2O 8%?D''1O **--!! 
 ..8>>O*8.6n$FXO,,Wh?r   c                     t        |dd       }t        |dd       }t        |dd       }t        |dd       }t        ||||      S )N_csp_config_csp_update_csp_replacer   )configupdatereplacer#   )r   r   )r   r   r/   r8   r9   r:   r#   s          r   r   zCSPMiddleware.build_policyS   sP    =$7=$7(ND9t46&'"') 	)r   N)r
   r   r   r   r    r$   r3   r   r   r   r   r   r      s    	"4<)r   r   )
__future__r   r   r   	functoolsr   django.confr   django.utils.functionalr   django.utils.six.movesr   ImportErrorhttp.clientclientdjango.utils.deprecationr   object	csp.utilsr   r   r   r   r   <module>rF      sh    & 	     4&2
8 #<)O <)#  &%&  & s"   A A AAA%$A%