Security concerns
SQL injection & Cross-site scripting
SQL injection & Cross-site scripting
Users provide data through the HTTP request in a number of ways.
Via search parameters in the URL like
/mypage?anything=danger.
| |
Via named groups in the URL like
/users/danger.
| |
Via form data in POST requests.
| |
If we trust this user data implicitly, then we make our system vulnerable to attack. ALL user data should be considered tainted.
So, how can data do harm to my application?
SQL injection is where users are able to execute arbitrary SQL code on our database. This can lead to data being lost or stolen, including potentially sensitive user data.
| |
Cross-site scripting is where users are able to inject arbitrary HTML code into the response. This potentially allows for user-submitted JavaScript code to be executed on user machines.
| |
We need to protect our application from malicious code.
SQL injection in detail
User enters data - we can’t stop this.
| |
We take the data from the request.
| |
The problem occurs when we construct our SQL code using the malicious data.
| |
If we use prepared statements then the database engine will sanitise the inputs for us.
| |
Protect the database. Use prepared statements!
Cross site scripting in more detail.
User enters data - we can’t stop this.
| |
We take the data from the request.
| |
The problem occurs when we construct our HTML code using the malicious data.
| |
Add the @std/html module
| |
We can fix this by escaping the user provided data
| |
Users are dangerous, don’t trust them.
Special characters such as < and > are meaningful to the HTML parser.
HTML named character references are escape sequences that render as special characters in HTML.
| Character reference | Renders as |
|---|---|
© | © |
& | & |
< | < |
> | > |
Passing strings through the escape function converts these active HTML characters into harmless character references.
| |
Keep safe, escape user content.
If you have any questions, now is a good time to ask.
Thanks for listening