Skip to content

fix(util): redact URL credentials in config values on the command line - #2299

Open
Keerthana-64 wants to merge 2 commits into
gitpython-developers:mainfrom
Keerthana-64:redact-config-url-credentials
Open

Keerthana-64 wants to merge 2 commits into
gitpython-developers:mainfrom
Keerthana-64:redact-config-url-credentials

Conversation

@Keerthana-64

Copy link
Copy Markdown
Contributor

remove_password_if_present only redacted an argument that was itself a URL with userinfo, or a -c http.extraHeader=Authorization: ... assignment. a URL given as a config value, such as -c http.proxy=http://user:pass@proxy:3128 or -c remote.origin.url=https://user:token@host/repo, has = before ://, so urlsplit finds no userinfo, and the --config=<key>=<value> form that transform_kwargs produces for config=... defeated both checks, the extra header one included. after repo.git.set_persistent_git_options(c="http.proxy=http://user:secret@proxy:3128"), every failing command raised a GitCommandError containing user:secret. the fix stays in remove_password_if_present because every logged or raised command line already goes through it: it now also tries the assignment after --config= and the value after the first =, and replaces only the userinfo. test_remove_password_from_config_values fails before the change and passes after it.

Keerthana-64 and others added 2 commits October 21, 2026 13:45
`remove_password_if_present` is applied to every command line that
GitPython logs or stores in `GitCommandError` and the other
`CommandError` subclasses. It redacted an argument only when the whole
argument was a URL with userinfo, or when it was an
`http.extraHeader=Authorization: ...` assignment. Two common forms were
missed:

- A URL that is the value of a configuration assignment, such as
  `-c http.proxy=http://user:pass@proxy:3128` or
  `-c remote.origin.url=https://user:token@host/repo`. The text before
  `://` contains `=`, so `urlsplit` finds no scheme and no userinfo, and
  the argument was kept verbatim.
- The `--config=<key>=<value>` form, which is what `transform_kwargs`
  produces for a `config=...` keyword argument. The leading `--config=`
  defeated both checks, so even
  `--config=http.extraHeader=Authorization: Bearer <token>` was printed
  in full.

For example, after
`repo.git.set_persistent_git_options(c="http.proxy=http://user:secret@proxy:3128")`
every failing command raised a `GitCommandError` whose message contained
`user:secret`, and the same text reached the log when command tracing is
on.

The URL redaction moves into a small `_redact_url_credentials` helper.
`remove_password_if_present` strips a leading `--config=` before the
extra header check, and when the whole argument is not a URL with
userinfo it tries the assignment after `--config=` (which covers
`url.<base>.insteadOf` keys that carry credentials) and then the value
after the first `=`. Only the userinfo is replaced, so the key and the
rest of the URL stay readable, and arguments without a URL are returned
unchanged. Arguments that were already redacted produce the same output
as before.

Adds `test_remove_password_from_config_values` to `test/test_util.py`.
Its five redaction cases fail before this change and pass after it, and
the existing redaction tests still pass. `ruff check`, `ruff format
--check` and `mypy` are clean.
- [P1] Redact the value when the config key also contains a URL —
  /Users/byron/dev/github.com/gitpython-developers/GitPython.redact-config-url-c
  redentials/git/util.py:700-705 When a URL-specific config
  key and its value both contain credentials, such as
  `--config=http.https://alice@host.proxy=http://bob:token@proxy`, redacting the
  key sets `redacted`, so the value is never checked. `Git.execute` traces and
  `GitCommandError.command` still expose `bob:token`. Redact the key and value
  independently.

- [P2] Recognize clone's compact -c configuration form —
  /Users/byron/dev/github.com/gitpython-developers/GitPython.redact-config-url-c
  redentials/git/util.py:686-688 When `Repo.clone_from` receives Git clone's
  valid `-c<key>=<value>` form through `multi_options`, a credentialed URL
  key such as `-curl.https://user:token@host/.insteadOf=https://old/` remains
  unchanged. The leading `-c` prevents URL parsing, while the fallback checks
  only the value, so traces and exceptions expose the token. Strip this prefix
  before checking the key.

Assisted-by: GPT 6.0 Astra
Assisted-by: GPT 6.1 Sol
Co-authored-by: GPT 6.1 Sol <codex@openai.com>
@Byron
Byron force-pushed the redact-config-url-credentials branch from c743518 to f89c343 Compare October 21, 2026 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants