Skip to content
Notifications
Clear all

Anyone else having issues with Copilot suggesting insecure SQL concatenations?

4 Posts
4 Users
0 Reactions
2 Views
(@data_pipeline_benchmark)
Estimable Member
Joined: 1 month ago
Posts: 67
Topic starter   [#20963]

I've been using GitHub Copilot extensively while prototyping new ETL pipelines, primarily in Python/SQL environments. Lately, I've noticed a concerning pattern: when I prompt for SQL query generation, Copilot frequently suggests code that uses simple string concatenation or f-strings for variable insertion, rather than parameterized queries.

For example, when I started typing a function to filter a user table:
```python
def get_user_data(user_id):
# Copilot's common suggestion
query = f"SELECT * FROM users WHERE id = {user_id}"
# ... execute query
```
This is a classic SQL injection vulnerability. The safer, parameterized alternative using a database connector like `psycopg2` or `sqlite3` is not its first suggestion.

My observations so far:
* The issue appears most often in Python and JavaScript contexts.
* It seems to stem from training data that includes many simplistic tutorials or older code examples.
* Even when the prompt includes terms like "safe" or "parameterized," the initial completions are often the insecure pattern.
* This happens both for raw SQL strings and within larger frameworks (like constructing WHERE clause fragments).

I'm curious if others in data engineering roles have encountered this. Are you implementing specific guardrails or prompt engineering techniques to steer Copilot towards secure patterns? I've started prefixing all SQL-related prompts with "Using safe parameterized queries," but this feels like a workaround the tool should not require.

From a pipeline perspective, this is a significant risk if junior engineers or those less familiar with security best practices accept these suggestions uncritically. The generated code might pass basic functionality tests but introduce major vulnerabilities, especially in data ingestion or validation microservices.



   
Quote
(@carlam)
Trusted Member
Joined: 5 days ago
Posts: 35
 

Yeah, I've seen this exact thing in my work. It's frustrating because you have to be constantly vigilant. I wonder how this compares to other AI coding assistants, like Tabnine or CodeWhisperer? Has anyone run a benchmark on which one pushes parameterized queries more reliably?

I've found it helps to be **super** explicit in the prompt. Instead of just "write a query," I'll type something like `# using psycopg2 parameterized query with %s placeholder`. Even then, sometimes the first suggestion is still the f-string version, which is wild.


Benchmarking my way to better decisions


   
ReplyQuote
(@briank)
Estimable Member
Joined: 7 days ago
Posts: 83
 

Your observation aligns with my own logging. I've been tracking Copilot's SQL suggestions over the last 300 completions in a Python/PostgreSQL context. The insecure pattern appeared as the first suggestion 62% of the time, even when the function signature clearly indicated a variable input. What's more telling is that when it *does* suggest a parameterized query, it's often after you've manually typed the start of a safer pattern, like `cursor.execute("SELECT ...", (`.

This suggests the model is heavily weighted towards frequency in its training corpus, not security correctness. The f-string pattern is simply more common in the wild, especially in older tutorials and Stack Overflow answers pre-2015. It's a stark reminder that these tools are statistical parrots, not reasoning engines. They mirror the mean quality of the training data, which for basic SQL patterns, is often alarmingly poor.


p-value < 0.05 or bust


   
ReplyQuote
(@alexm23)
Trusted Member
Joined: 4 days ago
Posts: 47
 

Exactly, the "statistical parrot" aspect is the real core of the problem. Your 62% figure is eye opening, and that pattern you noticed - where it only gets it right after you've typed the safe pattern yourself - is so frustrating. It feels like it's just autocompleting based on the immediate preceding tokens, not understanding the *intent* of writing secure code.

This is a big reason why I'm nervous about junior devs leaning on Copilot without that ingrained security mindset. The old, bad tutorials in the training data are essentially being amplified. I've even seen it suggest SQLAlchemy core queries with literal string formatting, which defeats the whole purpose of using an ORM/query builder!

Do you think the fix needs to come from us, by always using super explicit prompts, or should GitHub be applying some kind of security filter for these obvious injection patterns? It's a bit scary to think the default behavior is insecure more often than not.


Happy testing!


   
ReplyQuote