fix(detect): skip non-regular files so a FIFO can't hang extraction (#2463)
This commit is contained in:
@@ -5,6 +5,7 @@ import json
|
||||
import os
|
||||
import re
|
||||
import shlex
|
||||
import stat
|
||||
import unicodedata
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from enum import Enum
|
||||
@@ -781,12 +782,37 @@ def count_words(path: Path) -> int:
|
||||
return len(docx_to_markdown(path).split())
|
||||
if ext == ".xlsx":
|
||||
return len(xlsx_to_markdown(path).split())
|
||||
# Only regular files may be opened. A repository can contain named
|
||||
# pipes, sockets and device nodes, and `clone <github-url>` exists to
|
||||
# point the scan at trees the operator did not write. open() on a FIFO
|
||||
# with no writer BLOCKS FOREVER — it never raises, so the except below
|
||||
# cannot help, and `graphify update` hangs with no output. os.stat
|
||||
# follows symlinks on purpose: a link pointing at a FIFO blocks exactly
|
||||
# like the FIFO itself.
|
||||
if not stat.S_ISREG(os.stat(_os_path(path)).st_mode):
|
||||
return 0
|
||||
with open(_os_path(path), encoding="utf-8", errors="ignore") as f:
|
||||
return len(f.read().split())
|
||||
except Exception:
|
||||
return 0
|
||||
|
||||
|
||||
def _is_regular_file(path: Path) -> bool:
|
||||
"""True only for regular files (symlinks followed).
|
||||
|
||||
Named pipes, sockets and device nodes must never reach a reader:
|
||||
``open()`` on a FIFO with no writer blocks forever and never raises, so a
|
||||
single ``pipe.py`` in a scanned repository hangs the run with no output.
|
||||
A symlink is resolved deliberately — a link pointing at a FIFO blocks
|
||||
exactly like the FIFO itself. A path that cannot be stat'ed is treated as
|
||||
not readable rather than raising.
|
||||
"""
|
||||
try:
|
||||
return stat.S_ISREG(os.stat(path).st_mode)
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
|
||||
# Directory names to always skip - venvs, caches, build artifacts, deps
|
||||
_SKIP_DIRS = {
|
||||
"venv", ".venv", # "env"/".env"/"*_env" are gated on venv markers below (#2058)
|
||||
@@ -1488,6 +1514,18 @@ def detect(root: Path, *, follow_symlinks: bool | None = None, google_workspace:
|
||||
if not _resolves_under_root(p, root):
|
||||
skipped_sensitive.append(str(p) + " [symlink target outside scan root]")
|
||||
continue
|
||||
if not _is_regular_file(p):
|
||||
# A repository may contain named pipes, sockets and device nodes,
|
||||
# and `clone <github-url>` exists precisely to point the scan at
|
||||
# trees the operator did not write.
|
||||
#
|
||||
# This has to be caught HERE, at the one place where a path is
|
||||
# admitted to the corpus, rather than at each read: the readers
|
||||
# number in the hundreds across the extractors, and open() on a
|
||||
# FIFO with no writer BLOCKS FOREVER — it never raises, so their
|
||||
# try/except cannot help and the whole run hangs with no output.
|
||||
skipped_sensitive.append(str(p) + " [not a regular file]")
|
||||
continue
|
||||
if _is_sensitive(p):
|
||||
skipped_sensitive.append(str(p))
|
||||
continue
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
"""A repository may contain files that are not regular files.
|
||||
|
||||
``clone <github-url>`` exists to point the extractor at trees the operator did
|
||||
not write, and ``watch`` re-runs the scan silently, so the corpus walk has to
|
||||
survive whatever a repository happens to contain.
|
||||
|
||||
The decisive case is a FIFO carrying a source suffix: ``open()`` on a named
|
||||
pipe with no writer blocks forever and never raises, so the ``try``/``except``
|
||||
around every reader cannot help — ``graphify update`` simply never returns and
|
||||
prints nothing. A unix socket fails differently (``ENXIO``) but for the same
|
||||
reason: it is not a regular file.
|
||||
"""
|
||||
import os
|
||||
import socket
|
||||
import stat
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from graphify.detect import _is_regular_file
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def tree():
|
||||
d = tempfile.mkdtemp(prefix="nonregular-")
|
||||
src = Path(d, "src")
|
||||
src.mkdir()
|
||||
(src / "module.py").write_text("def f():\n return 1\n", encoding="utf-8")
|
||||
yield Path(d)
|
||||
|
||||
|
||||
def test_regular_source_file_is_accepted(tree):
|
||||
assert _is_regular_file(tree / "src" / "module.py") is True
|
||||
|
||||
|
||||
def test_fifo_is_rejected(tree):
|
||||
"""The shape that hangs the whole run."""
|
||||
fifo = tree / "src" / "pipe.py"
|
||||
os.mkfifo(fifo)
|
||||
assert stat.S_ISFIFO(os.stat(fifo).st_mode)
|
||||
assert _is_regular_file(fifo) is False
|
||||
|
||||
|
||||
def test_unix_socket_is_rejected(tree):
|
||||
sock_path = tree / "src" / "sock.py"
|
||||
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
try:
|
||||
sock.bind(str(sock_path))
|
||||
assert _is_regular_file(sock_path) is False
|
||||
finally:
|
||||
sock.close()
|
||||
|
||||
|
||||
def test_directory_named_like_a_source_file_is_rejected(tree):
|
||||
d = tree / "src" / "package.py"
|
||||
d.mkdir()
|
||||
assert _is_regular_file(d) is False
|
||||
|
||||
|
||||
def test_symlink_to_a_regular_file_is_accepted(tree):
|
||||
target = tree / "src" / "module.py"
|
||||
link = tree / "src" / "alias.py"
|
||||
link.symlink_to(target)
|
||||
assert _is_regular_file(link) is True
|
||||
|
||||
|
||||
def test_symlink_pointing_at_a_fifo_is_rejected(tree):
|
||||
"""A link to a FIFO blocks exactly like the FIFO, so stat must follow it."""
|
||||
fifo = tree / "src" / "real.py"
|
||||
os.mkfifo(fifo)
|
||||
link = tree / "src" / "link.py"
|
||||
link.symlink_to(fifo)
|
||||
assert _is_regular_file(link) is False
|
||||
|
||||
|
||||
def test_broken_symlink_is_rejected_without_raising(tree):
|
||||
link = tree / "src" / "dangling.py"
|
||||
link.symlink_to(tree / "src" / "does-not-exist.py")
|
||||
assert _is_regular_file(link) is False
|
||||
|
||||
|
||||
def test_missing_path_is_rejected_without_raising(tree):
|
||||
assert _is_regular_file(tree / "src" / "absent.py") is False
|
||||
|
||||
|
||||
def test_char_device_is_rejected():
|
||||
dev = Path("/dev/null")
|
||||
if not dev.exists():
|
||||
pytest.skip("/dev/null unavailable")
|
||||
assert _is_regular_file(dev) is False
|
||||
Reference in New Issue
Block a user