Make matching login form credentials case-sensitive

The keys of the form.data dictionary are case-sensitive and therefore a
KeyError was possible if the configured values are not identical to
the input element name attributes.
This commit is contained in:
Chris Mayo 2020-04-14 19:19:09 +01:00
parent 7a6ef938cc
commit 8fc0dcc055
2 changed files with 4 additions and 6 deletions

View file

@ -42,16 +42,14 @@ def search_form(content, cgiuser, cgipassword):
CGI fields. If no form is found return None.
"""
soup = htmlsoup.make_soup(content)
# The value of the name attribute is case-insensitive
# https://www.w3.org/TR/html401/interact/forms.html#adef-name-INPUT
cginames = {cgiuser.lower(), cgipassword.lower()}
cginames = {cgiuser, cgipassword}
for form_element in soup.find_all("form", action=True):
form = Form(form_element["action"])
for input_element in form_element.find_all("input",
attrs={"name": True}):
form.add_value(
input_element["name"], input_element.attrs.get("value"))
if cginames <= {x.lower() for x in form.data}:
if cginames <= set(form.data):
log.debug(LOG_CHECK, "Found form %s", form)
return form

View file

@ -43,7 +43,7 @@ class TestFormSearch(unittest.TestCase):
def test_search_form(self):
form = loginformsearch.search_form(login_form,
"USER_FIELD", "password_field")
"User_Field", "Password_Field")
self.assertIsNotNone(form)
self.assertEqual(form.url, "/log_me_in")
self.assertIn("User_Field", form.data)
@ -51,5 +51,5 @@ class TestFormSearch(unittest.TestCase):
def test_search_form_none(self):
form = loginformsearch.search_form(login_form,
"nouser", "nopassword")
"user_field", "password_field")
self.assertIsNone(form)