From bfe2dfb1d95d0d497c39ef81273ed4a2cc2df2f8 Mon Sep 17 00:00:00 2001 From: Daniel Pittman <7246312+daniel-pittman@users.noreply.github.com> Date: Wed, 18 Nov 2026 22:34:39 -0700 Subject: [PATCH 1/3] Validate workspace and repo slugs by character set on both surfaces Slugs are interpolated into URL paths unencoded, and neither surface limited them to the characters Bitbucket allows. A `?` or `#` in a slug moved the rest of the request path into a query string or fragment, so a request could reach a different endpoint than the tool named. Python also accepted inner whitespace that bash already rejected, a parity gap on a boundary check. Both surfaces now accept only `[A-Za-z0-9._-]` for workspace and repo slugs, after the existing empty, `/`, and `.`/`..` checks (whose messages are unchanged): - Python: new bb_api.validate_slug, used by repo_path, by the four workspace-level paths in bb_ops (projects_list, members_list, repos_list, and the workspace scope of the variables tools), and by mcp_server._resolve_repo before it builds the client. - bash: new _slug_chars_ok, used by repo_path and _require_workspace. It runs `tr` under LC_ALL=C so the allowed set is byte-exact with Python's ASCII regex in every locale. The MCP tools also catch http.client.InvalidURL, which subclasses neither OSError nor ValueError and escaped the error envelope. Tests cover each layer: bb_api, each bb_ops workspace path (no request made), _resolve_repo before _get_client, the InvalidURL envelope, and the bash security harness under host bash and bash 3.2. Disabling the character check fails 30 Python tests and 7 bash cases. Closes #69. Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 2 +- bb | 22 +++++++++++++ bb_api.py | 51 +++++++++++++++++++++++------- bb_ops.py | 6 +++- mcp_server.py | 9 ++++++ tests/bash/test_security_guards.sh | 26 +++++++++++++++ tests/test_bb_api.py | 46 +++++++++++++++++++++++++++ tests/test_bb_ops_repos.py | 23 ++++++++++++++ tests/test_mcp_server.py | 42 ++++++++++++++++++++++++ 9 files changed, 213 insertions(+), 14 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 9a9dffd..42be1bb 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -61,7 +61,7 @@ Loaded in this order (later overrides earlier): - HTTP helpers: `bb_get` / `bb_post` / `bb_put` / `bb_delete`. - `resolve_repo` sets `repo` + `BB_WORKSPACE` in the CALLER's scope (called as `resolve_repo "$1"`, NOT `repo=$(...)`, so it escapes the subshell and can set the workspace). Precedence: `-w` flag > `workspace/slug` arg > git origin auto-detect > `BB_WORKSPACE` default > error. `resolve_workspace` is the repo-less companion for workspace-level commands. Both mirror the Python `_resolve_repo` contract. - `BB_WORKSPACE` is OPTIONAL — only `BB_USER` + `BB_TOKEN` are required at load; a missing workspace fails at the point of use, not at startup. -- Boundary validation via `_require_*` helpers, one per user-supplied value that reaches a URL or a jq program: `_require_build_number` / `_require_pr_id` / `_require_step_index` (numeric guards — the last two also close jq/URL injection surfaces, `_require_step_index` specifically prevents `$ENV.BB_TOKEN` exfiltration through the logs jq program), `_require_count` and `_require_pr_state` (query-param injection). Every value Python validates (`_is_positive_int` / `_validate_pr_id`) MUST have a bash `_require_*` sibling — parity gaps here are security bugs, not cosmetics. +- Boundary validation via `_require_*` helpers, one per user-supplied value that reaches a URL or a jq program: `_require_build_number` / `_require_pr_id` / `_require_step_index` (numeric guards — the last two also close jq/URL injection surfaces, `_require_step_index` specifically prevents `$ENV.BB_TOKEN` exfiltration through the logs jq program), `_require_count` and `_require_pr_state` (query-param injection). Workspace and repo slugs are limited to `[A-Za-z0-9._-]` by `bb_api.validate_slug` (Python) and `_slug_chars_ok` in `repo_path` / `_require_workspace` (bash), because slugs reach URL paths unencoded. Every value Python validates (`_is_positive_int` / `_validate_pr_id`) MUST have a bash `_require_*` sibling — parity gaps here are security bugs, not cosmetics. - User-supplied values ride into JSON via `jq` (`--arg` / `--args`), never string interpolation, to prevent injection. Delimited streams are off-limits for value lists: bash arguments are C strings, so a NUL delimiter embedded in a jq program is silently dropped by the shell (jq sees `split("")` and shreds the stream per character), and any printable delimiter can collide with a value. `bb trigger` passes each `KEY=VALUE` pair as its own jq argument via `$ARGS.positional` (jq 1.6+). - Error rc capture: `local rc=0; out=$(cmd) || rc=$?` — NOT `if ! cmd; then rc=$?` (the `!` negation makes `$?` always 0; verified on bash 3.2 + 5.x). - bash 3.2+ floor (macOS system bash supported) — no `${var,,}` / `${var^^}` / `mapfile` / `declare -A`; a `bash:3.2` CI job (`bash32-floor`) parses `bb` to catch regressions. diff --git a/bb b/bb index fe08787..b276bcb 100755 --- a/bb +++ b/bb @@ -408,6 +408,16 @@ _resolve_pr_args() { [[ -n "$pr_id" ]] && _require_pr_id "$pr_id" } +# True when every byte of $1 is a slug character: [A-Za-z0-9._-]. +# Slugs are interpolated into URL paths unencoded, so a space, `?`, `#`, +# `%` or control character would change which endpoint a request reaches. +# LC_ALL=C keeps the ranges byte-exact, matching the ASCII-only +# bb_api._SLUG_RE on the Python side in every locale. Emptiness is the +# callers' check, so an empty value passes here. +_slug_chars_ok() { + [[ -z "$(printf '%s' "$1" | LC_ALL=C tr -d 'A-Za-z0-9._-')" ]] +} + repo_path() { local repo="$1" # Validate inputs at the boundary so a malformed slug doesn't @@ -443,6 +453,14 @@ repo_path() { echo "Error: repo must not be '.' or '..'." >&2 kill -TERM $$ fi + if ! _slug_chars_ok "$BB_WORKSPACE"; then + echo "Error: workspace must contain only letters, digits, '.', '_' and '-' (got '$BB_WORKSPACE')." >&2 + kill -TERM $$ + fi + if ! _slug_chars_ok "$repo"; then + echo "Error: repo must contain only letters, digits, '.', '_' and '-' (got '$repo')." >&2 + kill -TERM $$ + fi # `exit 1` inside a `$(repo_path ...)` command substitution only # terminates the subshell — the caller would proceed with an # empty path. `kill -TERM $$` terminates the parent script so @@ -570,6 +588,10 @@ _require_workspace() { echo "Error: workspace must not be '.' or '..' (got '$ws')." >&2 exit 1 fi + if ! _slug_chars_ok "$ws"; then + echo "Error: workspace must contain only letters, digits, '.', '_' and '-' (got '$ws')." >&2 + exit 1 + fi } # Allowlist the PR state before it's interpolated into the request URL. diff --git a/bb_api.py b/bb_api.py index 6f8ec29..8ad8728 100644 --- a/bb_api.py +++ b/bb_api.py @@ -257,23 +257,50 @@ def detect_repo( return repo -def repo_path(workspace: str, repo: str) -> str: - """Build the Bitbucket REST path for a repo (`/repositories/{ws}/{repo}`). +# Bitbucket workspace and repository slugs use only these characters. +# Slugs are interpolated into URL paths unencoded, so anything else (a +# space, `?`, `#`, `%`, a control character) would either be rejected by +# urllib or change which endpoint the request reaches. UUID-form +# identifiers (`{...}`) are not accepted here. Mirrored by the bash +# _require_slug_chars helper. +_SLUG_RE = re.compile(r"[A-Za-z0-9._-]+") + - Mirrors the bash repo_path helper. Validates inputs reject: - - empty / whitespace-only values (would produce `/repositories//repo`) +def validate_slug(label: str, value: Any) -> str: + """Validate a workspace or repo slug before it reaches a URL path. + + Rejects, in order (the earlier messages are the long-standing ones): + - non-strings and empty / whitespace-only values (would produce + `/repositories//repo`) - embedded `/` (would silently change path structure) - - `.` or `..` segments (path-traversal: after URL normalisation, + - `.` or `..` (path traversal: after URL normalisation, `/repositories/../widget` resolves to `/repositories/widget` with the wrong workspace) + - any character outside `_SLUG_RE` + Returns the value unchanged. + """ + if not isinstance(value, str) or not value.strip(): + raise ValueError(f"{label} must be a non-empty, non-whitespace string. Got {value!r}.") + if "/" in value: + raise ValueError(f"{label} must not contain '/'. Got {value!r}.") + if value in (".", ".."): + raise ValueError(f"{label} must not be '.' or '..'. Got {value!r}.") + if not _SLUG_RE.fullmatch(value): + raise ValueError( + f"{label} must contain only letters, digits, '.', '_' and '-'. " + f"Got {value!r}." + ) + return value + + +def repo_path(workspace: str, repo: str) -> str: + """Build the Bitbucket REST path for a repo (`/repositories/{ws}/{repo}`). + + Mirrors the bash repo_path helper; both slugs go through + validate_slug. """ - for label, value in (("workspace", workspace), ("repo", repo)): - if not value or not value.strip(): - raise ValueError(f"{label} must be a non-empty, non-whitespace string. Got {value!r}.") - if "/" in value: - raise ValueError(f"{label} must not contain '/'. Got {value!r}.") - if value in (".", ".."): - raise ValueError(f"{label} must not be '.' or '..'. Got {value!r}.") + validate_slug("workspace", workspace) + validate_slug("repo", repo) return f"/repositories/{workspace}/{repo}" diff --git a/bb_ops.py b/bb_ops.py index 617e601..efdda9a 100644 --- a/bb_ops.py +++ b/bb_ops.py @@ -23,7 +23,7 @@ from typing import Any, Iterable from urllib.parse import quote -from bb_api import BBApiError, BBClient, repo_path +from bb_api import BBApiError, BBClient, repo_path, validate_slug # --------------------------------------------------------------------------- @@ -1127,6 +1127,7 @@ def projects_list( raise ValueError(f"workspace must not contain '/', got {ws!r}") if ws in (".", ".."): raise ValueError(f"workspace must not be '.' or '..', got {ws!r}") + validate_slug("workspace", ws) if not _is_positive_int(count): raise ValueError(f"count must be a positive int, got {count!r}") @@ -1174,6 +1175,7 @@ def members_list( raise ValueError(f"workspace must not contain '/', got {ws!r}") if ws in (".", ".."): raise ValueError(f"workspace must not be '.' or '..', got {ws!r}") + validate_slug("workspace", ws) if not _is_positive_int(count): raise ValueError(f"count must be a positive int, got {count!r}") @@ -1226,6 +1228,7 @@ def repos_list( raise ValueError(f"workspace must not contain '/', got {ws!r}") if ws in (".", ".."): raise ValueError(f"workspace must not be '.' or '..', got {ws!r}") + validate_slug("workspace", ws) if not _is_positive_int(count): raise ValueError(f"count must be a positive int, got {count!r}") @@ -1629,6 +1632,7 @@ def _variables_base( raise ValueError(f"workspace must not contain '/', got {workspace!r}") if workspace in (".", ".."): raise ValueError(f"workspace must not be '.' or '..', got {workspace!r}") + validate_slug("workspace", workspace) # HYPHEN form — verified against the live API; the underscore # form 404s at the workspace scope. return f"/workspaces/{workspace}/pipelines-config/variables/" diff --git a/mcp_server.py b/mcp_server.py index 25c5d2d..9a32154 100644 --- a/mcp_server.py +++ b/mcp_server.py @@ -44,6 +44,7 @@ from __future__ import annotations +import http.client import os import re import subprocess @@ -386,6 +387,8 @@ def _resolve_repo(repo: str | None = "") -> tuple[bb_api.BBClient, str, str]: raise ValueError( f"workspace and repo must not be '.' or '..'; got {repo!r}" ) + bb_api.validate_slug("workspace", parts[0]) + bb_api.validate_slug("repo", parts[1]) client = _get_client() return client, parts[0], parts[1] @@ -397,6 +400,7 @@ def _resolve_repo(repo: str | None = "") -> tuple[bb_api.BBClient, str, str]: raise ValueError( f"repo must not be '.' or '..'; got {repo!r}" ) + bb_api.validate_slug("repo", repo) client = _get_client() # BB_WORKSPACE is optional (v1.2.0). A bare slug has no workspace of # its own, so it can only resolve against the configured default. If @@ -566,6 +570,10 @@ def _error_dict_with(e: Exception, **extras: Any) -> dict[str, Any]: # - AttributeError covers the JSON-null-into-string-arg case where # an MCP client sends {"repo": null} and `.strip()` would # otherwise crash uncaught. +# - http.client.InvalidURL is raised by urllib before sending when a +# URL holds a space or control character. It subclasses neither +# OSError nor ValueError. Slug validation keeps it off the repo +# path; this catches any other URL component that slips one in. # # Deliberately EXCLUDES TypeError — a refactor that renames a bb_ops # kwarg should surface as an obvious dev-time crash, not a fake @@ -580,6 +588,7 @@ def _error_dict_with(e: Exception, **extras: Any) -> dict[str, Any]: OSError, AttributeError, ValueError, + http.client.InvalidURL, ) diff --git a/tests/bash/test_security_guards.sh b/tests/bash/test_security_guards.sh index 874bec1..cd1e822 100755 --- a/tests/bash/test_security_guards.sh +++ b/tests/bash/test_security_guards.sh @@ -139,6 +139,28 @@ assert_rejects "logs build_number non-numeric" \ "build_number must be a positive integer" \ -- logs "$REPO" 'abc' +# repo / workspace slug → path rewrite or query smuggling (issue #69). +# A `?` or `#` turns the rest of the request path into a query string or +# fragment; a space or control character makes the URL invalid. +SLUG_MSG="must contain only letters, digits, '.', '_' and '-'" +assert_rejects "repo slug '?' query-smuggle" "$SLUG_MSG" \ + -- pipelines 'dummy-ws/widget?x=1' +assert_rejects "repo slug '#' fragment" "$SLUG_MSG" \ + -- prs 'dummy-ws/widget#frag' +assert_rejects "repo slug percent-escape" "$SLUG_MSG" \ + -- pipelines 'dummy-ws/widget%2Fother' +assert_rejects "workspace slug '?' via ws/repo" "$SLUG_MSG" \ + -- pipelines 'ws?x=1/widget' +assert_rejects "workspace slug control char" "$SLUG_MSG" \ + -- pipelines "$(printf 'ws\001x')/widget" +assert_rejects "projects workspace '?'" "$SLUG_MSG" \ + -- -w 'ws?x=1' projects +assert_rejects "members workspace '#'" "$SLUG_MSG" \ + -- -w 'ws#x' members +# Inner whitespace: the long-standing whitespace check catches it first. +assert_rejects "repo slug inner space" "non-whitespace" \ + -- pipelines 'dummy-ws/bad repo' + echo "" echo "== valid input must clear the guards ==" assert_passes_guard "logs valid step 0 clears guard" \ @@ -149,6 +171,10 @@ assert_passes_guard "pipelines valid count clears guard" \ "count must be" -- pipelines "$REPO" 3 assert_passes_guard "prs valid state clears guard" \ "state must be one of" -- prs "$REPO" MERGED +assert_passes_guard "slug with . _ - and caps clears guard" \ + "must contain only letters" -- pipelines 'My_ws-1/Widget.Service_2' +assert_passes_guard "projects valid workspace clears guard" \ + "must contain only letters" -- -w 'acme-co_2' projects echo "" echo "== summary: ${pass} passed, ${fail} failed ==" diff --git a/tests/test_bb_api.py b/tests/test_bb_api.py index 971d70e..9173d21 100644 --- a/tests/test_bb_api.py +++ b/tests/test_bb_api.py @@ -783,3 +783,49 @@ def test_paginate_yields_nothing_on_empty_first_page() -> None: opener = _CaptureOpener([{"values": []}]) client = _client(opener) assert list(client.paginate("/repos")) == [] + + +# Slug character set (issue #69). Slugs reach URL paths unencoded, so a +# `?` or `#` would move the rest of the path into a query or fragment, +# and a space or control character makes urllib reject the URL. +_BAD_SLUG_CHARS = [ + "bad repo", # inner space: Python used to accept it (bash rejected) + "widget?x=1", # query smuggling + "widget?", # truncates the endpoint path + "widget#frag", # fragment + "widget%2Fother", # pre-encoded escape + "ws\x01x", # control character + "wid\tget", # inner tab + "{uuid-form}", # UUID form is not accepted + "wïdget", # non-ASCII letter +] + + +@pytest.mark.parametrize("bad", _BAD_SLUG_CHARS) +def test_repo_path_rejects_non_slug_characters(bad: str) -> None: + with pytest.raises(ValueError, match="only letters, digits"): + repo_path(bad, "widget") + with pytest.raises(ValueError, match="only letters, digits"): + repo_path("acme", bad) + + +@pytest.mark.parametrize( + "good", ["acme", "widget-service", "Widget.Service_2", "a", "0", "a.b-c_d"] +) +def test_repo_path_accepts_slug_characters(good: str) -> None: + assert repo_path(good, good) == f"/repositories/{good}/{good}" + + +def test_validate_slug_rejects_non_string() -> None: + with pytest.raises(ValueError, match="non-empty"): + bb_api.validate_slug("repo", None) + with pytest.raises(ValueError, match="non-empty"): + bb_api.validate_slug("repo", 7) + + +def test_validate_slug_keeps_earlier_messages() -> None: + """The long-standing checks still fire first, with their messages.""" + with pytest.raises(ValueError, match="must not contain '/'"): + bb_api.validate_slug("repo", "a/b?") + with pytest.raises(ValueError, match=r"'\.' or '\.\.'"): + bb_api.validate_slug("repo", "..") diff --git a/tests/test_bb_ops_repos.py b/tests/test_bb_ops_repos.py index f402630..5672659 100644 --- a/tests/test_bb_ops_repos.py +++ b/tests/test_bb_ops_repos.py @@ -1694,3 +1694,26 @@ def test_rejects_bad_count_no_network(self, bad: Any) -> None: with pytest.raises(ValueError): bb_ops.members_list(_client(opener), count=bad) assert opener.calls == [] + + +class TestWorkspaceSlugCharacters: + """Workspace-level calls build `/workspaces/{ws}/...` or + `/repositories/{ws}` without repo_path, so each validates the slug + itself; a `?` or `#` must be rejected before any request (#69).""" + + @pytest.mark.parametrize("bad", ["ws?x=1", "ws#x", "w s", "ws%2F"]) + @pytest.mark.parametrize( + "call", + [ + lambda c, ws: bb_ops.projects_list(c, workspace=ws), + lambda c, ws: bb_ops.members_list(c, workspace=ws), + lambda c, ws: bb_ops.repos_list(c, workspace=ws), + lambda c, ws: bb_ops.vars_list(c, ws, scope="workspace"), + ], + ids=["projects_list", "members_list", "repos_list", "vars_list-workspace"], + ) + def test_rejects_before_any_request(self, call, bad: str) -> None: + opener = _CaptureOpener([]) + with pytest.raises(ValueError, match="workspace"): + call(_client(opener), bad) + assert opener.calls == [] diff --git a/tests/test_mcp_server.py b/tests/test_mcp_server.py index 205a687..914256d 100644 --- a/tests/test_mcp_server.py +++ b/tests/test_mcp_server.py @@ -299,6 +299,22 @@ def raise_config(*_args: Any, **_kwargs: Any) -> Any: with pytest.raises(ValueError, match=r"'\.'"): mcp_server._resolve_repo(bare) + @pytest.mark.parametrize( + "repo", ["widget?x=1", "acme/widget#f", "ws?x/widget", "acme/bad repo"] + ) + def test_non_slug_characters_rejected_before_get_client( + self, monkeypatch: pytest.MonkeyPatch, repo: str + ) -> None: + """Issue #69: a slug with `?`, `#` or inner whitespace is a clean + ValueError raised before _get_client, never a URL that reaches + another endpoint or an InvalidURL from urllib.""" + mcp_server._reset_client_cache() + def raise_config(*_args: Any, **_kwargs: Any) -> Any: + raise bb_api.BBConfigError("Missing BB_USER") + monkeypatch.setattr(bb_api, "load_config", raise_config) + with pytest.raises(ValueError, match="only letters, digits"): + mcp_server._resolve_repo(repo) + # --------------------------------------------------------------------------- # _error_dict @@ -2260,3 +2276,29 @@ def test_pipeline_alias_is_in_the_real_schema(self, real_server) -> None: listed = {t.name: t for t in asyncio.run(real_server.list_tools())} props = listed["pipeline_trigger"].input_schema["properties"] assert {"pattern", "pipeline", "variables"} <= set(props) + + +class TestInvalidUrlEnvelope: + """http.client.InvalidURL subclasses neither OSError nor ValueError; + a tool must still return the error envelope, not raise (#69).""" + + def test_invalid_url_is_enveloped(self, stub_client: bb_api.BBClient) -> None: + import http.client + + def boom(*_args: Any, **_kwargs: Any) -> Any: + raise http.client.InvalidURL("URL can't contain control characters") + + with patch.object(bb_ops, "pipelines_list", boom): + out = mcp_server.pipelines_list(repo="acme/widget-service") + assert out["ok"] is False + assert out["kind"] == "InvalidURL" + + def test_tool_returns_envelope_for_query_smuggling_slug( + self, stub_client: bb_api.BBClient + ) -> None: + recorder = _recorder([]) + with patch.object(bb_ops, "pipeline_trigger", recorder): + out = mcp_server.pipeline_trigger(branch="main", repo="acme/widget?") + assert out["ok"] is False + assert "only letters, digits" in out["message"] + assert recorder.calls == [] From bcbc0b3532572be1190cfee6a95e3085401f63b4 Mon Sep 17 00:00:00 2001 From: Daniel Pittman <7246312+daniel-pittman@users.noreply.github.com> Date: Wed, 18 Nov 2026 22:38:28 -0700 Subject: [PATCH 2/3] Keep a venv per mcp major, lock the install, and close the #70 debt Four residuals from the mcp 2.x migration (#70): - Rollback. The bootstrap venv was shared across releases, so once a newer release upgraded it to mcp 2.x, an older release (or a checkout switched back to one) saw a sentinel, skipped the install and failed importing mcp.server.fastmcp. The venv directory is now named for the mcp major version (`bitbucket-cli/venv-mcp2`), so releases built for mcp 1.x keep using `bitbucket-cli/venv` untouched. A test ties the directory name to the major in _VENV_DEPS. - Concurrent first launch. Install-and-stamp now runs under an exclusive fcntl.flock on `venv-mcp2.lock` beside the venv, and the sentinel is checked again once the lock is held, so a launch that waited for another one's install re-execs without installing again. - Real-SDK test reach. TestRealMCPServer now reads mcp_server.py's own `from mcp... import MCPServer` line and imports that module and class from the real SDK, so a wrong module path on that line fails the test. It skips only when the `mcp` package itself is absent; an installed SDK lacking the module now fails instead of skipping. - Corrupt sentinel. _venv_is_current also treats a UnicodeDecodeError as stale, so an undecodable sentinel triggers a reinstall instead of crashing startup. The sentinel is read and written as UTF-8. Verified by running the server over stdio in scratch data directories: a fresh bootstrap builds venv-mcp2; beside an mcp 1.30.0 `venv` it leaves that venv and its sentinel untouched, and the pre-#70 release still serves all 43 tools from it afterward; of three simultaneous first launches, exactly one installs and all three serve. Each of the four fixes was reverted once to confirm its test fails. Full suite: 709 passed on Python 3.10, 3.11 and 3.12 with mcp 2.3.0; every bash harness passes. Closes #71. Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 2 +- README.md | 12 ++- agents/bitbucket.md | 2 +- mcp_server.py | 97 +++++++++++++++--------- pyproject.toml | 2 +- tests/test_mcp_server.py | 160 ++++++++++++++++++++++++++++++++++++++- 6 files changed, 231 insertions(+), 44 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 42be1bb..7495683 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -45,7 +45,7 @@ bitbucket-cli/ - `BB_USER`: Bitbucket account email - `BB_TOKEN`: API token from id.atlassian.com (never echoed) - `BB_WORKSPACE`: workspace slug -- **MCP runtime**: stdlib-only at runtime; the `mcp` package is the only third-party dep, installed into a self-bootstrapped venv at `$XDG_DATA_HOME/bitbucket-cli/venv` (default `~/.local/share/bitbucket-cli/venv`) on first invocation. +- **MCP runtime**: stdlib-only at runtime; the `mcp` package is the only third-party dep, installed into a self-bootstrapped venv at `$XDG_DATA_HOME/bitbucket-cli/venv-mcp2` (default `~/.local/share/bitbucket-cli/venv-mcp2`) on first invocation. The directory is named for the mcp major version, so releases built for another major keep their own venv. ## Configuration diff --git a/README.md b/README.md index af81025..9a565c5 100644 --- a/README.md +++ b/README.md @@ -503,11 +503,15 @@ claude mcp add --scope user bitbucket \ # (see "Multiple workspaces" below). # 3. On first invocation, the server self-bootstraps a durable venv at -# $XDG_DATA_HOME/bitbucket-cli/venv (default: ~/.local/share/bitbucket-cli/venv) -# and installs the `mcp` package into it. Subsequent launches reuse the venv. +# $XDG_DATA_HOME/bitbucket-cli/venv-mcp2 (default: +# ~/.local/share/bitbucket-cli/venv-mcp2) and installs the `mcp` package +# into it. Subsequent launches reuse the venv. The directory is named for +# the mcp major version, so releases built for mcp 1.x keep their own +# `bitbucket-cli/venv`; once you no longer run one, that old directory +# can be deleted. # # To force a clean rebuild: -# rm -rf "${XDG_DATA_HOME:-$HOME/.local/share}/bitbucket-cli/venv" +# rm -rf "${XDG_DATA_HOME:-$HOME/.local/share}/bitbucket-cli/venv-mcp2" # and relaunch the MCP server. # 4. Verify the connection (handshake only — does NOT validate credentials): @@ -543,7 +547,7 @@ claude mcp add --scope user bitbucket-work \ |---|---| | `BB_API_BASE` | Override the Bitbucket REST base URL (default `https://api.bitbucket.org/2.0`). Useful for a test / proxied / staging mirror. | | `BB_DEFAULT_REPO_PATH` | Default working directory for repo auto-detection (when a Bitbucket tool is called with `repo=""`) AND for the `git_*` tools' default `path=""` resolution. Defaults to the MCP server's launch cwd. | -| `XDG_DATA_HOME` | Standard XDG override for the data root. The venv lives at `$XDG_DATA_HOME/bitbucket-cli/venv` (default `~/.local/share/bitbucket-cli/venv`). | +| `XDG_DATA_HOME` | Standard XDG override for the data root. The venv lives at `$XDG_DATA_HOME/bitbucket-cli/venv-mcp2` (default `~/.local/share/bitbucket-cli/venv-mcp2`). | ### Optional: install the bundled `bitbucket` agent for delegated use diff --git a/agents/bitbucket.md b/agents/bitbucket.md index c6aebc1..f3a16a1 100644 --- a/agents/bitbucket.md +++ b/agents/bitbucket.md @@ -245,7 +245,7 @@ Surface the log's relevant tail (last ~50 lines or the stderr region around the The CLI is two parallel implementations: - **`bb`** (bash) — human-facing CLI, ~1000 lines of pure bash + curl + jq. -- **`bb_api.py`** + **`bb_ops.py`** + **`git_ops.py`** + **`mcp_server.py`** (Python) — what the MCP server uses. Stdlib-only runtime (no `requests` etc.); `mcp` is the only third-party dep, installed by the self-bootstrapping venv at `~/.local/share/bitbucket-cli/venv`. +- **`bb_api.py`** + **`bb_ops.py`** + **`git_ops.py`** + **`mcp_server.py`** (Python) — what the MCP server uses. Stdlib-only runtime (no `requests` etc.); `mcp` is the only third-party dep, installed by the self-bootstrapping venv at `~/.local/share/bitbucket-cli/venv-mcp2`. ### When delegated a `bb` change diff --git a/mcp_server.py b/mcp_server.py index 9a32154..e20f04f 100644 --- a/mcp_server.py +++ b/mcp_server.py @@ -20,8 +20,8 @@ Run as a subprocess (stdio transport): python3 mcp_server.py -The script self-bootstraps a venv under `$XDG_DATA_HOME/bitbucket-cli/venv` -(default `~/.local/share/bitbucket-cli/venv`) on first run, installs the +The script self-bootstraps a venv under `$XDG_DATA_HOME/bitbucket-cli/venv-mcp2` +(default `~/.local/share/bitbucket-cli/venv-mcp2`) on first run, installs the `mcp` package into it, then re-execs under that venv. Any python3 on PATH that can run `python3 -m venv` works as the launcher (must be 3.10+; on macOS prefer Homebrew or pyenv over Apple's bundled 3.9 at @@ -39,11 +39,12 @@ BB_MCP_SKIP_BOOTSTRAP=1 — test escape hatch (skips venv + stubs MCPServer) XDG_DATA_HOME — overrides the venv parent dir (default ~/.local/share); the venv lives at - `$XDG_DATA_HOME/bitbucket-cli/venv` + `$XDG_DATA_HOME/bitbucket-cli/venv-mcp2` """ from __future__ import annotations +import fcntl import http.client import os import re @@ -55,11 +56,17 @@ # --------------------------------------------------------------------------- # Self-bootstrap: ensure the venv exists and re-exec into it. Built under -# `$XDG_DATA_HOME/bitbucket-cli/venv` (default `~/.local/share/bitbucket-cli/ -# venv`) so it survives reboots — the previous `/tmp/bbenv` location would -# get wiped at every boot, forcing a fresh ~30s rebuild. The new location -# follows the XDG Base Directory spec and matches the pattern used by -# zenhub-cli (`~/.local/share/zenhub-cli/venv`). +# `$XDG_DATA_HOME/bitbucket-cli/venv-mcp2` (default `~/.local/share/ +# bitbucket-cli/venv-mcp2`) so it survives reboots; a /tmp location would +# be wiped at every boot, forcing a fresh ~30s rebuild. The location +# follows the XDG Base Directory spec. +# +# The directory is named for the mcp MAJOR version this module imports. +# Releases built for another major (the mcp 1.x releases used +# `bitbucket-cli/venv`) keep their own venv, so running an older release +# after a newer one, or switching a checkout between them, never finds a +# venv holding an SDK it cannot import. Bump the name with the major in +# _VENV_DEPS (a test enforces they agree). # # Must run before any third-party import (mcp). # --------------------------------------------------------------------------- @@ -76,8 +83,13 @@ def _xdg_data_home() -> Path: return Path.home() / ".local" / "share" -_VENV_DIR = _xdg_data_home() / "bitbucket-cli" / "venv" +_VENV_DIR = _xdg_data_home() / "bitbucket-cli" / "venv-mcp2" _VENV_PY = _VENV_DIR / "bin" / "python3" +# Held (fcntl.flock, exclusive) around install-and-stamp so concurrent +# first launches do not run pip in the same site-packages at once, where +# one can re-exec into an SDK the other is still replacing. Lives beside +# the venv, not inside it, so it exists before the venv does. +_VENV_LOCK = _VENV_DIR.parent / f"{_VENV_DIR.name}.lock" # Sentinel file written ONLY after the full bootstrap (venv create + pip # install) succeeds. If pip is Ctrl-C'd / OOM-killed / disk-full mid-run, # _VENV_PY exists but `mcp` doesn't — without this sentinel, every @@ -107,10 +119,11 @@ def _venv_is_current() -> bool: """True when the venv finished bootstrapping for the CURRENT _VENV_DEPS. A missing sentinel, an empty one (written by releases before the stamp existed), or one naming another spec all mean the - install must run again.""" + install must run again. So does an unreadable or undecodable one + (a corrupt sentinel must trigger a reinstall, not crash startup).""" try: - return _VENV_READY.read_text() == _venv_stamp() - except OSError: + return _VENV_READY.read_text(encoding="utf-8") == _venv_stamp() + except (OSError, UnicodeDecodeError): return False @@ -174,6 +187,32 @@ def _pip_install_or_diagnose(args: list[str]) -> None: ) from e +def _install_venv() -> None: + """Create the venv if needed, install _VENV_DEPS, then stamp the + sentinel. The caller holds _VENV_LOCK.""" + builder = _find_builder_python() + # Log to stderr so MCP stdio transport isn't corrupted. + print( + f"[bb-mcp] bootstrapping {_VENV_DIR} with {builder}", + file=sys.stderr, + ) + # Only create the venv if it doesn't already exist (a previous + # half-finished bootstrap left _VENV_PY in place). + if not _VENV_PY.exists(): + subprocess.check_call([builder, "-m", "venv", str(_VENV_DIR)]) + _pip_install_or_diagnose( + [str(_VENV_PY), "-m", "pip", "install", + "--no-cache-dir", "--upgrade", "pip"] + ) + _pip_install_or_diagnose( + [str(_VENV_PY), "-m", "pip", "install", + "--no-cache-dir", *_VENV_DEPS] + ) + # Sentinel last — any earlier failure leaves it absent (or stale) + # so the next launch retries the install. + _VENV_READY.write_text(_venv_stamp(), encoding="utf-8") + + def _bootstrap_venv() -> None: """Create the venv on first run, install deps, re-exec under it. @@ -185,34 +224,24 @@ def _bootstrap_venv() -> None: Creates parent directories if needed (e.g. on a fresh machine the XDG data dir may not exist yet). + + Concurrent launches serialize on _VENV_LOCK, and the sentinel is + checked again once the lock is held, so a launch that waited for + another one's install re-execs without installing a second time. """ if not _venv_is_current(): - builder = _find_builder_python() - # Log to stderr so MCP stdio transport isn't corrupted. - print( - f"[bb-mcp] bootstrapping {_VENV_DIR} with {builder}", - file=sys.stderr, - ) # Make sure the parent directory exists. On a fresh machine # `~/.local/share` may exist but `~/.local/share/bitbucket-cli` # won't yet — `python -m venv` doesn't create intermediate - # directories above the target. + # directories above the target, and the lock file lives here. _VENV_DIR.parent.mkdir(parents=True, exist_ok=True) - # Only create the venv if it doesn't already exist (a previous - # half-finished bootstrap left _VENV_PY in place). - if not _VENV_PY.exists(): - subprocess.check_call([builder, "-m", "venv", str(_VENV_DIR)]) - _pip_install_or_diagnose( - [str(_VENV_PY), "-m", "pip", "install", - "--no-cache-dir", "--upgrade", "pip"] - ) - _pip_install_or_diagnose( - [str(_VENV_PY), "-m", "pip", "install", - "--no-cache-dir", *_VENV_DEPS] - ) - # Sentinel last — any earlier failure leaves it absent (or stale) - # so the next launch retries the install. - _VENV_READY.write_text(_venv_stamp()) + with open(_VENV_LOCK, "a") as lock: + fcntl.flock(lock.fileno(), fcntl.LOCK_EX) + try: + if not _venv_is_current(): + _install_venv() + finally: + fcntl.flock(lock.fileno(), fcntl.LOCK_UN) # Detect "are we already running under the bootstrap venv?" via # resolved sys.prefix. `python -m venv` on Linux/macOS symlinks diff --git a/pyproject.toml b/pyproject.toml index 2e80754..2e8e7a9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,7 +27,7 @@ classifiers = [ ] # Runtime dependencies are intentionally minimal. The MCP server self-bootstraps -# a venv at $XDG_DATA_HOME/bitbucket-cli/venv on first run and installs `mcp` +# a venv at $XDG_DATA_HOME/bitbucket-cli/venv-mcp2 on first run and installs `mcp` # there; consumers who install via `pip install -e .` get the same package # added explicitly via the `mcp` extra. The transport layer (bb_api) is # stdlib-only. diff --git a/tests/test_mcp_server.py b/tests/test_mcp_server.py index 914256d..0bfb927 100644 --- a/tests/test_mcp_server.py +++ b/tests/test_mcp_server.py @@ -2098,7 +2098,7 @@ def test_venv_dir_is_under_xdg_data_home(self) -> None: """The venv path must end in `bitbucket-cli/venv` so it lives alongside other XDG-spec app state.""" from pathlib import Path - assert mcp_server._VENV_DIR.parts[-2:] == ("bitbucket-cli", "venv") + assert mcp_server._VENV_DIR.parts[-2:] == ("bitbucket-cli", "venv-mcp2") # NOT /tmp (which would re-bootstrap every reboot). assert not str(mcp_server._VENV_DIR).startswith("/tmp") @@ -2143,6 +2143,11 @@ def venv(self, tmp_path, monkeypatch: pytest.MonkeyPatch): monkeypatch.setattr(mcp_server, "_VENV_DIR", venv_dir) monkeypatch.setattr(mcp_server, "_VENV_PY", venv_py) monkeypatch.setattr(mcp_server, "_VENV_READY", ready) + # The lock lives beside the venv; redirect it too, or the tests + # would take a lock beside the real venv directory. + monkeypatch.setattr( + mcp_server, "_VENV_LOCK", venv_dir.parent / "venv-mcp2.lock" + ) return ready @pytest.fixture @@ -2254,10 +2259,39 @@ class TestRealMCPServer: the pinned SDK major version. Skips when the SDK is not installed (CI installs the `mcp` extra so it runs there).""" + @staticmethod + def _module_server_import() -> tuple[str, str]: + """(module, class) from mcp_server.py's own real-SDK import, the + `from mcp... import MCPServer` inside the bootstrap branch. Read + from the source so a wrong module path on that line fails here; + the suite itself always runs the stub branch.""" + import ast + from pathlib import Path + + tree = ast.parse(Path(mcp_server.__file__).read_text()) + found = [ + (node.module, alias.name) + for node in ast.walk(tree) + if isinstance(node, ast.ImportFrom) + and node.module and node.module.startswith("mcp") + for alias in node.names + ] + assert len(found) == 1, found + return found[0] + @pytest.fixture def real_server(self): - mcpserver = pytest.importorskip("mcp.server.mcpserver") - server = mcpserver.MCPServer("bb") + import importlib + + # Skip only when the SDK is absent. An installed SDK that lacks + # the module mcp_server imports must FAIL, not skip. + pytest.importorskip("mcp") + module, cls = self._module_server_import() + server_cls = getattr(importlib.import_module(module), cls) + # The module-level server is built from the same name the stub + # uses, so `mcp = MCPServer("bb")` matches what this constructs. + assert cls == type(mcp_server.mcp).__name__ + server = server_cls("bb") for fn in mcp_server.mcp._tools.values(): server.tool()(fn) return server @@ -2302,3 +2336,123 @@ def test_tool_returns_envelope_for_query_smuggling_slug( assert out["ok"] is False assert "only letters, digits" in out["message"] assert recorder.calls == [] + + +class TestVenvPerMajor: + """The venv directory is named for the mcp major version in + _VENV_DEPS, so releases built for another major keep their own venv + and a rollback never meets an SDK it cannot import (#71).""" + + def test_venv_dir_names_the_pinned_major(self) -> None: + import re + + (spec,) = mcp_server._VENV_DEPS + m = re.search(r"<\s*(\d+)", spec) + assert m, f"no major-version ceiling in {spec!r}" + major = int(m.group(1)) - 1 + assert mcp_server._VENV_DIR.name == f"venv-mcp{major}" + + def test_old_major_venv_is_not_reused(self) -> None: + """The pre-2.x releases bootstrapped `bitbucket-cli/venv`.""" + assert mcp_server._VENV_DIR.name != "venv" + + def test_lock_lives_beside_the_venv(self) -> None: + assert mcp_server._VENV_LOCK.parent == mcp_server._VENV_DIR.parent + assert mcp_server._VENV_LOCK.name == mcp_server._VENV_DIR.name + ".lock" + + +class TestVenvInstallLock: + """Install-and-stamp runs under an exclusive flock, and the sentinel + is checked again once the lock is held (#71).""" + + @pytest.fixture + def venv(self, tmp_path, monkeypatch: pytest.MonkeyPatch): + venv_dir = tmp_path / "bitbucket-cli" / "venv-mcp2" + venv_py = venv_dir / "bin" / "python3" + venv_py.parent.mkdir(parents=True) + venv_py.write_text("") + ready = venv_dir / ".bbenv-ready" + monkeypatch.setattr(mcp_server, "_VENV_DIR", venv_dir) + monkeypatch.setattr(mcp_server, "_VENV_PY", venv_py) + monkeypatch.setattr(mcp_server, "_VENV_READY", ready) + monkeypatch.setattr( + mcp_server, "_VENV_LOCK", venv_dir.parent / "venv-mcp2.lock" + ) + return ready + + @pytest.fixture + def events(self, monkeypatch: pytest.MonkeyPatch): + """Record lock, pip and exec events in order, without running any.""" + log: list[str] = [] + real_flock = mcp_server.fcntl.flock + + def flock(fd, op): + log.append("lock" if op == mcp_server.fcntl.LOCK_EX else "unlock") + return real_flock(fd, op) + + monkeypatch.setattr(mcp_server.fcntl, "flock", flock) + monkeypatch.setattr( + mcp_server, "_find_builder_python", lambda: "builder-python" + ) + monkeypatch.setattr( + mcp_server, "_pip_install_or_diagnose", + lambda args: log.append("pip"), + ) + monkeypatch.setattr( + mcp_server.subprocess, "check_call", + lambda args, **kw: log.append("venv"), + ) + monkeypatch.setattr( + mcp_server.os, "execv", lambda path, argv: log.append("execv") + ) + return log + + def test_install_runs_inside_the_lock(self, venv, events) -> None: + mcp_server._bootstrap_venv() + assert events == ["lock", "pip", "pip", "unlock", "execv"] + assert venv.read_text() == mcp_server._venv_stamp() + assert mcp_server._VENV_LOCK.exists() + + def test_waiter_skips_install_another_launch_finished( + self, venv, events, monkeypatch: pytest.MonkeyPatch + ) -> None: + """While this launch waited on the lock, another launch finished + the install and stamped the sentinel: the re-check sees that and + this launch installs nothing.""" + def flock(fd, op): + if op == mcp_server.fcntl.LOCK_EX: + events.append("lock") + venv.write_text(mcp_server._venv_stamp()) # the other launch + else: + events.append("unlock") + + monkeypatch.setattr(mcp_server.fcntl, "flock", flock) + mcp_server._bootstrap_venv() + assert events == ["lock", "unlock", "execv"] + + def test_current_venv_takes_no_lock(self, venv, events) -> None: + venv.write_text(mcp_server._venv_stamp()) + mcp_server._bootstrap_venv() + assert events == ["execv"] + + def test_lock_released_when_install_fails( + self, venv, events, monkeypatch: pytest.MonkeyPatch + ) -> None: + def fail(args): + events.append("pip") + raise RuntimeError("[bb-mcp] pip install failed (exit 1)") + + monkeypatch.setattr(mcp_server, "_pip_install_or_diagnose", fail) + with pytest.raises(RuntimeError, match="pip install failed"): + mcp_server._bootstrap_venv() + assert events == ["lock", "pip", "unlock"] + assert not venv.exists() + + def test_corrupt_sentinel_reinstalls(self, venv, events) -> None: + """A sentinel holding bytes that are not UTF-8 counts as stale + and triggers a reinstall instead of crashing startup.""" + venv.write_bytes(b"\xff\xfe\x00garbage") + assert mcp_server._venv_is_current() is False + mcp_server._bootstrap_venv() + assert events == ["lock", "pip", "pip", "unlock", "execv"] + assert venv.read_text() == mcp_server._venv_stamp() From b249a905a918879bc5998fb1c8d57ad9d9a0b2ae Mon Sep 17 00:00:00 2001 From: Daniel Pittman <7246312+daniel-pittman@users.noreply.github.com> Date: Wed, 18 Nov 2026 22:46:22 -0700 Subject: [PATCH 3/3] Close the remaining bash slug gaps and the whoami probe The first #69 commit put the slug check in bash repo_path and _require_workspace, but some builders use neither: - `bb repos` interpolated the workspace into /repositories/{ws} with no check at all. It now calls _require_workspace, like projects and members. - `bb vars --workspace` (list, set and delete) built /workspaces/{ws}/pipelines-config/variables/ with only the empty, `/` and `.`/`..` checks, so a `#` sent a variables write to /workspaces/{ws}. It now also rejects inner whitespace and non-slug characters. - `bb whoami` and the MCP whoami probed /repositories/{ws} for any workspace. Python's quote() leaves `..` intact, which can reach the global repositories listing and report a false "auth OK". Both now skip the probe for an invalid slug and say why. A test that pinned the old behavior (probing a `/`-bearing workspace, encoded) now asserts the skip. - `bb open` and `bb approve` build bitbucket.org browser URLs; they now run the same slug checks through repo_path, and `approve` checks its build number. _slug_chars_ok also accepted values whose only bad bytes were newlines, because `$(...)` strips trailing newlines. A sentinel byte closes that. The bash security harness gains cases for each path, plus direct newline cases for _slug_chars_ok; it passes 34/34 on host bash and in the pinned bash 3.2 image. Removing any one of the guards fails its cases. Co-Authored-By: Claude Opus 5.5 --- bb | 28 +++++++++++++--- bb_api.py | 2 +- mcp_server.py | 4 +++ tests/bash/test_security_guards.sh | 44 ++++++++++++++++++++++++++ tests/test_mcp_server.py | 51 ++++++++++++++++++++++++++---- 5 files changed, 116 insertions(+), 13 deletions(-) diff --git a/bb b/bb index b276bcb..d6ac8e3 100755 --- a/bb +++ b/bb @@ -412,10 +412,12 @@ _resolve_pr_args() { # Slugs are interpolated into URL paths unencoded, so a space, `?`, `#`, # `%` or control character would change which endpoint a request reaches. # LC_ALL=C keeps the ranges byte-exact, matching the ASCII-only -# bb_api._SLUG_RE on the Python side in every locale. Emptiness is the -# callers' check, so an empty value passes here. +# bb_api._SLUG_RE on the Python side in every locale. The trailing `x` +# is load-bearing: `$(...)` strips trailing newlines, so without it a +# value whose only bad bytes are newlines would test as empty and pass. +# Emptiness is the callers' check, so an empty value passes here. _slug_chars_ok() { - [[ -z "$(printf '%s' "$1" | LC_ALL=C tr -d 'A-Za-z0-9._-')" ]] + [[ "$(printf '%s' "$1" | LC_ALL=C tr -d 'A-Za-z0-9._-'; printf x)" == "x" ]] } repo_path() { @@ -1082,6 +1084,10 @@ cmd_pipeline_approve() { echo "Usage: bb approve [repo] " >&2 exit 1 fi + _require_build_number "$build_number" + # Validate both slugs exactly as API URLs do (repo_path exits on a + # bad one); the path itself is not needed here. + repo_path "$repo" > /dev/null echo "Manual step approval is not supported by the Bitbucket public API." echo "Opening pipeline in browser..." @@ -2265,6 +2271,7 @@ cmd_members() { cmd_repos() { resolve_workspace + _require_workspace "$BB_WORKSPACE" echo "Repositories in ${BB_WORKSPACE}:" echo "" @@ -2836,8 +2843,10 @@ _vars_base_path() { # and Python's `.strip()` emptiness check (parity). local _ws_stripped _ws_stripped="$(printf '%s' "${BB_WORKSPACE:-}" | tr -d '[:space:]')" - if [[ -z "$_ws_stripped" || "$BB_WORKSPACE" == */* \ - || "$BB_WORKSPACE" == "." || "$BB_WORKSPACE" == ".." ]]; then + if [[ -z "$_ws_stripped" || "$_ws_stripped" != "$BB_WORKSPACE" \ + || "$BB_WORKSPACE" == */* \ + || "$BB_WORKSPACE" == "." || "$BB_WORKSPACE" == ".." ]] \ + || ! _slug_chars_ok "$BB_WORKSPACE"; then echo "Error: invalid workspace for workspace-scope variables: '${BB_WORKSPACE:-}'." >&2 # Inside `$(...)`; kill the parent, not just the subshell. kill -TERM $$ @@ -3375,6 +3384,12 @@ cmd_whoami() { if [[ -z "$probe_ws" ]]; then echo " Skipped — no workspace to probe (set BB_WORKSPACE or run" echo " inside a Bitbucket git checkout). Config + token look set." + elif [[ "$probe_ws" == "." || "$probe_ws" == ".." ]] \ + || ! _slug_chars_ok "$probe_ws"; then + # A `..` or `?`/`#` would probe a different endpoint (`..` reaches + # the global /repositories listing and falsely reports auth OK). + echo " Workspace '${probe_ws}' is not a valid slug (letters, digits," + echo " '.', '_' and '-' only); probe skipped. Fix BB_WORKSPACE / -w." elif bb_get "/repositories/${probe_ws}?pagelen=1" > /dev/null 2>&1; then echo " Workspace '${probe_ws}' reachable — auth OK." else @@ -3390,6 +3405,9 @@ cmd_open() { local repo resolve_repo "${1:-}" local section="${2:-}" + # Validate both slugs exactly as API URLs do (repo_path exits on a + # bad one); the path itself is not needed here. + repo_path "$repo" > /dev/null local url="https://bitbucket.org/${BB_WORKSPACE}/${repo}" diff --git a/bb_api.py b/bb_api.py index 8ad8728..17dfcb2 100644 --- a/bb_api.py +++ b/bb_api.py @@ -262,7 +262,7 @@ def detect_repo( # space, `?`, `#`, `%`, a control character) would either be rejected by # urllib or change which endpoint the request reaches. UUID-form # identifiers (`{...}`) are not accepted here. Mirrored by the bash -# _require_slug_chars helper. +# _slug_chars_ok helper. _SLUG_RE = re.compile(r"[A-Za-z0-9._-]+") diff --git a/mcp_server.py b/mcp_server.py index e20f04f..1db29f2 100644 --- a/mcp_server.py +++ b/mcp_server.py @@ -2170,6 +2170,10 @@ def whoami() -> dict[str, Any]: } else: try: + # quote() leaves `..` intact, which would reach the global + # /repositories listing and falsely report auth OK; the + # slug check rejects it (and `?`, `#`) as a ValueError. + bb_api.validate_slug("workspace", probe_ws) client.get( f"/repositories/{urllib.parse.quote(probe_ws, safe='')}", query={"pagelen": "1"}, diff --git a/tests/bash/test_security_guards.sh b/tests/bash/test_security_guards.sh index cd1e822..e5a5941 100755 --- a/tests/bash/test_security_guards.sh +++ b/tests/bash/test_security_guards.sh @@ -160,6 +160,50 @@ assert_rejects "members workspace '#'" "$SLUG_MSG" \ # Inner whitespace: the long-standing whitespace check catches it first. assert_rejects "repo slug inner space" "non-whitespace" \ -- pipelines 'dummy-ws/bad repo' +# Workspace-level builders that bypass repo_path (review of #69). +assert_rejects "repos workspace '?'" "$SLUG_MSG" \ + -- -w 'acme?x=1' repos +assert_rejects "repos workspace '..'" "must not be '.' or '..'" \ + -- -w '..' repos +assert_rejects "vars --workspace '#'" "invalid workspace for workspace-scope" \ + -- -w 'acme#f' vars --workspace +assert_rejects "vars set --workspace '?'" "invalid workspace for workspace-scope" \ + -- -w 'acme?x=1' vars set --workspace SOME_KEY --value v +assert_rejects "vars --workspace inner space" "invalid workspace for workspace-scope" \ + -- -w 'bad ws' vars --workspace +# Browser URLs go through the same slug checks. +assert_rejects "open repo '?'" "$SLUG_MSG" \ + -- open 'dummy-ws/widget?x=1' +assert_rejects "approve workspace '#'" "$SLUG_MSG" \ + -- approve 'ws#x/widget' 1 + +# whoami must not probe an invalid workspace (`..` reaches the global +# repositories listing and would report a false "auth OK"). +for bad_ws in '..' 'acme?x=1'; do + out="$("$BB" -w "$bad_ws" whoami 2>&1)" + if printf '%s' "$out" | grep -q "is not a valid slug"; then + echo "pass: whoami skips probe for '${bad_ws}'"; pass=$((pass + 1)) + else + echo "FAIL: whoami probed invalid workspace '${bad_ws}'" + echo " output: ${out}"; fail=$((fail + 1)) + fi +done + +# _slug_chars_ok itself: `$(...)` strips trailing newlines, so a value +# whose only bad bytes are newlines must still fail. +eval "$(sed -n '/^_slug_chars_ok() {/,/^}/p' "$BB")" +for v in $'acme\n' $'a\nb' $'\n'; do + if _slug_chars_ok "$v"; then + echo "FAIL: _slug_chars_ok accepted a newline-bearing value"; fail=$((fail + 1)) + else + echo "pass: _slug_chars_ok rejects newline-bearing value"; pass=$((pass + 1)) + fi +done +if _slug_chars_ok 'Widget.Service_2-x'; then + echo "pass: _slug_chars_ok accepts a valid slug"; pass=$((pass + 1)) +else + echo "FAIL: _slug_chars_ok rejected a valid slug"; fail=$((fail + 1)) +fi echo "" echo "== valid input must clear the guards ==" diff --git a/tests/test_mcp_server.py b/tests/test_mcp_server.py index 0bfb927..afbd9c7 100644 --- a/tests/test_mcp_server.py +++ b/tests/test_mcp_server.py @@ -2038,15 +2038,14 @@ def fake_get(path: str, *, query=None, timeout=None): # Token must NEVER be echoed, even on the auth-failure path. assert "tok-xyz" not in str(out) - def test_auth_probe_url_encodes_workspace( + def test_auth_probe_skips_workspace_with_slash( self, stub_client: bb_api.BBClient, monkeypatch: pytest.MonkeyPatch, ) -> None: - """If the workspace slug has a character that needs URL - encoding (rare, but `/` would break path parsing), the probe - must encode it — bash uses raw curl, Python uses urllib.quote - with safe=''. Test the encoding rather than the raw substitution.""" + """A workspace holding `/` is not a slug, so the probe does not + run (encoding it would still probe a workspace that cannot + exist). Both surfaces skip the probe for an invalid slug.""" # BBConfig is frozen; swap the whole client in. weird_cfg = bb_api.BBConfig( user="alice@example.com", @@ -2064,8 +2063,10 @@ def test_auth_probe_url_encodes_workspace( calls = self._stub_auth_ok(stub_client) out = mcp_server.whoami() assert out["ok"] is True - # `/` must be encoded as %2F so it doesn't fragment the path. - assert calls == [("/repositories/ws%2Fwith-slash", {"pagelen": "1"})] + assert calls == [] + assert out["auth"]["ok"] is False + assert out["auth"]["kind"] == "ValueError" + assert "'/'" in out["auth"]["message"] # --------------------------------------------------------------------------- @@ -2456,3 +2457,39 @@ def test_corrupt_sentinel_reinstalls(self, venv, events) -> None: mcp_server._bootstrap_venv() assert events == ["lock", "pip", "pip", "unlock", "execv"] assert venv.read_text() == mcp_server._venv_stamp() + + +class TestWhoamiProbeSlug: + """whoami must not probe an invalid workspace. quote() leaves `..` + intact, and `/repositories/..` can reach the global listing and + report a false "auth OK" (review of #69).""" + + @pytest.mark.parametrize("bad", ["..", "acme?x=1", "acme#f"]) + def test_invalid_probe_workspace_is_not_probed( + self, + stub_client: bb_api.BBClient, + monkeypatch: pytest.MonkeyPatch, + bad: str, + ) -> None: + import dataclasses + + monkeypatch.setattr( + stub_client, "config", + dataclasses.replace(stub_client.config, workspace=bad), + ) + monkeypatch.setattr(git_ops, "git_current_branch", lambda path=None: "main") + monkeypatch.setattr( + git_ops, "git_remote_repo", lambda path=None: ("acme", "widget-service") + ) + calls: list[str] = [] + + def fake_get(path: str, *, query=None, timeout=None): + calls.append(path) + return {} + + monkeypatch.setattr(stub_client, "get", fake_get) + out = mcp_server.whoami() + assert calls == [] + assert out["auth"]["ok"] is False + assert out["auth"]["workspace"] == bad + assert out["auth"]["kind"] == "ValueError"