-
Notifications
You must be signed in to change notification settings - Fork 225
fix: OpenNode callback accepts unauthenticated requests #454
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
Open
Anshumancanrock
wants to merge
6
commits into
cameri:main
Choose a base branch
from
Anshumancanrock:fix/opennode-webhook-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+398
−15
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
341ddea
fix: parse form-encoded body on OpenNode callback route
Anshumancanrock 6473ae5
fix: validate OpenNode webhook signature before processing
Anshumancanrock 5cc207c
test: add unit tests for OpenNode callback controller and route
Anshumancanrock 42ae225
fix: align amountPaid and test missing OPENNODE_API_KEY
Anshumancanrock e99c055
fix: reject malformed OpenNode hashed_order
Anshumancanrock 949b78a
fix: avoid logging sensitive OpenNode callback fields
Anshumancanrock 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
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
218 changes: 218 additions & 0 deletions
218
test/unit/controllers/callbacks/opennode-callback-controller.spec.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,218 @@ | ||
| import chai, { expect } from 'chai' | ||
| import Sinon from 'sinon' | ||
| import sinonChai from 'sinon-chai' | ||
|
|
||
| import * as httpUtils from '../../../../src/utils/http' | ||
| import * as settingsFactory from '../../../../src/factories/settings-factory' | ||
|
|
||
| import { hmacSha256 } from '../../../../src/utils/secret' | ||
| import { InvoiceStatus } from '../../../../src/@types/invoice' | ||
| import { OpenNodeCallbackController } from '../../../../src/controllers/callbacks/opennode-callback-controller' | ||
|
|
||
| chai.use(sinonChai) | ||
|
|
||
| describe('OpenNodeCallbackController', () => { | ||
| let createSettingsStub: Sinon.SinonStub | ||
| let getRemoteAddressStub: Sinon.SinonStub | ||
| let updateInvoiceStatusStub: Sinon.SinonStub | ||
| let confirmInvoiceStub: Sinon.SinonStub | ||
| let sendInvoiceUpdateNotificationStub: Sinon.SinonStub | ||
| let statusStub: Sinon.SinonStub | ||
| let setHeaderStub: Sinon.SinonStub | ||
| let sendStub: Sinon.SinonStub | ||
| let controller: OpenNodeCallbackController | ||
| let request: any | ||
| let response: any | ||
| let previousOpenNodeApiKey: string | undefined | ||
|
|
||
| beforeEach(() => { | ||
| previousOpenNodeApiKey = process.env.OPENNODE_API_KEY | ||
| process.env.OPENNODE_API_KEY = 'test-api-key' | ||
|
|
||
| createSettingsStub = Sinon.stub(settingsFactory, 'createSettings').returns({ | ||
| payments: { processor: 'opennode' }, | ||
| } as any) | ||
| getRemoteAddressStub = Sinon.stub(httpUtils, 'getRemoteAddress').returns('127.0.0.1') | ||
|
|
||
| updateInvoiceStatusStub = Sinon.stub() | ||
| confirmInvoiceStub = Sinon.stub() | ||
| sendInvoiceUpdateNotificationStub = Sinon.stub() | ||
|
|
||
| controller = new OpenNodeCallbackController({ | ||
| updateInvoiceStatus: updateInvoiceStatusStub, | ||
| confirmInvoice: confirmInvoiceStub, | ||
| sendInvoiceUpdateNotification: sendInvoiceUpdateNotificationStub, | ||
| } as any) | ||
|
|
||
| statusStub = Sinon.stub() | ||
| setHeaderStub = Sinon.stub() | ||
| sendStub = Sinon.stub() | ||
|
|
||
| response = { | ||
| send: sendStub, | ||
| setHeader: setHeaderStub, | ||
| status: statusStub, | ||
| } | ||
|
|
||
| statusStub.returns(response) | ||
| setHeaderStub.returns(response) | ||
| sendStub.returns(response) | ||
|
|
||
| request = { | ||
| body: {}, | ||
| headers: {}, | ||
| } | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| getRemoteAddressStub.restore() | ||
| createSettingsStub.restore() | ||
|
|
||
| if (typeof previousOpenNodeApiKey === 'undefined') { | ||
| delete process.env.OPENNODE_API_KEY | ||
| } else { | ||
| process.env.OPENNODE_API_KEY = previousOpenNodeApiKey | ||
| } | ||
| }) | ||
|
|
||
| it('rejects requests when OpenNode is not the configured payment processor', async () => { | ||
| createSettingsStub.returns({ | ||
| payments: { processor: 'lnbits' }, | ||
| } as any) | ||
|
|
||
| await controller.handleRequest(request, response) | ||
|
|
||
| expect(statusStub).to.have.been.calledOnceWithExactly(403) | ||
| expect(sendStub).to.have.been.calledOnceWithExactly('Forbidden') | ||
| expect(updateInvoiceStatusStub).not.to.have.been.called | ||
| }) | ||
|
|
||
| it('returns bad request for malformed callback bodies', async () => { | ||
| request.body = { | ||
| id: 'invoice-id', | ||
| } | ||
|
|
||
| await controller.handleRequest(request, response) | ||
|
|
||
| expect(statusStub).to.have.been.calledOnceWithExactly(400) | ||
| expect(setHeaderStub).to.have.been.calledOnceWithExactly('content-type', 'text/plain; charset=utf8') | ||
| expect(sendStub).to.have.been.calledOnceWithExactly('Bad Request') | ||
| expect(updateInvoiceStatusStub).not.to.have.been.called | ||
| }) | ||
|
|
||
| it('returns bad request for unknown status values', async () => { | ||
| request.body = { | ||
| hashed_order: 'some-hash', | ||
| id: 'invoice-id', | ||
| status: 'totally_made_up', | ||
| } | ||
|
|
||
| await controller.handleRequest(request, response) | ||
|
|
||
| expect(statusStub).to.have.been.calledOnceWithExactly(400) | ||
| expect(sendStub).to.have.been.calledOnceWithExactly('Bad Request') | ||
| expect(updateInvoiceStatusStub).not.to.have.been.called | ||
| }) | ||
|
|
||
| it('returns internal server error when OPENNODE_API_KEY is missing', async () => { | ||
| delete process.env.OPENNODE_API_KEY | ||
| request.body = { | ||
| hashed_order: 'some-hash', | ||
| id: 'invoice-id', | ||
| status: 'paid', | ||
| } | ||
|
|
||
| await controller.handleRequest(request, response) | ||
|
|
||
| expect(statusStub).to.have.been.calledOnceWithExactly(500) | ||
| expect(setHeaderStub).to.have.been.calledOnceWithExactly('content-type', 'text/plain; charset=utf8') | ||
| expect(sendStub).to.have.been.calledOnceWithExactly('Internal Server Error') | ||
| expect(updateInvoiceStatusStub).not.to.have.been.called | ||
| }) | ||
|
|
||
| it('returns bad request for malformed hashed_order', async () => { | ||
| request.body = { | ||
| hashed_order: 'invalid', | ||
| id: 'invoice-id', | ||
| status: 'paid', | ||
| } | ||
|
|
||
| await controller.handleRequest(request, response) | ||
|
|
||
| expect(statusStub).to.have.been.calledOnceWithExactly(400) | ||
| expect(setHeaderStub).to.have.been.calledOnceWithExactly('content-type', 'text/plain; charset=utf8') | ||
| expect(sendStub).to.have.been.calledOnceWithExactly('Bad Request') | ||
| expect(updateInvoiceStatusStub).not.to.have.been.called | ||
| }) | ||
|
|
||
| it('rejects callbacks with mismatched hashed_order', async () => { | ||
| request.body = { | ||
| hashed_order: '0'.repeat(64), | ||
| id: 'invoice-id', | ||
| status: 'paid', | ||
| } | ||
|
|
||
| await controller.handleRequest(request, response) | ||
|
|
||
| expect(statusStub).to.have.been.calledOnceWithExactly(403) | ||
| expect(sendStub).to.have.been.calledOnceWithExactly('Forbidden') | ||
| expect(updateInvoiceStatusStub).not.to.have.been.called | ||
| }) | ||
|
|
||
| it('accepts valid signed callbacks and processes the invoice update', async () => { | ||
| request.body = { | ||
| amount: 21, | ||
| created_at: '2026-04-11T00:00:00.000Z', | ||
| description: 'Admission fee', | ||
| hashed_order: hmacSha256('test-api-key', 'invoice-id').toString('hex'), | ||
| id: 'invoice-id', | ||
| lightning: { | ||
| expires_at: '2026-04-11T01:00:00.000Z', | ||
| payreq: 'lnbc1test', | ||
| }, | ||
| order_id: 'pubkey', | ||
| status: 'unpaid', | ||
| } | ||
|
|
||
Anshumancanrock marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| updateInvoiceStatusStub.resolves({ | ||
| confirmedAt: null, | ||
| status: InvoiceStatus.PENDING, | ||
| }) | ||
|
|
||
| await controller.handleRequest(request, response) | ||
|
|
||
| expect(updateInvoiceStatusStub).to.have.been.calledOnce | ||
| expect(confirmInvoiceStub).not.to.have.been.called | ||
| expect(sendInvoiceUpdateNotificationStub).not.to.have.been.called | ||
| expect(statusStub).to.have.been.calledOnceWithExactly(200) | ||
| expect(sendStub).to.have.been.calledOnceWithExactly() | ||
| }) | ||
|
|
||
| it('confirms and notifies on paid callbacks, setting confirmedAt when absent', async () => { | ||
| request.body = { | ||
| hashed_order: hmacSha256('test-api-key', 'invoice-id').toString('hex'), | ||
| id: 'invoice-id', | ||
| status: 'paid', | ||
| } | ||
|
|
||
| updateInvoiceStatusStub.resolves({ | ||
| amountRequested: 1000n, | ||
| confirmedAt: null, | ||
| id: 'invoice-id', | ||
| pubkey: 'somepubkey', | ||
| status: InvoiceStatus.COMPLETED, | ||
| }) | ||
| confirmInvoiceStub.resolves() | ||
| sendInvoiceUpdateNotificationStub.resolves() | ||
|
|
||
| await controller.handleRequest(request, response) | ||
|
|
||
| expect(updateInvoiceStatusStub).to.have.been.calledOnce | ||
| expect(confirmInvoiceStub).to.have.been.calledOnce | ||
| const confirmedAtArg = confirmInvoiceStub.firstCall.args[0].confirmedAt | ||
| expect(confirmedAtArg).to.be.instanceOf(Date) | ||
| expect(sendInvoiceUpdateNotificationStub).to.have.been.calledOnce | ||
| expect(statusStub).to.have.been.calledOnceWithExactly(200) | ||
| expect(sendStub).to.have.been.calledOnceWithExactly('OK') | ||
| }) | ||
| }) | ||
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.