Skip to content

Commit 7120d3c

Browse files
committed
Fix code injection via unescaped doc comments in code generators
Sanitize documentation comments by escaping the block comment closing sequence '*/' to '* /' before emitting them into generated source code. This prevents a malicious .fbs schema from injecting arbitrary code into generated Java, Kotlin, and C# files by prematurely terminating the Javadoc/KDoc block comment. The fix is applied to three locations: - GenComment() in code_generators.cpp (used by Java and C# generators) - GenerateComment() in idl_gen_kotlin.cpp - GenerateComment() in idl_gen_kotlin_kmp.cpp
1 parent 4e582b0 commit 7120d3c

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/code_generators.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,14 @@ void GenComment(const std::vector<std::string>& dc, std::string* code_ptr,
212212
? config->content_line_prefix
213213
: "///");
214214
for (auto it = dc.begin(); it != dc.end(); ++it) {
215-
code += line_prefix + *it + "\n";
215+
std::string sanitized = *it;
216+
// Sanitize comment content: escape block comment closing sequence
217+
// to prevent code injection via premature comment termination.
218+
for (size_t pos = sanitized.find("*/"); pos != std::string::npos;
219+
pos = sanitized.find("*/", pos + 2)) {
220+
sanitized.replace(pos, 2, "* /");
221+
}
222+
code += line_prefix + sanitized + "\n";
216223
}
217224
if (config != nullptr && config->last_line != nullptr) {
218225
code += std::string(prefix) + std::string(config->last_line) + "\n";

src/idl_gen_kotlin.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,14 @@ class KotlinGenerator : public BaseGenerator {
14101410
? config->content_line_prefix
14111411
: "///");
14121412
for (auto it = dc.begin(); it != dc.end(); ++it) {
1413-
writer += line_prefix + *it;
1413+
std::string sanitized = *it;
1414+
// Sanitize comment content: escape block comment closing sequence
1415+
// to prevent code injection via premature comment termination.
1416+
for (size_t pos = sanitized.find("*/"); pos != std::string::npos;
1417+
pos = sanitized.find("*/", pos + 2)) {
1418+
sanitized.replace(pos, 2, "* /");
1419+
}
1420+
writer += line_prefix + sanitized;
14141421
}
14151422
if (config != nullptr && config->last_line != nullptr) {
14161423
writer += std::string(config->last_line);

src/idl_gen_kotlin_kmp.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,14 @@ class KotlinKMPGenerator : public BaseGenerator {
13931393
? config->content_line_prefix
13941394
: "///");
13951395
for (auto it = dc.begin(); it != dc.end(); ++it) {
1396-
writer += line_prefix + *it;
1396+
std::string sanitized = *it;
1397+
// Sanitize comment content: escape block comment closing sequence
1398+
// to prevent code injection via premature comment termination.
1399+
for (size_t pos = sanitized.find("*/"); pos != std::string::npos;
1400+
pos = sanitized.find("*/", pos + 2)) {
1401+
sanitized.replace(pos, 2, "* /");
1402+
}
1403+
writer += line_prefix + sanitized;
13971404
}
13981405
if (config != nullptr && config->last_line != nullptr) {
13991406
writer += std::string(config->last_line);

0 commit comments

Comments
 (0)