aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Ionite <[email protected]>2023-02-24 18:32:25 -0500
committerGravatar Ionite <[email protected]>2023-02-24 18:32:25 -0500
commit36814de6f7a231c86b356ec27b649da5ffc85c44 (patch)
treea3b2ef8f11f250dd11ef84b4ae20d2dfcc462152
parentTruncate FileAttachment repr to avoid log spam (diff)
Ignore user uploads in output files, default output to `/home`
- Adds ignore of user uploaded files in scanning of output files - Adds ignore of files starting with leading underscores - Changes default output path to `home`
-rw-r--r--snekbox/memfs.py20
-rw-r--r--snekbox/nsjail.py9
2 files changed, 20 insertions, 9 deletions
diff --git a/snekbox/memfs.py b/snekbox/memfs.py
index 259f6f7..34e696e 100644
--- a/snekbox/memfs.py
+++ b/snekbox/memfs.py
@@ -4,7 +4,7 @@ from __future__ import annotations
import logging
import warnings
import weakref
-from collections.abc import Generator
+from collections.abc import Generator, Sequence
from contextlib import suppress
from pathlib import Path
from types import TracebackType
@@ -27,7 +27,7 @@ class MemFS:
instance_size: int,
root_dir: str | Path = "/memfs",
home: str = "home",
- output: str = "output",
+ output: str = "home",
) -> None:
"""
Initialize an in-memory temporary file system.
@@ -97,7 +97,7 @@ class MemFS:
@property
def output(self) -> Path:
"""Path to output directory."""
- return self.home / self._output_name
+ return self.path / self._output_name
def __enter__(self) -> MemFS:
return self
@@ -120,21 +120,28 @@ class MemFS:
folder.chmod(chmod)
return folder
- def files(self, limit: int, pattern: str = "**/*") -> Generator[FileAttachment, None, None]:
+ def files(
+ self, limit: int, pattern: str = "**/*", exclude_files: Sequence[str] = ()
+ ) -> Generator[FileAttachment, None, None]:
"""
Yields FileAttachments for files found in the output directory.
Args:
limit: The maximum number of files to parse.
pattern: The glob pattern to match files against.
+ exclude_files: Files (relative to home directory) to exclude.
"""
count = 0
for file in self.output.rglob(pattern):
+ if str(file.relative_to(self.home)) in exclude_files:
+ log.info(f"Skipping excluded file {file!s}")
+ continue
if count > limit:
log.info(f"Max attachments {limit} reached, skipping remaining files")
break
if file.is_file():
count += 1
+ log.info(f"Found file {file!s}")
yield FileAttachment.from_path(file, relative_to=self.output)
def files_list(
@@ -142,6 +149,7 @@ class MemFS:
limit: int,
pattern: str,
preload_dict: bool = False,
+ exclude_files: Sequence[str] = (),
) -> list[FileAttachment]:
"""
Return a sorted list of file paths within the output directory.
@@ -150,11 +158,11 @@ class MemFS:
limit: The maximum number of files to parse.
pattern: The glob pattern to match files against.
preload_dict: Whether to preload as_dict property data.
-
+ exclude_files: Files (relative to home directory) to exclude.
Returns:
List of FileAttachments sorted lexically by path name.
"""
- res = sorted(self.files(limit, pattern), key=lambda f: f.path)
+ res = sorted(self.files(limit, pattern, exclude_files), key=lambda f: f.path)
if preload_dict:
for file in res:
# Loads the cached property as attribute
diff --git a/snekbox/nsjail.py b/snekbox/nsjail.py
index ba68a84..d425d35 100644
--- a/snekbox/nsjail.py
+++ b/snekbox/nsjail.py
@@ -53,10 +53,10 @@ class NsJail:
read_chunk_size: int = 10_000,
memfs_instance_size: int = 48 * Size.MiB,
memfs_home: str = "home",
- memfs_output: str = "output",
+ memfs_output: str = "home",
files_limit: int | None = 100,
files_timeout: float | None = 8,
- files_pattern: str = "**/*",
+ files_pattern: str = "**/[!_]*",
):
"""
Initialize NsJail.
@@ -258,11 +258,14 @@ class NsJail:
# convert negative exit codes to the `N + 128` form.
returncode = -nsjail.returncode + 128 if nsjail.returncode < 0 else nsjail.returncode
+ # Exclude user uploaded files from output
+ to_exclude = [file.path for file in files]
# Parse attachments with time limit
try:
attachments = timed(
MemFS.files_list,
- (fs, self.files_limit, self.files_pattern, True),
+ (fs, self.files_limit, self.files_pattern),
+ {"preload_dict": True, "exclude_files": to_exclude},
timeout=self.files_timeout,
)
log.info(f"Found {len(attachments)} files.")