Skip to content

Commit

Permalink
fix(openapi): Fix ws for CKV_OPENAPI_20 (#5317)
Browse files Browse the repository at this point in the history
* Fix ws

* Add wss UTs
  • Loading branch information
tsmithv11 committed Jul 12, 2023
1 parent 53783ef commit fc591cd
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 2 deletions.
5 changes: 3 additions & 2 deletions checkov/openapi/checks/resource/generic/ClearTextAPIKey.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ def __init__(self) -> None:
def scan_entity_conf(self, conf: dict[str, Any], entity_type: str) -> tuple[CheckResult, dict[str, Any]]: # type:ignore[override] # return type is different than the base class
schemes = conf.get("schemes")
if schemes and isinstance(schemes, list):
if "http" not in schemes and "wp" not in schemes:
if "http" not in schemes and "ws" not in schemes:
return CheckResult.PASSED, conf

servers = conf.get("servers")
if servers and isinstance(servers, list):
if not any(server['url'].startswith('http://') for server in servers):
if not any(server['url'].startswith('http://') for server in servers) and \
not any(server['url'].startswith('ws://') for server in servers):
return CheckResult.PASSED, conf

components = conf.get("components")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"openapi": "3.0.0",
"info": {
"title": "Simple API overview",
"version": "1.0.0"
},
"servers": [
{
"url": "wss://localhost:8000",
"description": "Local server"
},
{
"url": "ws://example.com",
"description": "Example"
}
],
"paths": {
"/pets": {
"post": {
"description": "Creates a new pet in the store",
"responses": {
"200": {
"description": "200 response"
}
},
"operationId": "addPet",
"security": [
{
"apiKey1": [],
"apiKey2": [],
"apiKey3": []
}
]
}
}
},
"components": {
"securitySchemes": {
"apiKey1": {
"type": "apiKey",
"name": "X-API-Key",
"in": "header"
},
"apiKey2": {
"type": "apiKey",
"name": "X-API-Key",
"in": "cookie"
},
"apiKey3": {
"type": "apiKey",
"name": "X-API-Key",
"in": "query"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
openapi: 3.0.0
info:
title: Simple API overview
version: 1.0.0
servers:
- url: wss://localhost:8000
description: Local server
- url: ws://example.com
description: example
paths:
/pets:
post:
description: Creates a new pet in the store
responses:
'200':
description: 200 response
operationId: addPet
security:
- apiKey1: []
apiKey2: []
apiKey3: []
components:
securitySchemes:
apiKey1:
type: apiKey
name: X-API-Key
in: header
apiKey2:
type: apiKey
name: X-API-Key
in: cookie
apiKey3:
type: apiKey
name: X-API-Key
in: query
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"swagger": "2.0",
"info": {
"title": "Simple API overview",
"version": "1.0.0"
},
"schemes": [
"wss"
],
"paths": {
"/pets": {
"post": {
"description": "Creates a new pet in the store",
"responses": {
"200": {
"description": "200 response"
}
},
"operationId": "addPet",
"security": [
{
"apiKey1": [],
"apiKey3": []
}
]
}
}
},
"securityDefinitions": {
"apiKey1": {
"type": "apiKey",
"name": "X-API-Key",
"in": "header"
},
"apiKey3": {
"type": "apiKey",
"name": "X-API-Key",
"in": "query"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
swagger: "2.0"
info:
title: Simple API overview
version: 1.0.0
schemes:
- wss
paths:
/pets:
post:
description: Creates a new pet in the store
responses:
"200":
description: 200 response
operationId: addPet
security:
- apiKey1: []
apiKey3: []
securityDefinitions:
apiKey1:
type: apiKey
name: X-API-Key
in: header
apiKey3:
type: apiKey
name: X-API-Key
in: query
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"openapi": "3.0.0",
"info": {
"title": "Simple API overview",
"version": "1.0.0"
},
"servers": [
{
"url": "wss://example.com/socket",
"description": "Local server"
}
],
"paths": {
"/pets": {
"post": {
"description": "Creates a new pet in the store",
"responses": {
"200": {
"description": "200 response"
}
},
"operationId": "addPet",
"security": [
{
"apiKey1": [],
"apiKey2": [],
"apiKey3": []
}
]
}
}
},
"components": {
"securitySchemes": {
"apiKey1": {
"type": "apiKey",
"name": "X-API-Key",
"in": "header"
},
"apiKey2": {
"type": "apiKey",
"name": "X-API-Key",
"in": "cookie"
},
"apiKey3": {
"type": "apiKey",
"name": "X-API-Key",
"in": "query"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
openapi: 3.0.0
info:
title: Simple API overview
version: 1.0.0
servers:
- url: wss://example.com/socket
description: Local server
paths:
/pets:
post:
description: Creates a new pet in the store
responses:
'200':
description: 200 response
operationId: addPet
security:
- apiKey1: []
apiKey2: []
apiKey3: []
components:
securitySchemes:
apiKey1:
type: apiKey
name: X-API-Key
in: header
apiKey2:
type: apiKey
name: X-API-Key
in: cookie
apiKey3:
type: apiKey
name: X-API-Key
in: query
6 changes: 6 additions & 0 deletions tests/openapi/checks/resource/generic/test_ClearTextAPIKey.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def test_summary(self):
"/pass3.json",
"/pass4.yaml",
"/pass4.json",
"/pass5.yaml",
"/pass5.json",
"/pass6.yaml",
"/pass6.json",
}
failing_resources = {
"/fail.yaml",
Expand All @@ -37,6 +41,8 @@ def test_summary(self):
"/fail3.json",
"/fail4.yaml",
"/fail4.json",
"/fail5.yaml",
"/fail5.json",
}

passed_check_resources = {c.file_path for c in report.passed_checks}
Expand Down

0 comments on commit fc591cd

Please sign in to comment.