Reviewing sanitisation of HTML, files and structured content
Untrusted values need safe handling for the interpreter and context that will actually receive them. Understanding sanitisation of html and files and structured content helps establish which security property the feature needs and where that property is actually enforced.
What does this involve?
Input validation defines the structures, sizes and meanings an application expects to receive. It must happen before information influences a sensitive operation. Validation does not replace safe interpreter APIs, output encoding or permission checks, because those controls address different boundaries.
Where this is not possible, sanitization becomes necessary, removing potentially dangerous characters or content. In some cases, this may change the semantic meaning of the input, but for security reasons, there may be no alternative.
Why it matters
Consider a realistic failure: a reporting feature safely stores a value but later interprets it as executable query or presentation syntax.
Untrusted input can change application behaviour, consume excessive resources or reach data and execution paths outside the intended feature. The practical result depends on where the value is used and which privileges are available there.
A code audit connects a stated requirement to the implementation responsible for enforcing it. Source review and targeted behavioural checks should explain the same boundary and any relevant exceptions.
How penetration testers check for it
Review the specific control expectations below against the code, deployment and observable application behaviour.
- Confirm that all untrusted HTML input from WYSIWYG editors or similar is sanitized using a well-known and secure HTML sanitization library or framework feature.
- Confirm that the application avoids the use of eval or other dynamic code execution features such as Spring Expression Language (SpEL). Where there is no alternative, any user input being included must be sanitized before being executed.
- Confirm that data being passed to a potentially dangerous context is sanitized beforehand to enforce safety measures, such as only allowing characters which are safe for this context and trimming input which is too long.
- Confirm that user-supplied Scalable Vector Graphics (SVG) scriptable content is validated or sanitized to contain only tags and attributes that are safe for the application, e.g., do not contain scripts and foreignObject.
- Confirm that the application sanitizes or disables user-supplied scriptable or expression template language content, such as Markdown, CSS or XSL stylesheets, BBCode, or similar.
- Confirm that the application protects against Server-side Request Forgery (SSRF) attacks, by validating untrusted data against an allowlist of protocols, domains, paths and ports and sanitizing potentially dangerous characters before using the data to call another service.
- Confirm that the application protects against template injection attacks by not allowing templates to be built based on untrusted input. Where there is no alternative, any untrusted input being included dynamically during template creation must be sanitized or strictly validated.
- Confirm that the application appropriately sanitizes untrusted input before use in Java Naming and Directory Interface (JNDI) queries and that JNDI is configured securely to prevent JNDI injection attacks.
- Confirm that the application sanitizes content before it is sent to memcache to prevent injection attacks.
- Confirm that format strings which might resolve in an unexpected or malicious way when used are sanitized before being processed.
- Confirm that the application sanitizes user input before passing to mail systems to protect against SMTP or IMAP injection.
- Confirm that regular expressions are free from elements causing exponential backtracking, and ensure untrusted input is sanitized to mitigate ReDoS or Runaway Regex attacks.
- Compare parsing across components and alternate request formats.
- Use controlled invalid inputs to establish effects rather than assumptions.
How to fix it
Implement the required boundary in the component that actually performs the sensitive operation and retain focused regression coverage.
- Use explicit schemas and canonical representations.
- Prefer safe parsers and parameterised interpreter interfaces.
- Reject unexpected structures before sensitive processing.
- Apply resource bounds and predictable failure handling.
Blocking a few known attack strings is not a complete validation strategy. The intended data model and downstream interpretation determine which protections are needed.
Recheck the changed behaviour against the original failure conditions and the legitimate user journey. Keep enough evidence to show which release and configuration were verified, and include related entry points that depend on the same security decision.
Related issues
- Reviewing application encoding and sanitisation architecture
- Reviewing content interpretation and browser execution risks
- Auditing uploaded file content and processing controls
Read about our methodology or contact pentested.com to discuss the security of your application code.