Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@stoplight/spectral-owasp-ruleset

stoplightio136.3kMIT2.0.1TypeScript support: included

Probably don't want to beg hackers to come and take your stuff.

openapi, openapi3, openapi31, api-design

readme

Spectral OWASP API Security

NPM Downloads Stoplight Forest

Scan an OpenAPI document to detect security issues. As OpenAPI is only describing the surface level of the API it cannot see what is happening in your code, but it can spot obvious issues and outdated standards being used.

v2.x of this ruleset is based on the OWASP API Security Top 10 2023 edition, but if you would like to use the 2019 edition please use v1.x.

Installation

npm install --save -D @stoplight/spectral-owasp-ruleset@^2.0
npm install --save -D @stoplight/spectral-cli

Usage

Create a local ruleset that extends the ruleset. In its most basic form this just tells Spectral what ruleset you want to use, but it will allow you to customise things, add your own rules, turn bits off if its causing trouble.

cd ~/src/<your-api>

echo 'extends: ["@stoplight/spectral-owasp-ruleset"]' > .spectral.yaml

If you're using VS Code or Stoplight Studio then the NPM modules will not be available. Instead you can use the CDN hosted version:

echo 'extends: ["https://unpkg.com/@stoplight/spectral-owasp-ruleset/dist/ruleset.mjs"]' > .spectral.yaml

Note: You need to use the full URL with CDN hosted rulesets because Spectral cannot follow redirects through extends.

Next, use Spectral CLI to lint against your OpenAPI description. Don't have any OpenAPI? Record some HTTP traffic to make OpenAPI and then you can switch to API Design-First going forwards.

spectral lint api/openapi.yaml

You should see some output like this:

/Users/phil/src/protect-earth-api/api/openapi.yaml
  4:5        error    owasp:api8:2023-inventory-access            Declare intended audience of every server by defining servers[0].x-internal as true/false.  servers[0]
  4:10       error    owasp:api8:2023-no-server-http              Server URLs must not use http://. https:// is highly recommended.                        servers[0].url
  45:15        error  owasp:api4:2023-rate-limit                  All 2XX and 4XX responses should define rate limiting headers.  paths./upload.post.responses[201]
  47:15        error  owasp:api4:2023-rate-limit                  All 2XX and 4XX responses should define rate limiting headers.  paths./upload.post.responses[401]
  93:16  information  owasp:api2:2023-read-restricted             This operation is not protected by any security scheme.  paths./sites.get.security
 210:16  information  owasp:api2:2023-read-restricted             This operation is not protected by any security scheme.  paths./species.get.security

Now you have some things to work on for your API. Thankfully these are only at the warning and information severity, and that is not going to fail continuous integration (unless you want them to).

There are a bunch of other rulesets or Stoplight API Stylebook you can use, or use for inspiration for your own rulesets and API Style Guides.

🎉 Thanks

  • Andrzej - Great rules contributed to the Adidas style guide.
  • Roberto Polli - Created lots of excellent Spectral rules for API OAS Checker which aligned with the OWASP API rules.

📜 License

This repository is licensed under the MIT license.

🌲 Sponsor

If you would like to thank us for creating Spectral, we ask that you buy the world a tree.

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[2.0.0] - 2024-01-23

Added

  • Added owasp:api2:2023-short-lived-access-tokens to error on OAuth 2.x flows which do not use a refresh token.
  • Added owasp:api3:2023-no-unevaluatedProperties (format oas3_1 only.)
  • Added owasp:api3:2023-constrained-unevaluatedProperties (format oas3_1 only.)
  • Added owasp:api5:2023-admin-security-unique.
  • Added owasp:api7:2023-concerning-url-parameter to keep an eye out for URLs being passed as parameters and warn about server-side request forgery.
  • Added owasp:api8:2023-no-server-http which supports servers having a url which is a relative path.
  • Added owasp:api9:2023-inventory-access to indicate intended audience of every server.
  • Added owasp:api9:2023-inventory-environment to declare intended environment for every server.

Changed

  • Deleted owasp:api2:2023-protection-global-unsafe as it allowed for unprotected POST, PATCH, PUT, DELETE and that's always going to be an issue. Use the new owasp:api2:2023-write-restricted rule which does not allow these operations to ever disable security, or use Spectral overrides if you have an edge case.
  • Renamed owasp:api2:2019-protection-global-unsafe-strict to owasp:api2:2023-write-restricted.
  • Renamed owasp:api2:2019-protection-global-safe to owasp:api2:2023-read-restricted and increased severity from info to warn.
  • Renamed owasp:api2:2019-auth-insecure-schemes to owasp:api2:2023-auth-insecure-schemes.
  • Renamed owasp:api2:2019-jwt-best-practices to owasp:api2:2023-jwt-best-practices.
  • Renamed owasp:api2:2019-no-api-keys-in-url to owasp:api2:2023-no-api-keys-in-url.
  • Renamed owasp:api2:2019-no-credentials-in-url to owasp:api2:2023-no-credentials-in-url.
  • Renamed owasp:api2:2019-no-http-basic to owasp:api2:2023-no-http-basic.
  • Renamed owasp:api3:2019-define-error-validation to owasp:api8:2023-define-error-validation.
  • Renamed owasp:api3:2019-define-error-responses-401 to owasp:api8:2023-define-error-responses-401.
  • Renamed owasp:api3:2019-define-error-responses-500 to owasp:api8:2023-define-error-responses-500.
  • Renamed owasp:api4:2019-rate-limit to owasp:api4:2023-rate-limit and added support for the singular RateLimit header in draft-ietf-httpapi-ratelimit-headers-07.
  • Renamed owasp:api4:2019-rate-limit-retry-after to owasp:api4:2023-rate-limit-retry-after.
  • Renamed owasp:api4:2019-rate-limit-responses-429 to owasp:api4:2023-rate-limit-responses-429.
  • Renamed owasp:api4:2019-array-limit to owasp:api4:2023-array-limit.
  • Renamed owasp:api4:2019-string-limit to owasp:api4:2023-string-limit.
  • Renamed owasp:api4:2019-string-restricted to owasp:api4:2023-string-restricted and downgraded from error to warn.
  • Renamed owasp:api4:2019-integer-limit to owasp:api4:2023-integer-limit.
  • Renamed owasp:api4:2019-integer-limit-legacy to owasp:api4:2023-integer-limit-legacy.
  • Renamed owasp:api4:2019-integer-format to owasp:api4:2023-integer-format.
  • Renamed owasp:api6:2019-no-additionalProperties to owasp:api3:2023-no-additionalProperties and restricted rule to only run the oas3_0 format.
  • Renamed owasp:api6:2019-constrained-additionalProperties to owasp:api3:2023-constrained-additionalProperties and restricted rule to only run the oas3_0 format.
  • Renamed owasp:api7:2023-security-hosts-https-oas2 to owasp:api8:2023-no-scheme-http.
  • Renamed owasp:api7:2023-security-hosts-https-oas3 to owasp:api8:2023-no-server-http.

Removed

  • Deleted owasp:api2:2023-protection-global-unsafe as it allowed for unprotected POST, PATCH, PUT, DELETE and that's always going to be an issue. Use the new owasp:api2:2023-write-restricted rule which does not allow these operations to ever disable security, or use Spectral overrides if you have an edge case.