Skip to content

fix: strip git name-rev suffix operators from detected branch name#189

Merged
Martin Torp (mtorp) merged 1 commit intomainfrom
fix/branch-name-rev-suffix
Apr 17, 2026
Merged

fix: strip git name-rev suffix operators from detected branch name#189
Martin Torp (mtorp) merged 1 commit intomainfrom
fix/branch-name-rev-suffix

Conversation

@mtorp
Copy link
Copy Markdown
Contributor

@mtorp Martin Torp (mtorp) commented Apr 17, 2026

Summary

In detached-HEAD CI checkouts (Buildkite, CircleCI, Jenkins, and anything else not explicitly detected via GITHUB_REF / CI_COMMIT_BRANCH / BITBUCKET_BRANCH), the CLI falls back to git name-rev --name-only HEAD for branch detection. When the checked-out SHA isn't exactly at a branch tip — typically because a new commit landed on the target branch between pipeline trigger and scan start — name-rev returns output with a suffix operator:

  • remotes/origin/master~1 (one commit behind master)
  • master^0 (alternate form for exact tip)
  • remotes/origin/master~3^2

The existing split('/')[-1] cleanup only strips the remotes/origin/ prefix; the ~N/^N suffix survived and was sent to the Socket API as the branch name. The API then rejected it because ~ and ^ are forbidden characters in Git ref names, producing the error:

Invalid branch name: Branch names must follow Git branch name rules: ... cannot include ... ~^:?*[

The fix strips anything from the first ~ or ^ onward before the prefix split. Both characters are forbidden in Git ref names per check-ref-format(1), so cutting at them can never truncate a real branch name.

Verification

Reproduced end-to-end against a test repo in detached HEAD where git name-rev --name-only HEAD returned remotes/origin/master~1:

Version URL branch param Outcome
PyPI socketsecurity==2.2.81 (unpatched) branch=master~1 400 "Invalid branch name"
This branch branch=master Full scan created

Test plan

  • Unit-level: Git(path).branch returns master when git name-rev outputs remotes/origin/master~1
  • End-to-end: scan against test repo in detached HEAD succeeds
  • Confirmed the unpatched PyPI release exhibits the bug
  • CI checks pass

In detached-HEAD CI checkouts (common in Buildkite/CircleCI/Jenkins/etc.
where none of the GitHub/GitLab/Bitbucket CI env vars are recognized),
the Python CLI falls back to `git name-rev --name-only HEAD` to detect
the current branch. When the checked-out SHA is not exactly at a branch
tip (e.g. master moved forward after the pipeline started), name-rev
returns strings like `remotes/origin/master~1` or `master^0`. The
previous split('/')[-1] cleanup kept the `~N`/`^N` suffix, which the
Socket API then rejected with "Invalid branch name".

Strip anything from the first `~` or `^` onward before the prefix
split. Both characters are forbidden in git ref names per
check-ref-format(1), so truncating at them is always safe.

Assisted-by: Claude Code:opus-4-7
@mtorp Martin Torp (mtorp) requested a review from a team as a code owner April 17, 2026 05:48
@github-actions
Copy link
Copy Markdown

🚀 Preview package published!

Install with:

pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple socketsecurity==2.2.83.dev2

Docker image: socketdev/cli:pr-189

@mtorp Martin Torp (mtorp) merged commit aa1ba53 into main Apr 17, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants