-
-
Notifications
You must be signed in to change notification settings - Fork 795
security(context): harden Markdown import against TOCTOU symlink races #932
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
KaifAhmad1
merged 6 commits into
semantica-agi:main
from
lakshanmuruganandam:security/harden-markdown-import-toctou-symlink-races
Aug 14, 2026
+160
−1
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
64a9661
security(context): harden Markdown import against TOCTOU symlink races
lakshanmuruganandam 13c6252
Merge branch 'main' into security/harden-markdown-import-toctou-symli…
Sameer6305 7d2431d
fix(context): harden markdown import security tests
Sameer6305 1e33b2e
Merge branch 'main' into security/harden-markdown-import-toctou-symli…
Sameer6305 5253957
Merge branch 'main' into security/harden-markdown-import-toctou-symli…
KaifAhmad1 83fb1af
docs(changelog): add entry for Markdown import TOCTOU symlink hardening
KaifAhmad1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,9 +59,11 @@ | |
| """ | ||
|
|
||
| import copy | ||
| import errno | ||
| import hashlib | ||
| import os | ||
| import re | ||
| import stat | ||
| import tempfile | ||
| from collections import deque | ||
| from dataclasses import dataclass, field | ||
|
|
@@ -1906,7 +1908,49 @@ def _import_markdown_payload( | |
|
|
||
| return memories | ||
|
|
||
| def _read_markdown_file_content(self, file_path: Path) -> str: | ||
| if file_path.is_symlink(): | ||
| raise ValueError(f"Symlink Markdown import paths are rejected: {file_path}") | ||
|
|
||
| flags = os.O_RDONLY | ||
| if hasattr(os, "O_NOFOLLOW"): | ||
| # On POSIX, O_NOFOLLOW makes os.open() fail with ELOOP if the | ||
| # final path component is a symlink, atomically closing the TOCTOU | ||
| # window between the is_symlink() check above and the open call. | ||
| # On Windows, O_NOFOLLOW is not available; the is_symlink() pre-check | ||
| # above is the only symlink defense and remains vulnerable to a narrow | ||
| # race. The fstat()/S_ISREG guard below still rejects special files | ||
| # (FIFOs, devices) on both platforms. | ||
| flags |= os.O_NOFOLLOW | ||
|
|
||
| try: | ||
| fd = os.open(str(file_path), flags) | ||
| except OSError as exc: | ||
| if exc.errno == getattr(errno, "ELOOP", None): | ||
| raise ValueError( | ||
| f"Symlink Markdown import paths are rejected: {file_path}" | ||
| ) from exc | ||
| raise | ||
|
|
||
| try: | ||
| stat_res = os.fstat(fd) | ||
| if not stat.S_ISREG(stat_res.st_mode): | ||
| raise ValueError( | ||
| f"Markdown import path is not a regular file: {file_path}" | ||
| ) | ||
| with open(fd, "r", encoding="utf-8", closefd=True) as f: | ||
| return f.read() | ||
| except Exception: | ||
| try: | ||
| os.close(fd) | ||
| except OSError: | ||
| pass | ||
| raise | ||
|
|
||
| def _read_markdown_path(self, path: Path) -> List[Tuple[str, str]]: | ||
| if path.is_symlink(): | ||
| raise ValueError(f"Symlink Markdown import paths are rejected: {path}") | ||
|
|
||
| if not path.exists(): | ||
| raise FileNotFoundError(f"Markdown import path does not exist: {path}") | ||
|
|
||
|
|
@@ -1916,6 +1960,7 @@ def _read_markdown_path(self, path: Path) -> List[Tuple[str, str]]: | |
| file_path | ||
| for file_path in path.iterdir() | ||
| if file_path.is_file() | ||
| and not file_path.is_symlink() | ||
| and file_path.suffix.lower() in self._MARKDOWN_EXTENSIONS | ||
|
Comment on lines
1961
to
1964
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3. Symlink entries silently ignored When importing a directory, _read_markdown_path() filters out symlink entries (`and not file_path.is_symlink()`), so symlinked Markdown files are skipped instead of being rejected with the same actionable error as single-file imports. This can cause partial imports without any user-visible signal that some Markdown files were refused. Agent Prompt
|
||
| ), | ||
| key=lambda file_path: (file_path.name.casefold(), file_path.name), | ||
|
|
@@ -1926,7 +1971,7 @@ def _read_markdown_path(self, path: Path) -> List[Tuple[str, str]]: | |
| raise ValueError(f"Markdown import path is not a file or directory: {path}") | ||
|
|
||
| return [ | ||
| (str(file_path), file_path.read_text(encoding="utf-8")) | ||
| (str(file_path), self._read_markdown_file_content(file_path)) | ||
| for file_path in file_paths | ||
| ] | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Toctou possible without o_nofollow
📎 Requirement gap⛨ SecurityAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools