Repository navigation
fix: redact git credentials from clone failure logs - #2697
IsmaelMartinez merged 10 commits into
Conversation
PR Summary by QodoRedact git credentials from clone failure logs
AI Description
Diagram
High-Level Assessment
Files changed (1)
|
Code Review by Qodo
1.
|
|
Code review by qodo was updated up to the latest commit 7df3002 |
7df3002 to
c2562f8
Compare
|
Code review by qodo was updated up to the latest commit c2562f8 |
|
Code review by qodo was updated up to the latest commit 11f9a33 |
IsmaelMartinez
left a comment
There was a problem hiding this comment.
The redaction helper works. I confirmed a token leaks through both the traceback and the CalledProcessError argv on main, and that this branch is clean. Two spots still print a credential-bearing value, one of them on the very line this PR is fixing.
Happy for you to take both. The first has a suggestion you can commit directly. If it goes quiet for a few days I will push it myself rather than hold the rest of the fix up.
|
@dwin-gharibi, the tests you added after the earlier request made it possible to review the whole batch in one pass rather than one at a time. Eight are merged, each closing its companion issue, which took the repository from 122 open items to 105.
|
…rom clone failure logs
…r new string literals
… credential redaction
…idation-failure log redaction
11f9a33 to
5ea13e3
Compare
|
Code review by qodo was updated up to the latest commit 5ea13e3 |
…viders without subclassing their init
|
Code review by qodo was updated up to the latest commit efe4d45 |
|
|
||
| MAX_FILES_ALLOWED_FULL = 50 | ||
|
|
||
| _URL_USERINFO_RE = re.compile(r"(?P<scheme>[a-zA-Z][a-zA-Z0-9+.\-]*://)[^/@\s]+@") |
There was a problem hiding this comment.
Nearly there, but this one is quadratic. [a-zA-Z0-9+.\-]* is unbounded before ://, so on text that never completes a match the engine rescans the tail from every start position. Measured on a 32k string: 16x the input costs 131x to 255x the time, 659 ms for a single sub. redact_credentials is called on str(e) from a clone failure and on PR_HELP_DOCS.REPO_URL, so the input is not always short or trusted.
Bounding both quantifiers fixes it. I ran the replacement: identical output on every URL shape I tried, including multi-URL strings and exception text, 1.5 ms instead of 659 ms on the same adversarial input, and your suite stays green at 2044.
| _URL_USERINFO_RE = re.compile(r"(?P<scheme>[a-zA-Z][a-zA-Z0-9+.\-]*://)[^/@\s]+@") | |
| _URL_USERINFO_RE = re.compile(r"(?P<scheme>[a-zA-Z][a-zA-Z0-9+.\-]{0,30}://)[^/@\s]{1,512}@") |
_AUTH_HEADER_RE is fine as it stands, no change needed there.
There was a problem hiding this comment.
Correcting my own suggestion here, and I have already pushed the fix.
The {1,512} on the userinfo was wrong. From 512 characters up it leaves the credential in the log where the original stripped it, and Bitbucket builds x-token-auth:<bearer>@, where a JWT can reach that length. So the suggestion I asked you to take traded a slow regex for a leaky one.
The scheme bound alone is what removes the quadratic behaviour: 667 ms to 2.6 ms at 32k, still 2.0x per doubling, and byte-identical to the original across 200k random inputs and scheme lengths to 200. Sorry for the churn.
|
The redaction itself is right, and I confirmed it end to end: driving the three clone paths with a tokenised URL leaks the token on main and leaks nothing on this branch, including the Gitea copy I only mentioned in passing. Thanks for picking all three up. Holding the approval on one thing only, the suggestion above. I have to correct myself there: I said earlier the patterns were linear-time and that was wrong, I had reasoned about them rather than timed them. |
The unbounded quantifiers made redact_credentials quadratic on text that never completes a match, 378 ms for one sub on a 32k string against 0.97 ms bounded, with identical output on every URL shape tested. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01C1F7BmLnLhSrkpo3QJsehP
…tials in logs
The 30-char scheme bound is what removes the quadratic backtracking; {1,512} on
the userinfo added nothing measurable and let a longer credential through, and
Bitbucket builds x-token-auth:<bearer>@ where a JWT can exceed it.
IsmaelMartinez
left a comment
There was a problem hiding this comment.
Thanks @dwin-gharibi, and for turning round both of the leaks I raised the first time.
I have pushed to your branch rather than let it sit: current main merged in to clear the conflict, the regex from the review thread applied, and a test that a long scheme or credential does not open a redaction gap.
I also had to correct my own suggestion, see the thread above. The userinfo bound I asked for would have left long credentials in the log, so only the scheme is bounded now.
All checks green. Approving and merging.
Closes #2696.
Description
A live token, PAT or bearer token appears in cleartext in the application log on any clone failure.
Root cause
GitProvider.clone(pr_agent/git_providers/git_provider.py:194-196) logged the credential-bearing clone URL and a full traceback._prepare_clone_url_with_tokendeliberately embeds secrets:https://<token>@github.com/…,https://oauth2:<pat>@gitlab…, and Bitbucket Server passes-c http.extraHeader='Authorization: Bearer <token>'.The fix
Add a
redact_credentials()helper that strips URL userinfo and masksAuthorization:header values, apply it to both the URL and the error text, and use.error()rather than.exception()so the argv traceback is not rendered.Behaviour change
extra.artifact.url = 'https://oauth2:glpat-SECRET987@gitlab.acme.com/team/private.git'extra.artifact.url = 'https://gitlab.acme.com/team/private.git'; Bitbucket argv showsAuthorization: Bearer <redacted>Verified for all three credential shapes, including via the real
BitbucketServerProvider._clone_inner.Files changed
Testing
pytest tests/unittest— 1748 passed, 1 skipped, 1 xfailed (unchanged frommain).github/workflows/build-and-test.yamlruns it:python:3.12.13-slimmain, no longer reproducible on this branchruff— no new findings vsmain;isort— cleanRisk / compatibility
Diagnostics are preserved (host, path, dest folder, error text); only secrets are masked. The stack trace is dropped from this one call site because it was the leak vector.
Checklist