fix: mitigate CWE-78 command injection in harbor.sh#222
Open
sebastiondev wants to merge 2 commits intoav:mainfrom
Open
fix: mitigate CWE-78 command injection in harbor.sh#222sebastiondev wants to merge 2 commits intoav:mainfrom
sebastiondev wants to merge 2 commits intoav:mainfrom
Conversation
- Replace unsafe shell evaluation on alias values with re-dispatch through main command handler - Replace all unsafe echo expansion for tilde paths with safe expand_path() helper - Use indirect variable expansion for log level/label lookups - Pass model name via env var in llamacpp pull instead of direct shell interpolation - Add shell metacharacter validation for downloaded profile files - Replace unsafe shell evaluation on history selection with safe re-dispatch
- expand_path: quote tilde prefix in parameter expansion to fix bash 3.2 (macOS default) where unquoted ~/ pattern fails to strip - Profile validation: replace grep -qP (PCRE, unavailable on macOS BSD grep) with grep -qE for command substitution detection - Profile validation: replace hex escape in character class with literal backtick for reliable detection on BSD grep - Update test script to match corrected implementations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Security Fix: Mitigate CWE-78 Command Injection in
harbor.shVulnerability Summary
CWE: CWE-78 (OS Command Injection)
Severity: Medium (adjusted from initial "High" after disprove analysis — see below)
Affected file:
harbor.shHarbor's CLI script uses
evalon user-controllable data in several code paths. The primary realistic attack vector is through malicious profile files downloaded from URLs (harbor profile set <url>), where crafted.envvalues containing shell metacharacters are later executed viaeval echopatterns throughout the script.Data Flows
Profile download → eval chain (most realistic):
harbor profile set <url>→ downloads.envfile → values consumed byeval echo "$(env_manager get cli.path)"inrun_harbor_doctor,link_cli,run_fixfs,run_harbor_find→ arbitrary command executionAlias execution via eval:
harbor alias set <name> <cmd>(or loaded from profile) →harbor run <alias>→eval "$maybe_cmd"→ arbitrary command executionllamacpp model name injection:
harbor pull <model>→ model name interpolated into single-quoted shell string inside container script → single-quote breakout → command execution inside container (with mounted cache volumes)History re-execution:
harbor history→eval "$(cat "$output_file")"→ executes selected history entry via raw evalIndirect variable expansion via eval:
get_default_log_level/get_default_log_labelusedeval echo \$$var_name— shell metacharacters in variable values could be executedFix Description & Rationale
This patch makes minimal, targeted changes to eliminate
evalusage on user-controllable inputs:expand_path()function~/...paths using parameter expansion (${HOME}/${path#"~/"}) instead ofeval echo. Replaces 12 instances ofeval echo "$(env_manager get ...)"acrossrun_harbor_doctor,link_cli,unlink_cli,run_harbor_find, andrun_fixfs.$0run_runnow executes aliases by re-dispatching through Harbor's own command handler ($0 $maybe_cmd) instead ofeval "$maybe_cmd". This ensures aliases can only invoke valid Harbor subcommands.-e "HARBOR_PULL_MODEL=$model") and referenced as$HARBOR_PULL_MODELinside the script, instead of being interpolated into the shell string.$0run_historyuses$0 $(cat "$output_file")instead ofeval "$(cat "$output_file")".${!var_name}get_default_log_level/get_default_log_labeluse Bash indirect expansion instead ofeval echo.download_profilenow rejects profiles containing backticks or$()in non-comment lines, and warns users to review downloaded profiles.Files changed:
harbor.sh(55 lines changed),test_security_fix.sh(new, 354 lines)Test Results
All 26 tests pass (0 failures, 0 skips):
Disprove Analysis (Self-Adversarial Review)
We attempted to disprove our own findings. Here is the honest assessment:
What's genuinely valid
.envfile downloaded from a URL gets its values executed viaeval. The fix correctly addresses this by both rejecting suspicious profiles AND removingevalfrom the consumption paths.What's overstated (severity adjustment)
harbor config set: A user setting their own config to malicious values on their own machine is not a meaningful vulnerability — they already have shell access and could just run the malicious command directly.Mitigations already present
harbor profile set <url>— similar to piping a curl download to sh.curl --head --fail) before reaching the injection point — metacharacters would likely fail this HTTP check.Preconditions for exploitation
For the most realistic attack (malicious profile download):
.envfile at a URLharbor profile set <malicious-url>harbor doctor,harbor link)Exploit sketch (profile download, pre-fix)
Post-fix: Rejected at download time by metacharacter validation, and even if the validation were bypassed,
expand_pathdoes not execute embedded commands.Remaining eval usage
30
evaloccurrences remain inharbor.sh. These are inenv_managerinternals operating on programmer-controlled strings (hardcoded service configuration, internal callbacks), not user-supplied input. They are outside the scope of this fix.Verdict
LIKELY_VALID at Medium severity. The fix is technically sound, improves code quality, and eliminates unnecessary
evalusage on user-controllable data. The profile download path is a genuine (if socially-engineered) external input vector. Submitting because replacingevalwith safer alternatives is good practice, and the profile download validation is a sensible addition.No prior security reports or SECURITY.md
No prior security issues were found in the GitHub issue tracker, and no
SECURITY.mdexists in the repository.