Skip to content

feat(spec): warn on dubious $ref locations#258

Merged
fredbi merged 1 commit into
go-openapi:masterfrom
fredbi:feat/warn-on-dubious-ref-locations
Jun 10, 2026
Merged

feat(spec): warn on dubious $ref locations#258
fredbi merged 1 commit into
go-openapi:masterfrom
fredbi:feat/warn-on-dubious-ref-locations

Conversation

@fredbi

@fredbi fredbi commented Jun 10, 2026

Copy link
Copy Markdown
Member

Adds a warning-only check (validateDubiousRefs) over the unexpanded spec's $refs, to flag location patterns that may indicate an unsafe or adversarial spec when running spec validation:

  • Rule 1: an absolute local $ref (file://, Unix /abs, Windows C:, UNC) is flagged when it escapes the spec's base path. Absolute refs that stay beneath the base path are legitimate (flatten/expand introduces such anchors for cyclical $refs) and are not flagged. Relative and fragment-only refs are exempt.
  • Rule 2: when remote (http/https or protocol-relative) $refs resolve to two or more distinct hosts, a single aggregate warning lists them. A single consistent remote host is not flagged.

Findings are warnings only and do not affect validity. The check inspects refs as authored via analyzer.AllRefs(), before expansion.

The go-openapi/spec path normalization helpers are unexported, so a minimal slash/drive-letter normalize and a lexical "beneath base" containment check are replicated locally (cleanRefPath, isBeneathBase, localBaseDir).

Change type

Please select: 🆕 New feature or enhancement|🔧 Bug fix'|📃 Documentation update

Short description

Fixes

Full description

Checklist

  • I have signed all my commits with my name and email (see DCO. This does not require a PGP-signed commit
  • I have rebased and squashed my work, so only one commit remains
  • I have added tests to cover my changes.
  • I have properly enriched go doc comments in code.
  • I have properly documented any breaking change.

@codecov

codecov Bot commented Jun 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.50562% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.41%. Comparing base (0fa122c) to head (838587a).
⚠️ Report is 1 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
spec_ref_warnings.go 95.18% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #258      +/-   ##
==========================================
+ Coverage   92.33%   92.41%   +0.08%     
==========================================
  Files          23       24       +1     
  Lines        3183     3271      +88     
==========================================
+ Hits         2939     3023      +84     
- Misses        160      163       +3     
- Partials       84       85       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

Adds a warning-only check (validateDubiousRefs) over the unexpanded spec's
$refs, to flag location patterns that may indicate an unsafe or adversarial
spec when running spec validation:

- Rule 1: an absolute local $ref (file://, Unix /abs, Windows C:\, UNC) is
  flagged when it escapes the spec's base path. Absolute refs that stay
  beneath the base path are legitimate (flatten/expand introduces such
  anchors for cyclical $refs) and are not flagged. Relative and
  fragment-only refs are exempt.
- Rule 2: when remote (http/https or protocol-relative) $refs resolve to
  two or more distinct hosts, a single aggregate warning lists them. A
  single consistent remote host is not flagged.

Findings are warnings only and do not affect validity. The check inspects
refs as authored via analyzer.AllRefs(), before expansion.

The go-openapi/spec path normalization helpers are unexported, so a minimal
slash/drive-letter normalize and a lexical "beneath base" containment check
are replicated locally (cleanRefPath, isBeneathBase, localBaseDir).

Windows drive paths need care so the "beneath base" comparison holds across
every form they can be authored in. All of: bare C:\ / C:/, the canonical
file:///C:/ URL, and the (invalid but tolerated) hybrid file://C:/ form
(whose drive letter parses into the URL host) normalize to the same drive
path with no leading slash, matching the base derived from SpecFilePath.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
@fredbi fredbi force-pushed the feat/warn-on-dubious-ref-locations branch from b8e81f6 to 838587a Compare June 10, 2026 13:31
@fredbi fredbi merged commit 84c917d into go-openapi:master Jun 10, 2026
57 of 69 checks passed
@fredbi fredbi deleted the feat/warn-on-dubious-ref-locations branch June 10, 2026 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant