Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

fix(filesystem): escape plain text in generated PDFs

primorLeea6846b698a04
diff --git a/browser_use/filesystem/file_system.py b/browser_use/filesystem/file_system.py
index 097eca84c..1eb5b1103 100644
--- a/browser_use/filesystem/file_system.py
+++ b/browser_use/filesystem/file_system.py
@@ -1,6 +1,7 @@
import asyncio
import base64
import csv
+import html
import io
import os
import re
@@ -264,15 +265,16 @@ class PdfFile(BaseFile):
for line in content_lines:
if line.strip():
+ escaped_line = html.escape(line)
# Handle basic markdown headers
if line.startswith('# '):
- para = Paragraph(line[2:], styles['Title'])
+ para = Paragraph(escaped_line[2:], styles['Title'])
elif line.startswith('## '):
- para = Paragraph(line[3:], styles['Heading1'])
+ para = Paragraph(escaped_line[3:], styles['Heading1'])
elif line.startswith('### '):
- para = Paragraph(line[4:], styles['Heading2'])
+ para = Paragraph(escaped_line[4:], styles['Heading2'])
else:
- para = Paragraph(line, styles['Normal'])
+ para = Paragraph(escaped_line, styles['Normal'])
story.append(para)
else:
story.append(Spacer(1, 6))
diff --git a/tests/ci/infrastructure/test_filesystem.py b/tests/ci/infrastructure/test_filesystem.py
index d8196f379..99aa1223c 100644
--- a/tests/ci/infrastructure/test_filesystem.py
+++ b/tests/ci/infrastructure/test_filesystem.py
@@ -5,6 +5,7 @@ import tempfile
from pathlib import Path
import pytest
+from pypdf import PdfReader
from browser_use.filesystem.file_system import (
DEFAULT_FILE_SYSTEM_PATH,
@@ -222,6 +223,17 @@ class TestFileSystem:
except Exception:
pass
+ async def test_write_pdf_preserves_plain_text_angle_brackets(self, empty_filesystem):
+ """PDF content should be rendered as plain text, not ReportLab markup."""
+ content = 'Comparison: 2 <b y & 5 > 4'
+
+ result = await empty_filesystem.write_file('comparison.pdf', content)
+
+ assert result == 'Data written to file comparison.pdf successfully.'
+ pdf_path = empty_filesystem.data_dir / 'comparison.pdf'
+ extracted_text = '\n'.join(page.extract_text() or '' for page in PdfReader(pdf_path).pages)
+ assert content in extracted_text
+
def test_filesystem_initialization(self, temp_filesystem):
"""Test FileSystem initialization with default files."""
fs = temp_filesystem