Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

35 defensive html parsing #1

Merged
merged 2 commits into from
Jul 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions awsprocesscreds/saml.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ def _retrieve_login_form_from_endpoint(self, endpoint):
if not form_action.lower().startswith('https://'):
raise SAMLError('Your SAML IdP must use HTTPS connection')
payload = dict((tag.attrib['name'], tag.attrib.get('value', ''))
for tag in login_form_html_node.findall(".//input"))
for tag in login_form_html_node.findall(
".//input[@name]"))
return form_action, payload

def _assert_non_error_response(self, response):
Expand Down Expand Up @@ -287,7 +288,8 @@ def _dict2str(self, d):
# so that the output will be suitable to be fed into an ET later.
parts = []
for k, v in d.items():
escaped_value = escape(v) # pylint: disable=deprecated-method
escaped_value = escape( # pylint: disable=deprecated-method
v) if v is not None else None
parts.append('%s="%s"' % (k, escaped_value))
return ' '.join(sorted(parts))

Expand Down
67 changes: 67 additions & 0 deletions tests/unit/test_saml.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,73 @@ def tests_uses_default_form_values(self, generic_auth, generic_config,
}
)

def test_input_missing_name_attribute(self, generic_auth,
generic_config,
mock_requests_session):
saml_form = (
'<html>'
'<form action="/path/login/">'
'<input debug="true"/>'
'<input name="spam" value="eggs"/>'
'<input name="username"/>'
'<input name="password"/>'
'</form>'
'</html>'
)
mock_requests_session.get.return_value = mock.Mock(
spec=requests.Response, status_code=200, text=saml_form
)
mock_requests_session.post.return_value = mock.Mock(
spec=requests.Response, status_code=200, text=(
'<form><input name="SAMLResponse" '
'value="fakeassertion"/></form>'
)
)
saml_assertion = generic_auth.retrieve_saml_assertion(generic_config)
assert saml_assertion == 'fakeassertion'

mock_requests_session.post.assert_called_with(
"https://example.com/path/login/", verify=True,
data={
'username': 'monty',
'password': 'mypassword',
'spam': 'eggs'
}
)

def test_boolean_attribute_handling(self, generic_auth,
generic_config,
mock_requests_session):
saml_form = (
'<html>'
'<form action="/path/login/">'
'<input attr-is-true name="spam" value="eggs"/>'
'<input name="username"/>'
'<input name="password"/>'
'</form>'
'</html>'
)
mock_requests_session.get.return_value = mock.Mock(
spec=requests.Response, status_code=200, text=saml_form
)
mock_requests_session.post.return_value = mock.Mock(
spec=requests.Response, status_code=200, text=(
'<form><input name="SAMLResponse" '
'value="fakeassertion"/></form>'
)
)
saml_assertion = generic_auth.retrieve_saml_assertion(generic_config)
assert saml_assertion == 'fakeassertion'

mock_requests_session.post.assert_called_with(
"https://example.com/path/login/", verify=True,
data={
'username': 'monty',
'password': 'mypassword',
'spam': 'eggs'
}
)

def test_error_getting_form(self, generic_auth, mock_requests_session,
generic_config):
mock_requests_session.get.return_value = mock.Mock(
Expand Down