Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 12.1.0

* Adds `sanitise_content_for` parameter to `send_email_notification` endpoint. See [our documentation](https://docs.notifications.service.gov.uk/python.html#reducing-the-risk-of-malicious-content-injection-in-placeholders) for guidance on how to use this.

## 12.0.0

* Drop support for end-of-life Python 3.9
Expand Down
4 changes: 3 additions & 1 deletion integration_test/integration_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ def send_email_notification_test_response(python_client, reply_to=None):
personalisation=personalisation,
one_click_unsubscribe_url=one_click_unsubscribe_url,
email_reply_to_id=email_reply_to_id,
sanitise_content_for=["name"],
)
validate(response, post_email_response)
assert unique_name in response["content"]["body"] # check placeholders are replaced
# check placeholders are replaced and sanitised:
assert unique_name.replace("-", "\\-") in response["content"]["body"]
return response["id"]


Expand Down
8 changes: 6 additions & 2 deletions integration_test/schemas/v2/notification_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
},
"required": ["id", "content", "uri", "template"],
}

# TODO: this doesn't seem to be used anywhere, do we still need it?
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR that added this seems to indicate that notification_schemas.py was intended to be the same as its's API counterpart https://github.com/alphagov/notifications-api/blob/main/app/v2/notifications/notification_schemas.py so everything was copied over.

I think it is good to leave the TODO heading in this PR so it can be revisited in the future.

post_email_request = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST email notification schema",
Expand All @@ -128,6 +128,7 @@
"email_reply_to_id": uuid,
"personalisation": personalisation,
"one_click_unsubscribe_url": https_url,
"sanitise_content_for": {"type": "array", "items": {"type": "string"}},
},
"required": ["email_address", "template_id"],
}
Expand Down Expand Up @@ -156,8 +157,9 @@
"content": email_content,
"uri": {"type": "string"},
"template": template,
"sanitised_content": {"type": "object"},
},
"required": ["id", "content", "uri", "template"],
"required": ["id", "content", "uri", "template", "sanitised_content"],
}

post_letter_request = {
Expand Down Expand Up @@ -217,13 +219,15 @@ def create_post_sms_response_from_notification(notification, body, from_number,
}


# TODO: we don't seem to be using this, is it needed?
def create_post_email_response_from_notification(notification, content, subject, email_from, url_root):
return {
"id": notification.id,
"reference": notification.client_reference,
"content": {"from_email": email_from, "body": content, "subject": subject},
"uri": f"{url_root}/v2/notifications/{str(notification.id)}",
"template": __create_template_from_notification(notification=notification, url_root=url_root),
"sanitised_content": {},
}


Expand Down
2 changes: 1 addition & 1 deletion notifications_python_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
# -- http://semver.org/

__version__ = "12.0.0"
__version__ = "12.1.0"

from notifications_python_client.errors import ( # noqa
REQUEST_ERROR_MESSAGE,
Expand Down
3 changes: 3 additions & 0 deletions notifications_python_client/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def send_email_notification(
reference=None,
email_reply_to_id=None,
one_click_unsubscribe_url=None,
sanitise_content_for=None,
):
notification = {"email_address": email_address, "template_id": template_id}
if personalisation:
Expand All @@ -39,6 +40,8 @@ def send_email_notification(
notification.update({"email_reply_to_id": email_reply_to_id})
if one_click_unsubscribe_url:
notification.update({"one_click_unsubscribe_url": one_click_unsubscribe_url})
if sanitise_content_for:
notification.update({"sanitise_content_for": sanitise_content_for})

return self.post("/v2/notifications/email", data=notification)

Expand Down
19 changes: 19 additions & 0 deletions tests/notifications_python_client/test_notifications_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,25 @@ def test_create_email_notification_with_personalisation(notifications_client, rm
}


def test_create_email_notification_with_sanitise_content_for(notifications_client, rmock):
endpoint = f"{TEST_HOST}/v2/notifications/email"
rmock.request("POST", endpoint, json={"status": "success"}, status_code=200)

notifications_client.send_email_notification(
email_address="to@example.com",
template_id="456",
personalisation={"name": "chris", "link": "https://www.safe-link.gov.uk"},
sanitise_content_for=["name"],
)

assert rmock.last_request.json() == {
"template_id": "456",
"email_address": "to@example.com",
"personalisation": {"name": "chris", "link": "https://www.safe-link.gov.uk"},
"sanitise_content_for": ["name"],
}


def test_create_email_notification_with_one_click_unsubscribe_url(notifications_client, rmock):
endpoint = f"{TEST_HOST}/v2/notifications/email"
rmock.request("POST", endpoint, json={"status": "success"}, status_code=200)
Expand Down
Loading