Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/ezdxf/addons/dxf2code.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ def block_to_code(
block_name = dxfattribs.pop("name")
base_point = dxfattribs.pop("base_point")
code = _SourceCodeGenerator(layout="b")
prolog = f'b = {drawing}.blocks.new("{block_name}", base_point={base_point}, dxfattribs={{'
prolog = (
f"b = {drawing}.blocks.new({json.dumps(block_name)}, "
f"base_point={base_point}, dxfattribs={{"
)
code.add_source_code_line(prolog)
code.add_source_code_lines(_fmt_mapping(dxfattribs, indent=4))
code.add_source_code_line(" }")
Expand Down Expand Up @@ -474,11 +477,11 @@ def new_table_entry(self, dxftype: str, dxfattribs: dict) -> Iterable[str]:
"""
table = f"{self.doc}.{TABLENAMES[dxftype]}"
dxfattribs = _purge_handles(dxfattribs)
name = dxfattribs.pop("name")
name = json.dumps(dxfattribs.pop("name"))
s = [
f"if '{name}' not in {table}:",
f"if {name} not in {table}:",
f" t = {table}.new(",
f" '{name}',",
f" {name},",
" dxfattribs={",
]
s.extend(_fmt_mapping(dxfattribs, indent=12))
Expand Down
52 changes: 52 additions & 0 deletions tests/test_08_addons/test_803_entities_to_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,27 @@ def test_layer_entry():
assert layer.dxf.color == 3


def test_layer_entry_name_with_quote_is_data():
"""Regression test for issue #1489.

A table entry name that contains a quote is data in the generated
source code. The name must not break out of the string literal and
must not execute code.
"""
from ezdxf.entities.layer import Layer

executed = []
name = "Evil' if executed.append(1) else '"
layer = Layer.new("LAYER", dxfattribs={"name": name, "color": 3})
code = table_entries_to_code([layer], drawing="doc")
namespace = dict(globals())
namespace["executed"] = executed
exec(str(code), namespace)
assert executed == []
layer = doc.layers.get(name)
assert layer.dxf.color == 3


def test_ltype_entry():
from ezdxf.entities.ltype import Linetype

Expand Down Expand Up @@ -455,6 +476,37 @@ def test_block_to_code():
assert new_block[0].dxftype() == block[0].dxftype()


def test_block_to_code_name_with_double_quote_is_data():
"""Regression test for issue #1489.

A block name that contains a double quote is data in the generated
source code. The Python API rejects the name, but the DXF loader
accepts the name, so the name can come from a loaded file.
"""
import io

testdoc = ezdxf.new()
testdoc.blocks.new("Normal")
stream = io.StringIO()
testdoc.write(stream)
name = 'Test"Block'
loaded = ezdxf.read(io.StringIO(stream.getvalue().replace("Normal", name)))
block = loaded.blocks.get(name)
code = block_to_code(block, drawing="doc")

created = {}

class FakeBlocks:
def new(self, name, base_point=None, dxfattribs=None):
created["name"] = name

class FakeDoc:
blocks = FakeBlocks()

exec(str(code), {"doc": FakeDoc()})
assert created["name"] == name


def test_hatch_to_code():
from ezdxf.entities import Hatch

Expand Down