Skip to content

Turn any technical book PDF into a Claude Code skill — ready to study, reference, and use while you work.

fix(security): harden the extraction work directory against symlink and ownership attacks (#141)

Johnny chongc4c5e948caaa
diff --git a/book_to_skill/utils.py b/book_to_skill/utils.py
index e40ea7c..a04adda 100644
--- a/book_to_skill/utils.py
+++ b/book_to_skill/utils.py
@@ -723,6 +723,32 @@ def extract_single_file(input_path: Path, extraction_mode: str, install_mode: st
}
+def prepare_output_dir(path: Path) -> None:
+ """Create the work directory, guarding against two shared-tmp risks:
+ a pre-planted symlink at a predictable path, and reusing a directory
+ another user already owns (either could expose or tamper with the
+ extracted document text, which may be sensitive).
+ """
+ if path.is_symlink():
+ raise ExtractionError(
+ f"Refusing to use {path}: it is a symbolic link, not a real "
+ "directory. Remove it or set BOOK_SKILL_WORKDIR to a private path."
+ )
+ if path.exists():
+ if not path.is_dir():
+ raise ExtractionError(f"Refusing to use {path}: it exists and is not a directory.")
+ if hasattr(os, "getuid"):
+ owner_uid = path.stat().st_uid
+ if owner_uid != os.getuid():
+ raise ExtractionError(
+ f"Refusing to use {path}: it is owned by a different user "
+ f"(uid {owner_uid}). Set BOOK_SKILL_WORKDIR to a private directory."
+ )
+ os.chmod(path, 0o700)
+ else:
+ path.mkdir(parents=True, mode=0o700)
+
+
def print_banner() -> None:
"""Print the attribution banner. Done here (not only in SKILL.md) so it
shows on every run regardless of how the agent invokes extraction."""
@@ -773,7 +799,7 @@ def main():
print(f"ERROR: No supported files found matching: {', '.join(raw_input_paths)}", file=sys.stderr)
sys.exit(1)
- OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
+ prepare_output_dir(OUTPUT_DIR)
extracted_sources = []
combined_texts = []
diff --git a/tests/test_output_dir_security.py b/tests/test_output_dir_security.py
new file mode 100644
index 0000000..ddc7262
--- /dev/null
+++ b/tests/test_output_dir_security.py
@@ -0,0 +1,56 @@
+import os
+import stat
+
+import pytest
+
+from book_to_skill.exceptions import ExtractionError
+from book_to_skill.utils import prepare_output_dir
+
+
+def test_prepare_output_dir_creates_dir_with_restrictive_permissions(tmp_path):
+ target = tmp_path / "work"
+
+ prepare_output_dir(target)
+
+ assert target.is_dir()
+ assert stat.S_IMODE(target.stat().st_mode) == 0o700
+
+
+def test_prepare_output_dir_rejects_symlink(tmp_path):
+ real_dir = tmp_path / "real"
+ real_dir.mkdir()
+ link = tmp_path / "work"
+ link.symlink_to(real_dir)
+
+ with pytest.raises(ExtractionError, match="symbolic link"):
+ prepare_output_dir(link)
+
+
+def test_prepare_output_dir_rejects_non_directory(tmp_path):
+ target = tmp_path / "work"
+ target.write_text("not a directory")
+
+ with pytest.raises(ExtractionError, match="not a directory"):
+ prepare_output_dir(target)
+
+
+def test_prepare_output_dir_tightens_permissions_on_existing_own_dir(tmp_path):
+ target = tmp_path / "work"
+ target.mkdir()
+ os.chmod(target, 0o777) # simulate a pre-existing, overly-permissive dir
+
+ prepare_output_dir(target)
+
+ assert stat.S_IMODE(target.stat().st_mode) == 0o700
+
+
[email protected](not hasattr(os, "getuid"), reason="POSIX-only ownership check")
+def test_prepare_output_dir_rejects_directory_owned_by_another_user(tmp_path, monkeypatch):
+ target = tmp_path / "work"
+ target.mkdir()
+
+ real_getuid = os.getuid
+ monkeypatch.setattr(os, "getuid", lambda: real_getuid() + 1)
+
+ with pytest.raises(ExtractionError, match="owned by a different user"):
+ prepare_output_dir(target)