Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(query): refactored query to use walk #4067

Merged
merged 1 commit into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 7 additions & 111 deletions assets/queries/openAPI/3.0/header_object_without_schema/query.rego
Original file line number Diff line number Diff line change
@@ -1,126 +1,22 @@
package Cx

import data.generic.openapi as openapi_lib
import data.generic.common as common_lib
import data.generic.openapi as openapi_lib

CxPolicy[result] {
doc := input.document[i]
openapi_lib.check_openapi(doc) == "3.0"

header := doc.components.headers[h]
not_defined(header)

result := {
"documentId": doc.id,
"searchKey": sprintf("components.headers.{{%s}}", [h]),
"issueType": "MissingAttribute",
"keyExpectedValue": sprintf("components.headers.{{%s}} has schema defined", [h]),
"keyActualValue": sprintf("components.headers.{{%s}} does not have schema defined", [h]),
}
}

CxPolicy[result] {
doc := input.document[i]
openapi_lib.check_openapi(doc) == "3.0"

response := doc.paths[path][operation].responses[r]
openapi_lib.content_allowed(operation, r)
header_info := check_content_header(response)

result := {
"documentId": doc.id,
"searchKey": sprintf("paths.{{%s}}.{{%s}}.responses.{{%s}}.content.{{%s}}.encoding.{{%s}}.headers.{{%s}}}", [path, operation, r, header_info.c, header_info.e, header_info.h]),
"issueType": "MissingAttribute",
"keyExpectedValue": sprintf("paths.{{%s}}.{{%s}}.responses.{{%s}}.content.{{%s}}.encoding.{{%s}}.headers.{{%s}}} has schema defined", [path, operation, r, header_info.c, header_info.e, header_info.h]),
"keyActualValue": sprintf("paths.{{%s}}.{{%s}}.responses.{{%s}}.content.{{%s}}.encoding.{{%s}}.headers.{{%s}} does not have schema defined", [path, operation, r, header_info.c, header_info.e, header_info.h]),
}
}

CxPolicy[result] {
doc := input.document[i]
openapi_lib.check_openapi(doc) == "3.0"

header_info := check_content_header(doc.paths[path][operation].requestBody)

result := {
"documentId": doc.id,
"searchKey": sprintf("paths.{{%s}}.{{%s}}.requestBody.content.{{%s}}.encoding.{{%s}}.headers.{{%s}}", [path, operation, header_info.c, header_info.e, header_info.h]),
"issueType": "MissingAttribute",
"keyExpectedValue": sprintf("paths.{{%s}}.{{%s}}.requestBody.content.{{%s}}.encoding.{{%s}}.headers.{{%s}} has schema defined", [path, operation, header_info.c, header_info.e, header_info.h]),
"keyActualValue": sprintf("paths.{{%s}}.{{%s}}.requestBody.content.{{%s}}.encoding.{{%s}}.headers.{{%s}} does not have schema defined", [path, operation, header_info.c, header_info.e, header_info.h]),
}
}

CxPolicy[result] {
doc := input.document[i]
openapi_lib.check_openapi(doc) == "3.0"

header_info := check_content_header(doc.components.requestBodies[r])

result := {
"documentId": doc.id,
"searchKey": sprintf("components.requestBodies.{{%s}}.content.{{%s}}.encoding.{{%s}}.headers.{{%s}}", [r, header_info.c, header_info.e, header_info.h]),
"issueType": "MissingAttribute",
"keyExpectedValue": sprintf("components.requestBodies.{{%s}}.content.{{%s}}.encoding.{{%s}}.headers.{{%s}} has schema defined", [r, header_info.c, header_info.e, header_info.h]),
"keyActualValue": sprintf("components.requestBodies.{{%s}}.content.{{%s}}.encoding.{{%s}}.headers.{{%s}} does not have schema defined", [r, header_info.c, header_info.e, header_info.h]),
}
}

CxPolicy[result] {
doc := input.document[i]
openapi_lib.check_openapi(doc) == "3.0"

response := doc.components.responses[r]
header_info := check_content_header(response)

result := {
"documentId": doc.id,
"searchKey": sprintf("components.responses.{{%s}}.content.{{%s}}.encoding.{{%s}}.headers.{{%s}}}", [r, header_info.c, header_info.e, header_info.h]),
"issueType": "MissingAttribute",
"keyExpectedValue": sprintf("components.responses.{{%s}}.content.{{%s}}.encoding.{{%s}}.headers.{{%s}}} has schema defined", [r, header_info.c, header_info.e, header_info.h]),
"keyActualValue": sprintf("components.responses.{{%s}}.content.{{%s}}.encoding.{{%s}}.headers.{{%s}}} does not have schema defined", [r, header_info.c, header_info.e, header_info.h]),
}
}

CxPolicy[result] {
doc := input.document[i]
openapi_lib.check_openapi(doc) == "3.0"

response := doc.components.responses[r]
not_defined(response.headers[h])
[path, value] := walk(doc)

result := {
"documentId": doc.id,
"searchKey": sprintf("components.responses.{{%s}}.headers.{{%s}}", [r, h]),
"issueType": "MissingAttribute",
"keyExpectedValue": sprintf("components.responses.{{%s}}.headers.{{%s}} has schema defined", [r, h]),
"keyActualValue": sprintf("components.responses.{{%s}}.headers.{{%s}} does not have schema defined", [r, h]),
}
}

CxPolicy[result] {
doc := input.document[i]
openapi_lib.check_openapi(doc) == "3.0"

response := doc.paths[n][oper].responses[r]
openapi_lib.content_allowed(oper, r)
not_defined(response.headers[h])
headerObj := value.headers[header]
not common_lib.valid_key(headerObj, "schema")

result := {
"documentId": doc.id,
"searchKey": sprintf("paths.{{%s}}.{{%s}}.responses.{{%s}}.headers.{{%s}}", [n, oper, r, h]),
"searchKey": sprintf("%s.{{%s}}", [openapi_lib.concat_path(path), header]),
"issueType": "MissingAttribute",
"keyExpectedValue": sprintf("paths.{{%s}}.{{%s}}.responses.{{%s}}.headers.{{%s}} has schema defined", [n, oper, r, h]),
"keyActualValue": sprintf("paths.{{%s}}.{{%s}}.responses.{{%s}}.headers.{{%s}} does not have schema defined", [n, oper, r, h]),
"keyExpectedValue": sprintf("%s.{{%s}} has schema defined", [openapi_lib.concat_path(path), header]),
"keyActualValue": sprintf("%s.{{%s}} does not have schema defined", [openapi_lib.concat_path(path), header]),
}
}

not_defined(header) {
not common_lib.valid_key(header, "schema")
}

check_content_header(r) = header_info {
header := r.content[c].encoding[e].headers[h]
not_defined(header)
header_info := {"c": c, "e": e, "h": h}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"queryName": "Header Object Without Schema",
"severity": "MEDIUM",
"line": 71,
"line": 72,
"filename": "positive1.json"
},
{
Expand All @@ -14,7 +14,7 @@
{
"queryName": "Header Object Without Schema",
"severity": "MEDIUM",
"line": 43,
"line": 44,
"filename": "positive3.yaml"
},
{
Expand Down