feat: add oas-validator plugin#13344
Merged
Merged
Conversation
The oas-validator plugin validates inbound HTTP requests against an OpenAPI Specification (OAS) 3.x document before forwarding them to upstream services. Requests that fail validation are rejected with a configurable HTTP error status code. The OpenAPI spec can be provided as an inline JSON string or fetched from a remote URL. Remote specs are cached for a configurable TTL (configured via plugin metadata). Validation covers request method, path, query parameters, headers, and request body, with per-category skip flags for selective enforcement. Dependencies: - lua-resty-openapi-validator (>= 1.0.5-1) Co-authored-by: Copilot <[email protected]>
- Add Apache license headers to oas-validator.lua and test files - Remove trailing whitespace in t/plugin/oas-validator.t Co-authored-by: Copilot <[email protected]>
- Add oas-validator to expected plugin list in t/admin/plugins.t - Add OpenAPI spec fixture files (t/spec/*.json) required by tests Co-authored-by: Copilot <[email protected]>
- Change priority from 510 to 512 to avoid conflict with mcp-bridge - Move plugin registration position in config.lua accordingly - Add fake upstream server on port 1971 in test preprocessor - Replace unreachable port 6969/1980 with port 1971 in test upstreams - Fix oas-validator2.t: replace 1980 with 1970 for TEST 22/24/26 - Remove trailing scheme/pass_host fields from updated upstream blocks Co-authored-by: Copilot <[email protected]>
- Rename APISIX CRD tab to APISIX Ingress Controller - Simplify admin_key note block Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
nic-6443
previously approved these changes
May 9, 2026
shreemaan-abhishek
previously approved these changes
May 9, 2026
membphis
previously approved these changes
May 9, 2026
AlinsRan
dismissed stale reviews from membphis, shreemaan-abhishek, and nic-6443
via
May 9, 2026 09:38
633d26c
shreemaan-abhishek
previously approved these changes
May 9, 2026
nic-6443
previously approved these changes
May 9, 2026
membphis
reviewed
May 11, 2026
| type = "string", | ||
| minLength = 1 | ||
| }, | ||
| spec_url = { |
Member
There was a problem hiding this comment.
both support http and https
only https is not enough
Contributor
Author
There was a problem hiding this comment.
no, it support http
https? match oneOf http of https.
=== TEST 19: create route with spec_url for TTL test
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin")
local code, body = t.test('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"uri": "/*",
"plugins": {
"oas-validator": {
"spec_url": "http://127.0.0.1:1979/spec.json"
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:1970": 1
}
}
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed
|
|
||
| local function get_validator(conf) | ||
| if conf.spec then | ||
| if not conf._validator then |
Member
There was a problem hiding this comment.
can we use lrucache to store the validator?
Member
There was a problem hiding this comment.
store item in conf is not a best practise
another way: use metadata way
Contributor
Author
There was a problem hiding this comment.
Replace withconf._meta.
We don't need an extra lrucache here.
Storing the data directly in conf already acts as caching, and its lifecycle is naturally bound to the conf object.
AlinsRan
dismissed stale reviews from nic-6443 and shreemaan-abhishek
via
May 11, 2026 01:56
1d6ad38
nic-6443
approved these changes
May 11, 2026
shreemaan-abhishek
approved these changes
May 11, 2026
membphis
approved these changes
May 11, 2026
wistefan
pushed a commit
to wistefan/apisix
that referenced
this pull request
Jun 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces the
oas-validatorplugin, which validates inbound HTTP requests against an OpenAPI Specification (OAS) 3.x document before forwarding them to upstream services.Description
The
oas-validatorplugin rejects requests that do not conform to the configured OpenAPI spec, returning a configurable HTTP error code.Validation scope (each independently configurable):
Spec delivery:
plugin metadata.spec_url_ttl, default 3600 s)Use cases:
Example
The following example validates all requests to
/api/*against an inline OpenAPI 3.0 spec. Requests with an invalid body are rejected withHTTP 400.A request missing the required
namefield will be rejected:Alternatively, the spec can be served from a remote URL using
spec_urlinstead of the inlinespecfield.Dependencies
This plugin depends on the
lua-resty-openapi-validatorLuaRocks package (>= 1.0.5-1), added toapisix-master-0.rockspec.https://luarocks.org/modules/membphis/lua-resty-openapi-validator
Files changed
apisix/plugins/oas-validator.lua— plugin implementationt/plugin/oas-validator.t— basic test casest/plugin/oas-validator2.t— extended test cases (plugin metadata, OAS 3.1)t/plugin/oas-validator3.t— test cases forspec_urlfeaturet/spec/spec.json,t/spec/spec31.json,t/spec/spec31-gaps.json— test spec fixturesdocs/en/latest/plugins/oas-validator.md— English documentationdocs/zh/latest/plugins/oas-validator.md— Chinese documentationapisix-master-0.rockspec— addlua-resty-openapi-validatordependencyapisix/cli/config.lua— register plugin in default listdocs/en/latest/config.json/docs/zh/latest/config.json— add to sidebar