How it Works
The middleware iterates through all parameters in the Body, Query, and Path bags of the incoming request. For every parameter that contains a string value, it applies the following transformation:Transformations Applied:
&(ampersand) becomes&"(double quote) becomes"'(single quote) becomes'<(less than) becomes<>(greater than) becomes>
Scope
The middleware automatically sanitizes:- Body Parameters: All fields sent in the request body (JSON or POST data).
- Query Parameters: All values sent in the URL query string.
- Path Parameters: All dynamic segments of the matched route.
Usage
It is recommended to place theSanitizeMiddleware early in your middleware stack so that all subsequent middlewares and controllers work with sanitized data.
Considerations
While sanitizing input is a good first line of defense, remember that:- Context Matters: Sometimes you might need raw HTML (e.g., in a CMS). In such cases, you might want to skip this middleware or implement a more granular approach.
- Double Escaping: If your template engine (like Twig or Blade) also escapes data, you might end up with double-escaped characters in your UI. Ensure your architecture handles this consistently.