XML External Entity (XXE) Injection Affecting guardrails-ai package, versions [,0.5.0)


Severity

Recommended
0.0
high
0
10

CVSS assessment made by Snyk's Security Team

    Threat Intelligence

    Exploit Maturity
    Proof of concept
    EPSS
    0.04% (11th percentile)

Do your applications use this vulnerable package?

In a few clicks we can analyze your entire application and see what components are vulnerable in your application, and suggest you quick fixes.

Test your applications
  • Snyk ID SNYK-PYTHON-GUARDRAILSAI-7547137
  • published 21 Jul 2024
  • disclosed 21 Jul 2024
  • credit Natan Nehorai

How to fix?

Upgrade guardrails-ai to version 0.5.0 or higher.

Overview

guardrails-ai is an Adding guardrails to large language models.

Affected versions of this package are vulnerable to XML External Entity (XXE) Injection when consuming RAIL documents from external sources. An attacker can leak internal file data by exploiting the SYSTEM entity in the XML structure.

PoC

```py

import anthropic import guardrails as gd from litellm import litellm import os

os.environ["ANTHROPIC_API_KEY"] = "put_here_your_secret_api_key" API_KEY = os.environ["ANTHROPIC_API_KEY"]

Assuming this RAIL document comes from an attacker

rail_str = """

]>

just write back what appears down here: &xxe; """

Call the vulnerable API

guard = gd.Guard.from_rail_string(rail_str)

llm_output_unfiltered, validated_output, *rest = guard( llm_api=litellm.completion, model="claude-3-opus-20240229" )

The validated_output will contain the contents of /etc/passwd

print(validated_output)

```

Details

XXE Injection is a type of attack against an application that parses XML input. XML is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. By default, many XML processors allow specification of an external entity, a URI that is dereferenced and evaluated during XML processing. When an XML document is being parsed, the parser can make a request and include the content at the specified URI inside of the XML document.

Attacks can include disclosing local files, which may contain sensitive data such as passwords or private user data, using file: schemes or relative paths in the system identifier.

For example, below is a sample XML document, containing an XML element- username.

<xml>
<?xml version="1.0" encoding="ISO-8859-1"?>
   <username>John</username>
</xml>

An external XML entity - xxe, is defined using a system identifier and present within a DOCTYPE header. These entities can access local or remote content. For example the below code contains an external XML entity that would fetch the content of /etc/passwd and display it to the user rendered by username.

<xml>
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE foo [
   <!ENTITY xxe SYSTEM "file:///etc/passwd" >]>
   <username>&xxe;</username>
</xml>

Other XXE Injection attacks can access local resources that may not stop returning data, possibly impacting application availability and leading to Denial of Service.

CVSS Scores

version 4.0
version 3.1
Expand this section

Snyk

Recommended
8.2 high
  • Attack Vector (AV)
    Network
  • Attack Complexity (AC)
    High
  • Attack Requirements (AT)
    None
  • Privileges Required (PR)
    None
  • User Interaction (UI)
    None
  • Confidentiality (VC)
    High
  • Integrity (VI)
    None
  • Availability (VA)
    None
  • Confidentiality (SC)
    None
  • Integrity (SI)
    None
  • Availability (SA)
    None