MagicStack / MagicStack/asyncpg

Can't add schema name to query in fetch function

Open
#538 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
8.1k
Forks
468
PR merge metrics
No merged PRs in 30d

Description

* **asyncpg version**: 0.18.3
* **PostgreSQL version**: 11.4 +
* **Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
the issue with a local PostgreSQL install?**: No
* **Python version**: Python 3.7.3
* **Platform**: MacOS, Sanic
* **Do you use pgbouncer?**: No
* **Did you install asyncpg with pip?**: Yes
* **If you built asyncpg locally, which version of Cython did you use?**: No
* **Can the issue be reproduced under both asyncio and
[uvloop](https://github.com/magicstack/uvloop)?**: No

I had try to up to 0.20.1 but I have an other error in your code, so I only test on version 0.18.3

I got a problem when I try to add schema name to the query. My code:
```python3
dbpool = app.config["pool"]
async with dbpool.acquire() as conn:
sql = """
SELECT _id, name, address
FROM "{schema}".account
WHERE _id IN (
SELECT account_id
FROM "{schema}"."account-message"
WHERE message_id IN (
SELECT _id
FROM "{schema}".message
WHERE sender = $1
)
);
""".format(
schema=config.SCHEMA
)
resp = await conn.fetch(sql, address)
```
and it work very well, except for the `lint:security` give me a security issue:

> Issue: [B608:hardcoded_sql_expressions] Possible SQL injection vector through string-based query construction.

And when I try to pass my schema name to `conn.fetch()` function but it didn't work. Can you help me?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the conn.fetch() call and the PostgreSQL query shown in the issue, then check how schema names are passed to queries and how parameter binding treats identifiers. Done means determining whether a safe schema-name approach is supported and documenting or addressing the reported lint/security concern.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.