Repository navigation
Validate slugs on both surfaces; keep a venv per mcp major (#69, #71) - #72
daniel-pittman wants to merge 3 commits into
Conversation
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
Semgrep (OSS): no findingsClean scan. Posted before the Claude review. |
CI Tests✅ Tests passed: the pytest suite across Python 3.10/3.11/3.12 (and bash syntax checks) is green on Posted before the Claude review so the test outcome is folded into the review. |
|
Claude finished @daniel-pittman's task in 1m 28s —— View job PR #72 review: no blocking findingsI found no correctness bugs, security problems or CLAUDE.md violations in this diff. The one finding is LOW, posted inline. CI passed (pytest on 3.10, 3.11 and 3.12, plus the bash syntax checks, green on Findings
The PR already lists two residuals, and I agree they are LOW and not blocking:
What I checked#69, slug validation on both surfaces
#71, follow-ups from the mcp 2.x migration
Test coverageThe tests added here meaningfully cover the behavior that changed:
All tests passed in CI. |
|
|
||
| from __future__ import annotations | ||
|
|
||
| import fcntl |
There was a problem hiding this comment.
LOW: fcntl is now imported at module load, so the module can't be imported on non-POSIX systems. Only _bootstrap_venv uses fcntl, but this import runs on every import, including BB_MCP_SKIP_BOOTSTRAP=1 test imports. Today that changes nothing: the bootstrap already assumes POSIX (bin/python3), and CI is Linux-only. If Windows or a test-only import ever matters, move the import into _bootstrap_venv (or guard it). Not blocking.
Review Gate✅ Gate: PASS — Review reported no HIGH/CRITICAL findings. |
Closes #69. Closes #71.
Two issues on one branch, three commits. Each commit message has the full detail.
#69: workspace and repo slugs reach URLs unencoded
Neither surface limited slugs 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. For example,bb -w 'acme#f' vars set --workspace ...sent a variables write to/workspaces/acme. Python also accepted inner whitespace that bash rejected, which is a parity gap on a boundary check.Both surfaces now accept only
[A-Za-z0-9._-]for workspace and repo slugs. The existing empty,/and./..checks run first, with their messages unchanged.Python: a new
bb_api.validate_slugis used byrepo_path, by the four workspace-level paths inbb_ops, and bymcp_server._resolve_repobefore it builds the client.bash: a new
_slug_chars_okhelper is used by:repo_pathand_require_workspace;bb repos, which had no workspace check at all;bb vars;whoamiprobe;openandapprovebrowser URLs.It runs
trunderLC_ALL=C, so the allowed set is byte-exact with Python's ASCII regex in every locale. A sentinel byte stops$(...)from stripping newline-only bad bytes.whoami: on both surfaces it no longer probes an invalid workspace. Python'squote()leaves..intact, which can reach the global repositories listing and report a false "auth OK".InvalidURL: the MCP tools now also catchhttp.client.InvalidURL, which subclasses neitherOSErrornorValueErrorand escaped the error envelope.#71: residuals from the mcp 2.x migration (#70)
bitbucket-cli/venv-mcp2). Releases built for mcp 1.x keep usingbitbucket-cli/venv, untouched.fcntl.flock, and the sentinel is checked again once the lock is held.TestRealMCPServerreadsmcp_server.py's own import line and imports it from the real SDK. It skips only whenmcpis absent.On first launch after this merges, an existing install builds
venv-mcp2once, which takes about 30 seconds.How it was verified
tests/bash/test_security_guards.shpasses 34/34 on host bash and in the pinned bash 3.2 image.venv-mcp2, and bad slugs return theValueErrorenvelope from a real tool call.venv, the new code left that venv untouched, and the pre-Migrate the MCP server to mcp 2.x (FastMCP is now MCPServer) #70 release still served all 43 tools from it.bb -w <valid> whoamistill probes.Reviewed locally first
Two local rounds, each run by Claude (Opus), Codex and Antigravity.
bb reposandbb vars --workspacestill skipped the check, thatwhoamiprobed invalid workspaces, and that_slug_chars_okpassed newlines. Each was reproduced, then fixed in the third commit.Accepted residuals, which go into a debt ticket after the merge:
whoamiverdict for an invalid workspace (LOW). The MCPwhoamireturnsauth.ok: Falsewith aValueErrormessage naming the slug rule. The tool's convention for "not probed" isok: Nonewith askippedreason, and bash prints "probe skipped". The fix is to return the skipped shape and update the two tests.flocksupport fails the first bootstrap. A current venv skips the lock.bitbucket-cli/venvto mcp 2.x stay that way. The per-major directory protects future switches, and that old directory can be deleted oncevenv-mcp2is up.🤖 Generated with Claude Code