Skip to content

Security req rule - #290

Merged
fredbi merged 5 commits into
go-openapi:masterfrom
fredbi:security-req-rule
Aug 24, 2026
Merged

Security req rule#290
fredbi merged 5 commits into
go-openapi:masterfrom
fredbi:security-req-rule

Conversation

@fredbi

@fredbi fredbi commented Aug 24, 2026

Copy link
Copy Markdown
Member

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.

fredbi and others added 5 commits August 24, 2026 19:20
golangci-lint renamed exhaustruct to exhaustruct_v5, and the disable list
carried only the old name, so the linter fired 120 times across the repo.
wsl/wsl_v5 and gomodguard/gomodguard_v2 already list both names.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
A security requirement names a scheme and lists the scopes an operation
needs from it. The JSON meta-schema types it as an object of string
arrays and never reads securityDefinitions, so it cannot check either
against the other.

validateSecurityRequirements walks the document's own security array and
each operation's, and reports:

- a name securityDefinitions does not declare (error);
- scopes on a scheme that is not oauth2, which must list none (error);
- an oauth2 scope its scheme does not declare (warning).

The last one warns rather than errors because Swagger 2.0 never says a
requirement and its scheme must agree on scope names. testdata/bugs/2649
(the GoToSocial spec) requires read:bookmarks, read:reports and
write:reports without declaring any of them, and stays valid.

Findings carry a location, /paths/~1pets/get/security/0/basic_auth. An
empty requirement object and an empty security array both pass: they
mean optional security and no security.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
A discriminator names the property that tells subtypes apart, and Swagger
2.0 asks two things of it: the schema must define that property, and must
list it as required. An instance carries its subtype in that property, so
a subtype cannot be resolved from an instance with nowhere to put the
value, or free to leave it out. The JSON meta-schema types discriminator
as a plain string and never compares it against properties or required,
so it can express neither check.

validateDiscriminators walks every definition and the schemas it holds
inline, and reports both faults where they apply. A property an allOf
member contributes counts as defined, and one that member requires counts
as required: declaresProperty already reads a composed definition that
way for the required rule, and requiresProperty is its required-list
counterpart. A schema written as a $ref is left alone, so a fault is
reported where the definition is written rather than once per pointer at
it, and a recursive definition terminates.

The third clause, that the value must name this schema or one that
inherits it, constrains the data rather than the document. It stays
unimplemented and moves to the known limitations in doc.go.

Fixes go-openapi#54

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
collectionFormat says how to join the members of an array into one value
on the wire. There is nothing to join when the type is not array, so a
collectionFormat written on a string or an integer does nothing.
validateCollectionFormats warns about one, in a parameter, a response
header, or the items of either, at any nesting depth. Swagger 2.0 defines
the member as applying when the type is array without forbidding it
elsewhere, so this is a warning and the document stays valid.

Only spec.SimpleSchema carries a collectionFormat, through spec.Items,
spec.Header and spec.Parameter; spec.Schema has none. The rule therefore
walks what the operations declare and never reads a schema, so validating
an ordinary JSON schema cannot reach it. A test pins that down and breaks
if the member ever moves onto Schema.

The rest of the collectionFormat rules need no code: the meta-schema
already caps the value to csv, ssv, tsv or pipes, widens it with multi
for a query or formData parameter, and forbids the member outright on a
body parameter, at every location each applies to. doc.go claimed none of
the rules were implemented; it now records what is checked and where.

validateItems built its list of responses inline. That moves to
responsesOf, which both rules call.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
The check sat in the "not yet supported" list, which reads as a backlog
item. It is a decision: the clause constrains an instance rather than the
document, so it moves out of the list into a sentence saying why.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
@codecov

codecov Bot commented Aug 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.48428% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.94%. Comparing base (2f0ae12) to head (b677c6f).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
discriminator.go 95.00% 3 Missing ⚠️
security_requirements.go 97.43% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #290      +/-   ##
==========================================
+ Coverage   96.93%   96.94%   +0.01%     
==========================================
  Files          31       34       +3     
  Lines        3618     3763     +145     
==========================================
+ Hits         3507     3648     +141     
- Misses        106      110       +4     
  Partials        5        5              

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

@fredbi
fredbi merged commit bec3b87 into go-openapi:master Aug 24, 2026
24 checks passed
@fredbi
fredbi deleted the security-req-rule branch August 24, 2026 17:50
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