
    VeIiB                         d dl mZ d dlZdZdZdZdZdZdd	d
ZdZ	ddddd	ddZ
ddiZi Zi ZdZ G d de          Z	 d dlZd ZdS # e$ r d dlZd dlZ ej                    Zd ZY dS w xY w)    )OrderedDictNDENY
SAMEORIGINz
ALLOW-FROMi>zstrict-origin-when-cross-originz'self'z'none')default-src
object-srcLaxz1'self' themes.googleusercontent.com *.gstatic.comz%'self' www.google.com www.youtube.comzG'self' ajax.googleapis.com *.googleanalytics.com *.google-analytics.comz='self' ajax.googleapis.com fonts.googleapis.com *.gstatic.comz'self' *.gstatic.com)zfont-srcz	frame-srcz
script-srcz	style-srcr   r   zbrowsing-topicsz()    c                       e Zd ZdZddZeeedddeddde	de
dddeddeddfdZd Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZdS )TalismanzB
    Talisman is a Flask extension for HTTP security headers.
    Nc                 *    | | j         |fi | d S d S N)init_app)selfappkwargss      \/var/www/html/MCyber-Diagnostic/venv/lib/python3.11/site-packages/flask_talisman/talisman.py__init__zTalisman.__init__C   s.    ?DM#((((((( ?    TFc                    t          |t                    rt          |          | _        n|| _        t          |t                    rt          |          | _        n|| _        t          |t                    rt          |          | _        n|| _        || _        || _        || _        |	| _	        |
| _
        || _        || _        || _        t          |t                    rt          |          | _        n|| _        || _        || _        | j        r| j        t#          d          |pg | _        | j        |j        j        d<   || _        || _        ||j        d<   |r
d|j        d<   || _        || _        || _        || _        |                    | j                   |                    | j                   |                     | j!                   dS )a
  
        Initialization.

        Args:
            app: A Flask application.
            feature_policy: A string or dictionary describing the
                feature policy for the response.
            permissions_policy: A string or dictionary describing the
                permissions policy for the response.
            document_policy: A string or dictionary describing the
                document policy for the response.
            force_https: Redirects non-http requests to https, disabled in
                debug mode.
            force_https_permanent: Uses 301 instead of 302 redirects.
            frame_options: Sets the X-Frame-Options header, defaults to
                SAMEORIGIN.
            frame_options_allow_from: Used when frame_options is set to
                ALLOW_FROM and is a string of domains to allow frame embedding.
            strict_transport_security: Sets HSTS headers.
            strict_transport_security_preload: Enables HSTS preload. See
                https://hstspreload.org.
            strict_transport_security_max_age: How long HSTS headers are
                honored by the browser.
            strict_transport_security_include_subdomains: Whether to include
                all subdomains when setting HSTS.
            content_security_policy: A string or dictionary describing the
                content security policy for the response.
            content_security_policy_report_uri: A string indicating the report
                URI used for CSP violation reports
            content_security_policy_report_only: Whether to set the CSP header
                as "report-only", which disables the enforcement by the browser
                and requires a "report-uri" parameter with a backend to receive
                the POST data
            content_security_policy_nonce_in: A list of csp sections to include
                a per-request nonce value in
            referrer_policy: A string describing the referrer policy for the
                response.
            session_cookie_secure: Forces the session cookie to only be sent
                over https. Disabled in debug mode.
            session_cookie_http_only: Prevents JavaScript from reading the
                session cookie.
            session_cookie_samesite: Sets samesite parameter on session cookie
            force_file_save: Prevents the user from opening a file download
                directly on >= IE 8
            x_content_type_options: Prevents MIME type sniffing
            x_xss_protection: Prevents the page from loading when the browser
                detects reflected cross-site scripting attacks

        See README.rst for a detailed description of each option.
        Nz}Setting content_security_policy_report_only to True also requires a URI to be specified in content_security_policy_report_uri	csp_nonceSESSION_COOKIE_SAMESITETSESSION_COOKIE_HTTPONLY)"
