# operation-security-defined

Source: https://apigenie.pl/docs/linter/openapi/rules/operation-security-defined

A rule of the OpenAPI linter, which reads OpenAPI 3.0, 3.1 and 3.2 documents. Since 0.0.1. In the rulesets: Recommended at warning, Strict at warning, All at error.

This rule checks that every operation has security requirements, its own or the document's.

## Rule details

The security requirements tell a client how to authenticate, and generated clients, mocks and the
validating proxy what to send and check. An operation with none is public as far as every tool is
concerned, which is rarely what the server does.

The rule reports `Security requirement is not defined` on an operation without `security` when the
document has no top-level `security` either. An operation that resets the document's requirements
with `security: []` is taken as meant public and is not reported. Turn the rule off for an API
that restricts nothing.

## Examples

**Good**. The operation inherits the document's requirement.

```yaml
paths:
  /users:
    get:
      responses:
        "200":
          description: A page of users.
security:
  - api_key: []
components:
  securitySchemes:
    api_key:
      type: apiKey
      in: header
      name: X-Api-Key
```

**Good**. The operation has a requirement of its own.

```yaml
paths:
  /users:
    get:
      security:
        - api_key: []
      responses:
        "200":
          description: A page of users.
components:
  securitySchemes:
    api_key:
      type: apiKey
      in: header
      name: X-Api-Key
```

**Bad**. Neither the document nor the operation says how to authenticate.

```yaml
paths:
  /users:
    get:
      responses:
        "200":
          description: A page of users.
```
