posthog icon indicating copy to clipboard operation
posthog copied to clipboard

feat(hogli): add git:publish-signed command

Open rnegron opened this issue 4 months ago • 5 comments

Problem

  • Signed-commit enforcement (org ruleset) blocks agent pushes: local signing keys need a present human.
  • Riding the gh CLI's OAuth token instead would swap in a worse credential: never expires, broad scopes, readable by any same-user process.

Changes

flowchart LR
    H[Human, once per ~8h] -->|hogli git:signing-session<br/>device flow| C[(token cache<br/>0600, 8h TTL, no refresh token)]
    A{{Agent}} -->|unsigned checkpoints| L[Local branch]
    C --> R{"resolve: app, then env, then gh CLI"}
    L -->|git:publish-signed| R
    R -->|GH_TOKEN in subprocess env| M[createCommitOnBranch]
    M --> V[Verified commits, authored as the user]
    classDef phBlue fill:#1d4aff,stroke:#1d4aff,color:#fff;
    classDef phRed fill:#f54e00,stroke:#f54e00,color:#fff;
    classDef phYellow fill:#f9bd2b,stroke:#f9bd2b,color:#000;
    classDef phGray fill:#e5e7eb,stroke:#c7ccd1,color:#000;
    class A phBlue;
    class M phRed;
    class H,V phYellow;
    class L,C,R phGray;
  • hogli git:publish-signed: replays unpushed commits via GitHub's createCommitOnBranch, signed server-side (Verified), guard-railed (refuses default branch, merge commits, symlinks/submodules/exec-bit changes, diverged remote, oversized payloads).
  • hogli git:signing-session: one human interaction buys an 8h unattended window. Device-flow GitHub App user token, cached 0600; running it again shows time remaining, --end deletes the token. The 6-month refresh token is deliberately discarded.
  • Layered: github_app_auth.py is a general GitHub App device-flow/token-cache layer (no signing knowledge); signing_session.py is the special-purpose session policy on top.
  • --auth / HOGLI_PUBLISH_AUTH pins the token source. auto prefers the app token; app fails closed for agent environments.
  • Win vs the gh CLI token is blast radius, not secrecy: 8h expiry, two repo-scoped permissions, org-revocable by uninstalling the app.

[!NOTE] Needs a GitHub App registered on the org (device flow on, expiring user tokens on, Contents + Workflows read/write, installed on selected repos). Client ID goes in GITHUB_APP_CLIENT_ID; until then HOGLI_GITHUB_APP_CLIENT_ID overrides.

How did you test this code?

  • New pytest coverage: device-flow poll state machine, cache mode/expiry margin/corruption, resolver precedence (explicit modes never fall through), refresh token never written to disk.
  • First happy-path publish test threads a fake token through every gh subprocess. Catches silent fallback to the gh keyring token and caught a latent arity bug in the scratch-branch cleanup call.
  • Non-interactive with no token fails fast with session instructions; never starts a device flow an agent can't finish.
  • Manual e2e pending app registration.

Automatic notifications

  • [ ] Publish to changelog?
  • [ ] Alert Sales and Marketing teams?

Docs update

  • None. Self-documenting via hogli --help.

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

  • Claude Code session directed by Raúl; skills invoked: /hogli, /writing-tests.
  • Chose a user access token over an installation token so commits and PRs attribute to the human, not a bot.
  • App-first resolver order so an exported long-lived token can't silently bypass the short-lived path.

rnegron avatar Jul 09 '26 15:07 rnegron

Static Badge   Static Badge   Static Badge   Static Badge

View Full Report ↗︎ ⋅ Docs

trunk-io[bot] avatar Jul 09 '26 15:07 trunk-io[bot]

🤖 CI report

✅ ClickHouse migration SQL — none

No ClickHouse migrations in the latest push.

github-actions[bot] avatar Jul 16 '26 14:07 github-actions[bot]

[!WARNING] Finding on /tools/hogli-commands/hogli_commands/signed_commits.py:343 — could not attach an inline comment (line is not part of the diff), so reporting it here.

🟠 Encode branch names before using them as REST paths

Git permits # in branch names, but this branch value reaches a gh api URL path without encoding. An attacker can create/check out a remote branch named main#review; it passes the default-branch guard, but gh treats #review as a URL fragment and PATCHes refs/heads/main instead. This lets an untrusted feature branch cause the signed publish flow to target the default branch with the operator's token.

Prompt To Fix With AI
Treat branch names as URL path data, not a preformatted path. URL-encode the branch value before interpolating it into every `gh api` REST endpoint path (preserving literal `/` only if nested branch names must remain supported; at minimum encode `%`, `#`, and `?`). Add regression tests using a Git-valid branch such as `main#review` to verify the generated REST path cannot target `main`. Also consider passing `--hostname github.com` to every `gh api` invocation because this command only accepts github.com origins.

