Building web applications that are secure
Building a modern web application, it is not enough to merely _think_ about security. You have to actively apply the key security mechanisms that every web browser provides today.
Today, making a web application secure is not something optional – it is an implicit requirement that every application has to meet from the very start. Even if your app is meant purely for internal use, the moment a human operator works with it, you have to assume it may serve as an entry point for breaking into the systems behind it.
Intro
So how do you secure an application? Plenty of resources can help you figure out what it takes to build one safely. Among them is the OWASP Top Ten – a list of the most critical security risks that attackers may exploit against your application.
This post concentrates on a single one of those risks: Cross Side Scripting, or XSS for short.
To get a better grasp of what XSS is and how to defend against it, read this OWASP article.
Nowadays you will not find a web application without JavaScript – even the most basic one may rely on Bootstrap and, without realising it, pull in JQuery under the hood. If you don’t know how to use JavaScript securely, your users are exposed to serious danger. To fully protect both your app and its users, you need to know the techniques described below.
Content Security Policy
Content Security Policy, or CSP for short, is a mechanism built into every modern web browser that helps stop XSS and script injection attacks against your application.
To make use of it correctly, your backend has to add the appropriate HTTP header to every response: Content-Security-Policy.
This header tells the browser which policy to enforce for content loaded from the given URL. Follow the links to learn more about the values you can combine into a policy. You can, for example, limit the way scripts are loaded, forbid the use of eval(), and much more.
Among the most important options is the nonce attribute, introduced in version 2.0 of the policy. Adding it to each <script/> tag lets the browser know which scripts are safe to execute because they come from you rather than from an attacker.
The nonce holds a value generated with a secure random source, and it has to be different for every request. That same value must also appear in the CSP header, for example:
header
Content-Security-Policy: script-src 'nonce-CkZtNfGbz9t_1le0gkpbLMcc'
tag
<script nonce="CkZtNfGbz9t_1le0gkpbLMcc">
// your code
</script>
Thanks to the nonce, the browser can tell precisely which scripts are safe and delivered by your application.
All other scripts will not run (or, depending on how the policy is configured, they will be reported), and an attacker cannot guess the nonce – provided you make sure the value is truly random.
Cross Origin Opener Policy
Another highly important option is the Cross Origin Opener Policy header, which shields users by preventing a browsing context from being shared. Put simply, an attacker will not be able to load suspicious content through an <iframe/> tag. And the extra security is not the only benefit – you also unlock very powerful APIs such as SharedArrayBuffer, which can give your application a huge boost :)
The header accepts three values:
unsafe-none– the default and the least secure one, meaning that effectively no policy is enforcedsame-origin-allow-popups– with this value, newly opened windows (pop ups) or tabs share the same contextsame-origin– the strictest option, where the browsing context is isolated to the same document only
You should at the very least think about using one of the two more secure options.
Cross Origin Embedder Policy
Lastly, once Cross-Origin-Opener-Policy is in place, you can rely on Cross Origin Embedder Policy to decide which resources the browser may load. With the default value unsafe-none, every resource is accessible.
To gain tighter control over what the browser fetches over the wire, use the other value, require-corp.
Cross-Origin-Embedder-Policy: unsafe-none | require-corp
If you want to permit loading every resource from the same origin, these two headers will do the job:
Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin
With this setup, any request for an external resource such as this one:
<img src="https://thirdparty.com/img.png"/>
gets blocked. To allow such resources to load, add a crossorigin attribute.
Fetch Metadata
The last option is the fetch metadata request header, which supplies extra information about the context a resource request came from. Based on where the request originates, the server can then decide whether the resource should be accessible.
This makes it possible to enforce a resource isolation policy so that only resources meant for sharing are actually shared.
You can describe a particular context with one of the following headers:
Fetch metadata headers defend your application against CSRF, XSSI, and cross-origin information leaks.
Secure with Apache Struts
Starting with Apache Struts version 6.0.0, all
of these features are available to your application. This release of the framework introduced a set of interceptors covering every aspect of configuring a proper Content Security Policy:
- Content Security Policy Interceptor
- Cross-Origin Opener Policy Interceptor
- Cross-Origin Embedder Policy Interceptor
- Fetch Metadata Interceptor
You can go with the default interceptor stack and its predefined options or – which is the recommended way – build a stack of your own and tune how the Content Security Policy is configured so that it protects your application as securely as possible.
Summary
Building a modern web application, it is not enough to merely think about security. You have to actively apply the key security mechanisms that every web browser provides today. Even older browsers support part of the policies described here, so you can still build a secure web application while targeting them.
Security is not optional – it is something you simply must have :)