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

Add validation for invalid html tags #244

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions app/error_messages.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
DUMB_QUOTES_FOUND = "Found dumb quotes(s) in schema text"
HTML_FOUND = "Found invalid HTML in schema text"
INVALID_WHITESPACE_FOUND = "Found invalid white space(s) in schema text"
DUPLICATE_ID_FOUND = "Duplicate id found"
FOR_LIST_NEVER_POPULATED = "for_list is not populated by any ListCollector blocks or supplementary data sources"
Expand Down
42 changes: 42 additions & 0 deletions app/validators/questionnaire_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def validate(self):
self.validate_duplicates()
self.validate_smart_quotes()
self.validate_white_spaces()
self.validate_html()
self.validate_answer_references()
self.validate_list_references()

Expand Down Expand Up @@ -114,6 +115,47 @@ def validate_smart_quotes(self):
pointer=translatable_item.pointer,
)

def validate_html(self):
html_strings = []
schema_object = SurveySchema(self.schema_element)

# pylint: disable=invalid-string-quote
html_regex = re.compile(r"<[^>]*>")

for translatable_item in schema_object.translatable_items:
schema_text = translatable_item.value

values_to_check = [schema_text]

if isinstance(schema_text, dict):
values_to_check = schema_text.values()

html_strings.extend(
{"pointer": translatable_item.pointer, "text": schema_text}
for schema_text in values_to_check
if schema_text and html_regex.search(schema_text)
)
if html_strings:
self.check_invalid_html_tags(html_strings)

def check_invalid_html_tags(self, html_strings):
strong = re.compile(r"<strong>(?:(?!</strong>).)*</strong>")
anchor = re.compile(r"<a [^>]*>.*?</a>")
berroar marked this conversation as resolved.
Show resolved Hide resolved
all_tags = re.compile(
r"<([a-z0-9]+)(?=[\s>])(?:[^>=]|='[^']*'|=\"[^\"]*\"|=[^'\"\s]*)*\s?/?>"
)

for html_string in html_strings:
if len(strong.findall(html_string["text"])) + len(
anchor.findall(html_string["text"])
) != len(all_tags.findall(html_string["text"])):

self.add_error(
error_messages.HTML_FOUND,
pointer=html_string["pointer"],
text=html_string["text"],
)

def validate_white_spaces(self):
schema_object = SurveySchema(self.schema_element)

Expand Down
133 changes: 133 additions & 0 deletions tests/schemas/invalid/test_invalid_html_in_schema_text.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{
"mime_type": "application/json/ons/eq",
"language": "en",
"schema_version": "0.0.1",
"data_version": "0.0.3",
"survey_id": "144",
"theme": "default",
"title": "Test invalid html",
"legal_basis": "Notice is given under section 999 of the Test Act 2000",
"metadata": [
{
"name": "user_id",
"type": "string"
},
{
"name": "period_id",
"type": "string"
},
{
"name": "ru_name",
"type": "string"
},
{
"name": "ru_ref",
"type": "string"
},
{
"name": "trad_as",
"type": "string",
"optional": true
}
],
"questionnaire_flow": {
"type": "Linear",
"options": {}
},
"sections": [
{
"id": "introduction-section",
"title": "Introduction",
"groups": [
{
"id": "introduction-group",
"title": "General Business Information",
"blocks": [
{
"id": "introduction",
"type": "Introduction",
"primary_content": [
{
"id": "business-details",
"title": "Introduction with valid and invalid HTML",
"contents": [
{
"guidance": {
"contents": [
{
"title": "<invalid>Coronavirus (COVID-19) guidance</invalid>",
"description": "<strong>Explain your figures</strong> in the comment section to minimise us contacting you and to help us tell an industry story"
}
]
}
}
]
}
]
},
{
"type": "Interstitial",
"id": "intersitital-one",
"content": {
"title": "Page with invalid html",
"contents": [
{
"description": "<p>You have successfully completed this section<p>"
}
]
}
},
{
"type": "Interstitial",
"id": "interstitial-two",
"content": {
"title": "Page with link",
"contents": [
{
"description": "<a href='link'>Anchor</a>"
}
]
}
},
{
"type": "Interstitial",
"id": "interstitial-three",
"content": {
"title": "<strong>Page with mixed invalid tags<strong>",
"contents": [
{
"description": "<h1>Title</h1><em>Not valid tag</em>"
}
]
}
},
{
"type": "Interstitial",
"id": "interstitial-four",
"content": {
"title": "Valid double <strong>strong</strong> with another <strong>strong</strong>.",
petechd marked this conversation as resolved.
Show resolved Hide resolved
"contents": [
{
"description": "<strong>Title</strong><em>Not valid tag</em>"
}
]
}
},
{
"type": "Interstitial",
"id": "interstitial-five",
"content": {
"title": "Valid double anchor.",
"contents": [
{
"description": "<a href='link'>Title</a> and <a href='link-2'>Not valid tag</a>"
}
]
}
}
]
}
]
}
]
}
2 changes: 1 addition & 1 deletion tests/schemas/valid/test_introduction_with_guidance.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"title": "Section complete",
"contents": [
{
"description": "<p>You have successfully completed this section</p>"
"description": "You have successfully completed this section"
}
]
}
Expand Down
37 changes: 37 additions & 0 deletions tests/test_questionnaire_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,43 @@ def test_invalid_whitespaces_in_schema():
assert validator.errors == expected_error_messages


def test_invalid_html_in_schema():
filename = "schemas/invalid/test_invalid_html_in_schema_text.json"

validator = QuestionnaireValidator(_open_and_load_schema_file(filename))

expected_error_messages = [
{
"message": error_messages.HTML_FOUND,
"pointer": "/sections/0/groups/0/blocks/3/content/title",
"text": "<strong>Page with mixed invalid tags<strong>",
},
{
"message": error_messages.HTML_FOUND,
"pointer": "/sections/0/groups/0/blocks/1/content/contents/0/description",
"text": "<p>You have successfully completed this section<p>",
},
{
"message": error_messages.HTML_FOUND,
"pointer": "/sections/0/groups/0/blocks/3/content/contents/0/description",
"text": "<h1>Title</h1><em>Not valid tag</em>",
},
{
"message": error_messages.HTML_FOUND,
"pointer": "/sections/0/groups/0/blocks/4/content/contents/0/description",
"text": "<strong>Title</strong><em>Not valid tag</em>",
},
{
"message": error_messages.HTML_FOUND,
"pointer": "/sections/0/groups/0/blocks/0/primary_content/0/contents/0/guidance/contents/0/title",
"text": "<invalid>Coronavirus (COVID-19) guidance</invalid>",
},
]
validator.validate_html()

assert validator.errors == expected_error_messages


def test_invalid_answer_type_for_question_summary_concatenation():
filename = "schemas/invalid/test_invalid_answer_type_for_question_summary.json"

Expand Down