2023-05-15 00:57:58 +00:00
|
|
|
|
from contextlib import suppress
|
|
|
|
|
|
from os import getenv
|
|
|
|
|
|
from requests import get
|
|
|
|
|
|
from requests.exceptions import RequestException
|
|
|
|
|
|
from selenium import webdriver
|
|
|
|
|
|
from selenium.webdriver.common.by import By
|
|
|
|
|
|
from selenium.webdriver.firefox.options import Options
|
|
|
|
|
|
from selenium.common.exceptions import NoSuchElementException
|
|
|
|
|
|
from time import sleep
|
|
|
|
|
|
from traceback import format_exc
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
ready = False
|
|
|
|
|
|
retries = 0
|
|
|
|
|
|
while not ready:
|
|
|
|
|
|
with suppress(RequestException):
|
2023-10-26 11:49:44 +00:00
|
|
|
|
resp = get("http://www.example.com/ready", headers={"Host": "www.example.com"})
|
|
|
|
|
|
status_code = resp.status_code
|
|
|
|
|
|
text = resp.text
|
2023-05-15 00:57:58 +00:00
|
|
|
|
|
|
|
|
|
|
if status_code >= 500:
|
|
|
|
|
|
print("❌ An error occurred with the server, exiting ...", flush=True)
|
|
|
|
|
|
exit(1)
|
|
|
|
|
|
|
2023-10-26 11:49:44 +00:00
|
|
|
|
ready = status_code < 400 and text == "ready"
|
2023-05-15 00:57:58 +00:00
|
|
|
|
|
|
|
|
|
|
if retries > 10:
|
|
|
|
|
|
print("❌ The service took too long to be ready, exiting ...", flush=True)
|
|
|
|
|
|
exit(1)
|
|
|
|
|
|
elif not ready:
|
|
|
|
|
|
retries += 1
|
2023-10-03 10:01:24 +00:00
|
|
|
|
print("⚠️ Waiting for the service to be ready, retrying in 5s ...", flush=True)
|
2023-05-15 00:57:58 +00:00
|
|
|
|
sleep(5)
|
|
|
|
|
|
|
|
|
|
|
|
firefox_options = Options()
|
|
|
|
|
|
firefox_options.add_argument("--headless")
|
|
|
|
|
|
|
|
|
|
|
|
errors = getenv("ERRORS", "")
|
2023-10-03 10:01:24 +00:00
|
|
|
|
intercepted_error_codes = getenv("INTERCEPTED_ERROR_CODES", "400 401 403 404 405 413 429 500 501 502 503 504")
|
2023-05-15 00:57:58 +00:00
|
|
|
|
|
|
|
|
|
|
print("ℹ️ Starting Firefox ...", flush=True)
|
|
|
|
|
|
with webdriver.Firefox(options=firefox_options) as driver:
|
|
|
|
|
|
driver.delete_all_cookies()
|
|
|
|
|
|
driver.maximize_window()
|
|
|
|
|
|
|
|
|
|
|
|
print(
|
|
|
|
|
|
"ℹ️ Navigating to http://www.example.com/?id=/etc/passwd ...",
|
|
|
|
|
|
flush=True,
|
|
|
|
|
|
)
|
|
|
|
|
|
driver.get("http://www.example.com/?id=/etc/passwd")
|
|
|
|
|
|
|
|
|
|
|
|
default_message = None
|
|
|
|
|
|
with suppress(NoSuchElementException):
|
2023-10-03 10:01:24 +00:00
|
|
|
|
default_message = driver.find_element(By.XPATH, "//p[contains(text(), 'This website is protected with')]")
|
2023-05-15 00:57:58 +00:00
|
|
|
|
|
2023-10-03 10:01:24 +00:00
|
|
|
|
if default_message and (errors or intercepted_error_codes != "400 401 403 404 405 413 429 500 501 502 503 504"):
|
2023-05-15 00:57:58 +00:00
|
|
|
|
print(
|
|
|
|
|
|
"❌ The default error page is being displayed, exiting ...",
|
|
|
|
|
|
flush=True,
|
|
|
|
|
|
)
|
|
|
|
|
|
exit(1)
|
2023-10-03 10:01:24 +00:00
|
|
|
|
elif not default_message and not errors and intercepted_error_codes == "400 401 403 404 405 413 429 500 501 502 503 504":
|
2023-05-15 00:57:58 +00:00
|
|
|
|
print(
|
|
|
|
|
|
"❌ The default error page is not being displayed, exiting ...",
|
|
|
|
|
|
flush=True,
|
|
|
|
|
|
)
|
|
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
if errors:
|
|
|
|
|
|
custom_message = None
|
|
|
|
|
|
with suppress(NoSuchElementException):
|
2023-10-03 10:01:24 +00:00
|
|
|
|
custom_message = driver.find_element(By.XPATH, "//h1[contains(text(), 'It Works!')]")
|
2023-05-15 00:57:58 +00:00
|
|
|
|
|
|
|
|
|
|
if not custom_message:
|
|
|
|
|
|
print(
|
|
|
|
|
|
"❌ The custom error page is not being displayed while a custom one has been provided, exiting ...",
|
|
|
|
|
|
flush=True,
|
|
|
|
|
|
)
|
|
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
if intercepted_error_codes != "400 401 403 404 405 413 429 500 501 502 503 504":
|
|
|
|
|
|
nginx_message = None
|
|
|
|
|
|
with suppress(NoSuchElementException):
|
2023-10-03 10:01:24 +00:00
|
|
|
|
nginx_message = driver.find_element(By.XPATH, "//center[contains(text(), 'nginx')]")
|
2023-05-15 00:57:58 +00:00
|
|
|
|
|
|
|
|
|
|
if not nginx_message:
|
|
|
|
|
|
print(
|
|
|
|
|
|
"❌ The default nginx error page is not being displayed while a custom one has been provided, exiting ...",
|
|
|
|
|
|
flush=True,
|
|
|
|
|
|
)
|
|
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
print("✅ Errors are working as expected ...", flush=True)
|
|
|
|
|
|
except SystemExit:
|
|
|
|
|
|
exit(1)
|
|
|
|
|
|
except:
|
|
|
|
|
|
print(f"❌ Something went wrong, exiting ...\n{format_exc()}", flush=True)
|
|
|
|
|
|
exit(1)
|