Implementation:AI4Finance Foundation FinRobot sanitize untrusted block
| Knowledge Sources | |
|---|---|
| Domains | Security, Prompt_Injection_Defense, NLP |
| Last Updated | 2026-10-18 00:00 GMT |
Overview
Concrete tool for sanitizing attacker-controllable external text before injection into an LLM prompt, neutralizing prompt-injection vectors while preserving legitimate content, provided by the FinRobot coordinators layer.
Description
The sanitize_untrusted_text and sanitize_untrusted_block functions are the single choke points for flattening external text (news headlines, SEC filing excerpts) before interpolation into an LLM prompt. They address BUG-087: a malicious news title or filing excerpt could inject a new instruction line or fake a structural delimiter.
sanitize_untrusted_text (single-line):
- Strips angle-bracket tags (neutralizes fake XML tags)
- Removes leading markdown headings (neutralizes fake prompt sections)
- Strips control chars
- Collapses whitespace to a single line
- Bounds length (default 500 chars)
sanitize_untrusted_block (multi-line):
- Same escape-vector removal as the single-line variant
- Preserves newlines/paragraphs (a 10-K excerpt's value IS its prose)
- Bounds length (default 8,000 chars)
Both functions produce content meant to be wrapped in an explicit <untrusted_*> block by the caller — the sanitizer makes the content inert; the wrapper marks it as data.
Usage
- SEC_RAG_QA workflow (Step 3): Filing excerpts are sanitized via sanitize_untrusted_block and source labels via sanitize_untrusted_text before LLM prompt assembly.
- Equity_Research / IC_Memo workflows: News headlines are sanitized via sanitize_untrusted_text (via render_news_for_prompt).
- Import this when you need to safely inject external/untrusted text into an LLM prompt.
Code Reference
Source Location
- Repository: FinRobot
- File: finrobot_desktop/finrobot/engine/compute/coordinators/news.py
Signature
def sanitize_untrusted_text(text: str, *, max_len: int = 500) -> str:
"""Flatten attacker-controllable external text to a single safe prompt line.
Strips control chars / newlines, neutralizes fake XML tags and leading
markdown headings, collapses whitespace, and bounds length — so a
malicious news title can no longer inject a new instruction line or
fake a structural delimiter when interpolated into an LLM prompt (BUG-087).
The result is still meant to be wrapped in an explicit <untrusted_*> block
by the caller; this function makes the *content* inert, the wrapper
marks it data.
"""
def sanitize_untrusted_block(text: str, *, max_len: int = 8_000) -> str:
"""Multi-line variant of sanitize_untrusted_text for document excerpts.
A 10-K excerpt's value IS its prose, so unlike the headline sanitizer this
keeps newlines/paragraphs. It still removes every escape vector: angle-
bracket tags, leading markdown headings, non-newline control chars, and
unbounded length. Callers wrap the result in an explicit <untrusted_*>
block.
"""
Import
from finrobot.engine.compute.coordinators.news import (
sanitize_untrusted_text,
sanitize_untrusted_block,
)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| text | str | Yes | Raw external/untrusted text (news headline, filing excerpt) |
| max_len | int | No | Truncation limit (text: 500, block: 8,000) |
Outputs
| Name | Type | Description |
|---|---|---|
| cleaned | str | Sanitized text safe for LLM prompt injection (to be wrapped in <untrusted_*> by caller) |
Usage Examples
Sanitize Filing Excerpt
from finrobot.engine.compute.coordinators.news import (
sanitize_untrusted_text,
sanitize_untrusted_block,
)
# Source label (single line)
label = sanitize_untrusted_text(chunk.source or f"Chunk {chunk.chunk_index}", max_len=120)
# Filing excerpt (multi-line, preserves paragraphs)
excerpt = sanitize_untrusted_block(chunk.text)
# Assemble context with untrusted wrapper
context = (
f"[Excerpt 1] (Source: {label})\n"
f"<untrusted_filing_excerpt>\n{excerpt}\n</untrusted_filing_excerpt>"
)