CVE-2025-49142
Jinja2 template injection allows secret exposure/data modification in Nautobot.
- CVSS 6.0
- CWE-1336
- Configuration Issues
- Remote
Nautobot is a Network Source of Truth and Network Automation Platform. All users of Nautobot versions prior to 2.4.10 or prior to 1.6.32 are potentially affected. Due to insufficient security configuration of the Jinja2 templating feature used in computed fields, custom links, etc. in Nautobot, a malicious user could configure this feature set in ways that could expose the value of Secrets defined in Nautobot when the templated content is rendered or that could call Python APIs to modify data within Nautobot when the templated content is rendered, bypassing the object permissions assigned to the viewing user. Nautobot versions 1.6.32 and 2.4.10 will include fixes for the vulnerability. The vulnerability can be partially mitigated by configuring object permissions appropriately to limit certain actions to only trusted users.
- CWE
- CWE-1336
- CVSS base score
- 6.0
- Published
- 2025-06-10
- OWASP
- A03 Injection
- Orthogonal defect classification
- Interface
- Code defect classification
- Incorrect Functionality
- Category
- Configuration Issues
- Subcategory
- Security Misconfigurations
- Accessibility scope
- Remote
- Impact
- Information Disclosure
- Affected component
- Nautobot
- Fixed by upgrading
- Yes
Solution
Upgrade to Nautobot version 2.4.10 or 1.6.32.
Vulnerable code sample
from jinja2 import Environment
def render_template_unsafe(template_string, context):
"""
Renders a Jinja2 template string with a given context.
This function is vulnerable because it does not restrict Jinja2's environment.
"""
env = Environment() # Unrestricted Jinja2 environment
template = env.from_string(template_string)
return template.render(context)
# Example Usage demonstrating potential issues:
if __name__ == '__main__':
# Potential Secret Exposure
context = {'secret': 'ThisIsASecretValue'}
template_string = "{{ secret }}"
rendered_output = render_template_unsafe(template_string, context)
print(f"Secret Exposure Example: {rendered_output}")
# Potential Code Execution (Simulated, as direct code execution is difficult to represent safely)
# A real-world exploit might involve calling methods that modify Nautobot data
# However, directly simulating this is problematic for security reasons.
# Instead, we demonstrate the ability to access potentially sensitive attributes.
class ExampleObject:
def __init__(self):
self.sensitive_attribute = "Sensitive Info"
def get_sensitive_data(self):
return "Another piece of sensitive information"
obj = ExampleObject()
context = {'obj': obj}
template_string = "{{ obj.sensitive_attribute }} and also {{ obj.get_sensitive_data() }}" # Accessing attributes
rendered_output = render_template_unsafe(template_string, context)
print(f"Attribute Access Example: {rendered_output}")
# In a real-world vulnerable Nautobot instance, this unrestricted access could be used
# to access database credentials, API keys, or call methods that modify network configurations
# bypassing intended access controls. The vulnerability allowed carefully crafted templates
# to execute arbitrary Python code within the Nautobot server process itself.Patched code sample
import jinja2
import secrets
class NautobotObject:
"""
Represents a Nautobot object with data and permissions.
"""
def __init__(self, data, permissions):
self.data = data
self.permissions = permissions
def get_data(self, user):
"""
Returns data only if the user has permission to view it.
"""
if "view" in self.permissions.get(user, []):
return self.data
else:
return "Permission Denied"
def render_template(template_string, context):
"""
Renders a Jinja2 template with a restricted environment.
"""
# Create a restricted environment. This is the core mitigation.
# It is based on suggestions from Jinja2 documentation and best practices.
# Crucially, it removes access to potentially dangerous global functions.
env = jinja2.Environment(
loader=jinja2.BaseLoader(),
autoescape=True, # Enable autoescaping to prevent XSS
extensions=['jinja2.ext.do'], # Enable 'do' extension for variable assignment
# Add additional filters here as needed and after careful review.
)
env.globals['get_secret'] = safe_get_secret #Add a safe way to expose secrets and only to authorized user.
template = env.from_string(template_string)
return template.render(context)
def safe_get_secret(secret_name, user):
"""
Safely retrieves a secret only if the user has explicit permission.
"""
if user == "admin": #check if the user has admin privilleges
if secret_name in secrets.secrets:
return secrets.secrets[secret_name]
else:
return "Secret not found"
else:
return "Permission Denied for getting secret"
# Example usage demonstrating a SAFE implementation.
if __name__ == '__main__':
secrets.secrets = {"db_password": "supersecretpassword"} #Mock the database
user_data = {"username": "alice", "email": "alice@example.com"}
permissions = {"alice": ["view", "edit"], "bob":["view"]}
nautobot_object = NautobotObject(data=user_data, permissions=permissions)
#Example 1: Attempt to expose a secret unsafely (mitigated)
template_string_unsafe = "Username: {{ user_data.username }}, Secret: {{ get_secret('db_password', 'bob') }}"
context = {"user_data": nautobot_object.get_data("bob")}
rendered_output_unsafe = render_template(template_string_unsafe, context)
print(f"Unsafe Template Rendered for Bob: {rendered_output_unsafe}") # Output: Permission Denied
#Example 2: Admin Exposing a secret safely.
template_string_safe = "Username: {{ user_data.username }}, Secret: {{ get_secret('db_password', 'admin') }}"
context = {"user_data": nautobot_object.get_data("alice")}
rendered_output_safe = render_template(template_string_safe, context)
print(f"Safe Template Rendered for Admin: {rendered_output_safe}") #Output: Username: alice, Secret: supersecretpasswordPublished fix
Public sources link this CVE to the changes below. The code samples above were generated to illustrate the defect; these links lead to the code the project actually changed.
- Add `@unsafe`, `.alters_data`, and/or `.do_not_call_in_templates` to various model methods (nautobot/nautobot)
nautobot/core/models/__init__.py,nautobot/dcim/models/devices.py,nautobot/dcim/models/device_components.py,nautobot/dcim/models/device_component_templates.py,nautobot/extras/models/customfields.py,nautobot/extras/models/datasources.py,nautobot/extras/models/groups.py,nautobot/extras/models/jobs.py,nautobot/extras/models/metadata.py,nautobot/extras/models/models.py,nautobot/extras/models/secrets.py,nautobot/extras/models/secrets.py · Secret.get_valueand 9 more - [1.6] Add @unsafe, .alters_data, and/or .do_not_call_in_templates as applicable (nautobot/nautobot)
nautobot/core/models/__init__.py,nautobot/dcim/models/devices.py,nautobot/dcim/models/device_components.py,nautobot/dcim/models/device_component_templates.py,nautobot/extras/models/customfields.py,nautobot/extras/models/datasources.py,nautobot/extras/models/groups.py,nautobot/extras/models/jobs.py,nautobot/extras/models/models.py,nautobot/extras/models/secrets.py,nautobot/extras/models/secrets.py · SecretsGroup.get_secret_value,nautobot/extras/secrets/__init__.pyand 1 more
Payload
{{ secrets.my_secret }}
{{ request.user.is_superuser = True }}
Cite this entry
@misc{vaitp:cve202549142,
title = {{Jinja2 template injection allows secret exposure/data modification in Nautobot.}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2025},
note = {VAITP Python Vulnerability Dataset, entry CVE-2025-49142},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2025-49142/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.
The supreme art of war is to subdue the enemy without fighting.
Sun Tzu – “The Art of War”
:: Shaping the future through research and ingenuity ::
