html_sanitize_element

The `html_sanitize_element` directive specifies which HTML elements are allowed when sanitizing HTML content in NGINX.

Syntaxhtml_sanitize_element element_name [element_name ...];
Defaultnone
Contextlocation
Arguments1+

Description

The html_sanitize_element directive is part of the NGINX module designed to sanitize HTML5 content by allowing only a whitelisted set of elements to be included in the output. When defined within a location block, this directive instructs the server to parse incoming HTML and remove any tags that are not specified in the whitelist. This sanitization process is crucial for enhancing security by preventing the injection of malicious content, such as scripts or unwanted tags, that could exploit vulnerabilities in browsers or web applications.

The directive accepts one or more arguments that correspond to the names of HTML elements you wish to permit. For example, you could specify elements like div, span, or a, thereby allowing only these tags to remain in the provided HTML content. Users must carefully choose which elements to allow, as overly permissive settings can undermine the security benefits of this directive. The handling of each allowed element involves validation to ensure that any associated attributes also meet the specified criteria, which can include additional directives such as html_sanitize_attribute for attributes allowed on the elements.

Config Example

location /api/submit {
    html_sanitize_element div span a;
}

Ensure that you define at least one element; otherwise, no HTML will be allowed and content may be stripped entirely.

Be cautious when adding many elements to the whitelist; this may increase the risk of XSS if not properly controlled.

← Back to all directives