Every online application must have a high level of application security. The OWASP-recommended approach for preventing XSS vulnerabilities in web applications is HTML sanitization. HTML sanitization is the process of eliminating dangerous JavaScript elements from raw HTML strings. In this blog, we will discuss how to sanitize request body and dynamic URL params in Golang, assuming familiarity with Gin web framework.
Packages Required
For HTML sanitization, we used bluemonday, which could be customized according to use cases. StrictPolicy will be used to return an empty policy, effectively stripping all HTML elements and their attributes.
Sanitization Methods
Since parameters could be nested in arrays and objects, we will be using recursion for the entire parameter sanitization. We have only considered int, float, string, slice/array, map, and struct data types. Other types are not implemented yet.
String Sanitization
This method takes a string that contains an HTML fragment or document and applies the given policy allowlist. It returns an HTML string that has been sanitized by the policy or an empty string. It also removes any malicious javascript code present in that string. Here, any other logic could be added based on the use case.
Slice And Array Sanitization
Since all values in the array must be checked, we iterate over the array and call sanitizeRecursively for nested sanitizing.
Map Sanitization
Similarly, for maps, all pairs must be checked and sanitized using recursive methods.
Structure Sanitization
For structs, we check for each field and sanitize them.
Body And Query Params Sanitization
When using the gin web framework, we can get request params in c.Request
where c is gin’s context. But for that, first, we need to populate c.Request.PostForm
and c.Request.Form
using the ParseForm method.
ParseForm parses the raw query from the URL and updates c.Request.Form
. For POST, PUT, and PATCH requests, it also reads the request body, parses it as a form, and puts the results into both c.Request.PostForm
and c.Request.Form
.
getRequestParams function below gets request params from gin’s context.
SanitizeBodyAndQuery function will then recursively sanitize all the params.
Examples
Example 1
API Request - POST
- https://localhost:8080/api/web/users/:user_id
Input request body -
Result -
Example 2
API Request - GET
- https://localhost:8080/api/web/users?pinCode=<p>411043</p>
Result -
From the above response, you can see that the HTML tags like p, title, th, etc. are stripped out and the data is sanitized.