Skip to content

Commit ecb823c

Browse files
author
Enda
authored
feat(spectral): add rule to prevent external relative references (#9)
1 parent 8c9707b commit ecb823c

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

spectral/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,14 @@ List:
210210

211211
**Severity**: warning
212212

213+
### rhoas-external-$ref
214+
215+
`$ref` values cannot be a relative path to an external file. Please use the absolute URL or convert it to an internal `$ref`.
216+
217+
**Recommended**: Yes
218+
219+
**Severity**: error
220+
213221
## Development
214222

215223
> NOTE: This project uses [Yarn workspaces](https://classic.yarnpkg.com/en/docs/workspaces/) for easier development.

spectral/examples/openapi-invalid.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ paths:
2323
application/json:
2424
schema:
2525
$ref: '#/components/schemas/Error'
26+
"400":
27+
description: br
28+
content:
29+
application/json:
30+
schema:
31+
$ref: 'openapi-invalid.yaml#/components/schemas/Error'
2632
/api/foo_mgmt/v1beta/foos/{id}:
2733
get:
2834
operationId: getFooById
@@ -90,4 +96,4 @@ components:
9096
Bearer:
9197
scheme: 'bearer'
9298
bearerFormat: 'JWT'
93-
type: 'http'
99+
type: 'http'

spectral/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rhoas/spectral-ruleset",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "Spectral ruleset",
55
"private": false,
66
"main": "ruleset.yaml",

spectral/ruleset.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@ functions:
55
- securitySchemes
66
- infoLicenseApache2
77
- schemaDefinition
8+
- externalRefs
89
rules:
910
openapi-tags: off
1011
operation-tags: off
1112

13+
rhoas-external-$ref:
14+
given: "$..['$ref']"
15+
severity: error
16+
type: 'validation'
17+
resolved: false
18+
then:
19+
function: externalRefs
1220
rhoas-oas3minimum:
1321
given: "$"
1422
description: OpenAPI version must be >= 3
@@ -163,4 +171,4 @@ rules:
163171
required: true
164172
total:
165173
type: integer
166-
required: true
174+
required: true
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { IFunctionResult } from "@stoplight/spectral";
2+
3+
export default (targetVal: any): IFunctionResult[] => {
4+
if (!targetVal || !targetVal.length) {
5+
return
6+
}
7+
8+
if (targetVal.startsWith('https') || targetVal.startsWith('http') || targetVal.startsWith('#/')) {
9+
return;
10+
} else {
11+
return [{
12+
message: 'Only local relative `$ref` or absolute external URL `$ref` is allowed'
13+
}]
14+
}
15+
}

0 commit comments

Comments
 (0)