From 8fc0dcc05502fd61dfa3c66a23b852ae7938fc68 Mon Sep 17 00:00:00 2001 From: Chris Mayo Date: Tue, 14 Apr 2020 19:19:09 +0100 Subject: [PATCH] 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. --- linkcheck/htmlutil/loginformsearch.py | 6 ++---- tests/test_loginformsearch.py | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/linkcheck/htmlutil/loginformsearch.py b/linkcheck/htmlutil/loginformsearch.py index d10b5089..e8a4cd53 100644 --- a/linkcheck/htmlutil/loginformsearch.py +++ b/linkcheck/htmlutil/loginformsearch.py @@ -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 diff --git a/tests/test_loginformsearch.py b/tests/test_loginformsearch.py index 866e498f..6c2dab16 100644 --- a/tests/test_loginformsearch.py +++ b/tests/test_loginformsearch.py @@ -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)