Skip to content

Commit 07a123e

Browse files
Secure cache directory creation to prevent TOCTOU race conditions (#283)
Modified `Path.mkdir()` calls in `qwen3_embed/common/utils.py` and `qwen3_embed/common/model_management.py` to explicitly set `mode=0o700`. This prevents a Time-of-Check to Time-of-Use (TOCTOU) vulnerability where temporary directories were created with default umask permissions and subsequently restricted using a `chmod` call, leaving a brief window where unauthorized processes could interact with the directory contents. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 3cb68f5 commit 07a123e

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

qwen3_embed/common/model_management.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def _collect_file_metadata(
218218
def _save_file_metadata(cls, model_dir: Path, meta: dict[str, dict[str, int | str]]) -> None:
219219
try:
220220
if not model_dir.exists():
221-
model_dir.mkdir(parents=True, exist_ok=True)
221+
model_dir.mkdir(mode=0o700, parents=True, exist_ok=True)
222222
(model_dir / cls.METADATA_FILE).write_text(json.dumps(meta))
223223
except (OSError, ValueError) as e:
224224
logger.exception(e)
@@ -395,7 +395,7 @@ def retrieve_model_gcs(
395395
if model_tmp_dir.exists():
396396
shutil.rmtree(model_tmp_dir)
397397

398-
cache_tmp_dir.mkdir(parents=True, exist_ok=True)
398+
cache_tmp_dir.mkdir(mode=0o700, parents=True, exist_ok=True)
399399
with contextlib.suppress(OSError):
400400
os.chmod(cache_tmp_dir, 0o700)
401401

qwen3_embed/common/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def define_cache_dir(cache_dir: str | None = None) -> Path:
8585
cache_path = base_path / "qwen3_embed"
8686
else:
8787
cache_path = Path(cache_dir)
88-
cache_path.mkdir(parents=True, exist_ok=True)
88+
cache_path.mkdir(mode=0o700, parents=True, exist_ok=True)
8989

9090
with contextlib.suppress(OSError):
9191
cache_path.chmod(0o700)

0 commit comments

Comments
 (0)