
    >/i                         S r SSKrSSKrSSKrSSKJr  SSKJr  SSKJr  \R                  " S\R                  \R                  -  5      rS rS r\R                  " S	5      \R                   " S
5      S 5       5       rg)ab  
============================
B608: Test for SQL injection
============================

An SQL injection attack consists of insertion or "injection" of a SQL query via
the input data given to an application. It is a very common attack vector. This
plugin test looks for strings that resemble SQL statements that are involved in
some form of string building operation. For example:

 - "SELECT %s FROM derp;" % var
 - "SELECT thing FROM " + tab
 - "SELECT " + val + " FROM " + tab + ...
 - "SELECT {} FROM derp;".format(var)
 - f"SELECT foo FROM bar WHERE id = {product}"

Unless care is taken to sanitize and control the input data when building such
SQL statement strings, an injection attack becomes possible. If strings of this
nature are discovered, a LOW confidence issue is reported. In order to boost
result confidence, this plugin test will also check to see if the discovered
string is in use with standard Python DBAPI calls `execute` or `executemany`.
If so, a MEDIUM issue is reported. For example:

 - cursor.execute("SELECT %s FROM derp;" % var)

Use of str.replace in the string construction can also be dangerous.
For example:

- "SELECT * FROM foo WHERE id = '[VALUE]'".replace("[VALUE]", identifier)

However, such cases are always reported with LOW confidence to compensate
for false positives, since valid uses of str.replace can be common.

:Example:

.. code-block:: none

    >> Issue: Possible SQL injection vector through string-based query
    construction.
       Severity: Medium   Confidence: Low
       CWE: CWE-89 (https://cwe.mitre.org/data/definitions/89.html)
       Location: ./examples/sql_statements.py:4
    3 query = "DELETE FROM foo WHERE id = '%s'" % identifier
    4 query = "UPDATE foo SET value = 'b' WHERE id = '%s'" % identifier
    5

.. seealso::

 - https://www.owasp.org/index.php/SQL_Injection
 - https://security.openstack.org/guidelines/dg_parameterize-database-queries.html
 - https://cwe.mitre.org/data/definitions/89.html

.. versionadded:: 0.9.0

.. versionchanged:: 1.7.3
    CWE information added

.. versionchanged:: 1.7.7
    Flag when str.replace is used in the string construction

    N)issue)test_properties)utilszL(select\s.*from\s|delete\s+from\s|insert\s+into\s.*values\s|update\s.*set\s)c                 0    [         R                  U 5      S L$ )N)SIMPLE_SQL_REsearch)datas    V/home/james-whalen/.local/lib/python3.13/site-packages/bandit/plugins/injection_sql.py_check_stringr   S   s    %T11    c                    S nSnSn[        U R                  [        R                  5      (       a6  [        R
                  " X R                  5      nUS   R                  nUS   nGO[        U R                  [        R                  5      (       ad  U R                  R                  S;   aJ  U R                  nU R                  R                  R                  nU R                  R                  S:X  a  SnGO [        [        S5      (       a  [        U R                  [        R                  5      (       a  U R                  R                   Vs/ s HG  n[        U[        R                  5      (       d  M$  [        UR                  [        5      (       d  ME  UPMI     nnU(       aS  XS   :X  aK  SR                  U Vs/ s H  n[        UR                  5      PM     sn5      nU R                  R                  n[        U[        R                  5      (       a   S	S
/n[        R                   " U5      nX;   X#4$ SX#4$ s  snf s  snf )N Fr      )formatreplacer   T	JoinedStrexecuteexecutemany)
isinstance_bandit_parentastBinOpr   concat_string	Attributeattrvaluehasattrr   valuesConstantstrjoinCallget_called_name)	nodewrapper	statementstr_replaceoutchild
substringsnamesnames	            r
   _evaluate_astr-   W   s   GIK$%%syy11!!$(;(;<a&''F		S]]
 




"
"&;
;JJ	%%44CC##y0K	k	"	"zS]]( (
 ,,33
3%. 3=ekk33O 3 	 
 $Q-/z JzeU[[!1z JKI))88G'388$$M*$$W-y66y..%
 !Ks   	#H60H6H67H;StrB608c                 &   [        U R                  5      u  pn[        U5      (       ai  [        R                  " [        R
                  U(       a  U(       d  [        R
                  O[        R                  [        R                  R                  SS9$ g )NzFPossible SQL injection vector through string-based query construction.)severity
confidencecwetext)
r-   r$   r   banditIssueMEDIUMLOWr   CweSQL_INJECTION)contextexecute_callr&   r'   s       r
   hardcoded_sql_expressionsr=      sh     ,9+F(L[Y||]]   ZZ		''"

 
	
  r   )__doc__r   rer5   bandit.corer   r   testr   compile
IGNORECASEDOTALLr   r   r-   checkstest_idr=    r   r
   <module>rH      s}   
<z  	   / 

 MMBII2&/R Uf
  
r   