File size: 7,352 Bytes
6c50d1f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | import json
from flex.core import validate as flex_validate
class ViolationType:
SCHEMA_VIOLATION = "SCHEMA_VIOLATION"
STATUS_CODE = "STATUS_CODE"
SCHEMA_MISSING = "SCHEMA_MISSING"
# define four types of extra violation for payload comparison
RESPONSE_DIFFERENCE = "RESPONSE_DIFFERENCE" # response is normal yet baseline is 500+
PAYLOAD_INJECTION = "PAYLOAD_INJECTION" # an injection error; likely to happen with the first three types of error
MALICIOUS_OBJECT = "MALICIOUS_OBJECT" # malicious object creation
AUTH_BYPASS = "AUTHBYPASS" # access control bypass
class Violation:
def __init__(self, method, api_response, violate_type):
self.method = method
self.method_name = method.method_name
self.method_path = method.method_path
self.api_response = api_response
self.violate_type = violate_type
self.schema_violations = None
def __str__(self):
str_result = f'Method: {self.method_name}, Method Path: {self.method_path},' \
f' Status Code:{self.api_response.status_code}, Violation:{self.violate_type} ,'
if self.violate_type == ViolationType.SCHEMA_VIOLATION:
str_result += f'Violation Messages: {self.schema_violations} ,'
str_result += f'Request Content: {self.api_response.content}'
return str_result
def signature(self):
return f'{self.api_response.api_name}-{self.violate_type}-{self.api_response.status_code}'
def json(self):
result = {
"method": self.method_name,
"methodPath": self.method_path,
"statusCode": self.api_response.status_code,
"violation": self.violate_type,
"requestContent": self.api_response.content
}
if self.violate_type == ViolationType.SCHEMA_VIOLATION:
result["violationMessage"] = self.schema_violations
return result
class Reponse:
def __init__(self, body):
self.method = body["request"]["method"]
self.api_name = body["apiName"]
self.url = body["request"]["url"]
self.status_code = body["statusCode"]
self.content = None
if body.__contains__("content"):
try:
self.content = json.loads(body["content"])
except:
self.content = body["content"]
self.response = body
def wrap_response(response):
return Reponse(response)
def resolve_response(data):
responses = data
result = []
for resp in responses:
result.append(wrap_response(resp))
return result
def find_method_by_response(apis, response):
api_name = response.api_name
for api in apis:
for method in api.methods:
if method.method_name == api_name:
return method
print(api_name, "not found ")
assert False
def validate_mutations(method, baseline_response, response, mutation_index, current_index):
results = []
if response.status_code >= 500:
results.append(Violation(method, response, ViolationType.STATUS_CODE))
if not method.response_parameter.__contains__(str(response.status_code)):
results.append(Violation(method, response, ViolationType.SCHEMA_MISSING))
if method.response_parameter.__contains__(str(response.status_code)):
schema = method.response_parameter[str(response.status_code)].schema
try:
flex_validate(schema, response.content)
except Exception as ex:
if schema != None:
violation = Violation(method, response, ViolationType.SCHEMA_VIOLATION)
violation.schema_violations = str(ex)
results.append(violation)
if 300 > response.status_code >= 200 and baseline_response.status_code >= 500:
results.append(Violation(method, response, ViolationType.RESPONSE_DIFFERENCE))
if 300 > response.status_code >= 200 and 500 > baseline_response.status_code >=400:
results.append(Violation(method, response, ViolationType.AUTH_BYPASS))
if mutation_index == current_index and response.status_code>=500 and baseline_response.status_code<300:
results.append(Violation(method, response, ViolationType.PAYLOAD_INJECTION))
if mutation_index < current_index and response.status_code>=500 and baseline_response.status_code<300:
results.append(Violation(method, response, ViolationType.MALICIOUS_OBJECT))
return results
def validate_nominal(method, response):
results = []
if response.status_code >= 500:
results.append(Violation(method, response, ViolationType.STATUS_CODE))
if not method.response_parameter.__contains__(str(response.status_code)):
results.append(Violation(method, response, ViolationType.SCHEMA_MISSING))
if method.response_parameter.__contains__(str(response.status_code)):
schema = method.response_parameter[str(response.status_code)].schema
try:
flex_validate(schema, response.content)
except Exception as ex:
if schema != None:
violation = Violation(method, response, ViolationType.SCHEMA_VIOLATION)
violation.schema_violations = str(ex)
results.append(violation)
return results
def validate_non_nominal(method, response):
results = []
# FIXME: bypass status 0 in huawei
if response.status_code == 0:
return results
if response.status_code >= 500 and not method.response_parameter.__contains__(str(response.status_code)):
results.append(Violation(method, response, ViolationType.STATUS_CODE))
if not method.response_parameter.__contains__(str(response.status_code)):
results.append(Violation(method, response, ViolationType.SCHEMA_MISSING))
if method.response_parameter.__contains__(str(response.status_code)):
schema = method.response_parameter[str(response.status_code)].schema
try:
flex_validate(schema, response.content)
except Exception as ex:
violation = Violation(method.method_name, response, ViolationType.SCHEMA_VIOLATION)
violation.schema_violations = str(ex)
def validate_payload(baseline_resp, resp_data, mutation_index, apis, nominal=True, payload=True):
baseline_responses = resolve_response(baseline_resp)
responses = resolve_response(resp_data)
result = []
for i in range(0, len(responses)):
response = responses[i]
baseline_response = baseline_responses[i]
method = find_method_by_response(apis, response)
if payload:
result.extend(validate_mutations(method, baseline_response, response, mutation_index, i))
elif nominal:
result.extend(validate_nominal(method, response))
else:
result.extend(validate_non_nominal(method, response))
return result
def validate(resp_data, apis, nominal=True, payload=False):
responses = resolve_response(resp_data)
result = []
for response in responses:
method = find_method_by_response(apis, response)
if payload:
result.extend(validate_payload(method, response))
if nominal:
result.extend(validate_nominal(method, response))
else:
result.extend(validate_non_nominal(method, response))
return result
|