Skip to content

Fix code injection via unescaped doc comments in Java/Kotlin/C# generators#9038

Open
YLChen-007 wants to merge 1 commit intogoogle:masterfrom
YLChen-007:fix-doccomment-injection
Open

Fix code injection via unescaped doc comments in Java/Kotlin/C# generators#9038
YLChen-007 wants to merge 1 commit intogoogle:masterfrom
YLChen-007:fix-doccomment-injection

Conversation

@YLChen-007
Copy link
Copy Markdown

Summary

Fix a Code Injection vulnerability (CWE-94 / CWE-116) in the flatc compiler where documentation comments (///) from .fbs schema files were directly concatenated into generated source code without sanitizing the block comment closing sequence */.

Details

When generating Java, Kotlin, or C# source code, flatc wraps documentation comments inside /** ... */ Javadoc/KDoc-style block comments. An attacker who provides a malicious .fbs schema can include the */ character sequence in a documentation comment to prematurely close the generated comment block, thereby injecting arbitrary code into the generated source files.

For example, this malicious schema:

table Pwn {
  /// */ public static void main(String[] args) { Runtime.getRuntime().exec("malicious_command"); } /*
  name: string;
}
root_type Pwn;

Would generate a Java file where the payload escapes the comment block and becomes executable code.

Fix

This PR sanitizes the */ sequence by replacing it with * / (inserting a space) before emitting it into the generated output. The fix is applied to all three locations where comment content is written:

  1. GenComment() in src/code_generators.cpp — shared by the Java and C# generators
  2. GenerateComment() in src/idl_gen_kotlin.cpp — Kotlin generator
  3. GenerateComment() in src/idl_gen_kotlin_kmp.cpp — Kotlin KMP generator

Testing

  • Built and verified the fix against a malicious PoC schema — the payload now stays safely inside the comment block
  • All existing flattests pass: ALL TESTS PASSED

Before the patch (malicious code escapes the comment):

/**
 * */ public static void main(String[] args) { ... } /*
 */

After the patch (payload remains inside the comment):

/**
 * * / public static void main(String[] args) { ... } /*
 */

@github-actions github-actions bot added c++ codegen Involving generating code from schema kotlin labels Apr 10, 2026
@google-cla
Copy link
Copy Markdown

google-cla bot commented Apr 10, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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
@YLChen-007 YLChen-007 force-pushed the fix-doccomment-injection branch from 7870fc8 to 7120d3c Compare April 12, 2026 02:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ codegen Involving generating code from schema kotlin

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant