Skip to content

fix(repo-stats): security hardening from review - #8

Merged
g-husam merged 4 commits into
mainfrom
repo-stats-security-hardening
Oct 28, 2026
Merged

g-husam merged 4 commits into
mainfrom
repo-stats-security-hardening

Conversation

@g-husam

@g-husam g-husam commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

Security hardening for repo-stats, from the security review of repo-stats/ (findings S1–S6 and nits N1–N11). Rebased on main with #5, #6 and #7. Where #5 already covered something, this PR builds on it and doesn't redo it.

Findings

ID Status What changed Tests
S1 outputs follow symlinks done New write_output(): -o, --json and --csv go to a mkstemp file in the same directory, then os.replace moves it into place. An existing symlink (live or dangling) at the output path is refused with a clear error. The new file gets the usual umask permissions. The CI examples write outside the analysed checkout: $RUNNER_TEMP on GitHub; on GitLab, a directory the job itself creates, because GitLab only collects artifacts from inside the project dir. I1.test_write_output_refuses_symlinks, test_write_output_is_atomic, test_cli_refuses_symlinked_outputs
S2 Windows cwd lookup done New find_executable() resolves git, gh and glab to absolute paths and skips empty, . and relative PATH entries, so it never uses shutil.which, which on Windows with Python before 3.12 searches the cwd first. On Windows, child processes also get NoDefaultCurrentDirectoryInExePath=1. git still runs with -C <abs repo>. A separate safe cwd for git was not added: with an absolute exe path and -C, it adds nothing. I1.test_find_executable_skips_current_and_relative_dirs
S3 CI docs partly covered by #5 #5 already pins the tool checkout/fetch to a commit SHA. This PR adds persist-credentials: false on both actions/checkout steps, checks the analysed repo out into repo/ so the cwd and outputs are not inside it, and pins the GitLab image as tag@sha256:<digest> with how to look the digest up (docker buildx imagetools inspect / crane digest). It also notes that the actions themselves can be SHA-pinned. I used placeholders, not a real digest: I could not check the current image digest from here. docs
S4 CLI env host overrides done The gh/glab env is built from an allowlist (CLI_ENV_ALLOW + CI_SERVER_*, XDG_*, REPO_STATS_FAKE_*). It keeps tokens, CI autologin, config, keyring (DBUS_SESSION_BUS_ADDRESS), proxy, TLS CA/client-cert, locale and Windows essentials. It drops GH_HOST, GH_REPO, GITLAB_HOST, GL_HOST, GITLAB_URI, GITLAB_API_HOST, GLAB_API_PROTOCOL/API_PROTOCOL, GLAB_SKIP_TLS_VERIFY and debug switches. I checked this list against gh help environment (gh 2.103) and the glab env var docs. Current glab docs say an env token goes only to the host in GITLAB_HOST/config, so GITLAB_HOST is now set to the --hostname host for glab. That keeps self-managed GitLab with GITLAB_TOKEN working. REPO_STATS_FAKE_* is allowed only so the test suite's fake CLIs still get their mock URL and log path (there is a comment saying so); real CLIs ignore it. I1.test_cli_environment_is_an_allowlist, test_cli_never_sees_host_overrides_end_to_end; existing G1/G2 tests unchanged
S5 git env / README claim done git's env is an allowlist (GIT_ENV_ALLOW: PATH, HOME, user, locale, temp, Windows essentials, XDG_CONFIG_HOME, GIT_CONFIG_GLOBAL/SYSTEM/NOSYSTEM). git runs with -c core.fsmonitor=false, and the numstat log adds --no-textconv --no-ext-diff. The README now says that stripping credentials prevents accidental leaks but is not a boundary against code running as you, and recommends read-only, short-lived tokens. G1.test_git_never_sees_credential_variables (rewritten for the allowlist), I1.test_git_argv_is_hardened
S6 partial clones done git runs with GIT_NO_LAZY_FETCH=1 (git 2.44+) and also -c protocol.allow=never (blocks the lazy fetch on older git). is_partial_clone() checks extensions.partialClone / remote.*.promisor with git config --get-regexp, a read-only call (the read-only test and README list now include it). A partial clone prints a warning, and if missing objects make the scan fail, the error explains why. I1.test_partial_clone_warns_and_never_fetches (a real blobless clone; checks that no object was fetched)
N1 userinfo in remote done parse_remote never reads a URL with a scheme as scp-style, and drops everything up to the last @ before the host. https://user:TOKEN@github.com (no path) now gives None. I1.test_parse_remote_drops_user_info, test_token_in_pathless_remote_is_not_in_the_output
N2 $ anchors done HOSTNAME_RE and GITLAB_READ_ENDPOINT use \Z. HOSTNAME_RE rejects .., and the endpoint takes ASCII [0-9] only. I1.test_security_regexes_match_the_whole_string
N3 dot segments done GITLAB_READ_ENDPOINT rejects projects/./… and projects/../…. same
N4 pagination done MAX_LIST_PAGES (2000) caps the top-level GitHub and GitLab loops. next_page() requires a plain positive number above the current page (also used in the per-MR pages()). A GraphQL cursor that is missing or repeats raises an error. I1.test_next_page_must_be_a_plain_increasing_number, test_gitlab_repeating_next_page_stops, test_github_repeating_cursor_and_page_cap
N5 lone surrogates done Outputs are written with errors="backslashreplace". Inside the dashboard JSON this decodes back to the same string; --json already used ensure_ascii. I1.test_lone_surrogates_are_written
N6 option-like refs done A branch/ref that starts with - is refused (works on any git version). Revisions also follow --end-of-options on git ≥ 2.30, the first version where rev-parse accepts it (log has accepted it since 2.24); older git skips it. remote get-url gets --. I1.test_refs_starting_with_dash_are_refused, test_end_of_options_depends_on_git_version
N7 --serve headers done The header CSP adds frame-ancestors 'none', plus Cross-Origin-Resource-Policy: same-origin and Cross-Origin-Opener-Policy: same-origin. The meta CSP in the template is unchanged (frame-ancestors does not work there). I1.test_serve_sends_framing_and_cross_origin_headers
N8 CLI cwd covered by #5, plus one addition #5 already runs the CLIs from a private 0700 TemporaryDirectory. This PR only adds GIT_CEILING_DIRECTORIES=<temp dir>, so git, if a CLI runs it, cannot find a .git planted one level up in a shared /tmp. G1.test_every_call_is_read_only_explicit_host_and_outside_the_repo (one extra assertion)
N9 update notifiers done The CLIs get GH_NO_UPDATE_NOTIFIER=1, GH_PROMPT_DISABLED=1, GH_TELEMETRY=false (documented in gh 2.103), GLAB_CHECK_UPDATE=false, GLAB_NO_PROMPT=true, and GLAB_SEND_TELEMETRY=false as before. I1.test_cli_environment_is_an_allowlist
N10 inherited GIT_* done Covered by the git and CLI allowlists (GIT_DIR, GIT_WORK_TREE, GIT_CONFIG_PARAMETERS, … are dropped). G1.test_git_never_sees_credential_variables
N11 regex from PR number done re.escape(str(pr["number"])). I1.test_pr_number_is_escaped_in_regex

