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
Structure of the custom Spectral linting rule for checking the operationId of GET operations
The backend microservices that make up crAPI
The main stages of threat modeling
The Top 10 API security risks identified by OWASP
Validating requests in the gateway using OpenAPI
An example DevSecOps pipeline
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 likeretrieve 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 withretrieve or download.
Automating API Delivery ebook for free