def from_json(cls, json: MutableMapping[str, Any]) \ -> 'ScanIssueReturnedGetNone': with JsonParser(json): assert json.pop('messageType', None) == 'scanIssue' req_res = tuple(cls.__pop_requests_responses(json)) remed_back = ensure((str, type(None)), json.pop('remediationBackground', None)) remed_detail = ensure((str, type(None)), json.pop('remediationDetail', None)) issue_detail = ensure((str, type(None)), json.pop('issueDetail', None)) return ScanIssueReturnedGetNone( protocol=ensure(str, json.pop('protocol')), host=ensure(str, json.pop('host')), port=ensure(int, json.pop('port')), severity=IssueSeverity(json.pop('severity')), confidence=IssueConfidence(json.pop('confidence')), url=ensure(str, json.pop('url')), name=ensure(str, json.pop('name')), requests_responses=req_res, remediation_background=remed_back, remediation_detail=remed_detail, issue_background=ensure(str, json.pop('issueBackground')), issue_detail=issue_detail, in_scope=ensure(bool, json.pop('inScope')), issue_type=IssueType(json.pop('issueType')), )
def test_utils_jsonparser_contained_mapping_not_empty(self): json = {'a': {'b': {'c': 1, 'd': {}}}} jp = JsonParser(json).__enter__() a = json.pop('a') b = a.pop('b') b.pop('d') self.assertRaises(ElementStillInJSONError, jp.__exit__)
def from_json(cls, json: MutableMapping[str, Any]) -> 'RequestReturned2': with JsonParser(json): # TODO body, headers? return RequestReturned2( http_version=parse_http_version(json.pop('httpVersion')), **dict( chain( _common_returned(json).items(), pop_all(ensure_values(str, json)).items(), )))
def get(self, url: Optional[str] = None) -> Iterable[RequestReturned]: if url is None: response = self._get((200,)) else: url = b64encode(url.encode()).decode() response = self._get((200,), url) json = response.json() with JsonParser(json): return (RequestReturned.from_json(j.pop('request')) for j in json.pop('data'))
def from_json(cls, json: MutableMapping[str, Any]) -> 'Cookie': with JsonParser(json): expiration_raw = json.pop('expiration', None) expiration = None if expiration_raw is not None: expiration = datetime.strptime(expiration_raw, cls.__date_format) return Cookie(expiration=expiration, domain=ensure((str, type(None)), json.pop('domain', None)), **pop_all(ensure_values(str, json)))
def post(self, request: Request, response: Response) -> Tuple[RequestReturned2, ResponseReturned]: ret = self._post((201,), json=dict( request=request.to_json(), response=response.to_json(), )) json = ret.json() with JsonParser(json): return (RequestReturned2.from_json(json.pop('request')), ResponseReturned.from_json(json.pop('response')))
def get(self, url: Optional[str] = None) \ -> Union[Iterable[ScanIssueReturnedGetNone], Iterable[ScanIssueReturnedGetMulti]]: path = url and b64encode(url.encode()).decode() response = self._get((200, ), path) scan_issue_class = ScanIssueReturnedGetNone if url is not None: scan_issue_class = ScanIssueReturnedGetMulti # type: ignore json = response.json() with JsonParser(json): return (scan_issue_class.from_json(s) for s in json.pop('data'))
def from_json(cls, json: MutableMapping[str, Any]) -> 'RequestReturned': with JsonParser(json): json.pop('messageType') json.pop('query', None) # TODO sometimes it's there json.pop('comment', None) # TODO sometimes it's there return RequestReturned( http_version=parse_http_version(json.pop('httpVersion')), headers=tuple( (ensure(str, k), ensure(str, v)) for k, v in pop_all(json.pop('headers')).items()), body=b64decode(json.pop('body').encode()), **dict( chain( _common_returned(json).items(), pop_all(ensure_values(str, json)).items(), )))
def get(self, scan: Optional[Scan] = None) -> Iterable[Scan]: try: path = None if scan is not None: path = str(scan.id) response = self._get((200, ), path) except WeirdBurpResponseError as e: if e.response.status_code != 404: raise raise ScanNotFoundError(scan) json = response.json() if scan is not None: return iter([Scan.from_json(json)]) with JsonParser(json): return (Scan.from_json(j) for j in json.pop('data'))
def from_json(cls, json: MutableMapping[str, Any]) \ -> 'ResponseReturnedGetNone': with JsonParser(json): assert json.pop('messageType', None) == 'response' return ResponseReturnedGetNone( in_scope=ensure(bool, json.pop('inScope')), protocol=ensure(str, json.pop('protocol')), port=ensure(int, json.pop('port')), raw=b64decode(json.pop('raw').encode()), body=b64decode(json.pop('body').encode()), headers=tuple(cls.__pop_headers(json)), cookies=tuple(cls.__pop_cookies(json)), mime_type=ensure(str, json.pop('mimeType')), host=ensure(str, json.pop('host')), tool_flag=ensure(int, json.pop('toolFlag')), status_code=ensure(int, json.pop('statusCode')), reference_id=ensure(int, json.pop('referenceID')), )
def from_json(cls, json: MutableMapping[str, Any]) \ -> 'RequestReturnedGetNone': with JsonParser(json): assert json.pop('messageType', None) == 'request' return RequestReturnedGetNone( in_scope=ensure(bool, json.pop('inScope')), http_version=parse_http_version(json.pop('httpVersion')), body=b64decode(json.pop('body').encode()), tool_flag=ensure(int, json.pop('toolFlag')), url=ensure(str, json.pop('url')), method=ensure(str, json.pop('method')), protocol=ensure(str, json.pop('protocol')), path=ensure(str, json.pop('path')), headers=tuple(cls.__pop_headers(json)), port=ensure(int, json.pop('port')), host=ensure(str, json.pop('host')), raw=b64decode(json.pop('raw').encode()), reference_id=ensure(int, json.pop('referenceID')), query=ensure((str, type(None)), json.pop('query', None)), )
def from_json(cls, json: MutableMapping[str, Any]) \ -> 'ScanIssueReturnedPost': with JsonParser(json): return ScanIssueReturnedPost()
def from_json(cls, json: MutableMapping[str, Any]) -> 'ResponseReturned': with JsonParser(json): return ResponseReturned(status_code=ensure(int, json.pop('statusCode')), **dict(_common_returned(json).items()))
def post(self, cookie: Cookie) -> Cookie: ret = self._post((201, ), json=cookie.to_json()) json = ret.json() with JsonParser(json): return Cookie.from_json(json)
def get(self) -> Iterable[Cookie]: ret = self._get((200, )) json = ret.json() with JsonParser(json): return (Cookie.from_json(j) for j in json.pop('data'))
def get(self) -> Iterable[Any]: ret = self._get((200, )) json = ret.json() with JsonParser(json): for request in json.pop('data'): yield RequestReturned.from_json(request.pop('request'))
def test_utils_jsonparser_not_empty(self): json = {'a': 1} jp = JsonParser(json).__enter__() self.assertRaises(ElementStillInJSONError, jp.__exit__)
def test_utils_jsonparser_contained_mapping(self): json = {'a': {'b': 1}} with JsonParser(json): contained = json.pop('a') contained.pop('b')
def test_utils_jsonparser(self): json = {'a': 1} with JsonParser(json): json.pop('a')
def post(self, url: str) -> str: ret = self._post((201, ), json=dict(url=url)) json = ret.json() with JsonParser(json): return json.pop('url')