Web API Application Security
Notes taken from the 2-day workshop for Web API Applications Security: Attack and Defense from NDC Copenhagen 2020 held online on April 2020; this focuses primarily on practical application security on Web Applications whereat different attack vectors and mitigation techniques are reviewed.
Content was divided into the security topics: HTTP Protocol, SQL Injection, XSS where the latter is divided into different subcategories.
HTTP Protocol
A few best-practices to consider for both directions, request and response; we must always keep in mind the side-effects and limitations mitigation mechanisms might bring and ultimately impact the system functionality.
HTTP Request
Some considerations:
- Avoid putting sensitive information in the URL, either fragments or query string.
- Lock capabilities, accept only the HTTP Verbs that you need.
- Stick to good practices in terms of HTTP Verbs idem-potency and their side-effects.
Although HTTP GET requests are encrypted in transit, there are still some other ways information contained in it can be leaked:
- User Bookmarks
- Server Logs —of any intermediary component between the User and the Application
- HTTP Referrer Headers
As far as HTTP Referrer Headers, tweaking the Referrer-Policy HTTP Header or using the rel="noreferrer" in hyperlinks is a start to mitigate such issues.
HTTP Response
The response side is where we can some kind of control over the client, though we still depend on the client compliance to standards and not doing their own thing; some security considerations on this side are:
- Enable
Strict-Transport-Security(HSTS) Header - HTTPOnly cookies to counter Cookie Theft and Tampering
- X-XSS-Protection Header —refer to curent browser compatibility, things ar far from good here.
The usage of this header is discouraged as browsers are dropping compatibility as it is not a standard header; as a matter of fact, the usage of the CSP header is favored.
nosnifffor theX-Content-Type-Optionsheader.- Usage of
Allow-Access-Control-Orign - Client cache control through the HTTP Headers:
Expires,Cache-Control,Last-Modified&Pragma.
e.g.
Expires: -1
Cache-Control: no-store, no-cache, must-revalidate
Last-Modified: now
Pragma: no-cache
nonefor the HTTP HeaderX-Permitted-Cross-Domain-Policies.SAMEORIGINforX-Frame-OptionsX-Feature-Policyset according to the functionality needed,selfandsrcare the preferred values for theallowed-list.
X-Feature-Policyis still experimental at the time of this writing.
SQL Injection (SQLi)
It was mentioned as a recommendation, the following algorithms for password encryption when storing them in the database: bcrypt and scrypt.
Parameter sanitization and good access control are among the common mitigation techniques, still, we are given the following best practices:
- Good input validation & sanitization
- Use of Parameterized Queries
- No Dynamic SQL, not even in Stored Procedures
- Lock down access, as in other areas, use the Least Priviledge Principles
In addition, some of the attacks known using this vector:
- Blind SQL Injection, no data is returned neither rendered to the User.
- SQL
waitfordelay (Time-based attack) which are typically used for blind SQLi.
XSS
Among the defense mechanisms, we’ve got the following:
- Output Scaping
- HTML Sanitization
- JS Sinks
- Sandboxing
- Safe JSON UI Usage
- Use of the
Content-Security-Policyheader.
The following matrix was presented which encapsulates where we can use the different mechanisms:
| Data Type | Context | Defense |
|---|---|---|
| String | HTML Body / Attribute | HTML Entity/Attribute Encode |
| String | Javascript variable | Javascript Hex Encoding |
| String | HTTP GET parameter | URL Encoding |
| String | Untrusted URL | URL Validation, avoid javascript: links, Attribute Encoding and Safe URL verifications |
| String | CSS | CSS Hex Encoding |
| HTML | Anywhere | HTML Sanitization (Client and Server side) |
| Any | DOM | Safe Use JS APIs |
| Untrusted Javascript | Anywhere | Sandboxing & Delivery from Different Domain |
| JSON | Embedded | JSON Serialization |
A bit more on attacks, we’ve got the types: Reflected & Persisted, in esence the difference being that the persisted, as the name implies, sticks beyond the user requests or sessions, likely put in place through some feature of the Web Site or Application.
Now, some example of attacks:
- Cookie Thefts, i.e. Session Hijacking
- Same Site Request Forgery
- Virtual Site Defacement
With XSS, for the most part it is about what we can trust and where in the stack we’ve got enough control to put safety mechanisms in place; he are some assumptions:
- Assume all variable added to a UI are dangerous, likewise for UI Templates.
- Ensure all protection from XSS is done at the UI layer.
- Don’t depend, fully, on Server-Side mechanisms.
- Be wary of people disablng framework XSS protection features.
All protections must be built within the UI layer itself such as escaping, encoding/decoding, sanitization
DOM XSS
This requires unexpected modifications to the actual DOM structure rendered at the client, this is considered as yet another type in addition to reflected and persisted; [IFrame Sandboxing][html5rocks-iframe-sandboxing] can help us to dramatically reduce risks when running 3rd party components on our applications.
More information about [DOM-based XSS][owasp-dom-xss].
Tools & Frameworks
There we some tools and frameworks mentioned that are worth looking into:
- Svelte
- RetireJS
- Mozilla SSL Config
- Security Headers Check
- SSL Labs
- JS Fuck
- Web Encoders
- HTML Sanitizier
- DOM Purify
References
- OWASP Secure Headers Project - OWASP
- The XSS Auditor - Virtue Security
- X-Feature-Policy Header - Mozilla Developer Network
- DOM XSS Attack - OWASP
- OWASP Cheat Sheets - OWASP
- OWASP ASVS - OWASP
- Is TLS Fast Yet - Is TLS Fast Yet