Skip to content

Commit 343043b

Browse files
authored
Change oneOf to anyOf schema in owasp:api4:2019-string-restricted and owasp:api4:2019-string-limit (stoplightio#43)
* Change `oneOf` to `anyOf` schema in owasp:api4:2019-string-restricted The use of `oneOf` means that _only_ one of the schema constraints may be true, but it is valid if two or more of the string schema constraint keywords are present: `format`, `pattern`, `enum`, `const`. * Change `oneOf` to `anyOf` schema in owasp:api4:2019-string-limit Similar as with `2019-string-restricted`: the schema check should be `anyOf` instead of `oneOf` in case a schema has more than one of the keywords.
1 parent 2fd49c3 commit 343043b

File tree

3 files changed

+67
-10
lines changed

3 files changed

+67
-10
lines changed

__tests__/owasp-api4-2019-string-limit.test.ts

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ testRule("owasp:api4:2019-string-limit", [
5151
errors: [],
5252
},
5353

54-
5554
{
5655
name: "valid case: oas3.0",
5756
document: {
@@ -61,7 +60,24 @@ testRule("owasp:api4:2019-string-limit", [
6160
schemas: {
6261
Foo: {
6362
type: "string",
64-
enum: [ "a", "b", "c" ]
63+
enum: ["a", "b", "c"],
64+
},
65+
},
66+
},
67+
},
68+
errors: [],
69+
},
70+
71+
{
72+
name: "valid case: oas3.1",
73+
document: {
74+
openapi: "3.1.0",
75+
info: { version: "1.0" },
76+
components: {
77+
schemas: {
78+
Foo: {
79+
type: "string",
80+
const: "constant",
6581
},
6682
},
6783
},
@@ -78,7 +94,26 @@ testRule("owasp:api4:2019-string-limit", [
7894
schemas: {
7995
Foo: {
8096
type: "string",
81-
const: "constant"
97+
const: "constant",
98+
},
99+
},
100+
},
101+
},
102+
errors: [],
103+
},
104+
105+
{
106+
name: "valid case: pattern and maxLength, oas3.1",
107+
document: {
108+
openapi: "3.1.0",
109+
info: { version: "1.0" },
110+
components: {
111+
schemas: {
112+
Foo: {
113+
type: "string",
114+
format: "hex",
115+
pattern: "^[0-9a-fA-F]+$",
116+
maxLength: 16
82117
},
83118
},
84119
},
@@ -99,7 +134,8 @@ testRule("owasp:api4:2019-string-limit", [
99134
},
100135
errors: [
101136
{
102-
message: "Schema of type string must specify maxLength, enum, or const.",
137+
message:
138+
"Schema of type string must specify maxLength, enum, or const.",
103139
path: ["definitions", "Foo"],
104140
severity: DiagnosticSeverity.Error,
105141
},
@@ -121,7 +157,8 @@ testRule("owasp:api4:2019-string-limit", [
121157
},
122158
errors: [
123159
{
124-
message: "Schema of type string must specify maxLength, enum, or const.",
160+
message:
161+
"Schema of type string must specify maxLength, enum, or const.",
125162
path: ["components", "schemas", "Foo"],
126163
severity: DiagnosticSeverity.Error,
127164
},
@@ -142,7 +179,8 @@ testRule("owasp:api4:2019-string-limit", [
142179
},
143180
errors: [
144181
{
145-
message: "Schema of type string must specify maxLength, enum, or const.",
182+
message:
183+
"Schema of type string must specify maxLength, enum, or const.",
146184
path: ["components", "schemas", "Foo"],
147185
severity: DiagnosticSeverity.Error,
148186
},

__tests__/owasp-api4-2019-string-restricted.test.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ testRule("owasp:api4:2019-string-restricted", [
5050
},
5151

5252
{
53-
name: "valid case: format (oas3)",
53+
name: "valid case: pattern (oas3)",
5454
document: {
5555
openapi: "3.0.0",
5656
info: { version: "1.0" },
@@ -117,6 +117,25 @@ testRule("owasp:api4:2019-string-restricted", [
117117
errors: [],
118118
},
119119

120+
{
121+
name: "valid case: format + pattern (oas3.1)",
122+
document: {
123+
openapi: "3.1.0",
124+
info: { version: "1.0" },
125+
components: {
126+
schemas: {
127+
foo: {
128+
type: "string",
129+
format: "hex",
130+
pattern: "^[0-9a-fA-F]+$",
131+
maxLength: 16
132+
},
133+
},
134+
},
135+
},
136+
errors: [],
137+
},
138+
120139
{
121140
name: "valid case: const (oas3.1)",
122141
document: {

src/ruleset.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ export default {
529529
functionOptions: {
530530
schema: {
531531
type: "object",
532-
oneOf: [
532+
anyOf: [
533533
{
534534
required: ["maxLength"],
535535
},
@@ -551,15 +551,15 @@ export default {
551551
"owasp:api4:2019-string-restricted": {
552552
message: "Schema of type string must specify a format, pattern, enum, or const.",
553553
description:
554-
"To avoid unexpected values being sent or leaked, ensure that strings have either a format or a RegEx pattern. This can be done using `format`, `pattern`, `enum` or `const`.",
554+
"To avoid unexpected values being sent or leaked, ensure that strings have either a `format`, RegEx `pattern`, `enum`, or `const`.",
555555
severity: DiagnosticSeverity.Error,
556556
given: "#StringProperties",
557557
then: {
558558
function: schema,
559559
functionOptions: {
560560
schema: {
561561
type: "object",
562-
oneOf: [
562+
anyOf: [
563563
{
564564
required: ["format"],
565565
},

0 commit comments

Comments
 (0)