Application Security
Application security protects software, especially web applications, against attacks on their code and logic. The OWASP Top 10 lists the most serious web risks, including broken access control, cryptographic failures, injection, insecure design, security misconfiguration, vulnerable and outdated components, authentication failures, software and data integrity failures, logging and monitoring failures and server-side request forgery. Important attacks:
- SQL injection: user input changes a database query (stopped by parameterised queries).
- Cross-site scripting (XSS): the attacker's script is shown to other users in a page and runs in their browser, stealing cookies or actions. Defence: output encoding (escaping < > & " ') and a content security policy.
- Cross-site request forgery (CSRF): a malicious page makes the logged-in user's browser send a request to another site. Defence: anti-CSRF tokens and SameSite cookies.
- Broken authentication and session management: weak passwords, predictable session IDs. Defence: strong passwords, multi-factor authentication, new random session ID at login, secure cookie flags.
- Insecure file upload, path traversal and deserialisation flaws.
Good practice: validate input, encode output, use least privilege, keep libraries updated, store secrets safely, log security events, and test with code review, static and dynamic scanning and penetration testing.
Browser Defences
The browser is the front line against web threats.
- Same-origin policy: a page may read data only from the same protocol, host and port, so one site cannot read another's data.
- Sandboxing and site isolation: each tab or site runs in a separate restricted process.
- HTTPS and certificate checks: padlock and warnings; HSTS forces HTTPS.
- Safe browsing and SmartScreen: warn about phishing and malware sites.
- Content Security Policy (CSP): the site lists the sources of scripts that the browser may run.
- Cookie flags:HttpOnly (hidden from scripts), Secure (sent only over HTTPS) and SameSite (limits cross-site sending).
- Clickjacking protection (frame restrictions), pop-up and download controls, automatic updates, and extension permissions that users should review.
- User habits: keep the browser updated, avoid unknown extensions, check the address, use a password manager and multi-factor authentication.
Definition: Cross-site scripting (XSS). An attack in which malicious script is injected into a web page that other users view, so that the script runs in their browsers with the rights of the trusted site.
Table 4.3: Web attacks and defences
| Attack | Idea | Defence |
|---|---|---|
| SQL injection | Input changes the query | Parameterised queries |
| XSS | Script runs in other users' browsers | Output encoding, CSP |
| CSRF | Browser sends a forged request | Anti-CSRF token, SameSite |
| Session hijacking | Stolen session ID | HttpOnly and Secure cookies, HTTPS |
Example 4.3: Output encoding stops XSS
Problem: A comment box accepts the text <script>alert(1)</script>. What is shown to other users if the page inserts the text as it is, and if the page encodes special characters?
Solution:
- If inserted as it is, the browser treats the text as a script tag and runs it in every visitor's browser.
- If encoded, the characters < and > become < and >.
- The browser then displays the text as harmless characters and does not run it.
Result: The encoded output is <script>alert(1)</script>, shown as plain text; encoding output is the main defence against XSS.
import html
comment = "<script>alert(1)</script>"
print("Raw: ", comment)
print("Encoded:", html.escape(comment))
Output
Raw: <script>alert(1)</script>
Encoded: <script>alert(1)</script>
Common mistakes
- Inserting user input into a page without encoding it.
- Installing browser extensions without checking their permissions.
- Storing session cookies without HttpOnly and Secure flags.
- Using an outdated browser or ignoring certificate warnings.
Key takeaways
- OWASP Top 10: access control, injection, misconfiguration, outdated components.
- XSS: encode output and use CSP; CSRF: tokens and SameSite.
- Browser defences: same-origin policy, sandbox, HTTPS, safe browsing, CSP.
- Cookie flags: HttpOnly, Secure, SameSite.
Practice questions
- What is the same-origin policy?
- How is XSS prevented?
- What does the HttpOnly cookie flag do?
Answers to the practice questions
- A rule that a page can read data only from the same protocol, host and port.
- By encoding output and using a content security policy.
- It hides the cookie from scripts, so XSS cannot read it.
Sources
- Official BCA syllabus Computer Security, course code 25BCA406DS02, Unit IV
