1
0

plugin: nwc: improve filtering of expired requests

improve the filtering of incoming requests by checking if they have
explicitly set an expiration tag. If so, they will only be ignored if
this timestamp is exceeded. Otherwise requests older than 30 secons will
get ignored and an error will get sent to the client so the client is
aware it's request arrived too late.
This is done to prevent handling requests the user may already expects
to have failed.
This commit is contained in:
f321x
2025-06-10 19:29:19 +02:00
parent 724ddf0b41
commit 0f442f4c85

View File

@@ -269,7 +269,7 @@ class NWCServer(Logger, EventListener):
query = { query = {
"authors": list(self.connections.keys()), # the pubkeys of the client connections "authors": list(self.connections.keys()), # the pubkeys of the client connections
"kinds": [self.REQUEST_EVENT_KIND], "kinds": [self.REQUEST_EVENT_KIND],
"limit": 0, "limit": 0, # requests only new events after creating this subscription
"since": int(time.time()) "since": int(time.time())
} }
async for event in self.manager.get_events(query, single_event=False, only_stored=False): async for event in self.manager.get_events(query, single_event=False, only_stored=False):
@@ -290,8 +290,16 @@ class NWCServer(Logger, EventListener):
await self.send_error(event, "NOT_IMPLEMENTED") await self.send_error(event, "NOT_IMPLEMENTED")
continue continue
if event.created_at < int(time.time()) - 15: # if the request has an explicitly set expiration tag, ignore it if it is expired
# otherwise ignore requests older than 30 sec to not handle requests the user may
# already expect to have timed out
if event.expires_at() is not None:
if event.is_expired():
self.logger.debug(f"expired nwc request event: {event.content}")
continue
elif event.created_at < int(time.time()) - 30:
self.logger.debug(f"old nwc request event: {event.content}") self.logger.debug(f"old nwc request event: {event.content}")
await self.send_error(event, "OTHER", f"not handling too old request")
continue continue
# decrypt the requests content # decrypt the requests content