Skip to content

security: sanitize schema strings in code generators#8998

Open
mohammadmseet-hue wants to merge 1 commit intogoogle:masterfrom
mohammadmseet-hue:fix/sanitize-schema-strings
Open

security: sanitize schema strings in code generators#8998
mohammadmseet-hue wants to merge 1 commit intogoogle:masterfrom
mohammadmseet-hue:fix/sanitize-schema-strings

Conversation

@mohammadmseet-hue
Copy link
Copy Markdown

Summary

The C++, PHP, and Rust code generators concatenate parsed schema string values directly into generated source code without escaping. A crafted .fbs schema with a malicious file_extension or native_include value can inject arbitrary code into the generated output.

This adds SanitizeStringForCodeGen() to escape backslashes, quotes, newlines, and control characters before embedding schema strings. Applied to all affected code paths.

Similar to the fix for CVE-2023-36665 in protobuf's ruby_package generator. The Python generator already hex-escapes file_identifier, confirming this risk was previously recognized.

Changes

  • include/flatbuffers/util.h — new SanitizeStringForCodeGen() utility
  • src/idl_gen_cpp.cpp — sanitize file_extension and native_include
  • src/idl_gen_php.cpp — sanitize file_extension
  • src/idl_gen_rust.cpp — sanitize file_extension

Reproduction

table Monster { name:string; }
root_type Monster;
file_extension "ext\";\n}\n#include <cstdlib>\nstatic struct _X { _X() { system(\"id\"); } } _x;\ninline const char* _D() { return \"";

Running flatc --cpp on this schema generates a header with a static initializer that executes system("id") when compiled and run.

…tion

The .fbs parser processes escape sequences in string constants, but the
C++, PHP, and Rust code generators concatenate the parsed (unescaped)
values directly into generated source code. A crafted file_extension or
native_include value containing newlines and quotes can inject arbitrary
code into the generated output.

This adds SanitizeStringForCodeGen() which escapes backslashes, quotes,
newlines, and other control characters before embedding schema strings
in generated source code. Applied to:
- C++ generator: file_extension (line 705) and native_include (line 266)
- PHP generator: file_extension (line 801)
- Rust generator: file_extension (line 2660)

Similar to the fix for CVE-2023-36665 in protobuf's ruby_package
generator. The Python generator already hex-escapes file_identifier,
confirming this risk was previously recognized.
@github-actions github-actions bot added c++ rust codegen Involving generating code from schema php labels Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ codegen Involving generating code from schema php rust

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant