Join our Newsletter — 33% off our NHI Course
Home› Glossary› Cyber Security› Nokogiri
Cyber Security

Nokogiri

← Back to Glossary
By NHI Mgmt Group Updated November 3, 2026 Domain: Cyber Security

Nokogiri is a Ruby library for parsing, searching and modifying HTML and XML documents. It is commonly used for document processing, and its security relevance comes from how it handles untrusted markup, including parser choice, entity expansion, and sanitisation of content before use.

What Nokogiri Is Used For

Nokogiri is a document parsing and transformation library, so its core job is to turn HTML or XML into a structure that code can search, traverse, and modify. The security question is not whether it is “safe” in the abstract, but how safely it handles malformed, hostile, or unexpectedly complex markup before the application trusts the output.

Why Parsing Behavior Matters

Parsing libraries sit on a trust boundary. If an application accepts user-supplied markup, Nokogiri becomes part of the path that decides what the document actually contains after normalisation, decoding, and tree construction. Small differences in parser mode, error handling, and entity processing can change whether input stays inert text or becomes active structure that later logic interprets.

That is why the same library can be perfectly appropriate for benign document processing and still require careful handling when the source data is untrusted. The main security concern is not the library name itself, but whether the application assumes the parsed tree is already sanitized, validated, or safe for downstream rendering and storage.

Untrusted Markup, Entities, and Sanitisation

When Nokogiri processes untrusted HTML or XML, the dangerous cases are usually related to content expansion and transformation. Entity handling can amplify input, obscure what was originally submitted, or create unexpected resource use. Sanitisation also matters because parsing alone does not remove risky elements or attributes unless the application explicitly applies a safe policy after parsing.

In practice, the key distinction is between parsing and trusting. A parsed document may still contain active links, scripts, embedded references, or attacker-controlled text that is dangerous if it is later re-embedded into a web page, template, email, report, or API response without output encoding and policy checks.

Safe Use in Application Workflows

Nokogiri is most secure when it is used with a narrow purpose and a clear trust model. Applications should treat user content as hostile until it has been validated, parsed with an appropriate mode, and passed through a dedicated sanitisation or encoding step for the exact sink where it will be used. The same input may be safe for text extraction but unsafe for HTML rendering.

Its value is highest in workflows such as document inspection, metadata extraction, markup cleanup, or controlled transformations where developers need deterministic structure handling. The risk increases when Nokogiri output is reused across contexts, especially if a later component assumes the document has already been filtered or escaped.

Risk and Threat Considerations

Parsing untrusted markup can expose applications to denial of service, content smuggling, unsafe reserialization, and downstream injection if the parsed output is later trusted without a separate safety step. XML-style entity expansion and overly complex documents can also create resource pressure or unexpected processing behavior.

Failure mechanism: An attacker supplies markup that is parsed into a structure the application misunderstands, expands too much, or forwards into a sensitive sink without sanitisation or output encoding.

Impact: The result can be malformed output, broken business logic, resource exhaustion, or cross-site scripting and related injection issues when the content is later displayed or reused.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP ASVS, NIST SP 800-53 Rev 5 and CIS Controls v8 set the governance and control requirements practitioners need to meet.

FrameworkControl / ReferenceRelevance
OWASP ASVSV1 — Encoding and SanitizationNokogiri turns untrusted markup into structured content that must be encoded or sanitized before use.
V15 — Secure Coding and ArchitectureParser choice and trust boundaries are application-design issues that shape how Nokogiri is used safely.
Recommendation — Apply V1 controls to sanitize parsed markup before it reaches any rendering or storage sink. Design the document-processing flow so untrusted input is parsed, validated, and isolated before business logic uses it.
NIST SP 800-53 Rev 5SI-10 — Information Input ValidationMarkup parsing is an input-validation problem when hostile HTML or XML enters the application.
SC-18 — Mobile CodeActive markup and embedded content can behave like executable content when reused unsafely.
Recommendation — Validate and constrain markup input before parsing or transformation. Restrict active or embedded content in parsed documents before downstream processing.
CIS Controls v8CIS-16 — Application Software SecuritySecure handling of third-party input and unsafe content is part of application security hygiene.
Recommendation — Build sanitization and safe output handling into the application’s content-processing path.

Practitioner Guidance

Why practitioners should care: Nokogiri often sits at the point where external input becomes internal structure, so the control question is whether each downstream use treats the result as data, not as trusted content. The safest pattern is to make parser choice, entity handling, and sanitisation explicit design decisions rather than library defaults.

What to watch for: Be cautious when the same parsed tree is reused for rendering, storage, and programmatic decisions, because each sink has different safety requirements. A document that is acceptable for search or extraction may still be unsafe for direct HTML output.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

NHIMG Editorial Note
Reviewed and updated by the NHIMG editorial team on November 3, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org