Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions commitizen/commands/check.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
import re
import sys
from pathlib import Path
Expand Down Expand Up @@ -41,6 +42,8 @@ def __init__(self, config: BaseConfig, arguments: CheckArgs, *args: object) -> N
self.commit_msg_file = arguments.get("commit_msg_file")
self.commit_msg = arguments.get("message")
self.rev_range = arguments.get("rev_range")
if self.rev_range is not None:
self.rev_range = os.path.expandvars(self.rev_range)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest limiting the expansion to the two pre-commit ref variables to reduce the attack surface from arbitrary environment variable expansion in branch names, as discussed in the issues around the rev-range argument.

if self.rev_range is not None:
    for var in ("PRE_COMMIT_FROM_REF", "PRE_COMMIT_TO_REF"):
        value = os.environ.get(var)
        if value is None:
            continue

        self.rev_range = self.rev_range.replace(f"${{{var}}}", value)
        self.rev_range = self.rev_range.replace(f"${var}", value)
        self.rev_range = self.rev_range.replace(f"%{var}%", value)

Comment on lines +45 to +46
self.allow_abort = bool(
arguments.get("allow_abort", config.settings["allow_abort"])
)
Expand Down
23 changes: 23 additions & 0 deletions tests/commands/test_check_command.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
from io import StringIO
from typing import TYPE_CHECKING, Any

Expand Down Expand Up @@ -448,3 +449,25 @@ def test_check_command_with_custom_validator_failed(
util.run_cli(
"--name", "cz_custom_validator", "check", "--commit-msg-file", "some_file"
)


@pytest.mark.parametrize(
("rev_range", "expected"),
[
("HEAD~5..HEAD", "HEAD~5..HEAD"),
("$PRE_COMMIT_FROM_REF..$PRE_COMMIT_TO_REF", "v1.0.0..v1.1.0"),
("${PRE_COMMIT_FROM_REF}..${PRE_COMMIT_TO_REF}", "v1.0.0..v1.1.0"),
],
)
def test_check_command_expands_env_vars_in_rev_range(
config, mocker: MockFixture, rev_range: str, expected: str
):
git_get_commits = mocker.patch(
"commitizen.git.get_commits", return_value=_build_fake_git_commits(COMMIT_LOG)
)
mocker.patch.dict(
os.environ,
{"PRE_COMMIT_FROM_REF": "v1.0.0", "PRE_COMMIT_TO_REF": "v1.1.0"},
)
commands.Check(config=config, arguments={"rev_range": rev_range})()
git_get_commits.assert_called_once_with(None, expected)
Loading