TestWeb.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import urllib
  2. import pytest
  3. try:
  4. from selenium.webdriver.support.ui import WebDriverWait
  5. from selenium.webdriver.support.expected_conditions import staleness_of
  6. from selenium.common.exceptions import NoSuchElementException
  7. except:
  8. pass
  9. class WaitForPageLoad(object):
  10. def __init__(self, browser):
  11. self.browser = browser
  12. def __enter__(self):
  13. self.old_page = self.browser.find_element_by_tag_name('html')
  14. def __exit__(self, *args):
  15. WebDriverWait(self.browser, 5).until(staleness_of(self.old_page))
  16. def wget(url):
  17. content = urllib.urlopen(url).read()
  18. assert "server error" not in content.lower(), "Got a server error! " + repr(url)
  19. return content
  20. @pytest.mark.usefixtures("resetSettings")
  21. @pytest.mark.webtest
  22. class TestWeb:
  23. def testFileSecurity(self, site_url):
  24. assert "Not Found" in wget("%s/media/./sites.json" % site_url)
  25. assert "Forbidden" in wget("%s/media/../config.py" % site_url)
  26. assert "Forbidden" in wget("%s/media/1EU1tbG9oC1A8jz2ouVwGZyQ5asrNsE4Vr/../sites.json" % site_url)
  27. assert "Forbidden" in wget("%s/media/1EU1tbG9oC1A8jz2ouVwGZyQ5asrNsE4Vr/..//sites.json" % site_url)
  28. assert "Forbidden" in wget("%s/media/1EU1tbG9oC1A8jz2ouVwGZyQ5asrNsE4Vr/../../zeronet.py" % site_url)
  29. assert "Forbidden" in wget("%s/1EU1tbG9oC1A8jz2ouVwGZyQ5asrNsE4Vr/../sites.json" % site_url)
  30. assert "Forbidden" in wget("%s/1EU1tbG9oC1A8jz2ouVwGZyQ5asrNsE4Vr/..//sites.json" % site_url)
  31. assert "Forbidden" in wget("%s/1EU1tbG9oC1A8jz2ouVwGZyQ5asrNsE4Vr/../../zeronet.py" % site_url)
  32. assert "Forbidden" in wget("%s/content.db" % site_url)
  33. assert "Forbidden" in wget("%s/./users.json" % site_url)
  34. assert "Forbidden" in wget("%s/./key-rsa.pem" % site_url)
  35. assert "Forbidden" in wget("%s/././././././././././//////sites.json" % site_url)
  36. def testLinkSecurity(self, browser, site_url):
  37. browser.get("%s/1EU1tbG9oC1A8jz2ouVwGZyQ5asrNsE4Vr/test/security.html" % site_url)
  38. assert browser.title == "ZeroHello - ZeroNet"
  39. assert browser.current_url == "%s/1EU1tbG9oC1A8jz2ouVwGZyQ5asrNsE4Vr/test/security.html" % site_url
  40. # Switch to inner frame
  41. browser.switch_to.frame(browser.find_element_by_id("inner-iframe"))
  42. assert "wrapper_nonce" in browser.current_url
  43. browser.switch_to.default_content()
  44. # Clicking on links without target
  45. browser.switch_to.frame(browser.find_element_by_id("inner-iframe"))
  46. with WaitForPageLoad(browser):
  47. browser.find_element_by_id("link_to_current").click()
  48. assert "wrapper_nonce" not in browser.current_url # The browser object back to default content
  49. assert "Forbidden" not in browser.page_source
  50. # Check if we have frame inside frame
  51. browser.switch_to.frame(browser.find_element_by_id("inner-iframe"))
  52. with pytest.raises(NoSuchElementException):
  53. assert not browser.find_element_by_id("inner-iframe")
  54. browser.switch_to.default_content()
  55. # Clicking on link with target=_top
  56. browser.switch_to.frame(browser.find_element_by_id("inner-iframe"))
  57. with WaitForPageLoad(browser):
  58. browser.find_element_by_id("link_to_top").click()
  59. assert "wrapper_nonce" not in browser.current_url # The browser object back to default content
  60. assert "Forbidden" not in browser.page_source
  61. browser.switch_to.default_content()
  62. # Try to escape from inner_frame
  63. browser.switch_to.frame(browser.find_element_by_id("inner-iframe"))
  64. assert "wrapper_nonce" in browser.current_url # Make sure we are inside of the inner-iframe
  65. with WaitForPageLoad(browser):
  66. browser.execute_script("window.top.location = window.location")
  67. assert "wrapper_nonce" in browser.current_url # We try to use nonce-ed html without iframe
  68. assert "Forbidden" in browser.page_source # Only allow to use nonce once-time
  69. browser.switch_to.default_content()