Skip to content

Commit

Permalink
fixes #21 and makes no-nimeric-ids support any string
Browse files Browse the repository at this point in the history
  • Loading branch information
philsturgeon committed Jan 21, 2024
1 parent 2622b28 commit 165794e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 21 deletions.
2 changes: 1 addition & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
- Renamed `owasp:api4:2019-rate-limit-responses-429` to `owasp:api4:2023-rate-limit-responses-429`.
- Renamed `owasp:api4:2019-array-limit` to `owasp:api4:2023-array-limit`.
- Renamed `owasp:api4:2019-string-limit` to `owasp:api4:2023-string-limit`.
- Renamed `owasp:api4:2019-string-restricted` to `owasp:api4:2023-string-restricted`.
- Renamed `owasp:api4:2019-string-restricted` to `owasp:api4:2023-string-restricted` and downgraded from `error` to `warn`.
- Renamed `owasp:api4:2019-integer-limit` to `owasp:api4:2023-integer-limit`.
- Renamed `owasp:api4:2019-integer-limit-legacy` to `owasp:api4:2023-integer-limit-legacy`.
- Renamed `owasp:api4:2019-integer-format` to `owasp:api4:2023-integer-format`.
Expand Down
64 changes: 59 additions & 5 deletions __tests__/owasp-api1-2023-no-numeric-ids.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import testRule from "./__helpers__/helper";

testRule("owasp:api1:2023-no-numeric-ids", [
{
name: "valid case",
name: "valid case: uuid",
document: {
openapi: "3.1.0",
info: { version: "1.0" },
Expand All @@ -29,6 +29,60 @@ testRule("owasp:api1:2023-no-numeric-ids", [
errors: [],
},

{
name: "valid case: ulid",
document: {
openapi: "3.1.0",
info: { version: "1.0" },
paths: {
"/foo/{id}": {
get: {
description: "get",
parameters: [
{
name: "id",
in: "path",
required: true,
schema: {
type: "string",
format: "ulid",
},
},
],
},
},
},
},
errors: [],
},

{
name: "valid case: random",
document: {
openapi: "3.1.0",
info: { version: "1.0" },
paths: {
"/foo/{id}": {
get: {
description: "get",
parameters: [
{
name: "id",
in: "path",
required: true,
schema: {
type: "string",
example: "sfdjkhjk24kd9s",
},
},
],
},
},
},
},
errors: [],
},

{
name: "invalid if its an integer",
document: {
Expand Down Expand Up @@ -88,25 +142,25 @@ testRule("owasp:api1:2023-no-numeric-ids", [
errors: [
{
message:
"OWASP API1:2019 - Use random IDs that cannot be guessed. UUIDs are preferred.",
"Use random IDs that cannot be guessed. UUIDs are preferred but any other random string will do.",
path: ["paths", "/foo/{id}", "get", "parameters", "0", "schema"],
severity: DiagnosticSeverity.Error,
},
{
message:
"OWASP API1:2019 - Use random IDs that cannot be guessed. UUIDs are preferred.",
"Use random IDs that cannot be guessed. UUIDs are preferred but any other random string will do.",
path: ["paths", "/foo/{id}", "get", "parameters", "2", "schema"],
severity: DiagnosticSeverity.Error,
},
{
message:
"OWASP API1:2019 - Use random IDs that cannot be guessed. UUIDs are preferred.",
"Use random IDs that cannot be guessed. UUIDs are preferred but any other random string will do.",
path: ["paths", "/foo/{id}", "get", "parameters", "3", "schema"],
severity: DiagnosticSeverity.Error,
},
{
message:
"OWASP API1:2019 - Use random IDs that cannot be guessed. UUIDs are preferred.",
"Use random IDs that cannot be guessed. UUIDs are preferred but any other random string will do.",
path: ["paths", "/foo/{id}", "get", "parameters", "4", "schema"],
severity: DiagnosticSeverity.Error,
},
Expand Down
12 changes: 6 additions & 6 deletions __tests__/owasp-api4-2023-string-restricted.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ testRule("owasp:api4:2023-string-restricted", [
errors: [
{
message:
"Schema of type string must specify a format, pattern, enum, or const.",
"Schema of type string should specify a format, pattern, enum, or const.",
path: ["definitions", "Foo"],
severity: DiagnosticSeverity.Error,
severity: DiagnosticSeverity.Warning,
},
],
},
Expand All @@ -194,15 +194,15 @@ testRule("owasp:api4:2023-string-restricted", [
errors: [
{
message:
"Schema of type string must specify a format, pattern, enum, or const.",
"Schema of type string should specify a format, pattern, enum, or const.",
path: ["components", "schemas", "Foo"],
severity: DiagnosticSeverity.Error,
severity: DiagnosticSeverity.Warning,
},
{
message:
"Schema of type string must specify a format, pattern, enum, or const.",
"Schema of type string should specify a format, pattern, enum, or const.",
path: ["components", "schemas", "Bar"],
severity: DiagnosticSeverity.Error,
severity: DiagnosticSeverity.Warning,
},
],
},
Expand Down
13 changes: 4 additions & 9 deletions src/ruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default {
*/
"owasp:api1:2023-no-numeric-ids": {
description:
"OWASP API1:2019 - Use random IDs that cannot be guessed. UUIDs are preferred.",
"Use random IDs that cannot be guessed. UUIDs are preferred but any other random string will do.",
severity: DiagnosticSeverity.Error,
given:
'$.paths..parameters[*][?(@property === "name" && (@ === "id" || @.match(/(_id|Id|-id)$/)))]^.schema',
Expand All @@ -119,11 +119,6 @@ export default {
},
},
},
properties: {
format: {
const: "uuid",
},
},
},
},
},
Expand Down Expand Up @@ -558,10 +553,10 @@ export default {
*/
"owasp:api4:2023-string-restricted": {
message:
"Schema of type string must specify a format, pattern, enum, or const.",
"Schema of type string should specify a format, pattern, enum, or const.",
description:
"To avoid unexpected values being sent or leaked, ensure that strings have either a `format`, RegEx `pattern`, `enum`, or `const`.",
severity: DiagnosticSeverity.Error,
"To avoid unexpected values being sent or leaked, strings should have a `format`, RegEx `pattern`, `enum`, or `const`.",
severity: DiagnosticSeverity.Warning,
given: "#StringProperties",
then: {
function: schema,
Expand Down

0 comments on commit 165794e

Please sign in to comment.