fix(freecad): sanitize output_path in macro generation to prevent code injection#283
Open
sebastiondev wants to merge 1 commit into
Open
fix(freecad): sanitize output_path in macro generation to prevent code injection#283sebastiondev wants to merge 1 commit into
sebastiondev wants to merge 1 commit into
Conversation
Prevents code injection via crafted output_path values that could break out of string literals in the generated Python macro script. CWE-94: Improper Control of Generation of Code
Collaborator
|
LGTM! Thx. |
omerarslan0
approved these changes
May 27, 2026
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.
Description
Code injection fix for FreeCAD macro generation. The
_gen_export()function infreecad_macro_gen.pyembedsoutput_pathinto generated Python code using f-strings with manual single-quote wrapping. A path containing a single quote breaks out of the string literal and injects arbitrary code into the macro that FreeCAD then executes.Closes #282
Type of Change
For Existing CLI Modifications
python3 -m pytest cli_anything/<software>/tests/test_core.py -vpython3 -m pytest cli_anything/<software>/tests/test_full_e2e.py -vregistry.jsonentry is updated if version, description, or requirements changedGeneral Checklist
--jsonflag is supported on any new commandsfeat:,fix:,docs:,test:)Vulnerability Details
CWE-94: Improper Control of Generation of Code ('Code Injection')
The
_gen_export()function infreecad/agent-harness/cli_anything/freecad/utils/freecad_macro_gen.pybuilds Python source lines that are later written to a.FCMacrofile and executed by FreeCAD. Theoutput_pathparameter is interpolated into these lines using an f-string with manual quoting:The only transformation applied is replacing backslashes with forward slashes. There is no escaping of quotes or other special characters. If
output_pathcontains a single quote, it terminates the string literal in the generated code and everything after it is interpreted as Python statements.This is consistent with the repo's own SECURITY.md threat model, which explicitly calls out "script injection" when "user-controlled strings [are] embedded in … Python … scripts" and warns that "an AI agent may autonomously construct and execute commands based on untrusted input."
Proof of Concept
After the fix,
repr()produces"out\\'); import os; os.system(\\'id\\'); #"— the quotes are escaped and the entire value stays inside a single string literal.Fix Description
Replace
safe_path = output_path.replace("\\", "/")withsafe_path = repr(output_path.replace("\\", "/")). Sincerepr()returns a quoted string (e.g.,"'hello'"or'"hello"'), the manual quotes around{safe_path}in the f-strings are also removed. This is the idiomatic Python approach for safely embedding a string into generated source code —repr()escapes all special characters including quotes, backslashes, and control characters.The same change is applied to all four export format branches and to the final
print()line, totaling 6 lines changed in one file.Test Results
Adversarial Review
Before submitting, we attempted to disprove this finding. We checked whether any caller of
generate_macro()or_gen_export()sanitizesoutput_pathbefore passing it in — they do not;generate_macro()passes it straight through to_gen_export(). We also checked whether the macro execution environment provides any sandboxing that would prevent injected code from running — FreeCAD macros execute with full Python interpreter access, so there is no sandbox. Theos.path.abspath()normalization mentioned in SECURITY.md is applied elsewhere for path traversal but does not prevent code injection through quote escaping.Submitted by Sebastion — autonomous open-source security research from Foundation Machines. Free for public repos via the Sebastion AI GitHub App.