Severity: high | Confidence: 94% | React with 👍 if useful or 👎 if not

Fixed in 7054486c.

parameterai[bot] avatar Jul 16 '26 18:07 parameterai[bot]

Security Review

The final branch update does not atomically verify the previously observed remote tip. A concurrent push can therefore be overwritten, causing repository history loss.

Reviews (1): Last reviewed commit: "Merge branch 'master' into rauln/hogli-g..." | Re-trigger Greptile

greptile-apps avatar Jul 16 '26 18:07 greptile-apps

🤖 Posted by an agent (security review requested by a maintainer). Findings below; #1 was reproduced, #4 corrects an earlier bot comment.

Security review — 5 findings

1. Branch name is injected unencoded into the gh api URL path, bypassing the default-branch guard (High — confirmed)

signed_commits.py:343 (PATCH), reached from :538; guard at :473.

The default-branch refusal is an exact string compare (branch == default_branch), but git allows # in branch names and _fast_forward_remote_branch interpolates the branch straight into the REST path. gh api drops everything after # — I reproduced this: gh api "repos/PostHog/posthog#foo" returns the same repo as the plain call. So a branch named master#x passes the != master guard, but the PATCH actually targets refs/heads/master, landing commits on the default branch (Verified as the operator).

Only the PATCH path is live — it needs tip non-None, i.e. a remote branch with the literal # name must already exist. _create_remote_branch puts the branch in the request body where # is inert, and _delete_remote_branch only receives the generated scratch name. On a repo whose default branch requires PRs the PATCH would still be rejected; the exposure is that this is a general command usable on any installed repo, including ones without that protection.

Fix: URL-encode the branch before every REST path interpolation (or reject names containing #/%/?), add --hostname github.com to the gh api calls, and add a regression test with a name like master#x. (Credit: hex-security flagged this; mechanism now confirmed.)

2. --auth auto silently falls back to the long-lived gh CLI token (Medium)

token_for_mode → signed_commits.py:~787.

With no app session and no GH_TOKEN, auto uses the long-lived gh OAuth token and only prints a stderr warning — which an unattended agent ignores. The safe path (HOGLI_PUBLISH_AUTH=app, fail closed) is opt-in, so a misconfigured agent env quietly leans on the exact credential this PR sets out to retire.

Fix: when stdin is non-interactive and no app/env token resolved, refuse instead of falling back to gh (or default agent/non-TTY contexts to app).

3. One fixed-path 8h token grants signed-publish to every installed repo (Low–Medium)

signing_session.py (TOKEN_CACHE_PATH).

The session token isn't scoped to the current repo. During an active session, an agent in repo X can publish Verified commits authored as the human to any other repo the installation covers, for 8h. Mostly inherent to the app-token model — worth stating in the session UX and keeping the app installed on the minimum set of repos.

4. Final ref update isn't compare-and-swapped — but the fast-forward default prevents the claimed history loss (Low — corrects earlier comment)

signed_commits.py:343.

An earlier review comment flagged the unconditional PATCH as able to overwrite a concurrent push. It can't: no force is passed, and REST update-ref defaults force: false (fast-forward-only). The replayed head builds on the old tip, so any concurrent push is non-FF and GitHub rejects the PATCH — surfacing the handled "did it move during the publish?" error. No overwrite. The one caveat: this safety rests entirely on that default, so a comment noting "must stay force:false" would guard against a future edit making it real.

5. Failed scratch-branch cleanup leaves a remote branch and can trigger CI (Low)

signed_commits.py:353 (DELETE result ignored).

If the DELETE fails (token expired mid-publish, transient error), a hogli/publish-signed-tmp-* branch with partially replayed commits lingers with no warning. Separately, each createCommitOnBranch on the scratch branch is a push event that can trip on: push CI.

Fix: check the DELETE result and warn with the scratch name on failure; consider a scratch ref path CI ignores.

Piccirello avatar Jul 17 '26 04:07 Piccirello

PR overview

This pull request adds a new git:publish-signed command for Hogli, with logic around publishing signed commits to GitHub repositories.

There is one open security issue involving possible disclosure of Git credentials when an HTTPS remote URL containing userinfo is rejected and printed in an error message. The exposure is limited to people or systems that can read the command output or CI/agent logs, but it could reveal a GitHub token if such a remote is used. No issues have been addressed yet, so the PR still needs a small but important hardening change before merge.

Open issues (1)

Fixed/addressed: 0 · PR risk: 6/10

veria-ai[bot] avatar Jul 20 '26 12:07 veria-ai[bot]

Closed because I figured out how to use Secretive (which does not have a Vault to auto-lock) and added a new SSH key from there 😅

rnegron avatar Jul 20 '26 14:07 rnegron