-
Notifications
You must be signed in to change notification settings - Fork 12
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
Enhance UX and add features to the Callback pages #1855
Merged
Merged
Changes from 17 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
1eb677c
Add basic validation to callbacks URLs
whabanks 36fc3d3
Added dynamic hint text to the url field
whabanks 90694c1
Merge branch 'main' into task/verify-callback-url
whabanks 17edaeb
Various fixes
whabanks dd8c154
Merge branch 'main' into task/verify-callback-url
whabanks a1ae8b2
Fix tests
whabanks f0c7683
Merge branch 'main' into task/verify-callback-url
whabanks 40c743a
Add placeholder translations
whabanks 9927046
Merge branch 'main' into task/verify-callback-url
whabanks 8c79c8f
Merge branch 'main' into task/verify-callback-url
whabanks 0046a7d
Consider 5xx responses as valid
whabanks d8ea5cb
Merge branch 'main' into task/verify-callback-url
whabanks 144e65c
Improve the callback config UX
whabanks ab221c6
Merge remote-tracking branch 'origin/main' into task/verify-callback-url
whabanks eec3e7c
Add callback test button
whabanks ba989b9
Unify delivery-status-callback and received-text-messages-callback pages
whabanks 193e263
formatting fixes
whabanks f3f3857
Fix tests
whabanks 541b010
Merge branch 'main' into task/verify-callback-url
whabanks 104bfca
Fix updated translations in code
whabanks a7d565b
Merge branch 'main' into task/verify-callback-url
whabanks dd83ae9
Add & fix tests
whabanks 1468f21
Update delete message
whabanks 7164b9e
Update french translations
whabanks 3897819
Merge branch 'main' into task/verify-callback-url
jzbahrai 53f1f24
Update translations
whabanks 807433d
Merge branch 'main' into task/verify-callback-url
whabanks 71c4ba5
Translations & refresh lock file
whabanks f2330a2
Fix code QL issues
whabanks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,9 @@ | |
import time | ||
|
||
import pwnedpasswords | ||
from flask import current_app | ||
import requests | ||
import validators | ||
from flask import current_app, g | ||
from flask_babel import _ | ||
from flask_babel import lazy_gettext as _l | ||
from notifications_utils.field import Field | ||
|
@@ -11,7 +13,7 @@ | |
from wtforms import ValidationError | ||
from wtforms.validators import Email | ||
|
||
from app import formatted_list, service_api_client | ||
from app import current_service, formatted_list, service_api_client | ||
from app.main._blocked_passwords import blocked_passwords | ||
from app.utils import Spreadsheet, email_safe, email_safe_name, is_gov_user | ||
|
||
|
@@ -25,7 +27,8 @@ def __init__(self, message=None): | |
def __call__(self, form, field): | ||
if current_app.config.get("HIPB_ENABLED", None): | ||
hibp_bad_password_found = False | ||
for i in range(0, 3): # Try 3 times. If the HIPB API is down then fall back to the old banlist. | ||
# Try 3 times. If the HIPB API is down then fall back to the old banlist. | ||
for i in range(0, 3): | ||
try: | ||
response = pwnedpasswords.check(field.data) | ||
if response > 0: | ||
|
@@ -141,6 +144,57 @@ def __call__(self, form, field): | |
raise ValidationError(self.message) | ||
|
||
|
||
class ValidCallbackUrl: | ||
def __init__(self, message="Enter a URL that starts with https://"): | ||
self.message = message | ||
|
||
def __call__(self, form, field): | ||
if field.data: | ||
validate_callback_url(field.data, form.bearer_token.data) | ||
|
||
|
||
def validate_callback_url(service_callback_url, bearer_token): | ||
"""Validates a callback URL, checking that it is https and by sending a POST request to the URL with a health_check parameter. | ||
4xx responses are considered invalid. 5xx responses are considered valid as it indicates there is at least a service running | ||
at the URL, and we are sending a payload that the service will not understand. | ||
|
||
Args: | ||
service_callback_url (str): The url to validate. | ||
bearer_token (str): The bearer token to use in the request, specified by the user requesting callbacks. | ||
|
||
Raises: | ||
ValidationError: If the URL is not HTTPS or the http response is 4xx. | ||
""" | ||
if not validators.url(service_callback_url): | ||
current_app.logger.warning( | ||
f"Unable to create callback for service: {current_service.id}. Error: Invalid callback URL format: URL: {service_callback_url}" | ||
) | ||
raise ValidationError(_l("Enter a URL that starts with https://")) | ||
|
||
try: | ||
response = requests.post( | ||
url=service_callback_url, | ||
allow_redirects=True, | ||
data={"health_check": "true"}, | ||
headers={"Content-Type": "application/json", "Authorization": f"Bearer {bearer_token}"}, | ||
timeout=2, | ||
) | ||
|
||
g.callback_response_time = response.elapsed.total_seconds() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was going to say I was concerned that using the flask global object might leak the value here to other users, but just learned that values stored here are request-specific and last for only one request. Good to know! |
||
|
||
if response.status_code < 500 and response.status_code >= 400: | ||
current_app.logger.warning( | ||
f"Unable to create callback for service: {current_service.id} Error: Callback URL not reachable URL: {service_callback_url}" | ||
) | ||
raise ValidationError(_l("Check your service is running and not using a proxy we cannot access")) | ||
|
||
except requests.RequestException as e: | ||
current_app.logger.warning( | ||
f"Unable to create callback for service: {current_service.id} Error: Callback URL not reachable URL: {service_callback_url} Exception: {e}" | ||
) | ||
raise ValidationError(_l("Check your service is running and not using a proxy we cannot access")) | ||
|
||
|
||
def validate_email_from(form, field): | ||
if email_safe(field.data) != field.data.lower(): | ||
# fix their data instead of only warning them | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed in slack, this might be an issue since here it expects the callback servers to respond with a 200 when we call them with
{"health_check": "true"}
. If we want to do this we would probably have to advertise this in the docs so users are aware.