Our ~35 own rules left whole languages without SAST findings. Three permissively licensed community packs now run next to them, on the same semgrep invocation (--metrics=off, local files only): - GitLab sast-rules v2.10.1 (53bf5cf6): 148 rules for C#, Java, JavaScript/TypeScript, Python and one Go rule; only files whose header says "License: MIT (c) GitLab Inc.". Not vendored: c/ (every file GPL-2.0, generated from flawfinder), go/ apart from one file (Apache-2.0 gosec ports), scala/, rules/lgpl, rules/lgpl-cc (Commons Clause), rules/gitlab (EE). - 0xdea semgrep-rules (dae50da6, MIT): 35 C/C++ rules. - elttam semgrep-rules (24426856, MIT; its LICENSE carries the Semgrep template's copyright line): 34 rules for Java, Go and Struts/Spring XML. Each pack lives in compliance-agent/rules/vendor/<source>/ with LICENSE, NOTICE (provenance, what was left out and why) and UPSTREAM (pinned commit): upstream/ holds the selected files unchanged with their tests, pack.yaml the rules the agent runs, ids prefixed per source (gitlab-, 0xdea-, elttam-), rules.json an index for the tests. scripts/vendor-semgrep-rules.py regenerates all of it deterministically. Provenance: every candidate rule was compared with the Semgrep registry (semgrep/semgrep-rules, read only for the comparison). Eight GitLab Python/JS rules with the same message or the same patterns are not vendored, nor are elttam's Kubernetes rules (registry-derived). Also left out: elttam's entrypoint enumerators, its ORM-leak hints, three files whose upstream tests fail on semgrep 1.177 and a join-mode rule. Noise budget: run over the nightly fixtures, the public samples and FreeRTOS-Kernel, lwIP, OpenRA, eShopOnWeb, requests, express and spring-petclinic. 15 rules that flood real code (0xdea missing-return 1514 hits, typos 989, interesting-api-calls 718, signed-unsigned-conversion 221, suspicious-assert 176, format-string-bugs 61 ...; GitLab assert-used) are dropped from the pack, each listed with its reason in NOTICE. CPU time of the semgrep run roughly doubles on C-heavy trees (FreeRTOS-Kernel 70 s -> 136 s, lwIP 18 s -> 68 s), well inside the 10-minute stage timeout. Ids stay bare (#473 logic covers the vendored ids, fingerprint() unchanged). The control map gets one vendored semgrep binding per control, by CWE (by rule id for the few elttam rules without one), with exactly the languages of the rules behind it; a test checks every vendored rule maps to a control that claims its languages. Guard: semgrep_args() builds the one invocation; a test fails if any --config is not a staged local pack, or is auto, p/, r/ or a URL, and another scans the agent source, scripts, docker/, CI and Dockerfile.agent for a registry config. Nightly baselines with C, Python, C# or JS code change and need a bless run after the image is deployed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB
This commit was merged in pull request #504.
This commit is contained in:
1 parent
9bf7aad5b1
commit
d406741907
473 files changed
+50690
-116
No files matched your search
+131
@@ -0,0 +1,131 @@
|
||||
# License: Apache 2.0 (c) PyCQA
|
||||
# source: https://github.com/PyCQA/bandit/blob/master/examples/sql_statements.py
|
||||
# hash: 8eee173
|
||||
|
||||
import sqlalchemy
|
||||
|
||||
# ruleid: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute("SELECT * FROM foo WHERE id = '%s'" % identifier)
|
||||
# ruleid: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute("INSERT INTO foo VALUES ('a', 'b', '%s')" % value)
|
||||
# ruleid: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute("DELETE FROM foo WHERE id = '%s'" % identifier)
|
||||
# ruleid: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute("UPDATE foo SET value = 'b' WHERE id = '%s'" % identifier)
|
||||
# ruleid: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute("SELECT * FROM foo WHERE id = '" + identifier + "'")
|
||||
# ruleid: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute("SELECT * FROM foo WHERE id = '{}'".format(identifier))
|
||||
|
||||
# ok: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute("SELECT * FROM foo WHERE id = '%s'", identifier)
|
||||
# ok: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute("INSERT INTO foo VALUES ('a', 'b', '%s')", value)
|
||||
# ok: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute("DELETE FROM foo WHERE id = '%s'", identifier)
|
||||
# ok: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute("UPDATE foo SET value = 'b' WHERE id = '%s'", identifier)
|
||||
|
||||
# real world false positives
|
||||
choices=[('server_list', _("Select from active instances"))]
|
||||
print("delete from the cache as the first argument")
|
||||
|
||||
def select_note_by_id(conn, id=None):
|
||||
query = "SELECT * FROM notes"
|
||||
if id:
|
||||
query = query + " WHERE id = '%s'" % id
|
||||
cur = conn.cursor()
|
||||
# ruleid: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute(query)
|
||||
rows = cur.fetchall()
|
||||
return rows
|
||||
|
||||
# ok: python_sql_rule-hardcoded-sql-expression
|
||||
foo.execute("unrelated to select and sql" % identifier)
|
||||
|
||||
# ok: python_sql_rule-hardcoded-sql-expression
|
||||
foo.execute("updated" % identifier)
|
||||
|
||||
# ok: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute('SELECT * FROM dual')
|
||||
|
||||
# ok: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute('CREATE TABLE %s' % tableName)
|
||||
|
||||
ctStm = 'CREATE TABLE '
|
||||
ctStm = ctStm + ' %s' % tableName
|
||||
# ok: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute(ctStm)
|
||||
|
||||
upStm = 'UPDATE foo WHERE user = %s' % 'admin'
|
||||
# ok: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute(upStm)
|
||||
|
||||
upStm2 = f'UPDATE foo WHERE user = "admin"'
|
||||
# ok: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute(upStm2)
|
||||
|
||||
# ok: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute(f'UPDATE foo WHERE user = "admin"')
|
||||
|
||||
|
||||
s1 = "DELETE FROM tableX WHERE colY <= "
|
||||
s1 += str(response_data['delete_record'])
|
||||
# ruleid: python_sql_rule-hardcoded-sql-expression
|
||||
db.execute(s1)
|
||||
|
||||
s2 = "DELETE FROM tableX WHERE colY <= " + str(response_data['delete_record'])
|
||||
# ruleid: python_sql_rule-hardcoded-sql-expression
|
||||
db.execute(s2)
|
||||
|
||||
s3 = "DELETE FROM tableX WHERE id <= %s" % "something constant"
|
||||
# ok: python_sql_rule-hardcoded-sql-expression
|
||||
db.execute(s3)
|
||||
|
||||
s4 = "DELETE FROM tableX WHERE colY <= "
|
||||
s4 += "delete_record"
|
||||
# ok: python_sql_rule-hardcoded-sql-expression
|
||||
db.execute(s4)
|
||||
|
||||
con = sqlite3.connect(filepath)
|
||||
cur = con.cursor()
|
||||
foo = getSomeVar()
|
||||
bar = getAnotherVar()
|
||||
bazConst = "this is a literal"
|
||||
# ruleid: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute("DELETE FROM tableX WHERE colY <= " + str(response_data['delete_record']))
|
||||
# ruleid: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute("DELETE FROM tableX WHERE id = '" + bar + "'")
|
||||
# ruleid: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute("DELETE FROM tableX WHERE id = '" + bar + "'" + " AND somethingElse <= " + foo)
|
||||
# ruleid: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute("DELETE FROM tableX WHERE id = '" + bar + "'" + " AND somethingElse = '" + foo + "'")
|
||||
# ok: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute("DELETE FROM tableX WHERE id <= " + "something constant")
|
||||
# ruleid: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute("DELETE FROM tableX WHERE id <= %s" % bar)
|
||||
# ok: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute("DELETE FROM tableX WHERE id <= %s" % "something constant")
|
||||
# ok: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute("DELETE FROM tableX WHERE id <= %s" % bazConst)
|
||||
|
||||
# This is a false positive result since 3 is a constant.
|
||||
# ruleid: python_sql_rule-hardcoded-sql-expression
|
||||
cur.execute("DELETE FROM tableX WHERE id <= " + str(3))
|
||||
|
||||
|
||||
# Get user input for login
|
||||
username = input("Enter your username: ")
|
||||
password = input("Enter your password: ")
|
||||
|
||||
# Vulnerable Query: Directly embedding user input into the SQL query
|
||||
query = f"SELECT * FROM users WHERE username = '{username}' AND password = '{password}'"
|
||||
print(f"Executing query: {query}") # Debugging purpose
|
||||
# ruleid: python_sql_rule-hardcoded-sql-expression
|
||||
cursor.execute(query)
|
||||
|
||||
# ruleid: python_sql_rule-hardcoded-sql-expression
|
||||
cursor.execute(f"SELECT * FROM users WHERE username = '{username}' AND password = '{password}'")
|
||||
|
||||
# ok: python_sql_rule-hardcoded-sql-expression
|
||||
cursor.execute(f"SELECT * FROM users WHERE username = 'abc'")
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
# yamllint disable
|
||||
# License: MIT (c) GitLab Inc.
|
||||
# yamllint enable
|
||||
---
|
||||
rules:
|
||||
- id: "python_sql_rule-hardcoded-sql-expression"
|
||||
languages:
|
||||
- "python"
|
||||
message: |
|
||||
SQL Injection is a critical vulnerability that can lead to data or system compromise. By
|
||||
dynamically generating SQL query strings, user input may be able to influence the logic of
|
||||
the SQL statement. This could lead to an adversary accessing information they should
|
||||
not have access to, or in some circumstances, being able to execute OS functionality or code.
|
||||
|
||||
Replace all dynamically generated SQL queries with parameterized queries. In situations where
|
||||
dynamic queries must be created, never use direct user input, but instead use a map or
|
||||
dictionary of valid values and resolve them using a user supplied key.
|
||||
|
||||
For example, some database drivers do not allow parameterized queries for `>` or `<` comparison
|
||||
operators. In these cases, do not use a user supplied `>` or `<` value, but rather have the
|
||||
user
|
||||
supply a `gt` or `lt` value. The alphabetical values are then used to look up the `>` and `<`
|
||||
values to be used in the construction of the dynamic query. The same goes for other queries
|
||||
where
|
||||
column or table names are required but cannot be parameterized.
|
||||
|
||||
Example using `PreparedStatement` queries:
|
||||
```
|
||||
import sqlite3
|
||||
|
||||
# Create a new database (in memory)
|
||||
con = sqlite3.connect(":memory:")
|
||||
# Get a cursor from the connection
|
||||
cur = con.cursor()
|
||||
# Create a tuple of the value to be used in the parameterized query
|
||||
params = ('user-input',)
|
||||
# execute the statement, passing in the params for the value
|
||||
cur.execute("select name from sqlite_master where name = ?", params)
|
||||
# work with the result
|
||||
result = cur.fetchall()
|
||||
```
|
||||
|
||||
For more information on SQL Injection see OWASP:
|
||||
https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html
|
||||
metadata:
|
||||
cwe: "CWE-89"
|
||||
owasp:
|
||||
- "A1:2017-Injection"
|
||||
- "A03:2021-Injection"
|
||||
- "A05:2025-Injection"
|
||||
category: "security"
|
||||
shortDescription: "Improper neutralization of special elements used in an SQL
|
||||
Command ('SQL Injection')"
|
||||
security-severity: "High"
|
||||
mode: taint
|
||||
pattern-sources:
|
||||
- patterns:
|
||||
- pattern-either:
|
||||
- pattern: |
|
||||
"$QUERY" % ...
|
||||
- pattern: |
|
||||
"$QUERY".format(...)
|
||||
- pattern: |
|
||||
f"$QUERY{...}..."
|
||||
- pattern: |
|
||||
"$QUERY" + ...
|
||||
- pattern: |
|
||||
$TMP = "$QUERY"
|
||||
...
|
||||
$SQL = $TMP + "..." % ...
|
||||
- pattern: |
|
||||
$SQL = "$QUERY"
|
||||
...
|
||||
$SQL += ...
|
||||
- metavariable-regex:
|
||||
metavariable: $QUERY
|
||||
regex: (?i)^(SELECT|INSERT|UPDATE|DELETE)\s
|
||||
pattern-sinks:
|
||||
- patterns:
|
||||
- pattern: $DB.execute(...)
|
||||
- pattern-not:
|
||||
pattern-either:
|
||||
- pattern: $DB.execute("...")
|
||||
- pattern: $DB.execute("$QUERY" % "...")
|
||||
- pattern: $DB.execute("$QUERY" + "...")
|
||||
severity: "WARNING"
|
||||
Reference in new issue
Block a user