forked from BigfootDev/flatbuffers
codegen: escape string default values to prevent code injection (#8964)
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.
This commit is contained in:
@@ -1138,9 +1138,14 @@ class RustGenerator : public BaseGenerator {
|
||||
// need one for Rust's Default trait so we use empty string. The usual
|
||||
// value of field.value.constant is `0`, which is non-sensical except
|
||||
// maybe to c++ (nullptr == 0).
|
||||
// TODO: Escape strings?
|
||||
const std::string defval =
|
||||
field.IsRequired() ? "\"\"" : "\"" + field.value.constant + "\"";
|
||||
std::string defval;
|
||||
if (field.IsRequired()) {
|
||||
defval = "\"\"";
|
||||
} else {
|
||||
flatbuffers::EscapeString(field.value.constant.c_str(),
|
||||
field.value.constant.length(), &defval,
|
||||
true, false);
|
||||
}
|
||||
if (context == kObject) {
|
||||
return "alloc::string::ToString::to_string(" + defval + ")";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user