Spectral "Operation must define a security requirement" — should OpenAPI default to secure?
0 reputation · 11 May 2024, 11:16 UTC
0 reputation · 11 May 2024, 11:16 UTC
While linting an OpenAPI 3.0 document, Spectral's operation-security-defined rule reports Operation must define a security requirement for every operation that lacks an explicit security block when the document has no global security section. In OpenAPI 3.0, such operations inherit no security requirement at all, making them publicly accessible by default.
The specification itself prescribes no default security posture, so the linter is enforcing a convention the spec leaves open. Meanwhile, server generators such as OpenAPI Generator may omit authentication middleware entirely when no security is defined, silently producing open endpoints from a spec that simply omitted the section.
Assume OpenAPI 3.0.x and a recent Spectral version. This can be verified with a minimal YAML lacking global security, linted with that rule enabled, plus a generated Spring Boot stub inspected for missing security annotations.
security section as an error the right default, or should it remain a warning given the spec's silence?defaultSecurity field) that tooling should align with?32275 reputation · 11 May 2024, 11:52 UTC
OpenAPI 3.0 does not define a default security posture. If a document has no security section and an operation omits its own security block, the endpoint is effectively public. Therefore, treating the omission as a hard error is an opinionated enforcement of a convention that the spec does not mandate. The safer, more spec‑aligned default is to report the situation as a warning, allowing teams to opt‑in to stricter enforcement if they wish to require explicit security definitions.
operation-security-defined rule enforces this convention by flagging any operation that lacks an explicit security block when the document has no global security.security field as a literal lack of security requirements. Tooling such as OpenAPI Generator follows this rule and will generate stubs without authentication middleware when no security is declared.defaultSecurity field or similar mechanism in the spec to provide a global fallback. Consequently, any default behaviour must be imposed by tooling or by project conventions.warning (or info) in .spectral.yaml:
rules:
operation-security-defined:
severity: warning
This will surface the issue without breaking CI or build pipelines.--add-auth, enable it only for projects that require every endpoint to be protected. Document this flag as a non‑spec feature.To decide whether to shift the rule from warning to error in your own pipeline, ask:
Do you have any intentionally public endpoints, or does the entire API need to be protected by default?
If the answer is “all endpoints must be protected,” you can safely elevate the rule to error and optionally add a generator flag to inject authentication scaffolding.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.