Escape file_identifier in code generators to prevent injection via .bfbs#9083
Escape file_identifier in code generators to prevent injection via .bfbs#9083kagancapar wants to merge 1 commit intogoogle:masterfrom
Conversation
The `file_identifier` field was embedded raw into generated string literals across all code generators. A crafted .bfbs schema with a malicious file_ident value could inject arbitrary code into generated source files (C++, Rust, Go, Java, C#, Kotlin, PHP, TypeScript, Swift). This commit applies `flatbuffers::EscapeString` to all file_identifier embedding sites (20+ locations across 10 generators), consistent with the approach used for string defaults in PR google#8964. Additionally, BFBS deserialization in idl_parser.cpp now rejects file_identifier values that are not exactly 4 bytes, matching the validation already enforced for .fbs schemas. Python generator was already immune (hex-escapes all characters).
|
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. |
|
This is a variant of the codegen injection class fixed in #8964 (string defaults). That PR applied Verified locally: built |
Summary
The
file_identifierfield is embedded unescaped into generated string literals across 10 of 11 code generators. Whenflatcprocesses a crafted.bfbsschema (which has no length limit onfile_ident, unlike.fbswhich enforces exactly 4 bytes), an attacker-controlledfile_identifiercan break out of the string literal and inject arbitrary code into the generated source.This is the same class of codegen injection that was fixed for string defaults in #8964 — but
file_identifierwas not covered by that patch.Attack chain
.bfbsfile with a maliciousfile_identfieldflatcwith any language flag on the malicious.bfbsFix
idl_parser.cpp: Reject.bfbsschemas wherefile_identis not exactlykFileIdentifierLength(4) bytes — matching the validation already enforced for.fbsschemasflatbuffers::EscapeString()to allfile_identifierembedding sites (20+ locations), so that special characters (",\, control chars) are properly escaped before being placed inside string literalsThis is consistent with the approach used for string defaults in #8964.
Affected generators & sites patched
idl_gen_cpp.cppreturn "IDENT"idl_gen_rust.cpp= "IDENT"idl_gen_go.cppconst Identifier = "IDENT"idl_gen_java.cpp__has_identifier,finishidl_gen_csharp.cpp__has_identifier,VerifyBuffer,Finishidl_gen_kotlin.cpp__has_identifier,finish,finishSizePrefixedidl_gen_kotlin_kmp.cpphasIdentifier,finish,finishSizePrefixedidl_gen_php.cppreturn "IDENT",finishidl_gen_ts.cppfinish,__has_identifieridl_gen_swift.cppstatic var idtemplateRelated PRs
file_extensioninjection (separate PR)