Review API security

Catch API security gaps before the pentest

The pentest report lists a token in a query string and a route nobody documented, weeks after the release. Check the contract in each pull request, and the traffic at a proxy in front of staging.

Add the security checks to CIInstall API Genie

A credential in the URL, then an undocumented route

With the privacy ruleset next to recommended, the linter flags the token in the query string (privacy/no-credentials-in-url), the e-mail address (privacy/no-personal-data-in-url) and two operations with no security requirement (operation-security-defined), and exits with 1. Then the proxy sits in front of staging while the tests run. When it stops, it lists GET /v1/internal/debug, which the contract doesn't declare, and the request that came without a token. With --fail-on errors it exits with 5. API Genie checks the contract and the traffic against it and sends no attack payloads: injection and broken object-level authorization stay with the pentest.

$ cat apigenie.yaml
version: '1.0'
commands:
  lint:
    extends: [recommended, privacy]
$ apigenie lint api/orders.yaml; echo $?
  19:11  warning  Query parameter 'email' puts personal data (e-mail address) in the URL; send it in the request body instead  privacy/no-personal-data-in-url
  50:7   warning  Security requirement is not defined                                                                          operation-security-defined
  68:7   warning  Security requirement is not defined                                                                          operation-security-defined
  73:11  error    Query parameter 'token' puts a credential (token) in the URL; send it in a header instead                    privacy/no-credentials-in-url

  4 issues (1 errors, 3 warnings)

1
$ apigenie-server api/orders.yaml --proxy-to http://localhost:8080 \
    --report contract.json --record-har staging.har --fail-on errors &
$ # run the tests against http://127.0.0.1:4010/v1, then
$ curl -s -X POST http://127.0.0.1:4010/__apigenie/shutdown
Contract: 3 requests (1 undocumented), 1 errors, 1 warnings
Coverage: 37.5% of 8 branches, 1 of 3 operations
  not tested: GET /orders/{order_id} (getOrder), GET /exports (exportOrders)
  undocumented: GET /v1/internal/debug (1)
  error GET /orders: security: a bearer token (oauth) are missing (unauthorized; request.authorization.missing)
  warn GET /v1/internal/debug: path: GET /v1/internal/debug matches no path of the document (unknown-path; request.path-or-method.unknown)
Contract failed: 1 error violations
$ wait $!; echo $?
5

What it covers

  • The privacy rulesetCredentials and personal data in URLs, with the OWASP, CWE and GDPR references each rule cites.
  • The owasp rulesetSpectral's OWASP ruleset, built in: 8 of the 10 risks of the API Security Top 10 (2023), for OpenAPI and Swagger 2.0.
  • Code scanningSARIF 2.1.0 from apigenie lint, uploaded by the GitHub Action with sarif: true.
  • Validating proxyMissing credentials, ones other than those you configure with security.mode: enforce, undocumented routes, and a JSON or JUnit report for CI.
  • Every operation, with an OAuth 2 tokenapigenie test --generate sends a request per operation, with a token from your OAuth 2 token endpoint.
  • An inventory with ownersapigenie-catalog list answers which APIs a repository holds and who owns each.

The tools that do it

Step by step

Review an API for security: Lint the contract for security and privacy issues in every pull request, check staging's traffic against it at a proxy, test every operation with a token, and keep an inventory of the APIs.

Runs where your contracts are

The CLI, the server and the catalog run on your machines and in your CI, with no account and no API Genie service to send anything to. Lint functions and server scripts run in a sandbox with no file system or network access.

Other use cases