Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Decode local file URIs before SafeOpen validation (#1524)

HuaTNA299bc0903e41
diff --git a/src/python/txtai/pipeline/data/safeopen.py b/src/python/txtai/pipeline/data/safeopen.py
index d16b1403..7550914f 100644
--- a/src/python/txtai/pipeline/data/safeopen.py
+++ b/src/python/txtai/pipeline/data/safeopen.py
@@ -6,6 +6,7 @@ import os
import tempfile
from urllib.parse import urlparse
+from urllib.request import url2pathname
from .urlretrieve import URLRetrieve
@@ -51,8 +52,8 @@ class SafeOpen:
(path, exists)
"""
- # Convert file urls to local paths
- path = path.replace("file://", "")
+ # Decode file URIs while preserving literal percent sequences in plain paths.
+ path = url2pathname(path[7:]) if path.startswith("file://") else path
# Check if this is a local file path or local file url
exists = os.path.exists(path)
diff --git a/test/python/testpipeline/testdata/testtextractor.py b/test/python/testpipeline/testdata/testtextractor.py
index 144f1d69..2f2c8d9b 100644
--- a/test/python/testpipeline/testdata/testtextractor.py
+++ b/test/python/testpipeline/testdata/testtextractor.py
@@ -6,6 +6,7 @@ import os
import tempfile
import unittest
+from pathlib import Path
from unittest.mock import patch
from urllib.error import URLError
@@ -67,6 +68,19 @@ class TestTextractor(unittest.TestCase):
# Check number of sections is as expected
self.assertEqual(len(sections), 2)
+ def testFileURI(self):
+ """
+ Test encoded file URIs and plain paths with literal percent sequences.
+ """
+
+ with tempfile.TemporaryDirectory() as directory:
+ path = Path(directory) / "notes %20 #1.html"
+ path.write_text("<html><body><p>research note</p></body></html>", encoding="utf-8")
+ textractor = Textractor(backend=None)
+
+ self.assertEqual(textractor(str(path)), "research note")
+ self.assertEqual(textractor(path.as_uri()), "research note")
+
def testDownloadError(self):
"""
Test failed remote retrieval leaves no temporary file