Skip to content

Commit f3f8b92

Browse files
fix(ruleset): add security-scheme-name rule
1 parent ae9c5c4 commit f3f8b92

File tree

2 files changed

+219
-0
lines changed

2 files changed

+219
-0
lines changed
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
import testRule from './__helpers__/tester';
2+
3+
testRule('security-scheme-name', [
4+
{
5+
name: 'valid case - simple alphanumeric name',
6+
document: {
7+
openapi: '3.0.2',
8+
components: {
9+
securitySchemes: {
10+
apikey: {
11+
type: 'apiKey',
12+
name: 'apiKey123',
13+
in: 'header',
14+
},
15+
},
16+
},
17+
},
18+
errors: [],
19+
},
20+
21+
{
22+
name: 'valid case - name with allowed special characters',
23+
document: {
24+
openapi: '3.0.2',
25+
components: {
26+
securitySchemes: {
27+
oauth2: {
28+
type: 'oauth2',
29+
name: 'api_key-token.v1',
30+
flows: {
31+
authorizationCode: {
32+
authorizationUrl: 'https://example.com/api/oauth/dialog',
33+
tokenUrl: 'https://example.com/api/oauth/token',
34+
scopes: {
35+
'write:pets': 'modify pets in your account',
36+
'read:pets': 'read your pets',
37+
},
38+
},
39+
},
40+
},
41+
},
42+
},
43+
},
44+
errors: [],
45+
},
46+
47+
{
48+
name: 'valid case - OAS 2.0 security scheme',
49+
document: {
50+
swagger: '2.0',
51+
securityDefinitions: {
52+
petstore_auth: {
53+
type: 'oauth2',
54+
name: 'Authorization_Token',
55+
authorizationUrl: 'https://petstore.swagger.io/oauth/authorize',
56+
flow: 'implicit',
57+
scopes: {
58+
'write:pets': 'modify pets in your account',
59+
'read:pets': 'read your pets',
60+
},
61+
},
62+
},
63+
},
64+
errors: [],
65+
},
66+
67+
{
68+
name: 'invalid case - name with spaces',
69+
document: {
70+
openapi: '3.0.2',
71+
components: {
72+
securitySchemes: {
73+
apikey: {
74+
type: 'apiKey',
75+
name: 'api key with spaces',
76+
in: 'header',
77+
},
78+
},
79+
},
80+
},
81+
errors: [
82+
{
83+
message: '"api key with spaces" must match the pattern "^[a-zA-Z0-9._-]+$"',
84+
},
85+
],
86+
},
87+
88+
{
89+
name: 'invalid case - name with special characters',
90+
document: {
91+
openapi: '3.0.2',
92+
components: {
93+
securitySchemes: {
94+
oauth2: {
95+
type: 'oauth2',
96+
name: 'api@key#token',
97+
flows: {
98+
authorizationCode: {
99+
authorizationUrl: 'https://example.com/api/oauth/dialog',
100+
tokenUrl: 'https://example.com/api/oauth/token',
101+
scopes: {},
102+
},
103+
},
104+
},
105+
},
106+
},
107+
},
108+
errors: [
109+
{
110+
message: '"api@key#token" must match the pattern "^[a-zA-Z0-9._-]+$"',
111+
},
112+
],
113+
},
114+
115+
{
116+
name: 'invalid case - name with parentheses and brackets',
117+
document: {
118+
openapi: '3.0.2',
119+
components: {
120+
securitySchemes: {
121+
basic: {
122+
type: 'http',
123+
name: 'auth(token)[v1]',
124+
scheme: 'basic',
125+
},
126+
},
127+
},
128+
},
129+
errors: [
130+
{
131+
message: '"auth(token)[v1]" must match the pattern "^[a-zA-Z0-9._-]+$"',
132+
},
133+
],
134+
},
135+
136+
{
137+
name: 'mixed case - valid and invalid names',
138+
document: {
139+
openapi: '3.0.2',
140+
components: {
141+
securitySchemes: {
142+
validApiKey: {
143+
type: 'apiKey',
144+
name: 'valid_api-key.v1',
145+
in: 'header',
146+
},
147+
invalidApiKey: {
148+
type: 'apiKey',
149+
name: 'invalid api+key!',
150+
in: 'header',
151+
},
152+
anotherValid: {
153+
type: 'http',
154+
name: 'Bearer123',
155+
scheme: 'bearer',
156+
},
157+
},
158+
},
159+
},
160+
errors: [
161+
{
162+
message: '"invalid api+key!" must match the pattern "^[a-zA-Z0-9._-]+$"',
163+
},
164+
],
165+
},
166+
167+
{
168+
name: 'edge case - empty name',
169+
document: {
170+
openapi: '3.0.2',
171+
components: {
172+
securitySchemes: {
173+
apikey: {
174+
type: 'apiKey',
175+
name: '',
176+
in: 'header',
177+
},
178+
},
179+
},
180+
},
181+
errors: [
182+
{
183+
message: '"" must match the pattern "^[a-zA-Z0-9._-]+$"',
184+
},
185+
],
186+
},
187+
188+
{
189+
name: 'valid case - numeric only name',
190+
document: {
191+
openapi: '3.0.2',
192+
components: {
193+
securitySchemes: {
194+
apikey: {
195+
type: 'apiKey',
196+
name: '12345',
197+
in: 'header',
198+
},
199+
},
200+
},
201+
},
202+
errors: [],
203+
},
204+
]);

packages/rulesets/src/oas/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,5 +768,20 @@ const ruleset = {
768768
function: undefined,
769769
},
770770
},
771+
'security-scheme-name': {
772+
description: 'Ensure that security scheme should have valid name',
773+
message: '{{error}}',
774+
severity: 0,
775+
formats: [oas2, oas3],
776+
recommended: true,
777+
resolved: false,
778+
given: '$.components.securitySchemes[*].name',
779+
then: {
780+
function: pattern,
781+
functionOptions: {
782+
match: '^[a-zA-Z0-9._-]+$',
783+
},
784+
},
785+
},
771786
},
772787
};

0 commit comments

Comments
 (0)