Skip to content

fix(mcp): validate prefix in WithPrefix to reject shell metacharacters#3760

Merged
knative-prow[bot] merged 1 commit into
knative:mainfrom
Elvand-Lie:fix/mcp-validate-prefix
May 16, 2026
Merged

fix(mcp): validate prefix in WithPrefix to reject shell metacharacters#3760
knative-prow[bot] merged 1 commit into
knative:mainfrom
Elvand-Lie:fix/mcp-validate-prefix

Conversation

@Elvand-Lie
Copy link
Copy Markdown
Contributor

Summary

Fixes #3757

The buildArgs function passes the prefix directly to exec.CommandContext via strings.Fields splitting. The WithPrefix() option does not validate the prefix string, so a caller could provide a value containing shell metacharacters or a path to an arbitrary binary.

While exec.CommandContext does not invoke a shell (limiting the blast radius), the lack of any validation means arbitrary binary paths can be injected as the first field of the prefix.

Changes

  • pkg/mcp/mcp.go: Add disallowedPrefixChars constant and validation in WithPrefix() that panics on shell metacharacters or empty/whitespace-only prefixes.
  • pkg/mcp/mcp_test.go: Add TestWithPrefix_Validation covering valid prefixes (func, kn func, absolute paths) and invalid prefixes (semicolons, pipes, backticks, empty strings).

Testing

go test ./pkg/mcp/... -count=1    # PASS

@knative-prow knative-prow Bot requested review from dsimansk and jrangelramos May 16, 2026 11:18
@knative-prow knative-prow Bot added size/M 🤖 PR changes 30-99 lines, ignoring generated files. needs-ok-to-test 🤖 Needs an org member to approve testing labels May 16, 2026
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 16, 2026

Hi @Elvand-Lie. Thanks for your PR.

I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Tip

We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

The buildArgs function passes the prefix directly to exec.CommandContext
via strings.Fields splitting. If a caller provides a WithPrefix value
containing shell metacharacters (e.g. semicolons, pipes, backticks),
the executor could run unintended commands.

Add validation in WithPrefix to reject disallowed characters and
empty/whitespace-only prefixes, panicking at construction time
rather than silently accepting dangerous input.

Signed-off-by: Elvand Lie <elvandlie@gmail.com>
Signed-off-by: elvandlie@gmail.com <elvandlie@gmail.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented May 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 57.11%. Comparing base (2bbf9fe) to head (4f73214).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3760      +/-   ##
==========================================
+ Coverage   57.06%   57.11%   +0.04%     
==========================================
  Files         181      181              
  Lines       21145    21148       +3     
==========================================
+ Hits        12067    12078      +11     
+ Misses       7855     7848       -7     
+ Partials     1223     1222       -1     
Flag Coverage Δ
e2e 35.92% <0.00%> (+<0.01%) ⬆️
e2e go 31.46% <0.00%> (+<0.01%) ⬆️
e2e node 27.22% <0.00%> (-0.03%) ⬇️
e2e python 31.82% <0.00%> (-0.01%) ⬇️
e2e quarkus 27.36% <0.00%> (-0.01%) ⬇️
e2e rust 26.70% <0.00%> (-0.08%) ⬇️
e2e springboot 25.28% <0.00%> (-0.01%) ⬇️
e2e typescript 27.34% <0.00%> (-0.01%) ⬇️
e2e-config-ci 28.27% <0.00%> (-0.03%) ⬇️
integration 17.33% <0.00%> (-0.05%) ⬇️
unit macos-14 45.09% <100.00%> (+0.03%) ⬆️
unit macos-latest 45.09% <100.00%> (+0.03%) ⬆️
unit ubuntu-24.04-arm 45.34% <100.00%> (+0.02%) ⬆️
unit ubuntu-latest 46.04% <100.00%> (+0.03%) ⬆️
unit windows-latest 45.13% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Elvand-Lie Elvand-Lie force-pushed the fix/mcp-validate-prefix branch from 5a2b14d to 4f73214 Compare May 16, 2026 11:27
@Ankitsinghsisodya
Copy link
Copy Markdown
Contributor

rootCmd.Use is the Use field of the root cobra command which is gettin gset in Go source code at compile time, never from user input. It is always either "func" (standalone binary) or "kn func" (kn plugin). Neither contains any shell metacharacter in my opinion...

@Elvand-Lie
Copy link
Copy Markdown
Contributor Author

@Ankitsinghsisodya Thanks for looking into this, I appreciate a good discussion since I always open a PR with an issue for exactly this. You're correct that the current caller (cmd/mcp.go) passes rootCmd.Use which is a compile-time constant. The current usage is safe. However, WithPrefix() is a public API in pkg/mcp (not under internal/), so any external consumer can call it with arbitrary input that reaches exec.CommandContext via buildArgs(). While exec.CommandContext doesn't invoke a shell (so ; and | aren't actually dangerous), an unsanitized prefix could still point to an arbitrary binary path. The validation is a low-cost defense-in-depth measure at the API boundary. It runs once at construction and prevents a class of misuse if the library is consumed outside of cmd/.

@lkingland
Copy link
Copy Markdown
Member

/ok-to-test
/approve
/lgtm

@knative-prow knative-prow Bot added ok-to-test 🤖 Non-member PR verified by an org member that is safe to test. lgtm 🤖 PR is ready to be merged. and removed needs-ok-to-test 🤖 Needs an org member to approve testing labels May 16, 2026
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 16, 2026

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Elvand-Lie, lkingland

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow Bot added the approved 🤖 PR has been approved by an approver from all required OWNERS files. label May 16, 2026
@knative-prow knative-prow Bot merged commit bc40409 into knative:main May 16, 2026
69 of 70 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved 🤖 PR has been approved by an approver from all required OWNERS files. lgtm 🤖 PR is ready to be merged. ok-to-test 🤖 Non-member PR verified by an org member that is safe to test. size/M 🤖 PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(mcp): unsanitized prefix in WithPrefix enables arbitrary command execution

3 participants