Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Update output validation #1181

davidmezzettia0857e980a0f
diff --git a/src/python/txtai/pipeline/data/safeopen.py b/src/python/txtai/pipeline/data/safeopen.py
index af95520c..d16b1403 100644
--- a/src/python/txtai/pipeline/data/safeopen.py
+++ b/src/python/txtai/pipeline/data/safeopen.py
@@ -18,12 +18,13 @@ class SafeOpen:
- URLs must be public URLs (also has checks for redirects and DNS rebind attempts)
"""
- def __init__(self, headers=None, safeopen=False):
+ def __init__(self, headers=None, safeopen=False, allowurl=True):
"""
Creates a new safeopen instance.
Args:
safeopen: if safe validation checks should be enabled
+ allowurl: if urls should be allowed
"""
# HTTP headers
@@ -35,6 +36,9 @@ class SafeOpen:
# URL retriever
self.urlretrieve = URLRetrieve(self.headers, self.safeopen)
+ # Enable URL validation
+ self.allowurl = allowurl
+
def valid(self, path):
"""
Checks if path is a valid local file or web url. Returns path if valid along with a flag
@@ -96,7 +100,7 @@ class SafeOpen:
valid = True
if self.safeopen:
- if os.path.exists(url):
+ if os.path.exists(url) or not self.allowurl:
# Validate local file is in safe path
path = os.path.realpath(url)
prefix = os.path.commonpath([self.safeopen, path])
diff --git a/src/python/txtai/workflow/task/retrieve.py b/src/python/txtai/workflow/task/retrieve.py
index 9285f784..cfad597d 100644
--- a/src/python/txtai/workflow/task/retrieve.py
+++ b/src/python/txtai/workflow/task/retrieve.py
@@ -40,14 +40,15 @@ class RetrieveTask(Task):
self.directory = directory
self.flatten = flatten
- self.safeopen = SafeOpen(headers, safeopen)
+ self.safeinput = SafeOpen(headers, safeopen)
+ self.safeoutput = SafeOpen(safeopen=directory if safeopen else safeopen, allowurl=False)
def prepare(self, element):
# Extract file path from URL
path = urlparse(element).path
# Validate input element
- url, _ = self.safeopen.valid(element)
+ url, _ = self.safeinput.valid(element)
if url:
if self.flatten:
# Flatten directory structure (default)
@@ -59,7 +60,7 @@ class RetrieveTask(Task):
directory = os.path.dirname(path)
# Validate output path
- self.safeopen.valid(path)
+ self.safeoutput.valid(path)
# Create output directory, if necessary
if directory:
@@ -67,7 +68,7 @@ class RetrieveTask(Task):
os.makedirs(directory, exist_ok=True)
# Retrieve data
- data = self.safeopen.retrieve(url)
+ data = self.safeinput.retrieve(url)
# Write to destination path
with open(path, "wb") as output: