codegen: escape string default values to prevent code injection#8964
codegen: escape string default values to prevent code injection#8964jtdavis777 merged 2 commits intogoogle:masterfrom
Conversation
ade8648 to
c5f531c
Compare
|
hey @KevinZhao thank you for a fantastic PR and bug fix! This will take me a little time to ingest but I will run the workflows and plan to review and get merged. |
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.
c5f531c to
cb9398a
Compare
|
Thanks @aardappel — good call. Removed The diff no longer touches |
|
Hi @aardappel @jtdavis777 — I've addressed the review feedback (removed Happy to rebase on latest master if needed. Could you take another look when you get a chance? |
issues were addressed by the PR creator -- will re-review myself :D
|
@KevinZhao LGTM - would you mind at your leisure updating your PR description? will merge this anyway but would be nice if the description was accurate |
Summary
String default values parsed from
.fbsschemas are un-escaped by the IDL parser (e.g.,\x22becomes a raw"byte), but code generators embed these raw values directly into generated source code string literals without re-escaping. This allows specially crafted.fbsfiles to inject arbitrary code into generated C++, Rust, TypeScript, and Swift source files.Changes
Apply
EscapeString()to re-escape string default values at all 7 affected code generation sites across 5 generators:idl_gen_cpp.cpp): bfbs_string literal + CreateString defaultidl_gen_rust.cpp): string default valueidl_gen_ts.cpp): string default valueidl_gen_swift.cpp): string reader default + 2 constructor defaultsidl_gen_fbs.cpp): schema regeneration (strings only, not numeric defaults)No new helpers introduced — all sites call
flatbuffers::EscapeString()directly, per reviewer feedback.Test plan
cmake --build build)\x22in string defaults is correctly escaped to\"in generated C++ codeResolves the TODO comments in
idl_gen_cpp.cppandidl_gen_rust.cpp.