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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SQL Injection Web security documentation to the best practices. #37806

Merged
merged 4 commits into from
Jan 27, 2025

Conversation

younisdev
Copy link
Contributor

Description

Updated SQL Injection Web security documentation to the best practices.

Motivation

These changes ensure that developers are aware of the standard and best practices related to sql injection.

Additional details

None

Related issues and pull requests

Fixes #37783

@younisdev younisdev requested a review from a team as a code owner January 25, 2025 22:18
@younisdev younisdev requested review from hamishwillee and removed request for a team January 25, 2025 22:18
@github-actions github-actions bot added the size/s [PR only] 6-50 LoC changed label Jan 25, 2025
@@ -81,18 +81,21 @@ SELECT * FROM users WHERE name = 'a';DROP TABLE users; SELECT * FROM userinfo WH

The modified statement creates a valid SQL statement that deletes the `users` table and selects all data from the `userinfo` table (which reveals the information of every user). This works because the first part of the injected text (`a';`) completes the original statement.

To avoid this sort of attack, you must ensure that any user data that is passed to an SQL query cannot change the nature of the query. One way to do this is to [escape](https://en.wikipedia.org/wiki/Escape_character) all the characters in the user input that have a special meaning in SQL.
To avoid such attacks, the best practice is to use *parameterized queries* (prepared statements). This approach ensures that the user input is treated as a string of data rather than executable SQL.
Copy link
Contributor

Choose a reason for hiding this comment

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

[mdn-linter] reported by reviewdog 🐶

Suggested change
To avoid such attacks, the best practice is to use *parameterized queries* (prepared statements). This approach ensures that the user input is treated as a string of data rather than executable SQL.
To avoid such attacks, the best practice is to use _parameterized queries_ (prepared statements). This approach ensures that the user input is treated as a string of data rather than executable SQL.


In the following statement, we escape the **'** character. The SQL will now interpret the name as the whole string in bold (which is a very odd name indeed, but not harmful).
````SQL
Copy link
Contributor

Choose a reason for hiding this comment

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

[mdn-linter] reported by reviewdog 🐶

Suggested change
````SQL
```SQL

In the following statement, we escape the **'** character. The SQL will now interpret the name as the whole string in bold (which is a very odd name indeed, but not harmful).
````SQL
SELECT * FROM users WHERE name = ? AND password = ?;
````
Copy link
Contributor

Choose a reason for hiding this comment

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

[mdn-linter] reported by reviewdog 🐶

Suggested change
````
```

```
When executing the above query, for example, in Python, we pass the `name` and `password` as parameters, as shown below.

````Python
Copy link
Contributor

Choose a reason for hiding this comment

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

[mdn-linter] reported by reviewdog 🐶

Suggested change
````Python
```Python


````Python
cursor.execute("SELECT * FROM users WHERE name = ? AND password = ?", (name, password))
````
Copy link
Contributor

Choose a reason for hiding this comment

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

[mdn-linter] reported by reviewdog 🐶

Suggested change
````
```

Copy link
Contributor

github-actions bot commented Jan 25, 2025

Preview URLs

(comment last updated: 2025-01-27 17:47:11)

Copy link
Member

@Josh-Cena Josh-Cena left a comment

Choose a reason for hiding this comment

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

Thanks

Copy link
Member

@Josh-Cena Josh-Cena left a comment

Choose a reason for hiding this comment

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

Oops, thanks

@Josh-Cena Josh-Cena merged commit 36795aa into mdn:main Jan 27, 2025
8 checks passed
@younisdev
Copy link
Contributor Author

Thanks

Np

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/s [PR only] 6-50 LoC changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SQL Injection Docs Should Recommend Parameterized Queries
2 participants