
    4i"                    |    U d Z ddlm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  ee      Zded	<    G d
 d      Zy)a<  Security-hardened path utilities for Component v2.

This module centralizes path validation and resolution logic used by the
Component v2 registration and file access code paths. All helpers here are
designed to prevent path traversal, insecure prefix checks, and symlink escapes
outside of a declared package root.
    )annotationsN)Path)Final)StreamlitComponentRegistryError)
get_loggerr   _LOGGERc                  |    e Zd ZdZed	d       Zed
d       Zedd       Zedd       Zedd       Z	edd       Z
y)ComponentPathUtilszDUtility class for component path operations and security validation.c                ,     t         fddD              S )zCheck if a path contains glob pattern characters.

        Parameters
        ----------
        path : str
            The path to check

        Returns
        -------
        bool
            True if the path contains glob characters
        c              3  &   K   | ]  }|v  
 y wN ).0charpaths     v/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/streamlit/components/v2/component_path_utils.py	<genexpr>z9ComponentPathUtils.has_glob_characters.<locals>.<genexpr>4   s     AD44<As   )*?[])anyr   s   `r   has_glob_charactersz&ComponentPathUtils.has_glob_characters&   s     A,@AAA    c                2    t         j                  | d       y)a@  Validate that a path doesn't contain security vulnerabilities.

        Parameters
        ----------
        path : str
            The path to validate

        Raises
        ------
        StreamlitComponentRegistryError
            If the path contains security vulnerabilities like path traversal attempts
        zcomponent pathslabelN)r
   _assert_relative_no_traversalr   s    r   validate_path_securityz)ComponentPathUtils.validate_path_security6   s     	88EV8Wr   c                Z   t         j                  | d       t        |j                  |             }g }|D ]\  }	 |j	                         }|j	                         }|j                  |      st        j                  d|       L|j                  |       ^ t        |      dk(  rt        d|  d|       t        |      d	kD  r*d
j                  d |D              }t        d|  d| d      t        |d         S # t        t        f$ r!}t        j                  d||       Y d}~d}~ww xY w)a#  Resolve a glob pattern to a single file path with security checks.

        Parameters
        ----------
        pattern : str
            The glob pattern to resolve
        package_root : Path
            The package root directory for security validation

        Returns
        -------
        Path
            The resolved file path

        Raises
        ------
        StreamlitComponentRegistryError
            If zero or more than one file matches the pattern, or if security
            checks fail (path traversal attempts)
        zglob patternsr   z&Skipping file outside package root: %szFailed to resolve path %s: %sNr   z!No files found matching pattern 'z' in package root    z, c              3  2   K   | ]  }t        |        y wr   )str)r   fs     r   r   z:ComponentPathUtils.resolve_glob_pattern.<locals>.<genexpr>}   s     !BQ#a&!Bs   z'Multiple files found matching pattern '': z*. Exactly one file must match the pattern.)r
   r   listglobresolveis_relative_tor   warningappendOSError
ValueErrorlenr   joinr   )	patternpackage_rootmatching_filesvalidated_files	file_pathresolved_pathpackage_root_resolvede	file_lists	            r   resolve_glob_patternz'ComponentPathUtils.resolve_glob_patternF   sM   . 	888X l//89 ' 	I ) 1 1 3(4(<(<(>% %334IJOO@- &&}5	( 1$13G9<N|n]  !#		!B/!BBI19'#i[ Q; ; 
 OA&''! Z(  ?ANs   AC:C::D*	D%%D*c                  t        |       dk\  xr& | d   j                         xr | d   dk(  xr | d   dv }| j                  d      }| j                  d      xr | }t        j                  j                  |       s|s|s|rt        d	| d
|        | j                  dd      }|j                  d      D cg c]
  }|dk7  s	| }}t        d |D              rt        d| d
|        yc c}w )a	  Raise if ``path`` is absolute or contains ``..`` segments.

        Parameters
        ----------
        path : str
            Path string to validate.
        label : str
            Human-readable label used in error messages (e.g., "component paths").
           r   r"   :   )/\z\\r@   z"Absolute paths are not allowed in z: r?    c              3  &   K   | ]	  }|d k(    yw)z..Nr   )r   segs     r   r   zCComponentPathUtils._assert_relative_no_traversal.<locals>.<genexpr>   s     /ssd{/s   z+Path traversal attempts are not allowed in N)
r/   isalpha
startswithosr   isabsr   replacesplitr   )r   r   is_windows_drive_abs
is_unc_absis_rooted_backslash
normalizedrC   segmentss           r   r   z0ComponentPathUtils._assert_relative_no_traversal   s    IN 'Q!'Q3' Q;&	 	 __V,
 #ood3FJ GGMM$#"14UG2dVD 
 \\$,
#-#3#3C#8FCC2ICFF/h//1=eWBtfM  0 Gs   9
C.C.c          	         	 | j                         }|j                         }|j                  |      st        | d|  d| d      y# t        $ r}t        d| d|  d|       |d}~ww xY w)a  Ensure that abs_path is within root; raise if not.

        Parameters
        ----------
        abs_path : Path
            Absolute file path
        root : Path
            Root directory path
        kind : str
            Human-readable descriptor for error messages (e.g., "js" or "css")

        Raises
        ------
        StreamlitComponentRegistryError
            If the path cannot be resolved or if the resolved path does not
            reside within ``root`` after following symlinks.
        zFailed to resolve z path 'r&   Nz%' is outside the declared asset_dir 'z'.)r)   	Exceptionr   r*   )abs_pathrootkindresolvedroot_resolvedr8   s         r   ensure_within_rootz%ComponentPathUtils.ensure_within_root   s    &	'')H LLNM &&}51&z)NtfTVW  6  	1$TF'(3qcB	s    A 	A+A&&A+c                    | j                         }d|v sd|v ryt        j                  |      ry|j                  d      ryd|v sd|v ry|j	                         j                  d       S )	a  Heuristic to detect inline JS/CSS content strings.

        Treat a string as a file path ONLY if it looks path-like:
        - Does not contain newlines
        - Contains glob characters (*, ?, [, ])
        - Starts with ./, /, or \
        - Contains a path separator ("/" or "\\")
        - Or ends with a common asset extension like .js, .mjs, .cjs, or .css

        Otherwise, treat it as inline content.

        Parameters
        ----------
        value : str
            The string to classify as inline content or a file path.

        Returns
        -------
        bool
            True if ``value`` looks like inline content; False if it looks like a
            file path.
        
TF)z./r?   r@   r?   r@   )z.jsz.cssz.mjsz.cjs)stripr
   r   rE   lowerendswith)valuess     r   looks_like_inline_contentz,ComponentPathUtils.looks_like_inline_content   sk    0 KKM19	11!4<<)*!8tqyGGI&&'FGHHr   N)r   r$   returnbool)r   r$   r`   None)r1   r$   r2   r   r`   r   )r   r$   r   r$   r`   rb   )rQ   r   rR   r   rS   r$   r`   rb   )r]   r$   r`   ra   )__name__
__module____qualname____doc__staticmethodr   r    r:   r   rV   r_   r   r   r   r
   r
   #   s    NB B X X <( <(| & &P  @ %I %Ir   r
   )rf   
__future__r   rF   pathlibr   typingr   streamlit.errorsr   streamlit.loggerr   rc   r   __annotations__r
   r   r   r   <module>rn      s;    # 	   < 'H% %RI RIr   