Injection Attacks: SQLi & XSS
Make a database and a browser run your input as code, then learn how to stop it.
Injection happens when an application mixes untrusted input with code. The database or browser can't tell where the data ends and the code begins, so it runs both.
This room covers the two most famous kinds, SQL injection and cross-site scripting, and the defences that actually work.
1
SQL injection
This login code builds a SQL query by gluing user input into a string:
jsconst sql = "SELECT * FROM users WHERE name = '" + name + "' AND pass = '" + pass + "'";Type this as the username:
admin' --and the query becomes:
sqlSELECT * FROM users WHERE name = 'admin' --' AND pass = ''The quote closes the string early.
--turns the rest of the line into a comment, so the password check disappears. You're in as admin.The fix is to never build SQL from strings. Send the query and the values separately, so the database always treats input as data:
jsdb.query("SELECT * FROM users WHERE name = $1", [name]);
⚑ Report a problem with this room
Log in to report a problem.