Skip to content

Commit 91213fd

Browse files
authored
Merge pull request #1286 from shockey/bug/1259-security-validation
Fix and improve security validation
2 parents c0e0b9f + 4c76574 commit 91213fd

File tree

2 files changed

+147
-12
lines changed

2 files changed

+147
-12
lines changed

src/plugins/validation/semantic-validators/validators/security.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@ export function validate({ resolvedSpec }) {
2121
if (!securityDefinition) {
2222
errors.push({
2323
message: "security requirements must match a security definition",
24-
path: path.join(".")
24+
path: path
2525
})
2626
}
2727

28-
if (securityDefinition && securityDefinition.type === "oauth2") {
28+
if (securityDefinition) {
2929
let scopes = obj[key]
3030
if (Array.isArray(scopes)){
31-
let unresolvedScopes = []
32-
scopes.forEach((scope) => {
33-
if (!securityDefinition.scopes[scope]) { unresolvedScopes.push(scope) }
34-
})
35-
let unresolvedScopesLen = unresolvedScopes.length
36-
if ( unresolvedScopesLen ) {
37-
errors.push({
38-
message: `security scope definition${unresolvedScopesLen > 1 ? "s" : ""} ${unresolvedScopes.join(", ")} could not be resolved`,
39-
path: path.join(".")
31+
32+
// Check for unknown scopes
33+
34+
scopes.forEach((scope, i) => {
35+
if (!securityDefinition.scopes || !securityDefinition.scopes[scope]) {
36+
errors.push({
37+
message: `Security scope definition ${scope} could not be resolved`,
38+
path: path.concat([i.toString()])
39+
})
40+
}
4041
})
41-
}
4242
}
4343
}
4444
})
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import expect from "expect"
2+
import { validate } from "plugins/validation/semantic-validators/validators/security"
3+
4+
describe("validation plugin - semantic - security", () => {
5+
it("should return an error when an operation references a non-existing security scope", () => {
6+
const spec = {
7+
"securityDefinitions": {
8+
"api_key": {
9+
"type": "apiKey",
10+
"name": "apikey",
11+
"in": "query",
12+
"scopes": {
13+
"asdf": "blah blah"
14+
}
15+
}
16+
},
17+
"paths": {
18+
"/": {
19+
"get": {
20+
"description": "asdf",
21+
"security": [
22+
{
23+
"api_key": [
24+
"write:pets"
25+
]
26+
}
27+
]
28+
}
29+
}
30+
}
31+
}
32+
33+
let res = validate({ resolvedSpec: spec })
34+
expect(res.errors.length).toEqual(1)
35+
expect(res.errors[0].path).toEqual(["paths", "/", "get", "security", "0", "0"])
36+
expect(res.errors[0].message).toEqual("Security scope definition write:pets could not be resolved")
37+
expect(res.warnings.length).toEqual(0)
38+
})
39+
it("should return an error when an operation references a security definition with no scopes", () => {
40+
const spec = {
41+
"securityDefinitions": {
42+
"api_key": {
43+
"type": "apiKey",
44+
"name": "apikey",
45+
"in": "query"
46+
}
47+
},
48+
"paths": {
49+
"/": {
50+
"get": {
51+
"description": "asdf",
52+
"security": [
53+
{
54+
"api_key": [
55+
"write:pets"
56+
]
57+
}
58+
]
59+
}
60+
}
61+
}
62+
}
63+
64+
let res = validate({ resolvedSpec: spec })
65+
expect(res.errors.length).toEqual(1)
66+
expect(res.errors[0].path).toEqual(["paths", "/", "get", "security", "0", "0"])
67+
expect(res.errors[0].message).toEqual("Security scope definition write:pets could not be resolved")
68+
expect(res.warnings.length).toEqual(0)
69+
})
70+
71+
it("should return an error when an operation references a non-existing security definition", () => {
72+
const spec = {
73+
"securityDefinitions": {
74+
"api_key": {
75+
"type": "apiKey",
76+
"name": "apikey",
77+
"in": "query"
78+
}
79+
},
80+
"paths": {
81+
"/": {
82+
"get": {
83+
"description": "asdf",
84+
"security": [
85+
{
86+
"fictional_security_definition": [
87+
"write:pets"
88+
]
89+
}
90+
]
91+
}
92+
}
93+
}
94+
}
95+
96+
let res = validate({ resolvedSpec: spec })
97+
expect(res.errors.length).toEqual(1)
98+
expect(res.errors[0].path).toEqual(["paths", "/", "get", "security", "0"])
99+
expect(res.errors[0].message).toEqual("security requirements must match a security definition")
100+
expect(res.warnings.length).toEqual(0)
101+
})
102+
103+
it("should not return an error when an operation references an existing security scope", () => {
104+
const spec = {
105+
"securityDefinitions": {
106+
"api_key": {
107+
"type": "apiKey",
108+
"name": "apikey",
109+
"in": "query",
110+
"scopes": {
111+
"write:pets": "write to pets"
112+
}
113+
}
114+
},
115+
"paths": {
116+
"/": {
117+
"get": {
118+
"description": "asdf",
119+
"security": [
120+
{
121+
"api_key": [
122+
"write:pets"
123+
]
124+
}
125+
]
126+
}
127+
}
128+
}
129+
}
130+
131+
let res = validate({ resolvedSpec: spec })
132+
expect(res.errors.length).toEqual(0)
133+
expect(res.warnings.length).toEqual(0)
134+
})
135+
})

0 commit comments

Comments
 (0)