Skip to content

Commit

Permalink
feat: added owasp api limits for array, string and integer
Browse files Browse the repository at this point in the history
Fixes #16
  • Loading branch information
philsturgeon authored Nov 16, 2022
1 parent fbfd15e commit 8b95700
Show file tree
Hide file tree
Showing 7 changed files with 975 additions and 39 deletions.
78 changes: 78 additions & 0 deletions __tests__/owasp-api4-2019-array-limit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { DiagnosticSeverity } from "@stoplight/types";
import testRule from "./__helpers__/helper";

testRule("owasp:api4:2019-array-limit", [
{
name: "valid case: oas2",
document: {
swagger: "2.0",
info: { version: "1.0" },
definitions: {
Foo: {
type: "array",
maxItems: 99,
},
},
},
errors: [],
},

{
name: "valid case: oas3",
document: {
openapi: "3.0.0",
info: { version: "1.0" },
components: {
schemas: {
Foo: {
type: "array",
maxItems: 99,
},
},
},
},
errors: [],
},

{
name: "invalid case: oas2 missing maxItems",
document: {
swagger: "2.0",
info: { version: "1.0" },
definitions: {
Foo: {
type: "array",
},
},
},
errors: [
{
message: "Schema of type array must specify maxItems.",
path: ["definitions", "Foo"],
severity: DiagnosticSeverity.Error,
},
],
},

{
name: "invalid case: oas3 missing maxItems",
document: {
openapi: "3.0.0",
info: { version: "1.0" },
components: {
schemas: {
Foo: {
type: "array",
},
},
},
},
errors: [
{
message: "Schema of type array must specify maxItems.",
path: ["components", "schemas", "Foo"],
severity: DiagnosticSeverity.Error,
},
],
},
]);
77 changes: 77 additions & 0 deletions __tests__/owasp-api4-2019-integer-format.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { DiagnosticSeverity } from "@stoplight/types";
import testRule from "./__helpers__/helper";

testRule("owasp:api4:2019-integer-format", [
{
name: "valid case: format - int32",
document: {
openapi: "3.1.0",
info: { version: "1.0" },
components: {
schemas: {
Foo: {
type: "integer",
format: "int32",
},
},
},
},
errors: [],
},

{
name: "valid case: format - int64",
document: {
openapi: "3.1.0",
info: { version: "1.0" },
components: {
schemas: {
Foo: {
type: "integer",
format: "int64",
},
},
},
},
errors: [],
},

{
name: "valid case: format - whatever",
document: {
openapi: "3.1.0",
info: { version: "1.0" },
components: {
schemas: {
Foo: {
type: "integer",
format: "whatever",
},
},
},
},
errors: [],
},

{
name: "invalid case: no format",
document: {
openapi: "3.1.0",
info: { version: "1.0" },
components: {
schemas: {
Foo: {
type: "integer",
},
},
},
},
errors: [
{
message: "Schema of type integer must specify format (int32 or int64).",
path: ["components", "schemas", "Foo"],
severity: DiagnosticSeverity.Error,
},
],
},
]);
124 changes: 124 additions & 0 deletions __tests__/owasp-api4-2019-integer-limit-legacy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { DiagnosticSeverity } from "@stoplight/types";
import testRule from "./__helpers__/helper";

testRule("owasp:api4:2019-integer-limit-legacy", [
{
name: "valid case: oas2",
document: {
swagger: "2.0",
info: { version: "1.0" },
definitions: {
Foo: {
type: "integer",
minimum: 1,
maximum: 99,
},
},
},
errors: [],
},

{
name: "valid case: oas3.0",
document: {
openapi: "3.0.0",
info: { version: "1.0" },
components: {
schemas: {
Foo: {
type: "integer",
minimum: 1,
maximum: 99,
},
},
},
},
errors: [],
},

{
name: "invalid case: oas2 missing maximum",
document: {
swagger: "2.0",
info: { version: "1.0" },
definitions: {
Foo: {
type: "integer",
},
},
},
errors: [
{
message: "Schema of type integer must specify minimum and maximum.",
path: ["definitions", "Foo"],
severity: DiagnosticSeverity.Error,
},
],
},

{
name: "invalid case: oas3.0 missing maximum",
document: {
openapi: "3.0.0",
info: { version: "1.0" },
components: {
schemas: {
Foo: {
type: "integer",
},
},
},
},
errors: [
{
message: "Schema of type integer must specify minimum and maximum.",
path: ["components", "schemas", "Foo"],
severity: DiagnosticSeverity.Error,
},
],
},

{
name: "invalid case: oas2 has maximum but missing minimum",
document: {
swagger: "2.0",
info: { version: "1.0" },
definitions: {
Foo: {
type: "integer",
maximum: 99,
},
},
},
errors: [
{
message: "Schema of type integer must specify minimum and maximum.",
path: ["definitions", "Foo"],
severity: DiagnosticSeverity.Error,
},
],
},

{
name: "invalid case: oas3.0 has maximum but missing minimum",
document: {
openapi: "3.0.0",
info: { version: "1.0" },
components: {
schemas: {
Foo: {
type: "integer",
maximum: 99,
},
},
},
},
errors: [
{
message: "Schema of type integer must specify minimum and maximum.",
path: ["components", "schemas", "Foo"],
severity: DiagnosticSeverity.Error,
},
],
},
]);
Loading

0 comments on commit 8b95700

Please sign in to comment.