Application Deployment and Security · Topic 6 of 8
Application Security and OWASP Threats
Learn how to protect secrets, encrypt data at rest and in transit, handle data safely and spot the top OWASP threats: XSS, SQL injection and CSRF.
- 5 min
- Keep secrets out of code and Git, and load them from the environment or a secrets manager.
- Encrypt data at rest in storage and in transit with TLS.
- Store passwords as salted hashes, not as plain or reversibly encrypted text.
What it is
Application security is the set of practices that keep an application and its data safe from misuse.
This page covers three areas: protecting secrets, encrypting data in storage and in transit, and handling data carefully. It then looks at the common web attacks that the OWASP project lists, including XSS, SQL injection and CSRF.
OWASP is the Open Worldwide Application Security Project, and its Top 10 list ranks the most serious web application risks.
How it works
Security comes from layers: protect the secrets, protect the data, and validate every input.
Secret protection
Secrets are passwords, API keys, tokens and the private keys of certificates. Anyone who holds one can act as the owner.
- Never write secrets in source code or commit them to Git.
- Load them at run time from environment variables or a secrets manager.
- Give each secret the least access that it needs.
- Rotate secrets on a schedule, and replace one at once if it leaks.
- Keep them out of logs and error messages.
import os
API_KEY = os.environ["API_KEY"]
A secret that was committed to Git stays in the history even after the file is changed, so treat it as leaked and replace it.
Encryption in storage
Encryption at rest protects data on disks, in databases and in backups. If the storage is stolen, the data stays unreadable without the key.
Passwords are different. Do not encrypt them for later use. Store a salted hash made with a slow algorithm, so that the original password cannot be recovered.
Encryption in transport
Encryption in transit protects data as it crosses the network. Use TLS, which is the basis of HTTPS, for web traffic and APIs. Use SSH instead of Telnet for device access.
Check certificates and do not turn off verification in production. Without TLS, an attacker on the path can read or change the traffic.
Data handling
- Collect only the data you need.
- Validate input on the server, whatever the client checks.
- Limit who can read each type of data.
- Do not put personal data in logs.
- Delete data when it is no longer needed, and follow the rules that apply to your data.
OWASP threats
| Threat | What happens | Main defence |
|---|---|---|
| SQL injection | Attacker input changes a database query | Parameterised queries, input validation |
| Cross site scripting, XSS | Attacker script runs in another user's browser | Escape output, validate input, content security policy |
| Cross site request forgery, CSRF | A logged in user's browser is tricked into sending an unwanted request | Anti CSRF tokens, same site cookies |
| Broken authentication | Weak login or session handling | Strong passwords, multi factor authentication, session timeouts |
| Sensitive data exposure, now called cryptographic failures | Data is stored or sent without protection | Encryption at rest and in transit |
SQL injection
If code builds a query by joining text, an attacker can add their own SQL.
# unsafe
query = "SELECT * FROM users WHERE name = '" + name + "'"
# safe: the driver passes the value separately
cursor.execute("SELECT * FROM users WHERE name = ?", (name,))
An input such as ' OR '1'='1 would change the meaning of the unsafe query. In the safe form, the value is treated as data and never as SQL.
Cross site scripting
An application that shows user input without escaping it can deliver a script to other visitors. That script can steal session cookies or change the page. The fix is to escape or encode output for the place where it appears, and to validate input.
Cross site request forgery
A page on another site makes the victim's browser send a request to your application, and the browser attaches the user's login cookie. The application cannot tell it from a real request. A secret token in each form, which an attacker cannot guess, stops it.
General defences
Keep software patched, give accounts only the rights they need, log security events, and test for these flaws before release.
Match the attack to the target. SQL injection attacks the database, XSS attacks other users' browsers, and CSRF abuses a user's existing login.
What to remember for the exam
Seven facts cover this topic.
- Keep secrets out of code and Git, and load them from the environment or a secrets manager.
- Encrypt data at rest in storage and in transit with TLS.
- Store passwords as salted hashes, not as plain or reversibly encrypted text.
- SQL injection is prevented with parameterised queries.
- XSS is prevented by escaping output and validating input.
- CSRF is prevented with anti CSRF tokens.
- OWASP publishes a Top 10 list of web application risks.
Common mistakes
Four mistakes are typical.
Trusting client side checks. An attacker can skip the browser, so validate on the server.
Confusing XSS and CSRF. XSS runs a script in the victim's browser. CSRF forges a request that uses the victim's login.
Building SQL with string joins. Use parameters.
Deleting a leaked secret from Git and carrying on. The old value remains in the history and must be replaced.
Do not log secrets or tokens, even for debugging. Logs are copied and read by many people.
Check yourself
Work through these three questions.
How do you stop SQL injection?
Use parameterised queries, so input is treated as data and not as part of the SQL.
What is the difference between XSS and CSRF?
XSS runs an attacker's script in another user's browser. CSRF tricks a logged in user's browser into sending an unwanted request.
Why should a password be stored as a salted hash?
The original password cannot be recovered from a salted hash, so a stolen database does not reveal the passwords.
Topic 6 of 8 in Application Deployment and Security
Test yourself on Application Deployment and Security
The full Cisco CCNA 200-901 DEVASC question bank has 808 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Cisco. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.