Repository navigation
Fix code injection in dxf2code through unescaped table and block names - #1491
Closed
1915rising wants to merge 1 commit into
Closed
1915rising wants to merge 1 commit into
1915rising wants to merge 1 commit into
Conversation
Owner
|
See my comments at #1489 The problem is real, but for different reasons. |
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. |
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.
Fixes #1489.
ezdxf.addons.dxf2codeinterpolated 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 throughjson.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 lineif 'Walls' if exec(...) else '' not in doc.layers:and the payload runs with the generated script. After this fix the generated line isif "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 throughjson.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.'{dxftype}'literal ingeneric_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 reachblocks.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 afterpytest tests/test_08_addons/: 718 passed, 1 skipped aftermypy --ignore-missing-imports -p ezdxf: no issues before and after