Initial commit: hardened DeerFlow factory
Vendored deer-flow upstream (bytedance/deer-flow) plus prompt-injection hardening: - New deerflow.security package: content_delimiter, html_cleaner, sanitizer (8 layers — invisible chars, control chars, symbols, NFC, PUA, tag chars, horizontal whitespace collapse with newline/tab preservation, length cap) - New deerflow.community.searx package: web_search, web_fetch, image_search backed by a private SearX instance, every external string sanitized and wrapped in <<<EXTERNAL_UNTRUSTED_CONTENT>>> delimiters - All native community web providers (ddg_search, tavily, exa, firecrawl, jina_ai, infoquest, image_search) replaced with hard-fail stubs that raise NativeWebToolDisabledError at import time, so a misconfigured tool.use path fails loud rather than silently falling back to unsanitized output - Native client back-doors (jina_client.py, infoquest_client.py) stubbed too - Native-tool tests quarantined under tests/_disabled_native/ (collect_ignore_glob via local conftest.py) - Sanitizer Layer 7 fix: only collapse horizontal whitespace, preserve newlines and tabs so list/table structure survives - Hardened runtime config.yaml references only the searx-backed tools - Factory overlay (backend/) kept in sync with deer-flow tree as a reference / source See HARDENING.md for the full audit trail and verification steps.
This commit is contained in:
63
backend/packages/harness/deerflow/security/html_cleaner.py
Normal file
63
backend/packages/harness/deerflow/security/html_cleaner.py
Normal file
@@ -0,0 +1,63 @@
|
||||
"""HTML-to-text extraction with security-focused stripping."""
|
||||
|
||||
from html.parser import HTMLParser
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class SecureTextExtractor(HTMLParser):
|
||||
"""Extract visible text while stripping potentially dangerous elements.
|
||||
|
||||
Based on OpenClaw's fetch.sh implementation.
|
||||
"""
|
||||
|
||||
DANGEROUS_TAGS = {
|
||||
'script', 'style', 'noscript',
|
||||
'header', 'footer', 'nav', 'aside',
|
||||
'iframe', 'object', 'embed', 'form',
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.text = []
|
||||
self.skip_depth = 0
|
||||
|
||||
def handle_starttag(self, tag, attrs):
|
||||
if tag in self.DANGEROUS_TAGS:
|
||||
self.skip_depth += 1
|
||||
|
||||
def handle_endtag(self, tag):
|
||||
if tag in self.DANGEROUS_TAGS and self.skip_depth > 0:
|
||||
self.skip_depth -= 1
|
||||
|
||||
def handle_data(self, data):
|
||||
if self.skip_depth == 0:
|
||||
self.text.append(data)
|
||||
|
||||
def get_text(self) -> str:
|
||||
return ' '.join(self.text)
|
||||
|
||||
|
||||
def extract_secure_text(html: str, max_chars: Optional[int] = None) -> str:
|
||||
"""Extract clean text from HTML.
|
||||
|
||||
Args:
|
||||
html: Raw HTML content
|
||||
max_chars: Optional maximum length
|
||||
|
||||
Returns:
|
||||
Clean text without dangerous elements
|
||||
"""
|
||||
extractor = SecureTextExtractor()
|
||||
extractor.feed(html)
|
||||
text = extractor.get_text()
|
||||
|
||||
# Collapse whitespace
|
||||
import re
|
||||
text = re.sub(r'[ \t]+', ' ', text)
|
||||
text = re.sub(r'\n{3,}', '\n\n', text)
|
||||
text = text.strip()
|
||||
|
||||
if max_chars and len(text) > max_chars:
|
||||
text = text[:max_chars-3] + '...'
|
||||
|
||||
return text
|
||||
Reference in New Issue
Block a user