Skip to content

Validate log_file paths to prevent arbitrary file writes#3656

Closed
eddieran wants to merge 1 commit intopre-commit:mainfrom
eddieran:fix/log-file-path-validation
Closed

Validate log_file paths to prevent arbitrary file writes#3656
eddieran wants to merge 1 commit intopre-commit:mainfrom
eddieran:fix/log-file-path-validation

Conversation

@eddieran
Copy link
Copy Markdown

Summary

log_file in hook manifests (.pre-commit-hooks.yaml) accepts arbitrary strings and is passed directly to open(logfile_name, 'ab') in output.write_line_b. A malicious remote hook manifest can set log_file to an absolute path (e.g. /etc/cron.d/malicious) or use path traversal (../../../etc/passwd) to write hook output to arbitrary locations on the host filesystem.

Fix

Add path validation for log_file in clientlib.py during manifest schema validation:

  • Reject absolute paths
  • Reject paths that normalize to a parent directory traversal (e.g. ../foo)
  • Empty string (the default) is still allowed

The validation is applied via check_log_file on the MANIFEST_HOOK_DICT schema, which also propagates to CONFIG_HOOK_DICT and LOCAL_HOOK_DICT.

Test plan

  • Added test_check_log_file_valid — verifies relative paths like output.log, logs/hook.log, and empty string pass validation
  • Added test_check_log_file_invalid — verifies absolute paths and .. traversal paths are rejected with cfgv.ValidationError

Fixes #3655

A hook manifest can specify a `log_file` with an absolute path or path
traversal sequences (e.g. `../../../etc/cron.d/malicious`), causing
pre-commit to write hook output to arbitrary locations on the host
filesystem via `output.write_line_b`.

Reject absolute paths and paths that traverse above the working directory
during manifest validation.

Fixes #3655
@asottile
Copy link
Copy Markdown
Member

there's no such thing as a "malicious remote hook" every part of pre-commit is opt in

please don't send ai slop prs

@asottile asottile closed this Apr 11, 2026
@pre-commit pre-commit locked as spam and limited conversation to collaborators Apr 11, 2026
@asottile
Copy link
Copy Markdown
Member

also lol if they're truly evil they ALREADY HAVE ARBITRARY CODE EXECUTION

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Remote hook manifests can write to arbitrary host paths via log_file

2 participants