Skip to content

codegen: escape string default values to prevent code injection#8964

Open
KevinZhao wants to merge 1 commit intogoogle:masterfrom
KevinZhao:fix/escape-string-defaults
Open

codegen: escape string default values to prevent code injection#8964
KevinZhao wants to merge 1 commit intogoogle:masterfrom
KevinZhao:fix/escape-string-defaults

Conversation

@KevinZhao
Copy link

Summary

String default values parsed from .fbs schemas are un-escaped by the IDL parser (e.g., \x22 becomes a raw " byte), but code generators embed these raw values directly into generated source code string literals without re-escaping. This allows specially crafted .fbs files to inject arbitrary code into generated C++, Rust, TypeScript, and Swift source files.

Changes

  • Add EscapeCodeGenString() helper in util.h that re-escapes string content for safe embedding in generated code, reusing the existing EscapeString() infrastructure
  • Apply escaping to all 7 affected injection points across 5 code generators:
    • C++ (idl_gen_cpp.cpp): bfbs_string literal + CreateString default
    • Rust (idl_gen_rust.cpp): string default value
    • TypeScript (idl_gen_ts.cpp): string default value
    • Swift (idl_gen_swift.cpp): string reader default + 2 constructor defaults
    • FBS (idl_gen_fbs.cpp): schema regeneration

Test plan

  • Build passes (cmake --build build)
  • Verified: \x22 in string defaults is correctly escaped to \" in generated C++ code
  • CI tests should pass (existing tests use normal string defaults, unaffected by this change)

Resolves the TODO comments in idl_gen_cpp.cpp:2782 and idl_gen_rust.cpp:1141.

String default values parsed from .fbs schemas are un-escaped by the IDL
parser (e.g., \x22 becomes a raw " byte), but code generators embed these
raw values directly into generated source code string literals. This allows
specially crafted .fbs files to break out of string literals and inject
arbitrary code into generated C++, Rust, TypeScript, and Swift source.

Fix by adding EscapeCodeGenString() helper that re-escapes string content
before embedding, and applying it to all 7 affected injection points across
5 code generators (C++, Rust, TypeScript, Swift, FBS).

Resolves the TODO comments in idl_gen_cpp.cpp and idl_gen_rust.cpp.
@KevinZhao KevinZhao requested a review from dbaileychess as a code owner March 7, 2026 12:19
@github-actions github-actions bot added c++ javascript rust typescript codegen Involving generating code from schema swift labels Mar 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant