# Web Application Security: Understanding Common Risks and How to Prevent Them

Web applications are widely used in modern services.

They allow users to purchase items, manage accounts, work in teams, and interact with Cloud services from anywhere in the world.

For example, a customer may update their account details, or download an invoice from a web portal.

However, due to these services being accessible via the internet, vulnerabilities in these applications can allow attackers to gain access to sensitive data and take control of user accounts.

As such, security within web applications is very important.

This article will highlight common security issues within web applications, and how to reduce them.

## 1. What is Web Application Security is the implementation of security measures to protect web applications and services.

This includes:

* Only allowing authorised users to perform certain actions.
* Validating user input.
* Encrypting sensitive data.
* Preventing session and cookie hijacking.
* Minimising misconfigurations.
* Patching known vulnerabilities in libraries.

Web application security should be considered at all stages of development.

## 2. Why is Web Application Security Important?

Web applications are located between users and backend systems/databases. As such, any vulnerabilities within web applications can lead to significant issues.

Some of these issues include:

* **Data breaches.** Sensitive personal information, passwords and payment details can be accessed.
* **Account Takeover.** Poor authentication/session management allows attackers to take control of a user’s account.
* **Page Manipulation.** Attackers can alter page content, or inject malicious scripts.
* **Service Disruption.** Expensive endpoints can be misused, causing performance issues or crashing.
* **Fines.** If user data is exposed, companies may be fined by regulators.

A well-known source for critical web application security issues is the OWASP Top 10.

## 3. Common Web Application Security Issues

### 3.1 Improper Access Control

Improper access control occurs when users can access functionality beyond what they are authorised to.

For example, changing the URL in a web page from `/account/settings` to `/admin/users` may give users access to admin functionality.

**Prevention:**

* Ensure users can only access functionality they are authorised for.
* Implement server-side checks to ensure users have the correct role and ownership for each request.
* Disable directory browsing, and ensure sensitive files cannot be accessed.
* Log access control errors, and alert users if many occur.

### 3.2 Injection (SQL, Command, NoSQL)

Injection occurs when user input is passed directly to an interpreter.

This can allow attackers to perform malicious actions. For example, if user input is directly inserted into a SQL query, users may be able to bypass authentication or view contents of the database.

**Prevention:**

* Use parameterized queries.
* Use secure ORM frameworks, and avoid string concatenation.
* Sanitise and validate all user input on the server.
* Ensure database users have the minimum amount of privileges.

### 3.3 Cross-Site Scripting (XSS)

XSS occurs when untrusted data is not properly validated and escaped within a web page. This allows malicious scripts to be executed within another user’s browser.

Attackers may be able to steal session tokens, redirect users, and perform actions on their behalf.

**Prevention:**

* Escape untrusted user data correctly within HTML.
* Use modern front end frameworks, as they will by default escape output.
* Ensure a robust Content Security Policy (CSP) is in place.
* Ensure secure cookies are set with `HttpOnly` and `Secure` flags.

### 3.4 Insecure Data Storage

Sensitive data (passwords, tokens, personal information) may be insecurely stored or sent. This can include not using HTTPS, storing passwords in plain text, or using outdated hash algorithms.

**Prevention:**

* Ensure HTTPS is used throughout the application with TLS.
* Hash passwords using strong adaptive hashing methods (Argon2id or Bcrypt).
* Never hardcode encryption keys/secret values in code.
* Disable weak TLS versions and ciphers.

### 3.4 Cross-Site Request Forgery (CSRF)

CSRF causes authenticated users to make an unintended request to a web application.

For example, an attacker may cause a hidden form on a malicious website to be submitted, which may cause the users email to be changed, or money to be transferred.

**Prevention:**

* Use an anti-CSRF token.
* Set cookies with `SameSite=Strict`/`SameSite=Lax`.
* Ensure users re-authenticate when making sensitive changes.

### 3.5 Misconfiguration

A misconfigured server, framework or cloud storage may lead to sensitive functionality, or error messages being exposed.

This can include:

* Default admin credentials not changed.
* Verbose error messages shown to users.
* Unnecessary ports, functionality, and example apps enabled.
* Missing HTTP security headers.

**Prevention:**

* Ensure production configurations are hardened. Disable any unnecessary functionality.
* Provide generalised error messages to users. Ensure detailed error messages are logged securely on the server.
* Ensure HTTP security headers (`Strict-Transport-Security`, `X-Content-Type-Options` etc) are set.
* Automate configuration validation within deployment.

## 4. Client-Side and Server-Side Security

**Client-side and server-Side Security**

Both help secure web applications however have different purposes.

**Client-Side Security**

* Improves UX and can help identify simple input errors.
* However, client-side security is easily bypassed by altering requests in the browser, or using proxy tools.
* Client-side security should never be relied upon.

**Server-Side Security**

* Is the final point of security. It secures backend databases and application logic.
* All requests must be validated on the server, regardless of what is shown in the frontend.
* All data received from the browser must be considered untrusted, and should be validated on the server.

## 5. Best Practice

To improve web application security, developers should follow best practice:

* **Ensure HTTPS is enforced.** All communication between browser and server should be secure.
* **Ensure all input is validated on the server.** Check for correct data types, length, format and ranges.
* **Ensure secure session management.** Regenerate session id’s on login, and set inactivity expiry.
* **Ensure dependencies are up to date.** Ensure any third party packages are monitored for known CVE’s.
* **Ensure least privilege is applied.** Limit permissions of users, services and databases within the application.
* **Ensure secure HTTP headers.** Help prevent clickjacking, MIME sniffing, and script injection.
* **Ensure secrets are protected.** Store API keys and credentials in environment variables or a secret manager.
* **Log security related events.** Log successful/unsuccessful logins, permission errors, and admin actions.

## 6. Testing

Security testing should be performed throughout development. Possible tests include:

* Static Application Security Testing (SAST) to detect common vulnerabilities in source code.
* Dynamic Application Security Testing (DAST) in staging environments.
* Vulnerability scanning in dependencies and containers.
* Manual testing of authentication, session management, and user permissions.
* Checking HTTP response headers and TLS configuration.

> Ensure you have written permission to perform any security testing on a system.

## 7. OWASP

OWASP provides open source standards and guidance to help create secure software.

The OWASP Top 10 highlights the most critical security issues in web applications. These include:

* Improper Access Control
* Insecure Data Storage
* Injection
* Insecure Design
* Misconfiguration
* Vulnerable Components
* Authentication Failure
* Integrity Failure
* Monitoring Failure
* Server Side Request Forgery.

These can be used to provide a checklist for security within applications.

## 8. Future of Web Application Security

With modern web applications using single page applications, serverless architecture, edge computing, and integration with third parties, there are many more potential attack vectors.

Within development teams, there will be increasing attention to:

* Implementing security checks within CI/CD pipelines.
* Stronger phishing resistant authentication (passkeys, WebAuthn).
* Strict verification of open source packages within the supply chain.
* Zero trust security between frontend services and backend microservices.
* Continuous monitoring for unusual user activity.

Creating security awareness within engineering teams is one of the best ways to protect web applications.

## Conclusion

Web applications are very important for digital services, and so are often targeted by attackers.

If not properly secured, issues such as improper access control, injection, XSS and misconfiguration may lead to sensitive information being exposed.

By validating data on the server, implementing secure coding practices, and following OWASP guidelines, developers can create much more secure web applications.

## References

* [OWASP Top 10](https://owasp.org/www-project-top-ten/)
* [OWASP Cheat Sheet Series](https://cheatsheetseries.owasp.org/)
* [MDN Web Security Guide](https://developer.mozilla.org/en-US/docs/Web/Security)
