Overview

10 More on API consistency: Custom linting and security checks

This chapter explains how API consistency and security can be enforced automatically through linting, tests, and gateway validation. It begins by showing why built-in linting functions are sometimes not enough and how custom rules and custom functions can extend a tool like Spectral to match an organization’s style guide. Using an example rule for GET operation IDs, it demonstrates a test-driven approach: write a failing test, define the custom rule, implement the function, and then confirm the rule behaves as expected.

The chapter then shifts to API security review, using an intentionally vulnerable OpenAPI example to show how design-time checks can reveal common weaknesses. It walks through several OWASP API Security Top 10 risks and highlights which ones can be detected by linting, such as guessable object identifiers, missing security schemes, overly permissive schemas, missing limits, and insecure HTTP server URLs. It also shows how OpenAPI-based validation in an API gateway can reject invalid requests and responses before they reach backend services, reducing risk and centralizing enforcement.

Finally, the chapter broadens the lens to DevSecOps, arguing that secure APIs depend on security being integrated across the full delivery lifecycle. It emphasizes automation, shared responsibility, and the use of practices such as SAST, DAST, fuzzing, container scanning, secrets management, and runtime protection. The overall message is that secure API delivery is not just about writing good definitions, but about continuously applying checks, controls, and governance from design through production.

Structure of the API linting rule test to check the prefix of operationIds
figure
Structure of the custom Spectral linting rule for checking the operationId of GET operations
figure
The backend microservices that make up crAPI
figure
The main stages of threat modeling
figure
The Top 10 API security risks identified by OWASP
figure
Validating requests in the gateway using OpenAPI
figure
An example DevSecOps pipeline
figure

Summary

  • For some of your API style guidelines, you may need to define custom functions. A custom function takes in the target object you need to run checks on and gives you the full power of your linting tool’s programming language to run the necessary checks.
  • It’s good practice to write unit tests for your custom functions. But before resorting to custom functions, first check to see if your API linting tool has a built-in function or rule that does what you need. In addition, check if there are publicly available linting rules you can import that do what you need.
  • When designing APIs, you have to keep in mind the likely security threats your API will face. The OWASP API Security Top 10 provides an industry consensus on popular API threats. You should incorporate API security checks into your API linting rules to help you catch vulnerabilities at design time.
  • Techniques such as runtime validation of API traffic against your OpenAPI definitions in your API gateway help reduce the load on your backend services, provide a common validation layer, and help improve your API security. This technique can also be used to validate traffic with third parties (using their API definitions) to protect against vulnerabilities relating to the unsafe consumption of third-party APIs.
  • Beyond designing APIs for security, consider integrating with API security platform vendors to provide controls throughout your API development life cycle. You should also incorporate DevSecOps practices into your deployment pipeline to protect your applications.

FAQ

What is the main purpose of custom linting functions in API consistency checks?Custom linting functions let you enforce style-guide rules that built-in linting functions cannot express, such as checking that GET operationIds start with specific prefixes like retrieve or download.
When should you write a custom linting rule instead of using a built-in one?You should first look for a built-in rule or a reusable open-source ruleset. If none matches your requirement, then create a custom rule; only write a custom function as a last resort.
Why is it recommended to write a failing test before implementing a custom linting rule?Writing a failing test first helps define the intended behavior, supports a test-driven approach, and protects the rule from regressions if it is refactored later.
What should a unit test for a linting rule include?A good test should include the rule being tested, a sample API definition, and the expected linting output. It should also isolate the rule by turning off unrelated rules.
How does the example rule check operationId naming for GET operations?The rule targets GET operationIds using a JSONPath expression and applies a custom function that checks whether the value starts with retrieve or download.
What does the custom function return when an operationId violates the naming convention?It returns an array of linting խնդիր results containing an error message and the path to the violation in the API definition.
What is one security issue that API linting can help detect in OpenAPI definitions?API linting can help detect Broken Object Level Authorization by flagging guessable identifiers, such as numeric IDs, that may be vulnerable to enumeration or tampering.
How can API linting help identify weak security misconfiguration?It can detect insecure setup choices such as using HTTP instead of HTTPS in server URLs, which can expose traffic to interception.
How can gateway validation improve API security?An API gateway can validate requests and responses against an OpenAPI definition, rejecting invalid traffic before it reaches backend services and reducing load and attack surface.
What is the role of DevSecOps in API security?DevSecOps integrates security throughout the software delivery lifecycle by combining practices like SAST, DAST, fuzz testing, container scanning, secrets management, and runtime protection into the pipeline.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Automating API Delivery Automating API Delivery ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Automating API Delivery Automating API Delivery ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Automating API Delivery Automating API Delivery ebook for free