Skip to content

Commit

Permalink
fix(core): resolve bug in broadcasting encrypted messages (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyTseng authored Feb 3, 2024
1 parent 07ede14 commit 4f721f0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/core/__test__/services/event.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('eventService', () => {
subscriptionService = new SubscriptionService(
new Map(),
new ConsoleLoggerService(),
true,
);
subscriptionService.broadcast = jest.fn();
pluginManagerService = new PluginManagerService();
Expand Down
39 changes: 39 additions & 0 deletions packages/core/__test__/services/subscription.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ClientReadyState,
ConsoleLoggerService,
Event,
EventKind,
EventUtils,
Filter,
MessageType,
Expand All @@ -21,6 +22,7 @@ describe('SubscriptionService', () => {
subscriptionService = new SubscriptionService(
clientsMap,
new ConsoleLoggerService(),
true,
);
client = {
readyState: ClientReadyState.OPEN,
Expand Down Expand Up @@ -128,6 +130,43 @@ describe('SubscriptionService', () => {
expect(client.send).not.toHaveBeenCalled();
});

it('should not broadcast event to client if client is unauthorized', async () => {
const subscriptionId = 'subscriptionId';
const filters = [{}] as Filter[];
const event = {
id: 'id',
kind: EventKind.ENCRYPTED_DIRECT_MESSAGE,
} as Event;

jest.spyOn(EventUtils, 'isMatchingFilter').mockReturnValue(true);
jest.spyOn(EventUtils, 'checkPermission').mockReturnValue(false);

subscriptionService.subscribe(ctx, subscriptionId, filters);
await subscriptionService.broadcast(event);

expect(client.send).not.toHaveBeenCalled();
});

it('should broadcast event to client if nip-42 is disabled and client is unauthorized', async () => {
(subscriptionService as any).isNip42Enabled = false;
const subscriptionId = 'subscriptionId';
const filters = [{}] as Filter[];
const event = {
id: 'id',
kind: EventKind.ENCRYPTED_DIRECT_MESSAGE,
} as Event;

jest.spyOn(EventUtils, 'isMatchingFilter').mockReturnValue(true);
jest.spyOn(EventUtils, 'checkPermission').mockReturnValue(false);

subscriptionService.subscribe(ctx, subscriptionId, filters);
await subscriptionService.broadcast(event);

expect(client.send).toHaveBeenCalledWith(
JSON.stringify([MessageType.EVENT, subscriptionId, event]),
);
});

it('should catch error', async () => {
const subscriptionId = 'subscriptionId';
const filters = [{}] as Filter[];
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/nostr-relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class NostrRelay {
this.subscriptionService = new SubscriptionService(
this.clientContexts,
logger,
!!options.domain,
);
this.eventService = new EventService(
eventRepository,
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/services/subscription.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class SubscriptionService {
constructor(
private readonly clientsMap: Map<Client, ClientContext>,
private readonly logger: Logger,
private readonly isNip42Enabled: boolean,
) {}

subscribe(
Expand All @@ -37,11 +38,14 @@ export class SubscriptionService {

ctx.subscriptions.forEach((filters, subscriptionId) => {
if (
!filters.some(filter => EventUtils.isMatchingFilter(event, filter))
filters.some(filter =>
EventUtils.isMatchingFilter(event, filter),
) &&
(!this.isNip42Enabled ||
EventUtils.checkPermission(event, ctx.pubkey))
) {
return;
ctx.sendMessage(createOutgoingEventMessage(subscriptionId, event));
}
ctx.sendMessage(createOutgoingEventMessage(subscriptionId, event));
});
}
} catch (error) {
Expand Down

0 comments on commit 4f721f0

Please sign in to comment.