Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import unittest
- import requests
- import httpConstants
- import ejs
- proxyUrl = "http://ur-proxy"
- SAFE_BODY = {}
- MALICIOUS_BODY = {}
- template = ""
- patterns = [{"description": ""}]
- SAFE_QUERY_STRING = "?username=tom&password=jones"
- SAFE_BODY = {
- "username": "tom",
- "password": "jones"
- }
- MALICIOUS_BODY = {
- "username": "tom",
- "password": "jones' OR 5=5"
- }
- proxy_url = "http://localhost:" + str(config.proxyPort)
- class TestSafeRequests(unittest.TestCase):
- def test_safe_get_no_params(self):
- response = requests.get(proxy_url + "/default")
- self.assertEqual(response.status_code, httpConstants.responseCodes.HTTP_SUCCESS_OK)
- self.assertTrue(httpConstants.headers.HEADER_KEY_CONTENT in response.headers)
- self.assertRegex(response.headers[httpConstants.headers.HEADER_KEY_CONTENT], httpConstants.headers.HEADER_VALUE_JSON_REGEX)
- self.assertNotEqual(response.text, "")
- def test_safe_get_with_params(self):
- response = requests.get(proxy_url + "/default" + SAFE_QUERY_STRING)
- self.assertEqual(response.status_code, httpConstants.responseCodes.HTTP_SUCCESS_OK)
- self.assertTrue(httpConstants.headers.HEADER_KEY_CONTENT in response.headers)
- self.assertRegex(response.headers[httpConstants.headers.HEADER_KEY_CONTENT], httpConstants.headers.HEADER_VALUE_JSON_REGEX)
- self.assertNotEqual(response.text, "")
- def test_safe_delete_with_params(self):
- response = requests.delete(proxy_url + "/default" + SAFE_QUERY_STRING)
- self.assertEqual(response.status_code, httpConstants.responseCodes.HTTP_SUCCESS_OK)
- self.assertTrue(httpConstants.headers.HEADER_KEY_CONTENT in response.headers)
- self.assertRegex(response.headers[httpConstants.headers.HEADER_KEY_CONTENT], httpConstants.headers.HEADER_VALUE_JSON_REGEX)
- self.assertNotEqual(response.text, "")
- def test_safe_post_no_body(self):
- response = requests.post(proxy_url + "/default")
- self.assertEqual(response.status_code, httpConstants.responseCodes.HTTP_SUCCESS_OK)
- self.assertTrue(httpConstants.headers.HEADER_KEY_CONTENT in response.headers)
- self.assertRegex(response.headers[httpConstants.headers.HEADER_KEY_CONTENT], httpConstants.headers.HEADER_VALUE_JSON_REGEX)
- self.assertNotEqual(response.text, "")
- def test_safe_post_with_body(self):
- response = requests.post(proxy_url + "/default", json=SAFE_BODY)
- self.assertEqual(response.status_code, httpConstants.responseCodes.HTTP_SUCCESS_OK)
- self.assertTrue(httpConstants.headers.HEADER_KEY_CONTENT in response.headers)
- self.assertRegex(response.headers[httpConstants.headers.HEADER_KEY_CONTENT], httpConstants.headers.HEADER_VALUE_JSON_REGEX)
- self.assertNotEqual(response.text, "")
- def test_safe_PUT():
- response = requests.put(f"{proxyUrl}/default", json=SAFE_BODY)
- assert response.status_code == httpConstants.responseCodes.HTTP_SUCCESS_OK
- assert httpConstants.headers.HEADER_KEY_CONTENT in response.headers
- assert httpConstants.headers.HEADER_VALUE_JSON_REGEX.match(response.headers[httpConstants.headers.HEADER_KEY_CONTENT])
- assert response.text != ""
- def test_GET_resource_not_found():
- response = requests.get(f"{proxyUrl}/someNonexistentEndpoint")
- assert response.status_code == httpConstants.responseCodes.HTTP_NOT_FOUND
- def test_GET_server_error():
- response = requests.get(f"{proxyUrl}/server_error")
- assert response.status_code == httpConstants.responseCodes.HTTP_INTERNAL_SERVER_ERROR
- assert response.text != ""
- def test_basic_injection_GET():
- response = requests.get(f"{proxyUrl}/default", params={"username": "tom", "password": "jones' OR 1=1"})
- assert response.status_code == httpConstants.responseCodes.HTTP_SUCCESS_OK
- assert httpConstants.headers.HEADER_KEY_CONTENT in response.headers
- assert httpConstants.headers.HEADER_VALUE_TEXT_REGEX.match(response.headers[httpConstants.headers.HEADER_KEY_CONTENT])
- assert response.text == ejs.render(template, {"description": patterns[0]["description"]})
- def test_basic_injection_POST():
- response = requests.post(f"{proxyUrl}/default", headers={httpConstants.headers.HEADER_KEY_CONTENT: httpConstants.headers.HEADER_VALUE_FORM}, json=MALICIOUS_BODY)
- assert response.status_code == httpConstants.responseCodes.HTTP_SUCCESS_OK
- assert httpConstants.headers.HEADER_KEY_CONTENT in response.headers
- assert httpConstants.headers.HEADER_VALUE_JSON_REGEX.match(response.headers[httpConstants.headers.HEADER_KEY_CONTENT])
- assert response.json()["message"] == patterns[0]["description"]
- def test_basic_injection_PUT():
- response = requests.put(f"{proxyUrl}/default", headers={httpConstants.headers.HEADER_KEY_CONTENT: httpConstants.headers.HEADER_VALUE_FORM}, json=MALICIOUS_BODY)
- assert response.status_code == httpConstants.responseCodes.HTTP_SUCCESS_OK
- assert httpConstants.headers.HEADER_KEY_CONTENT in response.headers
- assert httpConstants.headers.HEADER_VALUE_JSON_REGEX.match(response.headers[httpConstants.headers.HEADER_KEY_CONTENT])
- assert response.json()["message"] == patterns[0]["description"]
- def test_basic_injection_DELETE():
- response = requests.delete(f"{proxyUrl}/default", params={"username": "tom", "password": "jones' OR 1=1"})
- assert response.status_code == httpConstants.responseCodes.HTTP_SUCCESS_OK
- assert httpConstants.headers.HEADER_KEY_CONTENT in response.headers
- assert httpConstants.headers.HEADER_VALUE_JSON_REGEX.match(response.headers[httpConstants.headers.HEADER_KEY_CONTENT])
- assert response.json()["message"] == patterns[0]["description"]
- def test_safe_GET_with_attributes():
- response = requests.get(f"{proxyUrl}/customers/7/users/129")
- assert response.status_code == httpConstants.responseCodes.HTTP_SUCCESS_OK
- assert httpConstants.headers.HEADER_KEY_CONTENT in response.headers
- assert httpConstants.headers.HEADER_VALUE_JSON_REGEX.match(response.headers[httpConstants.headers.HEADER_KEY_CONTENT])
- assert response.text != ""
- if __name__ == '__main__':
- unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement