Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SQL_Injection_Prevention_Cheat_Sheet.md #1228

Merged
merged 12 commits into from
Nov 22, 2023
Merged

Update SQL_Injection_Prevention_Cheat_Sheet.md #1228

merged 12 commits into from
Nov 22, 2023

Conversation

jmanico
Copy link
Member

@jmanico jmanico commented Nov 20, 2023

Major cleanup

Thank you for submitting a Pull Request (PR) to the Cheat Sheet Series.

馃毄 If your PR is related to grammar/typo mistakes, please double-check the file for other mistakes in order to fix all the issues in the current cheat sheet.

Please make sure that for your contribution:

  • In case of a new Cheat Sheet, you have used the Cheat Sheet template.
  • All the markdown files do not raise any validation policy violation, see the policy.
  • All the markdown files follow these format rules.
  • All your assets are stored in the assets folder.
  • All the images used are in the PNG format.
  • Any references to websites have been formatted as TEXT
  • You verified/tested the effectiveness of your contribution (e.g., the defensive code proposed is really an effective remediation? Please verify it works!).
  • The CI build of your PR pass, see the build status here.

If your PR is related to an issue, please finish your PR text with the following line:

This PR covers issue #.

Thank you again for your contribution 馃槂

@jmanico jmanico merged commit 12eb05e into master Nov 22, 2023
6 checks passed
@jmanico jmanico deleted the SQLi-cleanup branch November 22, 2023 14:31
@jmanico
Copy link
Member Author

jmanico commented Nov 22, 2023

Thank you to Robert Thornton for this work!


This article provides a set of simple techniques for preventing SQL Injection vulnerabilities by avoiding these two problems. These techniques can be used with practically any kind of programming language with any type of database. There are other types of databases, like XML databases, which can have similar problems (e.g., XPath and XQuery injection) and these techniques can be used to protect them as well.
Attackers can use SQL injection on an application if it has dynamic database queries that use string concatenation and user supplied input. To avoid SQL injection flaws, developers need to:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel that "What is a SQL Injection attack" should not be followed with - Attacker can use SQL injection . My proposal below

SQL injection is an attack in which malicious code is inserted into strings that are later passed to the database server. The malicious code is parsed and executed by the database server which could cause destruction or leakage of data.


**Primary Defenses:**

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defense Principles for SQL injection:

  1. Stop writing dynamic queries with string concatenation and
  2. Prevent malicious SQL input from being included in executed queries.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the SQL example below - #1201 you can see SQL injection is possible even without dynamic queries .
The wrapper libraries internally escape the inputs provided.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example in 1201 points to a dynamic query in a stored procedure if I am looking at the right code. I cant accept this PR as is, its a big change to a CS that we just made big updates on. Can you perhaps drop this PR and maybe give me several small PR's?


When we say "implemented safely," the stored procedure does not include any unsafe dynamic SQL generation. Developers do not usually generate dynamic SQL inside stored procedures. However, it can be done, but should be avoided.

If it can't be avoided, the stored procedure must use input validation or proper escaping as described in this article to make sure that all user supplied input to the stored procedure can't be used to inject SQL code into the dynamically generated query. Auditors should always look for uses of `sp_execute`, `execute` or `exec` within SQL Server stored procedures. Similar audit guidelines are necessary for similar functions for other vendors.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auditor / Reviewers

- Hibernate - use `createQuery()` with bind variables (called named parameters in Hibernate)
- SQLite - use `sqlite3_prepare()` to create a [statement object](http://www.sqlite.org/c3ref/stmt.html)
If database queries use this coding style, the database will always distinguish between code and data, regardless of what user input is supplied.
Also, prepared statements ensure that an attacker is not able to change the intent of a query, even if SQL commands are inserted by an attacker.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By escaping malicious user input provided

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameterized queries do not escape under the hood, its about the ordering of query plan construction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants