Skip to content

Fix code injection in dxf2code through unescaped table and block names - #1491

Closed
1915rising wants to merge 1 commit into
mozman:masterfrom
1915rising:fix/1489-dxf2code-name-escaping
Closed

1915rising wants to merge 1 commit into
mozman:masterfrom
1915rising:fix/1489-dxf2code-name-escaping

Conversation

@1915rising

Copy link
Copy Markdown
Contributor

Fixes #1489.

ezdxf.addons.dxf2code interpolated table entry names into the generated Python source inside single quotes without escaping. A layer name that contains a quote breaks out of the string literal, and the injected code runs when the generated script is executed. Every other string in the generator already goes through json.dumps(); the entry name was the exception.

Reproduction (the issue PoC, run in a sandbox): a document with a layer name of the form Walls' if exec(...) else ' produces the generated line if 'Walls' if exec(...) else '' not in doc.layers: and the payload runs with the generated script. After this fix the generated line is if "Walls' if exec(...) else '" not in doc.layers:, the payload does not run, and the layer is recreated with its exact name.

Fix:

  • new_table_entry(): the entry name is emitted through json.dumps(), like all other strings in the module. This covers LAYER, LTYPE, STYLE, DIMSTYLE and APPID entries.
  • block_to_code(): the block name gets the same treatment. The Python API rejects a double quote in block names, but the DXF loader accepts it, so the name is reachable from a loaded file.
  • The '{dxftype}' literal in generic_api_call() is unchanged on purpose: every call site passes a hard-coded type string, so it cannot carry file data.

Tests: two regression tests in tests/test_08_addons/test_803_entities_to_code.py. A quote-bearing layer name must round-trip as data and execute nothing; a double-quote block name loaded from a DXF stream must reach blocks.new() as data. Both tests fail on the unfixed code and pass with the fix.

  • pytest tests/test_08_addons/test_803_entities_to_code.py: 26 passed before, 28 passed after
  • pytest tests/test_08_addons/: 718 passed, 1 skipped after
  • mypy --ignore-missing-imports -p ezdxf: no issues before and after

@mozman

mozman commented Nov 26, 2026

Copy link
Copy Markdown
Owner

See my comments at #1489

The problem is real, but for different reasons.

@mozman mozman closed this Nov 26, 2026
@1915rising

Copy link
Copy Markdown
Contributor Author

Understood, thank you for the explanation. A quotation mark is an invalid character in DXF table names, reliable CAD applications do not export such names, and files from unreliable sources are meant to go through ezdxf.recover and the Auditor, which checks table names. I will not pursue this further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ezdxf: code injection in dxf2code output via unescaped DXF table entry names (CWE-94)

2 participants