Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(server): strip credentials from the remote sent to the overview (#3343)

Raghav Chamadiya787aaf4f22e1
diff --git a/packages/server/src/repowise/server/routers/overview.py b/packages/server/src/repowise/server/routers/overview.py
index 7805bc210..d242fa89b 100644
--- a/packages/server/src/repowise/server/routers/overview.py
+++ b/packages/server/src/repowise/server/routers/overview.py
@@ -35,6 +35,7 @@ from repowise.core.persistence.models import (
from repowise.core.stats_highlights import file_mix
from repowise.server.deps import get_db_session, verify_api_key
from repowise.server.routers.git import _hotspot_from_row
+from repowise.server.schemas.repository import strip_credentials
from repowise.server.services.attention import build_attention
from repowise.server.services.knowledge_map import (
compute_knowledge_silos,
@@ -82,7 +83,7 @@ async def _index_storage_bytes(repowise_dir: Path) -> int:
def _remote_url(stored_url: str | None, local_path: str | None) -> str | None:
- """Best-effort git remote for a repo.
+ """Best-effort git remote for a repo, with any credentials removed.
``repositories.url`` is client-supplied and empty for most CLI-registered
repos, so fall back to reading ``origin`` out of ``.git/config``. Parsed
@@ -91,8 +92,13 @@ def _remote_url(stored_url: str | None, local_path: str | None) -> str | None:
file we can read.
Used only to resolve a repo avatar, so every failure path returns ``None``
- and the UI falls back to initials.
+ and the UI falls back to initials. The value is sent to the browser, so it
+ passes through ``strip_credentials`` first.
"""
+ return strip_credentials(_read_remote_url(stored_url, local_path))
+
+
+def _read_remote_url(stored_url: str | None, local_path: str | None) -> str | None:
if stored_url:
return stored_url
if not local_path:
diff --git a/packages/server/src/repowise/server/schemas/repository.py b/packages/server/src/repowise/server/schemas/repository.py
index 130b0b66d..f78a9721f 100644
--- a/packages/server/src/repowise/server/schemas/repository.py
+++ b/packages/server/src/repowise/server/schemas/repository.py
@@ -3,6 +3,7 @@
from __future__ import annotations
import json
+import re
from datetime import datetime
from pathlib import Path
@@ -11,6 +12,57 @@ from pydantic import BaseModel, field_validator
from repowise.core.docs_mode import DocsMode
from repowise.core.index_scope import load_index_scope
+_URL_SCHEME_RE = re.compile(r"([A-Za-z][A-Za-z0-9+.-]*)://")
+_QUERY_RE = re.compile(r"[?#]")
+_SSH_SCHEMES = frozenset({"ssh", "git+ssh", "ssh+git"})
+
+
+def strip_credentials(url: str | None) -> str | None:
+ """Drop secrets from a remote before it leaves the server.
+
+ A token can sit in the userinfo (``https://user:glpat-x@host/...``,
+ ``git+https://ghp_x@host/...``) or the query (``?private_token=``). Every
+ scheme loses its query, fragment and userinfo, except that ssh keeps a bare
+ username: ``ssh://git@host/...`` and scp-style ``git@host:path`` name an
+ account, not a secret, and the UI's avatar parser expects ``git@``.
+ """
+ if not url:
+ return url
+ url = url.strip()
+ scheme_match = _URL_SCHEME_RE.match(url)
+ if not scheme_match:
+ # scp-style `user[:password]@host:path`. Git never sends a password here,
+ # but one typed in (even with a `/`) would still reach the browser. A
+ # local path (`/srv/a:b@c`, `.\x`, `C:\x`) is not a remote, and without a
+ # `host:` after the `@` the `@` is in the path (`host:org/[email protected]`).
+ userinfo, at, rest = url.partition("@")
+ local = "\\" in userinfo or userinfo[:1] in ("/", ".")
+ if at and ":" in userinfo and ":" in rest and not local:
+ return f"{userinfo.split(':', 1)[0]}@{rest}"
+ return url
+ scheme = scheme_match.group(1)
+ rest = url[scheme_match.end() :]
+ userinfo, hostpart = "", rest
+ # Userinfo is found before the query is cut, so a `?` or `#` typed into a
+ # password cannot split it and leave a prefix behind.
+ head = rest.split("/", 1)[0]
+ if "@" in head:
+ userinfo, _, host = head.rpartition("@")
+ hostpart = host + rest[len(head) :]
+ elif ":" in head:
+ # An unencoded `/` in a password moves the authority past the first
+ # `/`, and `user:123/x` even passes for host:port. Such a password
+ # always leaves a `:` before that `/`, so only then is a later `@` the
+ # end of the userinfo; `host/[email protected]` keeps its path. Ceiling:
+ # `host:8443/[email protected]` loses its host. A port plus a path `@` is
+ # rare, and losing a host beats a leak.
+ before_query = _QUERY_RE.split(rest, maxsplit=1)[0]
+ if "@" in before_query:
+ userinfo, _, hostpart = before_query.partition("@")
+ hostpart = _QUERY_RE.split(hostpart, maxsplit=1)[0]
+ user = userinfo.split(":", 1)[0] if scheme.lower() in _SSH_SCHEMES else ""
+ return f"{scheme}://{user + '@' if user else ''}{hostpart}"
+
class RepoCreate(BaseModel):
name: str
diff --git a/tests/unit/server/test_overview_remote_url.py b/tests/unit/server/test_overview_remote_url.py
index d59c319de..bee1b9cc3 100644
--- a/tests/unit/server/test_overview_remote_url.py
+++ b/tests/unit/server/test_overview_remote_url.py
@@ -13,6 +13,8 @@ from __future__ import annotations
from pathlib import Path
+import pytest
+
from repowise.server.routers.overview import _remote_url
_ORIGIN = "https://github.com/repowise-dev/repowise.git"
@@ -79,3 +81,48 @@ def test_returns_none_when_no_remote_is_configured(tmp_path: Path) -> None:
def test_returns_none_without_a_local_path() -> None:
assert _remote_url("", None) is None
+
+
+ ("remote", "expected"),
+ [
+ ("https://user:[email protected]/g/sub/p.git", "https://gitlab.com/g/sub/p.git"),
+ ("https://[email protected]/o/r.git", "https://github.com/o/r.git"),
+ (
+ "https://user%40corp.com:[email protected]/org/proj/_git/repo",
+ "https://dev.azure.com/org/proj/_git/repo",
+ ),
+ ("http://u:[email protected]:8080/g/p", "http://git.corp:8080/g/p"),
+ ("ssh://git:[email protected]:2222/g/p.git", "ssh://[email protected]:2222/g/p.git"),
+ ("ssh://[email protected]/o/r.git", "ssh://[email protected]/o/r.git"),
+ ("[email protected]:o/r.git", "[email protected]:o/r.git"),
+ ("https://github.com/o/r", "https://github.com/o/r"),
+ ("HTTPS://[email protected]/o/r", "HTTPS://github.com/o/r"),
+ (" https://[email protected]/o/r ", "https://github.com/o/r"),
+ ("git+https://[email protected]/o/r", "git+https://github.com/o/r"),
+ ("ftp://tok@host/x", "ftp://host/x"),
+ ("https://user:pa/ss@host/p.git", "https://host/p.git"),
+ ("https://gitlab.com/g/p.git?private_token=abc#frag", "https://gitlab.com/g/p.git"),
+ ("https://user:123/secret@host/repo.git", "https://host/repo.git"),
+ ("file:///srv/git/[email protected]", "file:///srv/git/[email protected]"),
+ ("https://host/org/[email protected]", "https://host/org/[email protected]"),
+ ("https://git.example.com/org/[email protected]", "https://git.example.com/org/[email protected]"),
+ ("git@host:org/[email protected]", "git@host:org/[email protected]"),
+ ("host:org/[email protected]", "host:org/[email protected]"),
+ ("ssh://ghp_tok@host/x", "ssh://ghp_tok@host/x"),
+ ("user:pw@host:g/p.git", "user@host:g/p.git"),
+ ("user:pa/ss@host:g/p.git", "user@host:g/p.git"),
+ ("https://user:SE?CRET@host/r.git", "https://host/r.git"),
+ ("https://user:SE#CRET@host/r.git", "https://host/r.git"),
+ ("https://host:8080/p.git?x=a@b", "https://host:8080/p.git"),
+ ("/srv/a:b@c/p.git", "/srv/a:b@c/p.git"),
+ ("https://[::1]:8443/g/p", "https://[::1]:8443/g/p"),
+ ("file:///srv/git/p.git", "file:///srv/git/p.git"),
+ ],
+)
+def test_credentials_never_reach_the_ui(tmp_path: Path, remote: str, expected: str) -> None:
+ """The remote is sent to the browser, so a token in userinfo must not survive."""
+ assert _remote_url(remote, None) == expected
+ _write_config(tmp_path / ".git", f'[remote "origin"]\n\turl = {remote}\n')
+ assert _remote_url("", str(tmp_path)) == expected
+