isinstancedictr   feature_policypermissions_policydocument_policyforce_httpsforce_https_permanentframe_optionsframe_options_allow_fromstrict_transport_security!strict_transport_security_preload!strict_transport_security_max_age,strict_transport_security_include_subdomainscontent_security_policy"content_security_policy_report_uri#content_security_policy_report_only
ValueError content_security_policy_nonce_in
_get_nonce	jinja_envglobalsreferrer_policysession_cookie_secureconfigforce_file_savex_content_type_optionsx_xss_protectionr   before_request_force_https_make_nonceafter_request_set_response_headers)r   r   r   r   r   r   r   r1   r    r!   r"   r#   r$   r%   r&   r'   r(   r*   r.   r/   session_cookie_http_onlysession_cookie_samesiter2   r3   s                           r   r   zTalisman.init_appG   s   V nd++ 	1"-n"="=D"0D($// 	9&12D&E&ED##&8D#ot,, 	3#.#?#?D  #2D &%:"*(@%)B&- 	. . 	. 9 	9 -t44 	C+67N+O+OD((+BD(. 	/ 0 	03 	67?56 6 6
 -2 	- .2_k*.%:"0G
,-# 	948CJ01.&<# 04,---4+,,,$455555r   c                 >   t           j        j                            t           j        j                  }t          |di           }|                    d| j                   |                    d| j	                   |                    d| j
                   |                    d| j                   |                    d| j                   |                    d| j                   |                    d| j                   |                    d	| j                   |S )
Ntalisman_view_optionsr   r    r!   r&   r*   r   r   r   )flaskcurrent_appview_functionsgetrequestendpointgetattr
setdefaultr   r    r!   r&   r*   r   r   r   )r   view_functionview_optionss      r   _get_local_optionszTalisman._get_local_options   sA   )8<<M"$ $2B8 8 	t/?@@@1CDDD&(E	G 	G 	G%t'C	E 	E 	E.1	3 	3 	3 	 $"9	; 	; 	;t3	5 	5 	5d1	
 	
 	
 r   c                    | j         r| j        j        sd| j        j        d<   | j        j        t          j        j        t          j        j                            dd          dk    g}| 	                                }|d         r|t          |          sot          j        j                            d          rMt          j        j                            ddd	          }d
}| j        rd}t	          j        ||          }|S dS dS dS )zZRedirect any non-https requests to https.

        Based largely on flask-sslify.
        TSESSION_COOKIE_SECUREX-Forwarded-Protohttphttpsr   zhttp://zhttps://   i.  i-  )codeN)r/   r   debugr0   r=   rA   	is_secureheadersr@   rG   anyurl
startswithreplacer   redirect)r   criterialocal_optionsrS   rN   rs         r   r5   zTalisman._force_https   s    % 	@8> @;? 78 HNM#M!%%&96BBgM
 //11' 	H 	} ++I66 m'//	:qII- DN3T222	 	 	 	 r   c                    |                                  }|                     |j        |           |                     |j        |           |                     |j        |           |                     |j        |           |                     |j        |           |                     |j                   |                     |j                   |S )z5Applies all configured headers to the given response.)	rG   _set_feature_policy_headersrQ   _set_permissions_policy_headers_set_document_policy_headers_set_frame_options_headers$_set_content_security_policy_headers_set_hsts_headers_set_referrer_policy_headers)r   responseoptionss      r   r8   zTalisman._set_response_headers  s    ))++(()97CCC,,X-=wGGG))(*:GDDD''(8'BBB11(2BGLLLx/000))(*:;;;r   c                     |                                  }|d         rH|d         rBt          t          j        dd           s)t	          t
                    t          j        _        d S d S d S d S )Nr&   r*   r   )rG   rC   r=   rA   get_random_stringNONCE_LENGTHr   )r   rX   s     r   r6   zTalisman._make_nonce  s    //1178	F@A	F EM;==	F '8&E&EEM###		F 	F 	F 	F 	F 	Fr   c                 8    t          t          j        dd          S )Nr    )rC   r=   rA   )r   s    r   r+   zTalisman._get_nonce   s    u}k2666r   c                     t          |t                    r|S g }|                                D ]0\  }}d                    ||          }|                    |           1d                    |          }|S )Nz{}={}z, )r   stritemsformatappendjoin)r   policypoliciessectioncontentpolicy_parts         r   _parse_structured_header_policyz(Talisman._parse_structured_header_policy#  sx    fc"" 	M & 	) 	)GW!..'::KOOK((((8$$r   c                    |                                  }t          |t                    ru|}t                      }|                    d          D ]O}|                                                    d          }d                    |dd                    ||d         <   Pg }|                                D ]\  }}t          |t                    sd                    |          }d                    ||          }t          t          j        d          r1||d         v r'|d                    t          j        j                  z  }|                    |           d	                    |          }|S )
N; rM   r   z{} {}r   r*   z 'nonce-{}'z; )rG   r   rj   r   splitstriprn   rk   rl   hasattrr=   rA   r   rm   )	r   ro   rX   policy_stringrs   policy_partsrp   rq   rr   s	            r   _parse_policyzTalisman._parse_policy1  s_   //11fc"" 	E"M ]]F,22377 E E*002288==*-((<3C*D*D|A'' & 
	) 
	)GWgs++ ,((7++!..'::K EM;77M}-OPPP}33EM4KLLLOOK((((8$$r   c                 ^    |d         sd S |d         }|                      |          }||d<   d S )Nr   zFeature-Policy)r}   r   rQ   rc   ro   s       r   r[   z$Talisman._set_feature_policy_headersM  sD    '( 	F)*##F++$* !!!r   c                 ^    |d         sd S |d         }|                      |          }||d<   d S )Nr   zPermissions-Policyrt   r   s       r   r\   z(Talisman._set_permissions_policy_headersV  sD    +, 	F-.55f==(.$%%%r   c                 ^    |d         sd S |d         }|                      |          }||d<   d S )Nr   zDocument-Policyr   r   s       r   r]   z%Talisman._set_document_policy_headers_  sD    () 	F*+55f==%+!"""r   c                     |d         sd S |d         |d<   |d         t           k    r+|dxx         d                    |d                   z  cc<   d S d S )Nr    zX-Frame-Optionsz {}r!   )
ALLOW_FROMrl   )r   rQ   rc   s      r   r^   z#Talisman._set_frame_options_headersh  sx    ' 	F%,_%=!"?#z11%&&&%,,23+5 +5 5&&&&& 21r   c                     | j         rd|d<   | j        rd|d<   | j        rd|d<   |d         sd S |d         }|                     |          }| j        rd|vr|d	| j        z   z  }d
}| j        r|dz  }|||<   d S )Nz1; mode=blockzX-XSS-ProtectionnosniffzX-Content-Type-OptionsnoopenzX-Download-Optionsr&   z
report-uriz; report-uri zContent-Security-Policyz-Report-Only)r3   r2   r1   r}   r'   r(   )r   rQ   rc   ro   
csp_headers        r   r_   z-Talisman._set_content_security_policy_headersq  s      	:*9G&'& 	:09G,- 	5,4G()01 	F23##F++2 	PF**o(OOOF.
3 	).(J$
r   c                    t           j        j        t           j        j                            dd          dk    g}| j        rt          |          sd S d                    | j                  }| j	        r|dz  }| j
        r|dz  }||d<   d S )NrJ   rK   rL   z
max-age={}z; includeSubDomainsz	; preloadzStrict-Transport-Security)r=   rA   rP   rQ   r@   r"   rR   rl   r$   r%   r#   )r   rQ   rW   values       r   r`   zTalisman._set_hsts_headers  s    M#M!%%&96BBgM
 - 	S]] 	F##D$JKK< 	+**E1 	![ E/4+,,,r   c                     | j         |d<   d S )NzReferrer-Policy)r.   )r   rQ   s     r   ra   z%Talisman._set_referrer_policy_headers  s    %)%9!"""r   c                     fd}|S )a  Use talisman as a decorator to configure options for a particular
        view.

        Only force_https, frame_options, frame_options_allow_from,
        content_security_policy, content_security_policy_nonce_in
        and feature_policy can be set on a per-view basis.

        Example:

            app = Flask(__name__)
            talisman = Talisman(app)

            @app.route('/normal')
            def normal():
                return 'Normal'

            @app.route('/embeddable')
            @talisman(frame_options=ALLOW_FROM, frame_options_allow_from='*')
            def embeddable():
                return 'Embeddable'
        c                 *    t          | d           | S )Nr<   )setattr)fr   s    r   	decoratorz$Talisman.__call__.<locals>.decorator  s    A.777Hr    )r   r   r   s    ` r   __call__zTalisman.__call__  s$    ,	 	 	 	 	 r   r   )__name__
__module____qualname____doc__r   DEFAULT_FEATURE_POLICYDEFAULT_PERMISSIONS_POLICYDEFAULT_DOCUMENT_POLICYr   ONE_YEAR_IN_SECSDEFAULT_CSP_POLICYDEFAULT_REFERRER_POLICYDEFAULT_SESSION_COOKIE_SAMESITEr   rG   r5   r8   r6   r+   rt   r}   r[   r\   r]   r^   r_   r`   ra   r   r   r   r   r   r   >   ss        ) ) ) ) 293"'!$%)&*.3.>9=$6/305-13"&%)$C#'"1O6 O6 O6 O6b  2  6
 
 
F F F7 7 7    8+ + +/ / /, , ,5 5 5% % %45 5 5$: : :    r   r   c                 :    t          j        |           d |          S r   )secretstoken_urlsafe)lengths    r   re   re     s     $V,,WfW55r   c                     t           j        t           j        z   t           j        z   d                    fdt          |           D                       S )Nrh   c              3   L   K   | ]}t                                         V  d S r   )rndchoice).0_allowed_charss     r   	<genexpr>z$get_random_string.<locals>.<genexpr>  sC       $ $ JJ}%%$ $ $ $ $ $r   )stringascii_lowercaseascii_uppercasedigitsrn   range)r   r   s    @r   re   re     sg    ""#M 	 ww $ $ $ $6]]$ $ $ $ $ 	$r   )collectionsr   r=   r   r   r   r   r   r   r   GOOGLE_CSP_POLICYr   r   r   rf   objectr   r   re   ImportErrorrandomr   SystemRandomr   r   r   r   <module>r      s_   $ # # # # #  

 ;    
 #(  F:+!+  " t 
   { { { { {v { { {|$NNN6 6 6 6 6  $ $ $MMMMMM
&


C$ $ $ $ $ $$s   A A*)A*