
    2~hz                     J    d dl Z d dlZd dlmZ  G d de j                        Zy)    N)OrderedDictc                       e Zd ZdZej
                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                  ej                   ej"                  ej$                  fZdZdZdZd Zed        Zd Zd Zd Zd Z fd	Zd
 Zd Z d Z! xZ"S )SampleCodeValidatora  
    Class that checks if a string is a valid and "safe" Python expression

    What is considered "safe" for this class is limited to the context of generating
    provider method sample code and output for documentation purposes. The end goal
    is to pass a command string to `eval()` should the string pass the validation
    performed by this class.

    The main assumption this class will make is that the command string passed during
    class instantiation will always be in the form "{generator}.{method}({arguments})".
    In said form, {generator} is a `Generator` object variable that already exists
    within the scope where `eval()` will be called, {method} will be the provider
    method name which is also available within the `eval()` scope, and {arguments}
    will be sample arguments parsed from docstrings. This means that {arguments} can
    potentially be used as a vector for code injection.

    In order to neuter the impact of code injection, the following validation steps
    will be applied:

    - The command string is parsed using 'eval' mode, meaning expressions only.
    - The command string can only have whitelisted code elements. See `_whitelisted_nodes`.
    - The command string can only have one instance of variable access.
    - The command string can only have one instance of attribute access.
    - The command string can only have one instance of a function/method call.
    - The argument values in the command string can only be literals.
    - The only literals allowed are numbers (integers, floats, or complex numbers),
      strings (but not f-strings), bytes, lists, tuples, sets, dictionaries, True,
      False, and None.

    There is, however, an exception. In order to accommodate sample code with custom
    probability distribution, variable access to `OrderedDict` will not count against
    the maximum limit of variable access, and invoking `OrderedDict` constructor calls
    will not count against the maximum limit of function/method calls. In order to
    neuter the impact of code injection, please ensure that `OrderedDict` refers to
    the standard library's `collections.OrderedDict` within the `eval()` scope before
    passing the command string to `eval()` for execution. This can be done in code review.
       c                 $   t               | _        d| _        d| _        d| _        || _        	 t        j                  |d      | _        | j                          y # t        t        f$ r& | j                  t        j                                Y y w xY w)Nr   eval)mode)set_errors_function_call_count_attribute_access_count_variable_access_count_commandastparse_tree	_validateSyntaxError
ValueError
_log_error	traceback
format_exc)selfcommands     _/var/www/peopleoo.sandbox-dev.co.uk/venv/lib/python3.12/site-packages/faker/sphinx/validator.py__init__zSampleCodeValidator.__init__E   sx    u$%!'($&'#	78DJ NN Z( 	4OOI0023	4s   A 2BBc                     | j                   S N)r   r   s    r   errorszSampleCodeValidator.errorsS   s    ||    c                 .    t        || j                        S r   )
isinstance_whitelisted_nodes)r   nodes     r   _is_whitelistedz#SampleCodeValidator._is_whitelistedW   s    $ 7 788r!   c                 :    | j                   j                  |       y r   )r   add)r   msgs     r   r   zSampleCodeValidator._log_errorZ   s    r!   c                 :    | j                  | j                         y r   )visitr   r   s    r   r   zSampleCodeValidator._validate]   s    

4::r!   c                     d}t        |t        j                        r| j                  |j                        }|S t        |t        j
                        r|j                  t        j                  k(  rd}|S )NFT)	r#   r   Call_is_node_using_ordereddictfuncNameidr   __name__)r   r%   is_valids      r   r.   z.SampleCodeValidator._is_node_using_ordereddict`   s]     dCHH%66tyyAH  chh'DGG{7K7K,KHr!   c                     | j                  |      s*d|j                  j                  z  }| j                  |       t        |   |      S )Nz!Code element `%s` is not allowed.)r&   	__class__r2   r   superr+   )r   r%   r)   r5   s      r   r+   zSampleCodeValidator.visitm   s@    ##D)58O8OOCOOC w}T""r!   c                     | j                  |      sB| j                  | j                  k  r| xj                  dz  c_        nd}| j                  |       | j	                  |       y )Nr   z9There can only be one instance of a function/method call.)r.   r   _max_function_call_countr   generic_visitr   r%   r)   s      r   
visit_CallzSampleCodeValidator.visit_Callu   sV    ..t4((4+H+HH))Q.)Q$ 	4 r!   c                     | j                   | j                  k  r| xj                   dz  c_         nd}| j                  |       | j                  |       y )Nr   z3There can only be one instance of attribute access.)r   _max_attribute_access_countr   r9   r:   s      r   visit_Attributez#SampleCodeValidator.visit_Attribute   sH    ''$*J*JJ((A-(GCOOC  	4 r!   c                     | j                  |      sB| j                  | j                  k  r| xj                  dz  c_        nd}| j                  |       | j	                  |       y )Nr   z2There can only be one instance of variable access.)r.   r   _max_variable_access_countr   r9   r:   s      r   
visit_NamezSampleCodeValidator.visit_Name   sV    ..t4**T-L-LL++q0+J$ 	4 r!   )#r2   
__module____qualname____doc__r   
Expressionr-   	Attributer0   LoadkeywordNumStrBytesListTupleSetDictNameConstantr$   r8   r=   r@   r   propertyr    r&   r   r   r.   r+   r;   r>   rA   __classcell__)r5   s   @r   r   r      s    $P 					!&  !"#!"  9#
!	!
!r!   r   )r   r   collectionsr   NodeVisitorr    r!   r   <module>rV      s     
  #O!#// O!r!   