§
    íóÉj�1  ã                  ó  — d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	m
Z
 dZ G d„ dej        ¦  «        Z G d	„ d
ej        ¦  «        Zdddœd"d„ZdZ eh d£¦  «        Z G d„ dej        ¦  «        Z eh d£¦  «        Zdddœd#d!„ZdS )$aZ  
Helpers for safer and/or more explicit pickle usage in NLTK.

- RestrictedUnpickler: blocks unpickling of *any* globals (classes/functions).
  Intended for loading NLTK data packages where we control the serialization.
- WarningUnpickler: emits a security warning before unpickling (does not make
  unpickling safe).
- AllowlistUnpickler: only reconstructs an explicit, audited allowlist of
  globals. Use it for loading objects whose set of legitimate classes is known
  (e.g. a trained model) so that arbitrary-code gadgets such as ``os.system``
  can never be reconstructed from an untrusted file.
é    )ÚannotationsN)ÚIterable)ÚAnyÚBinaryIOz Security warning: loading pickles can execute arbitrary code. Only load pickle files from trusted sources and never from untrusted or unauthenticated locations.c                  ó   — e Zd ZdZd	d„ZdS )
ÚRestrictedUnpicklerzW
    Unpickler that prevents any class or function from being used during loading.
    ÚmoduleÚstrÚnameÚreturnr   c                ó8   — t          j        d|› d|› d�¦  «        ‚)Núglobal 'ú.z' is forbidden)ÚpickleÚUnpicklingError©Úselfr	   r   s      úq/home/lighthouse/badge-speaker-benchmark/.benchmark-venv-community/lib/python3.11/site-packages/nltk/picklesec.pyÚ
find_classzRestrictedUnpickler.find_class*   s'   € åÔ$Ð%M°Ð%MÐ%M¸Ð%MÐ%MÐ%MÑNÔNÐNó    N©r	   r
   r   r
   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   © r   r   r   r   %   s8   € € € € € ðð ðOð Oð Oð Oð Oð Or   r   c                  ó6   ‡ — e Zd ZdZddœdˆ fd
„Zdˆ fd„Zˆ xZS )ÚWarningUnpicklerz6Unpickler that emits PICKLE_WARNING once per instance.N©ÚcontextÚfiler   r    ú
str | NoneÚkwargsr   c               óX   •—  t          ¦   «         j        |fi |¤Ž || _        d| _        d S )NF)ÚsuperÚ__init__Ú_contextÚ_warned)r   r!   r    r#   Ú	__class__s       €r   r&   zWarningUnpickler.__init__2   s4   ø€ Ø�‰ŒÔ˜Ð(Ð( Ð(Ð(Ð(ØˆŒØˆŒˆˆr   r   c                óØ   •— | j         sC| j        €t          nt          › d| j        › d�}t          j        |t
          d¬¦  «         d| _         t          ¦   «                              ¦   «         S )Nz (ú)é   )Ú
stacklevelT)r(   r'   ÚPICKLE_WARNINGÚwarningsÚwarnÚRuntimeWarningr%   Úload)r   Úmsgr)   s     €r   r2   zWarningUnpickler.load7   sk   ø€ ØŒ|ð 	 ð ”=Ð(õ �å&Ð:Ð:¨$¬-Ð:Ð:Ð:ð õ
 ŒM˜#�~¸!Ð<Ñ<Ô<Ð<ØˆDŒLÝ‰wŒw�|Š|‰~Œ~Ðr   )r!   r   r    r"   r#   r   )r   r   )r   r   r   r   r&   r2   Ú__classcell__©r)   s   @r   r   r   /   sn   ø€ € € € € Ø@Ð@à@Dð ð ð ð ð ð ð ð ð
	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	r   r   F)r    Ú
restrictedr!   r   r    r"   r6   Úboolr   r   c               óŽ   — |r!t          | ¦  «                             ¦   «         S t          | |¬¦  «                             ¦   «         S )zÎ
    Convenience wrapper similar to pickle.load(file).

    - If restricted=True, uses RestrictedUnpickler (no warning by default).
    - If restricted=False, uses WarningUnpickler and emits a warning.
    r   )r   r2   r   )r!   r    r6   s      r   Úpickle_loadr9   C   sE   € ð ð 0Ý" 4Ñ(Ô(×-Ò-Ñ/Ô/Ð/Ý˜D¨'Ð2Ñ2Ô2×7Ò7Ñ9Ô9Ð9r   )ÚosÚntÚposixÚ
subprocessÚsysÚsocketÚshutilÚsignalÚptyÚpopen2ÚcommandsÚctypesÚ	importlibÚrunpyÚbuiltinsÚ
webbrowserÚpdbÚbdbÚcodeÚcodeopÚmultiprocessingÚasyncioÚ	threadingr   z
numpy.f2pyznumpy.ctypeslibznumpy.distutilsznumpy.testingznltk.tokenize.reppznltk.internals>   ©Únumpyr2   ©rR   Úsave©rR   Úsavez©rR   Úmemmap©rR   Úloadtxt©rR   Úsavetxt©rR   Úfromfile©rR   Ú	fromregex©rR   Ú	vectorize©rR   Ú
DataSource©rR   Ú
genfromtxt©rR   Úsavez_compressed©ÚpandasÚread_picklec                  ó`   ‡ — e Zd ZdZdddœdˆ fd„Zedd„¦   «         Zdd„Zdˆ fd„Zdd„Z	ˆ xZ
S )ÚAllowlistUnpickleraF  
    Unpickler that only reconstructs an explicit, audited allowlist of globals.

    Two complementary allowlists are supported:

    - ``allowed_globals``: a set of exact ``(module, qualname)`` pairs. Use this
      for individual safe globals from an otherwise-dangerous module, e.g.
      ``("builtins", "int")``.
    - ``allowed_modules``: a set of module names; a global is permitted when its
      module equals an allowed name or is a submodule of one (i.e. ``module``
      equals ``name`` or starts with ``name + "."``).

    Three hard guards apply *before* the allowlists and cannot be opted out of:

    1. A dotted ``name`` is rejected. For pickle protocol >= 4,
       ``pickle.Unpickler.find_class`` resolves a dotted ``name`` through
       :func:`getattr` chaining, so an attacker who is allowed the module
       ``sklearn`` could otherwise request ``sklearn.os.system`` and reach a
       command-execution sink the module allowlist never intended (GHSA-4489).
       Legitimate model globals are always a single qualname.
    2. Globals from a denied module (:data:`_DENIED_MODULE_PREFIXES`) are
       rejected even if a parent namespace is allowlisted, so an in-namespace
       gadget cannot ride a broad ``allowed_modules`` entry (GHSA-x99w).
    3. ``builtins`` is denied wholesale; individual safe builtins must be named
       explicitly via ``allowed_globals``.

    Anything not covered by either allowlist raises ``UnpicklingError`` before
    the global is resolved, so dangerous callables such as ``os.system`` or
    ``builtins.eval`` can never be reconstructed from an untrusted pickle. This
    is stricter than :func:`pickle_load` (which only warns and then executes)
    but, unlike :class:`RestrictedUnpickler` (which blocks *all* globals), it
    still allows the known-good classes a saved object legitimately needs.
    r   ©Úallowed_globalsÚallowed_modulesr!   r   ro   úIterable[tuple[str, str]]rp   úIterable[str]r#   r   c               ó¼   •—  t          ¦   «         j        |fi |¤Ž t          |t          ¦  «        r|f}t	          |¦  «        | _        t          |¦  «        | _        d S )N)r%   r&   Ú
isinstancer
   ÚsetÚ_allowed_globalsÚtupleÚ_allowed_modules)r   r!   ro   rp   r#   r)   s        €r   r&   zAllowlistUnpickler.__init__µ   sd   ø€ ð 	�‰ŒÔ˜Ð(Ð( Ð(Ð(Ð(Ý�o¥sÑ+Ô+ð 	1Ø.Ð0ˆOÝ # OÑ 4Ô 4ˆÔÝ % oÑ 6Ô 6ˆÔÐÐr   r	   r
   r   r7   c                óD   ‡ — t          ˆ fd„t          D ¦   «         ¦  «        S )Nc              3  óT   •K  — | ]"}‰|k    p‰                      |d z   ¦  «        V — Œ#dS ©r   N©Ú
startswith)Ú.0Údenyr	   s     €r   ú	<genexpr>z4AllowlistUnpickler._module_denied.<locals>.<genexpr>Å   óR   øè è € ð 
ð 
àð �dŠNÐ;˜f×/Ò/°°s±
Ñ;Ô;ð
ð 
ð 
ð 
ð 
ð 
r   )ÚanyÚ_DENIED_MODULE_PREFIXES)r	   s   `r   Ú_module_deniedz!AllowlistUnpickler._module_deniedÃ   s;   ø€ åð 
ð 
ð 
ð 
å/ð
ñ 
ô 
ñ 
ô 
ð 	
r   c                óD   ‡— t          ˆfd„| j        D ¦   «         ¦  «        S )Nc              3  óT   •K  — | ]"}‰|k    p‰                      |d z   ¦  «        V — Œ#dS r{   r|   )r~   r   r	   s     €r   r€   z5AllowlistUnpickler._module_allowed.<locals>.<genexpr>Ë   r�   r   )r‚   rx   )r   r	   s    `r   Ú_module_allowedz"AllowlistUnpickler._module_allowedÊ   s>   ø€ Ýð 
ð 
ð 
ð 
àÔ-ð
ñ 
ô 
ñ 
ô 
ð 	
r   r   c                óØ  •— t          ¦   «                              ||¦  «        }t          |t          j        ¦  «        rt          j        d|› d|› d�¦  «        ‚t          |dd ¦  «        }t          |d|¦  «        }||ft          vr8|r6|  	                    |¦  «        r!t          j        d|› d|› d|› d|› d�	¦  «        ‚||ft          v r!t          j        d|› d|› d|› d|› d	�	¦  «        ‚|S )
Nr   r   zL' resolves to a module object, which is not a reconstructable class/functionr   r   z' re-exports z from a denied modulez' (zK) is a denied callable and cannot be reconstructed from an untrusted pickle)r%   r   rt   ÚtypesÚ
ModuleTyper   r   ÚgetattrÚ_SAFE_DENIED_GLOBALSr„   Ú_DENIED_GLOBALS)r   r	   r   ÚobjÚtrue_moduleÚtrue_qualnamer)   s         €r   Ú_resolvezAllowlistUnpickler._resolveÐ   sv  ø€ õ ‰gŒg× Ò  ¨Ñ.Ô.ˆÝ�c�5Ô+Ñ,Ô,ð 	ÝÔ(ð7˜6ð 7ð 7 Dð 7ð 7ð 7ñô ð õ ˜c <°Ñ6Ô6ˆÝ  ^°TÑ:Ô:ˆð ˜Ð'Õ/CÐCÐCØð ˜t×2Ò2°;Ñ?Ô?ð ÝÔ,ð<˜vð <ð <¨ð <ð <¸;ð <ð <Ø$ð<ð <ð <ñô ð ð ˜Ð'­?Ð:Ð:ÝÔ(ð˜6ð ð  Dð ð ¨[ð ð ¸=ð ð ð ñô ð ð
 ˆ
r   c                óº  — d|v rt          j        d|› d|› d�¦  «        ‚|                     d¦  «        r0|                     d¦  «        rt          j        d|› d|› d�¦  «        ‚||ft          v rt          j        d|› d|› d�¦  «        ‚||f| j        v rQ|                      |¦  «        r&||ft          vrt          j        d|› d|› d�¦  «        ‚|                      ||¦  «        S |                      |¦  «        rt          j        d|› d|› d�¦  «        ‚|  	                    |¦  «        r|                      ||¦  «        S t          j        d|› d|› d	�¦  «        ‚)
Nr   r   zS' has a dotted name, which is forbidden (attribute-traversal pickle RCE, GHSA-4489)Ú__z'' has a dunder name, which is forbiddenzK' is a denied callable and cannot be reconstructed from an untrusted picklezN' is from a denied module and cannot be reconstructed from an untrusted picklez@' is from a denied module (defense-in-depth backstop, GHSA-x99w)z ' is not in the pickle allowlist)
r   r   r}   Úendswithr�   rv   r„   rŒ   r‘   r‡   r   s      r   r   zAllowlistUnpickler.find_classð   s  € ð �$ˆ;ˆ;ÝÔ(ð>˜6ð >ð > Dð >ð >ð >ñô ð ð �?Š?˜4Ñ Ô ð 	 T§]¢]°4Ñ%8Ô%8ð 	ÝÔ(ØQ˜6ÐQÐQ DÐQÐQÐQñô ð ð
 �Dˆ>�_Ð,Ð,ÝÔ(ð9˜6ð 9ð 9 Dð 9ð 9ð 9ñô ð ð �Dˆ>˜TÔ2Ð2Ð2à×#Ò# FÑ+Ô+ðà˜T�NÕ*>Ð>Ð>åÔ,ð@˜vð @ð @¨ð @ð @ð @ñô ð ð —=’= ¨Ñ.Ô.Ð.Ø×Ò˜vÑ&Ô&ð 	ÝÔ(ð9˜6ð 9ð 9 Dð 9ð 9ð 9ñô ð ð ×Ò Ñ'Ô'ð 	/Ø—=’= ¨Ñ.Ô.Ð.ÝÔ$ØF�vÐFÐF ÐFÐFÐFñ
ô 
ð 	
r   )r!   r   ro   rq   rp   rr   r#   r   )r	   r
   r   r7   r   )r   r   r   r   r&   Ústaticmethodr„   r‡   r‘   r   r4   r5   s   @r   rm   rm   ’   sÁ   ø€ € € € € ð ð  ðL 68Ø)+ð7ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð ð
ð 
ð 
ñ „\ð
ð
ð 
ð 
ð 
ðð ð ð ð ð ð@+
ð +
ð +
ð +
ð +
ð +
ð +
ð +
r   rm   >   ©rH   Úint©rH   ru   ©rH   r
   ©rH   r7   ©rH   Údict©rH   Úlist©rH   Úbytes©rH   Úfloat©rH   rw   ©rH   Úcomplex©rH   Ú	frozensetr   rn   ro   rq   rp   rr   c               óJ   — t          | ||¬¦  «                             ¦   «         S )a~  
    Load a pickle while only permitting an explicit allowlist of globals.

    See :class:`AllowlistUnpickler` for the meaning of ``allowed_globals`` and
    ``allowed_modules``. Prefer this over the warn-only :func:`pickle_load` when
    the set of legitimate classes in the file is known (e.g. a trained model),
    because :func:`pickle_load` still executes arbitrary code.
    rn   )rm   r2   )r!   ro   rp   s      r   Úallowlisted_pickle_loadr©   3  s+   € õ Ø˜o¸ðñ ô ç
‚d�f„fðr   )r!   r   r    r"   r6   r7   r   r   )r!   r   ro   rq   rp   rr   r   r   )r   Ú
__future__r   r   r‰   r/   Úcollections.abcr   Útypingr   r   r.   Ú	Unpicklerr   r   r9   rƒ   r§   r�   rm   rŒ   r©   r   r   r   ú<module>r®      s­  ððð ð #Ð "Ð "Ð "Ð "Ð "à €€€Ø €€€Ø €€€Ø $Ð $Ð $Ð $Ð $Ð $Ø  Ð  Ð  Ð  Ð  Ð  Ð  Ð  ð$ð ðOð Oð Oð Oð O˜&Ô*ñ Oô Oð Oðð ð ð ð �vÔ'ñ ô ð ð* .2Àeð:ð :ð :ð :ð :ð :ð*Ð ðN �)ðð ð ñô €ð&I
ð I
ð I
ð I
ð I
˜Ô)ñ I
ô I
ð I
ð` !�yðð ð ñô Ð ð( 24Ø%'ð	ð ð ð ð ð ð ð r   