Skip to content

Commit e41829f

Browse files
committed
docs: restrict snippet includes to paths inside the repo root
Mirrors the restrict_base_path default of pymdownx.snippets, so the hook and the extension accept the same set of include paths.
1 parent 8b97885 commit e41829f

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

docs/hooks/llms_txt.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,13 @@ def on_page_markdown(markdown: str, page: Page, config: MkDocsConfig, files: Fil
8282

8383
def include(match: re.Match[str]) -> str:
8484
indent, path = match["indent"], match["path"]
85+
# Mirror the snippets extension's restrict_base_path: reject paths
86+
# that resolve outside the repo root.
87+
resolved_path = (repo_root / path).resolve()
88+
if not resolved_path.is_relative_to(repo_root.resolve()):
89+
raise PluginError(f"llms_txt: snippet path {path!r} in {page.file.src_uri} escapes the repo root")
8590
try:
86-
content = (repo_root / path).read_text(encoding="utf-8").rstrip("\n")
91+
content = resolved_path.read_text(encoding="utf-8").rstrip("\n")
8792
except OSError as exc:
8893
raise PluginError(f"llms_txt: cannot read snippet {path!r} in {page.file.src_uri}") from exc
8994
if indent:

0 commit comments

Comments
 (0)