Files
insights/.semgrep/rules/security/datastore-sql-injection.py
zeekay 1b367e2b62 chore: rebase the tree onto upstream master
The tree was a debranded snapshot of upstream taken around 2026-02-20, and
upstream has moved about five months since. This replaces it with upstream
master, mechanically re-derived: bin/debrand does the rename, and the
pipeline and squashed migrations are restored from the shipped tree.

Not a merge. The previous import replaced the tree wholesale in a single
commit, so there is no shared content history to merge against. Reproducing
the rename on the new tree is what actually converges.

What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints,
compose files, LLM.md, and insights/migrations. The squashed initial
migration is load-bearing — the live database records it by name, so
upstream's ~918 individual migrations cannot come back.

Nine products are dropped as unreferenced. Twenty-one more that we do not run
are kept anyway because the core app still imports them; unpicking those is a
refactor and does not belong in a rebase.

This base does not build yet, and the reasons are not in the rename:
upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against
our 4.2, and the dependency manifests still name upstream packages that we
republish under our own names. Those are decisions, not renames.
2026-08-01 15:44:40 -07:00

150 lines
4.7 KiB
Python

# Test cases for datastore-sql-injection rule
# ============================================
# VULNERABLE CASES - should match both rules (data is a function param AND data.get is a source)
# ============================================
def vuln_data_get(data):
column = data.get("incremental_field")
# ruleid: datastore-sql-injection, datastore-fstring-param-audit
sync_execute(f"SELECT max(`{column}`) FROM table")
def vuln_data_bracket(data):
column = data["field"]
# ruleid: datastore-sql-injection, datastore-fstring-param-audit
sync_execute(f"SELECT * FROM t WHERE col = '{column}'")
def vuln_validated_data(validated_data):
field = validated_data.get("column_name")
# ruleid: datastore-sql-injection, datastore-fstring-param-audit
sync_execute(f"SELECT {field} FROM table")
def vuln_request_data(request):
field = request.data.get("field_name")
# ruleid: datastore-sql-injection, datastore-fstring-param-audit
sync_execute(f"SELECT * FROM t WHERE col = '{field}'")
def vuln_request_data_bracket(request):
field = request.data["field"]
# ruleid: datastore-sql-injection, datastore-fstring-param-audit
async_execute(f"SELECT * FROM t WHERE col = '{field}'")
def vuln_request_get(request):
table = request.GET.get("table")
# ruleid: datastore-sql-injection, datastore-fstring-param-audit
sync_execute(f"SELECT * FROM {table}")
def vuln_request_post(request):
col = request.POST["column"]
# ruleid: datastore-sql-injection, datastore-fstring-param-audit
sync_execute(f"SELECT {col} FROM t")
def vuln_query_params(request):
field = request.query_params.get("field")
# ruleid: datastore-sql-injection, datastore-fstring-param-audit
sync_execute(f"SELECT {field} FROM table")
def vuln_self_request(self):
field = self.request.data.get("field")
# ok: datastore-fstring-param-audit
# ruleid: datastore-sql-injection
sync_execute(f"SELECT {field} FROM t")
def vuln_self_request_query_params(self):
col = self.request.query_params["col"]
# ok: datastore-fstring-param-audit
# ruleid: datastore-sql-injection
execute_on_connection(f"SELECT {col} FROM t")
# ============================================
# SAFE CASES - should NOT match
# ============================================
def safe_with_escape_identifier(data):
column = escape_datastore_identifier(data.get("field"))
# ok: datastore-sql-injection, datastore-fstring-param-audit
sync_execute(f"SELECT max(`{column}`) FROM table")
def safe_with_escape_string(data):
value = escape_datastore_string(data.get("value"))
# ok: datastore-sql-injection, datastore-fstring-param-audit
sync_execute(f"SELECT * FROM t WHERE col = {value}")
def safe_hardcoded_constant():
TABLE_NAME = "events"
# ok: datastore-sql-injection, datastore-fstring-param-audit
sync_execute(f"SELECT * FROM {TABLE_NAME}")
def safe_parameterized(data):
# ok: datastore-sql-injection, datastore-fstring-param-audit
sync_execute("SELECT * FROM t WHERE col = %(val)s", args={"val": data.get("field")})
def safe_no_fstring(data):
field = data.get("field")
# ok: datastore-sql-injection, datastore-fstring-param-audit
sync_execute("SELECT * FROM t WHERE col = %(f)s", args={"f": field})
def safe_internal_variable():
table_name = "internal_table"
# ok: datastore-sql-injection, datastore-fstring-param-audit
sync_execute(f"SELECT COUNT(*) FROM {table_name}")
def safe_loop_index():
for i in range(10):
# ok: datastore-sql-injection, datastore-fstring-param-audit
sync_execute(f"SELECT * FROM t LIMIT {i}")
# ============================================
# FUNCTION PARAMETER CASES - only datastore-fstring-param-audit should match
# ============================================
def vuln_param_str_typed(self, column: str):
# ok: datastore-sql-injection
# ruleid: datastore-fstring-param-audit
result = sync_execute(f"SELECT max(`{column}`) FROM table")
return result
def vuln_param_untyped(self, column):
# ok: datastore-sql-injection
# ruleid: datastore-fstring-param-audit
result = sync_execute(f"SELECT {column} FROM table")
return result
def vuln_param_async(field: str):
# ok: datastore-sql-injection
# ruleid: datastore-fstring-param-audit
async_execute(f"SELECT * FROM t WHERE col = '{field}'")
def safe_param_escaped(self, column: str):
safe_col = escape_datastore_identifier(column)
# ok: datastore-sql-injection, datastore-fstring-param-audit
sync_execute(f"SELECT max(`{safe_col}`) FROM table")
def safe_param_not_in_fstring(self, column: str):
# ok: datastore-sql-injection, datastore-fstring-param-audit
sync_execute("SELECT * FROM t WHERE col = %(c)s", args={"c": column})