Skip to content

Commit

Permalink
Fix error when encountering web pushkeys with missing endpoints (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
squahtx authored Dec 9, 2021
1 parent 1d5c680 commit 2742acd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
1 change: 1 addition & 0 deletions changelog.d/288.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug introduced in Sygnal 0.9.1 where web pushkeys with missing endpoints would cause an error.
2 changes: 1 addition & 1 deletion docs/applications.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ In your web application, [the push manager subscribe method](https://developer.m
will return
[a subscription](https://developer.mozilla.org/en-US/docs/Web/API/PushSubscription)
with an `endpoint` and `keys` property, the latter containing a `p256dh` and `auth`
property. The `p256dh` key is used as the push key, and the push data is expected
property. The `p256dh` key is used as the push key, and the push data must contain
`endpoint` and `auth`. You can also set `default_payload` in the push data;
any properties set in it will be present in the push messages you receive,
so it can be used to pass identifiers specific to your client
Expand Down
25 changes: 13 additions & 12 deletions sygnal/webpushpushkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def __init__(self, name: str, sygnal: "Sygnal", config: Dict[str, Any]):
)
self.http_request_factory = HttpRequestFactory()

self.allowed_endpoints = None # type: Optional[List[Pattern]]
self.allowed_endpoints: Optional[List[Pattern[str]]] = None
allowed_endpoints = self.get_config("allowed_endpoints", list)
if allowed_endpoints:
self.allowed_endpoints = list(map(glob_to_regex, allowed_endpoints))
Expand Down Expand Up @@ -150,6 +150,17 @@ async def _dispatch_notification_unlimited(

endpoint = device.data.get("endpoint")
auth = device.data.get("auth")

if not p256dh or not isinstance(endpoint, str) or not isinstance(auth, str):
logger.warn(
"Rejecting pushkey; subscription info incomplete or invalid "
+ "(p256dh: %s, endpoint: %r, auth: %r)",
p256dh,
endpoint,
auth,
)
return [device.pushkey]

endpoint_domain = urlparse(endpoint).netloc
if self.allowed_endpoints:
allowed = any(
Expand All @@ -163,16 +174,6 @@ async def _dispatch_notification_unlimited(
# abort, but don't reject push key
return []

if not p256dh or not endpoint or not auth:
logger.warn(
"Rejecting pushkey; subscription info incomplete "
+ "(p256dh: %s, endpoint: %s, auth: %s)",
p256dh,
endpoint,
auth,
)
return [device.pushkey]

subscription_info = {
"endpoint": endpoint,
"keys": {"p256dh": p256dh, "auth": auth},
Expand Down Expand Up @@ -289,7 +290,7 @@ def _handle_response(
response: IResponse,
response_text: str,
pushkey: str,
endpoint_domain: bytes,
endpoint_domain: str,
) -> bool:
"""
Logs and determines the outcome of the response
Expand Down

0 comments on commit 2742acd

Please sign in to comment.