Not done / notes

  • S2: "refuse an executable found inside the analysed repo" is not implemented. With relative and . entries skipped, that would only matter if the user's own PATH named an absolute directory inside the repo.
  • S1: only the last path component is protected. If -o some/dir/file is used and some/dir is a symlink inside an untrusted checkout, the write still follows it. The CI examples avoid this by writing outside the checkout.
  • glab's documented GLAB_CHECK_UPDATE env override is set to false. I could not run a real glab here to confirm it disables the check; at worst the setting has no effect.

Testing

…d environments

- Resolve git, gh and glab to absolute paths, skipping empty, "." and relative
  PATH entries (on Windows a bare name also searches the current directory).
- Build git's environment from an allowlist (drops credentials, GIT_DIR,
  GIT_WORK_TREE, GIT_CONFIG_PARAMETERS, ...), set GIT_NO_LAZY_FETCH=1 and run
  git with core.fsmonitor=false and protocol.allow=never; the numstat log
  uses --no-textconv and --no-ext-diff.
- Build the gh/glab environment from an allowlist: host and repo overrides
  (GH_HOST, GITLAB_HOST, GL_HOST, GITLAB_URI, GITLAB_API_HOST, ...) and TLS
  switches never reach the CLI; glab's GITLAB_HOST is set to the --hostname
  host. Turn off update checks, prompts and gh telemetry, and stop git
  discovery above the private CLI directory (GIT_CEILING_DIRECTORIES).
- Warn on partial clones and explain the failure when objects are missing.
- Refuse refs that start with "-" and pass revisions after --end-of-options
  on git 2.30+.
…-serve headers

- parse_remote drops any user info before the host and never reads a URL
  with a scheme as scp-style, so a path-less https://user:TOKEN@host remote
  cannot put the token into the dashboard name.
- HOSTNAME_RE and GITLAB_READ_ENDPOINT match the whole string (\Z, not $),
  reject empty host labels, "." and ".." project segments and non-ASCII digits.
- Cap top-level GitHub and GitLab pagination, require X-Next-Page to be a
  plain number above the current page and the GraphQL cursor to change.
- --serve adds frame-ancestors 'none' to the header CSP and same-origin
  Cross-Origin-Resource-Policy and Cross-Origin-Opener-Policy.
- re.escape the PR number in the squash-subject regex.
-o, --json and --csv are written to a new temp file in the same directory and
moved into place with os.replace, and an existing symlink at the output path
is refused, so a symlink planted in an untrusted checkout cannot redirect the
write. Characters that cannot be encoded (lone surrogates from API text) are
written as backslash escapes instead of crashing the run.
…the hardening

- GitHub Actions: check the analysed repository out into its own directory
  with persist-credentials: false, write the dashboard to $RUNNER_TEMP.
- GitLab CI: pin the image by digest (with how to look it up) and write the
  dashboard into a directory the job creates.
- Security: describe the allowlisted environments, absolute executable
  paths, no lazy fetch, safe output writes and --serve headers, and say
  plainly that stripping credentials from git's environment prevents
  accidental leaks but is not a boundary against code running as you.
@g-husam
g-husam merged commit eabfe90 into main Oct 28, 2026
2 checks passed
@g-husam
g-husam deleted the repo-stats-security-hardening branch October 28, 2026 21:36
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.

1 participant