-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[flake8-bandit
]: Implement S610
rule
#10316
Merged
charliermarsh
merged 2 commits into
astral-sh:main
from
mkniewallner:feat/add-flake8-bandit-S610
Mar 12, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
crates/ruff_linter/resources/test/fixtures/flake8_bandit/S610.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from django.contrib.auth.models import User | ||
|
||
# Errors | ||
User.objects.filter(username='admin').extra(dict(could_be='insecure')) | ||
User.objects.filter(username='admin').extra(select=dict(could_be='insecure')) | ||
User.objects.filter(username='admin').extra(select={'test': '%secure' % 'nos'}) | ||
User.objects.filter(username='admin').extra(select={'test': '{}secure'.format('nos')}) | ||
User.objects.filter(username='admin').extra(where=['%secure' % 'nos']) | ||
User.objects.filter(username='admin').extra(where=['{}secure'.format('no')]) | ||
|
||
query = '"username") AS "username", * FROM "auth_user" WHERE 1=1 OR "username"=? --' | ||
User.objects.filter(username='admin').extra(select={'test': query}) | ||
|
||
where_var = ['1=1) OR 1=1 AND (1=1'] | ||
User.objects.filter(username='admin').extra(where=where_var) | ||
|
||
where_str = '1=1) OR 1=1 AND (1=1' | ||
User.objects.filter(username='admin').extra(where=[where_str]) | ||
|
||
tables_var = ['django_content_type" WHERE "auth_user"."username"="admin'] | ||
User.objects.all().extra(tables=tables_var).distinct() | ||
|
||
tables_str = 'django_content_type" WHERE "auth_user"."username"="admin' | ||
User.objects.all().extra(tables=[tables_str]).distinct() | ||
|
||
# OK | ||
User.objects.filter(username='admin').extra( | ||
select={'test': 'secure'}, | ||
where=['secure'], | ||
tables=['secure'] | ||
) | ||
User.objects.filter(username='admin').extra({'test': 'secure'}) | ||
User.objects.filter(username='admin').extra(select={'test': 'secure'}) | ||
User.objects.filter(username='admin').extra(where=['secure']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
crates/ruff_linter/src/rules/flake8_bandit/rules/django_extra.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::{self as ast, Expr, ExprAttribute, ExprDict, ExprList}; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for uses of Django's `extra` function. | ||
/// | ||
/// ## Why is this bad? | ||
/// Django's `extra` function can be used to execute arbitrary SQL queries, | ||
/// which can in turn lead to SQL injection vulnerabilities. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// from django.contrib.auth.models import User | ||
/// | ||
/// User.objects.all().extra(select={"test": "%secure" % "nos"}) | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Django documentation: SQL injection protection](https://docs.djangoproject.com/en/dev/topics/security/#sql-injection-protection) | ||
/// - [Common Weakness Enumeration: CWE-89](https://cwe.mitre.org/data/definitions/89.html) | ||
#[violation] | ||
pub struct DjangoExtra; | ||
|
||
impl Violation for DjangoExtra { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Use of Django `extra` can lead to SQL injection vulnerabilities") | ||
} | ||
} | ||
|
||
/// S610 | ||
pub(crate) fn django_extra(checker: &mut Checker, call: &ast::ExprCall) { | ||
let Expr::Attribute(ExprAttribute { attr, .. }) = call.func.as_ref() else { | ||
return; | ||
}; | ||
|
||
if attr.as_str() != "extra" { | ||
return; | ||
} | ||
|
||
if is_call_insecure(call) { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(DjangoExtra, call.arguments.range())); | ||
} | ||
} | ||
|
||
fn is_call_insecure(call: &ast::ExprCall) -> bool { | ||
for (argument_name, position) in [("select", 0), ("where", 1), ("tables", 3)] { | ||
if let Some(argument) = call.arguments.find_argument(argument_name, position) { | ||
match argument_name { | ||
"select" => match argument { | ||
Expr::Dict(ExprDict { keys, values, .. }) => { | ||
if !keys.iter().flatten().all(Expr::is_string_literal_expr) { | ||
return true; | ||
} | ||
if !values.iter().all(Expr::is_string_literal_expr) { | ||
return true; | ||
} | ||
} | ||
_ => return true, | ||
}, | ||
"where" | "tables" => match argument { | ||
Expr::List(ExprList { elts, .. }) => { | ||
if !elts.iter().all(Expr::is_string_literal_expr) { | ||
return true; | ||
} | ||
} | ||
_ => return true, | ||
}, | ||
_ => (), | ||
} | ||
} | ||
} | ||
|
||
false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...rules/flake8_bandit/snapshots/ruff_linter__rules__flake8_bandit__tests__S610_S610.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs | ||
--- | ||
S610.py:4:44: S610 Use of Django `extra` can lead to SQL injection vulnerabilities | ||
| | ||
3 | # Errors | ||
4 | User.objects.filter(username='admin').extra(dict(could_be='insecure')) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ S610 | ||
5 | User.objects.filter(username='admin').extra(select=dict(could_be='insecure')) | ||
6 | User.objects.filter(username='admin').extra(select={'test': '%secure' % 'nos'}) | ||
| | ||
|
||
S610.py:5:44: S610 Use of Django `extra` can lead to SQL injection vulnerabilities | ||
| | ||
3 | # Errors | ||
4 | User.objects.filter(username='admin').extra(dict(could_be='insecure')) | ||
5 | User.objects.filter(username='admin').extra(select=dict(could_be='insecure')) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ S610 | ||
6 | User.objects.filter(username='admin').extra(select={'test': '%secure' % 'nos'}) | ||
7 | User.objects.filter(username='admin').extra(select={'test': '{}secure'.format('nos')}) | ||
| | ||
|
||
S610.py:6:44: S610 Use of Django `extra` can lead to SQL injection vulnerabilities | ||
| | ||
4 | User.objects.filter(username='admin').extra(dict(could_be='insecure')) | ||
5 | User.objects.filter(username='admin').extra(select=dict(could_be='insecure')) | ||
6 | User.objects.filter(username='admin').extra(select={'test': '%secure' % 'nos'}) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ S610 | ||
7 | User.objects.filter(username='admin').extra(select={'test': '{}secure'.format('nos')}) | ||
8 | User.objects.filter(username='admin').extra(where=['%secure' % 'nos']) | ||
| | ||
|
||
S610.py:7:44: S610 Use of Django `extra` can lead to SQL injection vulnerabilities | ||
| | ||
5 | User.objects.filter(username='admin').extra(select=dict(could_be='insecure')) | ||
6 | User.objects.filter(username='admin').extra(select={'test': '%secure' % 'nos'}) | ||
7 | User.objects.filter(username='admin').extra(select={'test': '{}secure'.format('nos')}) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ S610 | ||
8 | User.objects.filter(username='admin').extra(where=['%secure' % 'nos']) | ||
9 | User.objects.filter(username='admin').extra(where=['{}secure'.format('no')]) | ||
| | ||
|
||
S610.py:8:44: S610 Use of Django `extra` can lead to SQL injection vulnerabilities | ||
| | ||
6 | User.objects.filter(username='admin').extra(select={'test': '%secure' % 'nos'}) | ||
7 | User.objects.filter(username='admin').extra(select={'test': '{}secure'.format('nos')}) | ||
8 | User.objects.filter(username='admin').extra(where=['%secure' % 'nos']) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ S610 | ||
9 | User.objects.filter(username='admin').extra(where=['{}secure'.format('no')]) | ||
| | ||
|
||
S610.py:9:44: S610 Use of Django `extra` can lead to SQL injection vulnerabilities | ||
| | ||
7 | User.objects.filter(username='admin').extra(select={'test': '{}secure'.format('nos')}) | ||
8 | User.objects.filter(username='admin').extra(where=['%secure' % 'nos']) | ||
9 | User.objects.filter(username='admin').extra(where=['{}secure'.format('no')]) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ S610 | ||
10 | | ||
11 | query = '"username") AS "username", * FROM "auth_user" WHERE 1=1 OR "username"=? --' | ||
| | ||
|
||
S610.py:12:44: S610 Use of Django `extra` can lead to SQL injection vulnerabilities | ||
| | ||
11 | query = '"username") AS "username", * FROM "auth_user" WHERE 1=1 OR "username"=? --' | ||
12 | User.objects.filter(username='admin').extra(select={'test': query}) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ S610 | ||
13 | | ||
14 | where_var = ['1=1) OR 1=1 AND (1=1'] | ||
| | ||
|
||
S610.py:15:44: S610 Use of Django `extra` can lead to SQL injection vulnerabilities | ||
| | ||
14 | where_var = ['1=1) OR 1=1 AND (1=1'] | ||
15 | User.objects.filter(username='admin').extra(where=where_var) | ||
| ^^^^^^^^^^^^^^^^^ S610 | ||
16 | | ||
17 | where_str = '1=1) OR 1=1 AND (1=1' | ||
| | ||
|
||
S610.py:18:44: S610 Use of Django `extra` can lead to SQL injection vulnerabilities | ||
| | ||
17 | where_str = '1=1) OR 1=1 AND (1=1' | ||
18 | User.objects.filter(username='admin').extra(where=[where_str]) | ||
| ^^^^^^^^^^^^^^^^^^^ S610 | ||
19 | | ||
20 | tables_var = ['django_content_type" WHERE "auth_user"."username"="admin'] | ||
| | ||
|
||
S610.py:21:25: S610 Use of Django `extra` can lead to SQL injection vulnerabilities | ||
| | ||
20 | tables_var = ['django_content_type" WHERE "auth_user"."username"="admin'] | ||
21 | User.objects.all().extra(tables=tables_var).distinct() | ||
| ^^^^^^^^^^^^^^^^^^^ S610 | ||
22 | | ||
23 | tables_str = 'django_content_type" WHERE "auth_user"."username"="admin' | ||
| | ||
|
||
S610.py:24:25: S610 Use of Django `extra` can lead to SQL injection vulnerabilities | ||
| | ||
23 | tables_str = 'django_content_type" WHERE "auth_user"."username"="admin' | ||
24 | User.objects.all().extra(tables=[tables_str]).distinct() | ||
| ^^^^^^^^^^^^^^^^^^^^^ S610 | ||
25 | | ||
26 | # OK | ||
| |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
flatten()
is used to remove optional keys here. Not sure that this is really a clean way to do this, let me know if not.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to flag on
select(**kwargs)
? It seems like this wouldn't, but it probably should, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, I misread.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to consider allowing:
select(**{"foo": "bar"})