fix: escape attribute-based code injection vectors in C++ codegen (bypass of #8964 fix)#9068
Open
adilburaksen wants to merge 2 commits intogoogle:masterfrom
Open
fix: escape attribute-based code injection vectors in C++ codegen (bypass of #8964 fix)#9068adilburaksen wants to merge 2 commits intogoogle:masterfrom
adilburaksen wants to merge 2 commits intogoogle:masterfrom
Conversation
…pass of google#8964 fix) The fix in google#8964 (commit 8afb68f) patched code injection via string default values in FlatBuffers C++ codegen, but missed four additional injection vectors where attribute values are embedded verbatim into generated C++ source: 1. native_include: the path is embedded in `#include "..."` — a `"` character or newline in the path value closes the directive and allows content injection. 2. native_type: the value is used as a C++ type name in typedefs and struct declarations — characters like `}`, `;`, `{` allow breaking out of the type context and injecting arbitrary declarations. 3. cpp_type: same risk as native_type; the value is used as a C++ type name in generated member declarations and smart pointer type expressions. 4. cpp_ptr_type_get: the value is returned verbatim as a C++ method call expression — a `;` allows injecting additional statements. Fix by adding: - IsValidCppTypeExpression(): allows only characters legal in a C++ type name or type expression (alphanumerics, _::< >*& ,.()); rejects anything else. - IsValidIncludePath(): rejects paths containing `"`, `\n`, or `\r`. - ValidateAttributeSafety(): upfront validation pass in generate() that checks native_type, cpp_type, and cpp_ptr_type_get on all struct/field definitions before any code is emitted, returning false on the first unsafe value found. - Inline check in GenIncludeDependencies() for each native_include path. All four vectors now cause flatc to exit non-zero with a descriptive error message rather than silently emitting injectable output. Adds tests/attribute_injection_test.fbs documenting all four injection vectors and a safe-attribute baseline that must still compile successfully.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The fix in #8964 (commit 8afb68f) patched code injection via string default values in FlatBuffers C++ codegen, but missed four additional injection vectors where attribute values are embedded verbatim into generated C++ source.
Affected vectors
native_include#include "..."path"or newline closes the directivenative_type},;,{break out of type contextcpp_typenative_typecpp_ptr_type_get;injects additional statementsReproduction (before this fix)
Generated (pre-fix):
Fix
IsValidCppTypeExpression(): allows only characters legal in a C++ type name ([A-Za-z0-9_::< >*& ,.()])) — rejects anything else.IsValidIncludePath(): rejects paths containing",\n, or\r.ValidateAttributeSafety(): upfront validation pass at the start ofgenerate()that checksnative_type,cpp_type, andcpp_ptr_type_geton all struct/field definitions before any code is emitted.GenIncludeDependencies()for eachnative_includepath.All four vectors now cause
flatcto exit non-zero with a descriptive error message.Test
tests/attribute_injection_test.fbsdocuments all four injection vectors (as comments, since they are expected to be rejected) and provides a safe-attribute baseline table that must continue to compile successfully.Relationship to #8964
This PR is a targeted follow-up to #8964 covering the remaining injection surface in the C++ code generator. The same
.fbstrust-boundary issue applies: a malicious or compromised.fbsschema processed byflatccan produce C++ source that executes attacker-controlled code when compiled and run.