Skip to content
This repository has been archived by the owner on May 9, 2022. It is now read-only.

Commit

Permalink
Admin metadata upload form validation
Browse files Browse the repository at this point in the history
  • Loading branch information
peppelinux committed Feb 16, 2021
1 parent a499c1b commit c9fe627
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion testenv/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ def _wsgiconf(self):
_cnf = {
'host': self._config.host,
'port': self._config.port,
'debug': self._config.debug,
'debug': self._config.debug
}
if self._config.https:
key = self._config.https_key_file_path
Expand Down
2 changes: 1 addition & 1 deletion testenv/spmetadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def __init__(self, conf):

def load(self, entity_id):
metadata = self._provider.get(entity_id)
if metadata is None:
if not metadata:
raise MetadataNotFoundError(entity_id)
return ServiceProviderMetadata(metadata, self, 'db')

Expand Down
16 changes: 13 additions & 3 deletions testenv/storages.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.dialects.postgresql import JSON
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import sessionmaker, validates

from .utils import saml_to_dict

Base = declarative_base()

Expand Down Expand Up @@ -228,8 +230,16 @@ def __init__(self, model, session,
class DatabaseSPProvider(AbstractDatabaseProvider):
class DatabaseSPRecord(Base):
__tablename__ = 'spmetadata'
entity_id = Column(String, primary_key=True)
body = Column(String)
entity_id = Column(String, primary_key=True, nullable=False)
body = Column(String, nullable=False)

@validates('body')
def validate_body(self, key, body):
try:
saml_to_dict(body)
except Exception as e:
raise AssertionError(f'Metadata invalid: {e}')
return body

TABLE = DatabaseSPRecord

Expand Down
9 changes: 5 additions & 4 deletions testenv/tests/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from lxml import objectify

from testenv.exceptions import (
DeserializationError, RequestParserError, SPIDValidationError, XMLFormatValidationError, XMLSchemaValidationError,
DeserializationError, RequestParserError, SPIDValidationError, ValidationError, XMLFormatValidationError,
XMLSchemaValidationError,
)
from testenv.parser import HTTPPostRequestParser, HTTPRedirectRequestParser, HTTPRequestDeserializer, SAMLTree
from testenv.tests.utils import FakeRequest
Expand Down Expand Up @@ -220,10 +221,10 @@ def test_deserialization(self):
class SAMLStupidMetadataTestCase(unittest.TestCase):
def test_broken_metadata_xml_valuerror(self):
with open("testenv/tests/data/sp-invalid-xml-starttag.xml") as xmlstr:
self.assertEqual({}, saml_to_dict(xmlstr))
self.assertRaises(ValidationError, saml_to_dict, xmlstr)

def test_stupid_metadata_xml_valuerror(self):
saml_to_dict('test.stupido')
self.assertRaises(ValidationError, saml_to_dict, 'test.stupido')

def test_nullmetadata_xml_valuerror(self):
self.assertEqual({}, saml_to_dict(None))
self.assertRaises(ValidationError, saml_to_dict, None)
13 changes: 8 additions & 5 deletions testenv/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from testenv import log
from testenv.settings import MULTIPLE_OCCURRENCES_TAGS, SPID_ERRORS

from .exceptions import ValidationError

TIME_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
TIME_FORMAT_WITH_FRAGMENT = re.compile(
r'^(\d{4,4}-\d{2,2}-\d{2,2}T\d{2,2}:\d{2,2}:\d{2,2})(\.\d*)?Z?$')
Expand Down Expand Up @@ -91,22 +93,23 @@ def saml_to_dict(xmlstr):

if not xmlstr:
logger.error(f'{_err_msg} [Null Value Error] on xmlstr')
return {}
raise ValidationError(_err_msg)
# sometimes a bytes objects, sometimes a '_io.TextIOWrapper' object ...
elif isinstance(xmlstr, io.TextIOWrapper):
xmlstr = xmlstr.read()

try:
root = objectify.fromstring(xmlstr)
except ValueError:
except ValueError as e:
logger.error(f'{_err_msg} [ValueError] on: '
f'{xmlstr[0:_trunc]}')
return {}
raise ValidationError(e)

# that's for resiliency ...
except Exception:
except Exception as e:
logger.error(f'{_err_msg} [Unknown Error] on: '
f'{xmlstr[0:_trunc]}')
return {}
raise ValidationError(e)

def _obj(elem):
children = {}
Expand Down

0 comments on commit c9fe627

Please sign in to comment.