2017-04-10 08:07:48 -05:00
|
|
|
##########################################################################
|
|
|
|
#
|
|
|
|
# pgAdmin 4 - PostgreSQL Tools
|
|
|
|
#
|
2023-01-02 00:23:55 -06:00
|
|
|
# Copyright (C) 2013 - 2023, The pgAdmin Development Team
|
2017-04-10 08:07:48 -05:00
|
|
|
# This software is released under the PostgreSQL Licence
|
|
|
|
#
|
|
|
|
##########################################################################
|
|
|
|
|
2022-08-12 06:40:26 -05:00
|
|
|
import secrets
|
2022-12-22 02:55:18 -06:00
|
|
|
import time
|
2019-06-03 10:33:32 -05:00
|
|
|
|
2017-04-10 08:07:48 -05:00
|
|
|
from selenium.webdriver import ActionChains
|
2017-08-29 08:57:56 -05:00
|
|
|
from selenium.common.exceptions import TimeoutException
|
2017-04-10 08:07:48 -05:00
|
|
|
from regression.python_test_utils import test_utils
|
|
|
|
from regression.feature_utils.base_feature_test import BaseFeatureTest
|
2019-11-12 23:49:21 -06:00
|
|
|
from regression.feature_utils.tree_area_locators import TreeAreaLocators
|
2017-08-29 08:57:56 -05:00
|
|
|
from selenium.webdriver.support.ui import WebDriverWait
|
|
|
|
from selenium.webdriver.support import expected_conditions as EC
|
|
|
|
from selenium.webdriver.common.by import By
|
2022-12-22 02:55:18 -06:00
|
|
|
from regression.feature_utils.locators import NavMenuLocators
|
2017-08-29 08:57:56 -05:00
|
|
|
|
2017-04-10 08:07:48 -05:00
|
|
|
|
|
|
|
class CheckDebuggerForXssFeatureTest(BaseFeatureTest):
|
|
|
|
"""Tests to check if Debugger is vulnerable to XSS."""
|
|
|
|
|
|
|
|
scenarios = [
|
2017-05-15 00:10:46 -05:00
|
|
|
("Tests to check if Debugger is vulnerable to XSS", dict())
|
2017-04-10 08:07:48 -05:00
|
|
|
]
|
2019-06-03 10:33:32 -05:00
|
|
|
function_name = ""
|
2017-04-10 08:07:48 -05:00
|
|
|
|
|
|
|
def before(self):
|
|
|
|
# Some test function is needed for debugger
|
2019-06-03 10:33:32 -05:00
|
|
|
self.function_name = "a_test_function" + \
|
2022-08-12 06:40:26 -05:00
|
|
|
str(secrets.choice(range(10000, 65535)))
|
2018-02-09 06:57:37 -06:00
|
|
|
test_utils.create_debug_function(
|
2019-11-10 23:19:00 -06:00
|
|
|
self.server, self.test_db, self.function_name
|
2018-02-09 06:57:37 -06:00
|
|
|
)
|
2017-04-10 08:07:48 -05:00
|
|
|
|
2019-11-10 23:19:00 -06:00
|
|
|
if test_utils.does_function_exist(self.server, self.test_db,
|
2019-06-03 10:33:32 -05:00
|
|
|
self.function_name) != 'True':
|
2020-08-07 02:07:00 -05:00
|
|
|
raise RuntimeError("The required function is not found")
|
2019-05-23 03:31:52 -05:00
|
|
|
|
2017-04-10 08:07:48 -05:00
|
|
|
def runTest(self):
|
|
|
|
self.page.wait_for_spinner_to_disappear()
|
2017-08-29 08:57:56 -05:00
|
|
|
self.page.add_server(self.server)
|
2017-04-10 08:07:48 -05:00
|
|
|
self._function_node_expandable()
|
|
|
|
self._debug_function()
|
|
|
|
|
|
|
|
def after(self):
|
2018-03-19 08:23:29 -05:00
|
|
|
self.page.remove_server(self.server)
|
2019-11-10 23:19:00 -06:00
|
|
|
test_utils.drop_debug_function(self.server, self.test_db,
|
2019-06-03 10:33:32 -05:00
|
|
|
self.function_name)
|
2017-04-10 08:07:48 -05:00
|
|
|
|
|
|
|
def _function_node_expandable(self):
|
2021-11-10 00:20:20 -06:00
|
|
|
self.page.expand_schema_child_node("Server", self.server['name'],
|
|
|
|
self.server['db_password'],
|
|
|
|
self.test_db, 'public', "Functions")
|
|
|
|
function_node = self.page.check_if_element_exists_with_scroll(
|
|
|
|
TreeAreaLocators.function_node(self.function_name + "()"))
|
2023-01-03 23:14:22 -06:00
|
|
|
|
|
|
|
self.assertTrue(bool(function_node),
|
|
|
|
self.function_name + ' function node not found.')
|
|
|
|
|
2021-11-10 00:20:20 -06:00
|
|
|
function_node.click()
|
2017-04-10 08:07:48 -05:00
|
|
|
|
|
|
|
def _debug_function(self):
|
2022-12-22 02:55:18 -06:00
|
|
|
self.page.driver.find_element(By.CSS_SELECTOR,
|
|
|
|
NavMenuLocators.object_menu_css).click()
|
2018-02-09 06:57:37 -06:00
|
|
|
ActionChains(
|
|
|
|
self.page.driver
|
|
|
|
).move_to_element(
|
2022-12-22 02:55:18 -06:00
|
|
|
self.page.driver.find_element(
|
|
|
|
By.CSS_SELECTOR, "div[data-label='Debugging']")
|
2018-02-09 06:57:37 -06:00
|
|
|
).perform()
|
2022-12-22 02:55:18 -06:00
|
|
|
time.sleep(2)
|
|
|
|
self.page.driver.find_element(
|
|
|
|
By.CSS_SELECTOR, "li[data-label='Debug']").click()
|
2017-08-29 08:57:56 -05:00
|
|
|
|
2017-04-10 08:07:48 -05:00
|
|
|
# We need to check if debugger plugin is installed or not
|
|
|
|
try:
|
2017-08-29 08:57:56 -05:00
|
|
|
wait = WebDriverWait(self.page.driver, 2)
|
|
|
|
is_error = wait.until(EC.presence_of_element_located(
|
2022-03-07 04:06:10 -06:00
|
|
|
(By.XPATH, "//div[contains(@class,'MuiDialogTitle-root')]"
|
2021-12-07 07:22:40 -06:00
|
|
|
"//div[text()='Debugger Error']")
|
2018-02-09 06:57:37 -06:00
|
|
|
))
|
2018-01-23 04:01:20 -06:00
|
|
|
|
2020-07-24 01:45:29 -05:00
|
|
|
except TimeoutException:
|
2017-04-10 08:07:48 -05:00
|
|
|
is_error = None
|
|
|
|
|
|
|
|
# If debugger plugin is not found
|
2017-08-29 08:57:56 -05:00
|
|
|
if is_error and is_error.text == "Debugger Error":
|
2019-08-22 04:20:51 -05:00
|
|
|
click = True
|
|
|
|
while click:
|
|
|
|
try:
|
2022-09-09 04:53:18 -05:00
|
|
|
self.page.click_modal('OK')
|
2019-08-22 04:20:51 -05:00
|
|
|
wait.until(EC.invisibility_of_element(
|
2021-12-07 07:22:40 -06:00
|
|
|
(By.XPATH, "//div[@class ='MuiDialogTitle-root']"
|
|
|
|
"//div[text()='Debugger Error']")
|
2019-08-22 04:20:51 -05:00
|
|
|
))
|
|
|
|
click = False
|
|
|
|
except TimeoutException:
|
|
|
|
pass
|
2017-08-29 08:57:56 -05:00
|
|
|
self.skipTest(
|
|
|
|
"Please make sure that debugger plugin is properly configured"
|
|
|
|
)
|
2017-04-10 08:07:48 -05:00
|
|
|
else:
|
2017-08-29 08:57:56 -05:00
|
|
|
self.page.driver.switch_to.frame(
|
2022-06-23 07:49:32 -05:00
|
|
|
self.page.driver.find_element(By.TAG_NAME, 'iframe')
|
2017-08-29 08:57:56 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
wait.until(EC.presence_of_element_located(
|
|
|
|
(By.XPATH, "//span[contains(.,'Hello, pgAdmin4')]"))
|
|
|
|
)
|
|
|
|
self.page.click_element(
|
2022-06-23 07:49:32 -05:00
|
|
|
self.page.driver.find_elements(By.XPATH, "//button")[2]
|
2017-08-29 08:57:56 -05:00
|
|
|
)
|
2017-04-10 08:07:48 -05:00
|
|
|
|
2017-08-29 08:57:56 -05:00
|
|
|
wait.until(EC.presence_of_element_located(
|
2022-06-15 01:07:54 -05:00
|
|
|
(By.XPATH, "//div[@id='id-results']//td "
|
|
|
|
"[contains(.,'Hello, pgAdmin4')]"))
|
2017-08-29 08:57:56 -05:00
|
|
|
)
|
|
|
|
|
2022-09-10 03:52:49 -05:00
|
|
|
# Only this tab is vulnerable rest are Code Mirror
|
2017-08-29 08:57:56 -05:00
|
|
|
# control which are already tested in Query tool test case
|
2022-06-15 01:07:54 -05:00
|
|
|
self.page.click_tab('id-debugger-messages', rc_dock=True)
|
2017-04-10 08:07:48 -05:00
|
|
|
source_code = self.page.find_by_xpath(
|
2022-06-15 01:07:54 -05:00
|
|
|
"//div[@id='id-debugger-messages'] //div[@id='debugger-msg']"
|
2017-04-10 08:07:48 -05:00
|
|
|
).get_attribute('innerHTML')
|
2023-03-15 08:24:22 -05:00
|
|
|
|
|
|
|
self.assertIsNotNone(source_code, 'Messages tab is empty.')
|
|
|
|
|
2017-04-10 08:07:48 -05:00
|
|
|
self._check_escaped_characters(
|
|
|
|
source_code,
|
|
|
|
'NOTICE: <img src="x" onerror="console.log(1)">',
|
|
|
|
'Debugger'
|
|
|
|
)
|
|
|
|
self._close_debugger()
|
|
|
|
|
|
|
|
def _close_debugger(self):
|
2021-11-12 00:26:44 -06:00
|
|
|
self.page.driver.switch_to.default_content()
|
2017-04-10 08:07:48 -05:00
|
|
|
self.page.click_element(
|
2018-02-09 06:57:37 -06:00
|
|
|
self.page.find_by_xpath(
|
|
|
|
"//*[@id='dockerContainer']/div/div[3]/div/div[2]/div[1]")
|
2017-04-10 08:07:48 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
def _check_escaped_characters(self, source_code, string_to_find, source):
|
|
|
|
# For XSS we need to search against element's html code
|
2022-06-15 01:07:54 -05:00
|
|
|
assert source_code.find(
|
|
|
|
string_to_find) != -1, "{0} might be vulnerable to XSS ".format(
|
|
|
|
source)
|