Is String.sanitize, the best way to protect from SQL injection in rails or sinatra apps

FredyK

Is string.sanitize the best way to protect from sql injection.

Do we need to install the Sanitize gem for it or is there a better way?

value = "O'Brian"
value.sanitize =>"O\'Brian"
or 
value.escape =>"O\'Brian"

It s probably included by default in Rails 5 , but what about using sinatra.

Holger Just

To protect against SQL injections, you should us prepared statements. About all high-level database adapters offer capabilities to use and properly escape variables. In ActiveRecord, this looks like this:

value = "O'Brian"
Person.where(name: value).to_sql
# => "SELECT `people`.* FROM `people`  WHERE `people`.`name` = 'O\\'Brian'"

Other database adapters like Sequel or DataMapper have similar capabilities.

When using a plain database adapter like pg or mysql2, you can use plain prepared statements on the database level.

With mysql2, this can look like this:

value = "O'Brian"
statement = @client.prepare("SELECT * FROM people WHERE name = ?")
result = statement.execute(value)

Alternatively, all adapters offer database-specific string escape methods. But you should generally stick to prepared statements as they are safer to use when you just don't attempt to reason about escaping but delegate all of this to a library which does this consistently.

As a final note about the sanitize method and the sanitize gem, they are not intended for escaping SQL fragments and won't save you from SQL injections when used that way. The sanitize gem is used to ensure that HTML code only contains safe whitelisted tags and attributes. It has nothing to do with escaping SQL and will result in vulnerable code if used that way!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Best way to strip punctuation from a string

From Dev

Best way to protect against SQL injection in SqlDataAdapter

From Dev

Best way to sanitize a multiple choice select list

From Dev

Best way to extract data from string

From Dev

Java Best way to extract parts from a string

From Dev

What is the best way to protect a flask endpoint?

From Dev

What's the best way to protect against 'unrecognized selector' for objects returned from NSDictionary

From Dev

SQL Injection in Rails

From Dev

Best way to grab a specific position from a string

From Dev

The best way to secure yourself against sql injection in nodejs

From Dev

Best way to remove string prefix from std::string_view?

From Dev

MySQL - Best way to extract integer from string

From Dev

Properly protect edit control from SQL injection and nonsense characters

From Dev

Rails - how to protect against sql injection for a postgres find_by_sql query?

From Dev

Java Best way to extract parts from a string

From Dev

Can activeadmin be used with sinatra or other non-rails apps?

From Dev

Best way to password protect a dropbox file

From Dev

Rails scope and sql injection

From Dev

How to secure query from sql injection in find_by_sql in Rails

From Dev

Protect select statement from sql injection

From Dev

Best way to protect javascript code from working with modified parameters array

From Dev

Best way to grab a specific position from a string

From Dev

Best way to prevent sql "injection" when using column as variable

From Dev

How can protect PostgREST from sql injection and other security issues?

From Dev

Best way to sanitize params in controller spec

From Dev

Is there a way to modify FILTER_SANITIZE_STRING

From Dev

Protect variables from injection

From Dev

What is the best way to sanitize POST data?

From Dev

Is there a way to protect all existing rows from being edited in an SQL table?

Related Related

  1. 1

    Best way to strip punctuation from a string

  2. 2

    Best way to protect against SQL injection in SqlDataAdapter

  3. 3

    Best way to sanitize a multiple choice select list

  4. 4

    Best way to extract data from string

  5. 5

    Java Best way to extract parts from a string

  6. 6

    What is the best way to protect a flask endpoint?

  7. 7

    What's the best way to protect against 'unrecognized selector' for objects returned from NSDictionary

  8. 8

    SQL Injection in Rails

  9. 9

    Best way to grab a specific position from a string

  10. 10

    The best way to secure yourself against sql injection in nodejs

  11. 11

    Best way to remove string prefix from std::string_view?

  12. 12

    MySQL - Best way to extract integer from string

  13. 13

    Properly protect edit control from SQL injection and nonsense characters

  14. 14

    Rails - how to protect against sql injection for a postgres find_by_sql query?

  15. 15

    Java Best way to extract parts from a string

  16. 16

    Can activeadmin be used with sinatra or other non-rails apps?

  17. 17

    Best way to password protect a dropbox file

  18. 18

    Rails scope and sql injection

  19. 19

    How to secure query from sql injection in find_by_sql in Rails

  20. 20

    Protect select statement from sql injection

  21. 21

    Best way to protect javascript code from working with modified parameters array

  22. 22

    Best way to grab a specific position from a string

  23. 23

    Best way to prevent sql "injection" when using column as variable

  24. 24

    How can protect PostgREST from sql injection and other security issues?

  25. 25

    Best way to sanitize params in controller spec

  26. 26

    Is there a way to modify FILTER_SANITIZE_STRING

  27. 27

    Protect variables from injection

  28. 28

    What is the best way to sanitize POST data?

  29. 29

    Is there a way to protect all existing rows from being edited in an SQL table?

HotTag

Archive