fix(mcp): validate prefix in WithPrefix to reject shell metacharacters#3760
Conversation
|
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 Tip We noticed you've done this a few times! Consider joining the org to skip this step and gain Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions 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 Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
5a2b14d to
4f73214
Compare
|
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... |
|
@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/. |
|
/ok-to-test |
|
[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 DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Summary
Fixes #3757
The
buildArgsfunction passes the prefix directly toexec.CommandContextviastrings.Fieldssplitting. TheWithPrefix()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.CommandContextdoes 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: AdddisallowedPrefixCharsconstant and validation inWithPrefix()that panics on shell metacharacters or empty/whitespace-only prefixes.pkg/mcp/mcp_test.go: AddTestWithPrefix_Validationcovering valid prefixes (func,kn func, absolute paths) and invalid prefixes (semicolons, pipes, backticks, empty strings).Testing