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:
Kevin Zhao
2026-03-19 10:01:23 +08:00
committed by GitHub
parent 2e07f269b9
commit 8afb68f074
5 changed files with 54 additions and 18 deletions

View File

@@ -529,7 +529,11 @@ class TsGenerator : public BaseGenerator {
if (value.constant == "0" || value.constant == "null") {
return "null";
} else {
return "\"" + value.constant + "\"";
std::string escaped;
flatbuffers::EscapeString(value.constant.c_str(),
value.constant.length(), &escaped,
true, false);
return escaped;
}
}
case BASE_TYPE_UNION: