-
Notifications
You must be signed in to change notification settings - Fork 16
fix: inner event signature verification (fixes #64) #69
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
Merged
ContextVM-org
merged 3 commits into
ContextVM:master
from
1amKhush:fix/inner-event-verification-64
May 4, 2026
Merged
Changes from all commits
Commits
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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@contextvm/sdk": patch | ||
| --- | ||
|
|
||
| fix(nostr): verify signatures for decrypted and unencrypted events and dedupe inner event ids |
This file contains hidden or 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 hidden or 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
159 changes: 159 additions & 0 deletions
159
src/transport/nostr-server-transport.inner-event-verification.test.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| import { describe, it, expect, mock } from 'bun:test'; | ||
| import type { RelayHandler } from '../core/interfaces.js'; | ||
| import type { NostrEvent } from 'nostr-tools'; | ||
| import { finalizeEvent, generateSecretKey, getPublicKey } from 'nostr-tools/pure'; | ||
| import { NostrServerTransport } from './nostr-server-transport.js'; | ||
| import { PrivateKeySigner } from '../signer/private-key-signer.js'; | ||
| import { EncryptionMode } from '../core/interfaces.js'; | ||
| import { GIFT_WRAP_KIND } from '../core/constants.js'; | ||
|
|
||
| function makeNoopRelayHandler(): RelayHandler { | ||
| return { | ||
| async connect() {}, | ||
| async disconnect() {}, | ||
| async publish() {}, | ||
| async subscribe() { | ||
| return () => {}; | ||
| }, | ||
| } as unknown as RelayHandler; | ||
| } | ||
|
|
||
| /** | ||
| * Helper: creates a cryptographically valid inner event using a real keypair. | ||
| */ | ||
| function createValidInnerEvent( | ||
| secretKey: Uint8Array, | ||
| content: string, | ||
| serverPubkey: string, | ||
| ): NostrEvent { | ||
| return finalizeEvent( | ||
| { | ||
| kind: 25910, | ||
| created_at: Math.floor(Date.now() / 1000), | ||
| tags: [['p', serverPubkey]], | ||
| content, | ||
| }, | ||
| secretKey, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Helper: creates a forged inner event with a valid id but garbage signature. | ||
| */ | ||
| function createForgedInnerEvent( | ||
| secretKey: Uint8Array, | ||
| content: string, | ||
| serverPubkey: string, | ||
| ): NostrEvent { | ||
| const valid = createValidInnerEvent(secretKey, content, serverPubkey); | ||
| return { | ||
| ...valid, | ||
| sig: '0'.repeat(128), | ||
| }; | ||
| } | ||
|
|
||
| describe.serial('Inner event signature verification (fixes #64)', () => { | ||
| it('rejects a decrypted inner event with an invalid signature', async () => { | ||
| const serverSk = generateSecretKey(); | ||
| const serverPubkey = getPublicKey(serverSk); | ||
|
|
||
| const whitelistedSk = generateSecretKey(); | ||
| const whitelistedPubkey = getPublicKey(whitelistedSk); | ||
|
|
||
| const transport = new NostrServerTransport({ | ||
| signer: new PrivateKeySigner(Buffer.from(serverSk).toString('hex')), | ||
| relayHandler: makeNoopRelayHandler(), | ||
| encryptionMode: EncryptionMode.REQUIRED, | ||
| allowedPublicKeys: [whitelistedPubkey], | ||
| }); | ||
|
|
||
| // Track onmessage calls — should never fire for a forged event. | ||
| const onmessageSpy = mock(() => {}); | ||
| transport.onmessage = onmessageSpy; | ||
|
|
||
| // Forge an inner event with a whitelisted pubkey but garbage signature. | ||
| const forgedInner = createForgedInnerEvent( | ||
| whitelistedSk, | ||
| JSON.stringify({ | ||
| jsonrpc: '2.0', | ||
| id: 1, | ||
| method: 'tools/list', | ||
| params: {}, | ||
| }), | ||
| serverPubkey, | ||
| ); | ||
|
|
||
| // Stub decryption to return the forged inner event. | ||
| const signer = transport['signer']; | ||
| signer.nip44 = { | ||
| encrypt: async () => { | ||
| throw new Error('encrypt not used'); | ||
| }, | ||
| decrypt: async () => JSON.stringify(forgedInner), | ||
| }; | ||
|
|
||
| const gw: NostrEvent = { | ||
| id: 'gw-forged', | ||
| kind: GIFT_WRAP_KIND, | ||
| pubkey: 'a'.repeat(64), | ||
| created_at: 1, | ||
| tags: [['p', serverPubkey]], | ||
| content: 'ciphertext', | ||
| sig: '0'.repeat(128), | ||
| }; | ||
|
|
||
| await transport['processIncomingEvent'](gw); | ||
|
|
||
| // The forged event must be rejected — onmessage should never be called. | ||
| expect(onmessageSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('accepts a decrypted inner event with a valid signature', async () => { | ||
| const serverSk = generateSecretKey(); | ||
| const serverPubkey = getPublicKey(serverSk); | ||
|
|
||
| const transport = new NostrServerTransport({ | ||
| signer: new PrivateKeySigner(Buffer.from(serverSk).toString('hex')), | ||
| relayHandler: makeNoopRelayHandler(), | ||
| encryptionMode: EncryptionMode.REQUIRED, | ||
| }); | ||
|
|
||
| // Create a legitimate inner event with a real key. | ||
| const clientSk = generateSecretKey(); | ||
| const validInner = createValidInnerEvent( | ||
| clientSk, | ||
| JSON.stringify({ | ||
| jsonrpc: '2.0', | ||
| id: 1, | ||
| method: 'tools/list', | ||
| params: {}, | ||
| }), | ||
| serverPubkey, | ||
| ); | ||
|
|
||
| // Stub decryption to return the valid inner event. | ||
| const signer = transport['signer']; | ||
| signer.nip44 = { | ||
| encrypt: async () => { | ||
| throw new Error('encrypt not used'); | ||
| }, | ||
| decrypt: async () => JSON.stringify(validInner), | ||
| }; | ||
|
|
||
| const gw: NostrEvent = { | ||
| id: 'gw-valid', | ||
| kind: GIFT_WRAP_KIND, | ||
| pubkey: 'a'.repeat(64), | ||
| created_at: 1, | ||
| tags: [['p', serverPubkey]], | ||
| content: 'ciphertext', | ||
| sig: '0'.repeat(128), | ||
| }; | ||
|
|
||
| await transport['processIncomingEvent'](gw); | ||
|
|
||
| // The valid event should be processed — check correlation store has the route. | ||
| const state = transport.getInternalStateForTesting(); | ||
| expect(state.correlationStore.eventRouteCount).toBe(1); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.