Skip to content

Escape .proto comments emitted as docstrings - #685

Open
r3vpwnx wants to merge 1 commit into
danielgtaylor:masterfrom
r3vpwnx:fix-comment-docstring-escape
Open

r3vpwnx wants to merge 1 commit into
danielgtaylor:masterfrom
r3vpwnx:fix-comment-docstring-escape

Conversation

@r3vpwnx

@r3vpwnx r3vpwnx commented Nov 26, 2026

Copy link
Copy Markdown

Fixes #684.

get_comment() in src/betterproto/plugin/models.py copied .proto comment text verbatim into a triple-quoted docstring. A comment containing """ ended the docstring and the rest of the comment became code in the generated module, which ran when the module was imported.

This escapes backslashes and double quotes in each comment line before the docstring is built. Escaping every " (not only """) also covers a comment that ends with a quote, which would otherwise produce """" and break the generated file. The docstrings read the same at runtime; only the source representation changes.

Test: tests/inputs/documentation_escaping has comments with """, embedded and trailing quotes and a backslash; tests/test_documentation_escaping.py imports the generated module and checks the docstring text. Without the fix the plugin fails to generate this input; with it, the new test and the existing test_documentation.py pass.

get_comment() copied comment text verbatim into a triple-quoted docstring,
so a comment containing """ ended the docstring and the rest of the line
became code in the generated module, which ran on import. Escape backslashes
and double quotes before emitting the docstring.

Fixes danielgtaylor#684
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.

betterproto: code injection into generated Python via unescaped .proto comments, runs on import (CWE-94)

1 participant