Skip to content

Commit 3ddbff2

Browse files
committed
refactor: use exact pubkey matching for whitelist/blacklist
1 parent 3543d6c commit 3ddbff2

6 files changed

Lines changed: 30 additions & 19 deletions

File tree

src/app/static-mirroring-worker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export class StaticMirroringWorker implements IRunnable {
239239
if (
240240
typeof limits.pubkey?.whitelist !== 'undefined' &&
241241
limits.pubkey.whitelist.length > 0 &&
242-
!limits.pubkey.whitelist.some((prefix) => event.pubkey.startsWith(prefix))
242+
!limits.pubkey.whitelist.includes(event.pubkey)
243243
) {
244244
debug(`event ${event.id} not accepted: pubkey not allowed: ${event.pubkey}`)
245245
return false
@@ -248,7 +248,7 @@ export class StaticMirroringWorker implements IRunnable {
248248
if (
249249
typeof limits.pubkey?.blacklist !== 'undefined' &&
250250
limits.pubkey.blacklist.length > 0 &&
251-
limits.pubkey.blacklist.some((prefix) => event.pubkey.startsWith(prefix))
251+
limits.pubkey.blacklist.includes(event.pubkey)
252252
) {
253253
debug(`event ${event.id} not accepted: pubkey not allowed: ${event.pubkey}`)
254254
return false
@@ -288,7 +288,7 @@ export class StaticMirroringWorker implements IRunnable {
288288

289289
const isApplicableFee = (feeSchedule: FeeSchedule) =>
290290
feeSchedule.enabled &&
291-
!feeSchedule.whitelists?.pubkeys?.some((prefix) => event.pubkey.startsWith(prefix)) &&
291+
!feeSchedule.whitelists?.pubkeys?.includes(event.pubkey) &&
292292
!feeSchedule.whitelists?.event_kinds?.some(isEventKindOrRangeMatch(event))
293293

294294
const feeSchedules = currentSettings.payments?.feeSchedules?.admission?.filter(isApplicableFee)

src/controllers/invoices/post-invoice-controller.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class PostInvoiceController implements IController {
8888
}
8989

9090
const isApplicableFee = (feeSchedule: FeeSchedule) =>
91-
feeSchedule.enabled && !feeSchedule.whitelists?.pubkeys?.some((prefix) => pubkey.startsWith(prefix))
91+
feeSchedule.enabled && !feeSchedule.whitelists?.pubkeys?.includes(pubkey)
9292
const admissionFee = currentSettings.payments?.feeSchedules.admission.filter(isApplicableFee)
9393

9494
if (!Array.isArray(admissionFee) || !admissionFee.length) {
@@ -106,9 +106,7 @@ export class PostInvoiceController implements IController {
106106
}
107107

108108
let invoice: Invoice
109-
const amount = admissionFee.reduce((sum, fee) => {
110-
return fee.enabled && !fee.whitelists?.pubkeys?.includes(pubkey) ? BigInt(fee.amount) + sum : sum
111-
}, 0n)
109+
const amount = admissionFee.reduce((sum, fee) => BigInt(fee.amount) + sum, 0n)
112110

113111
try {
114112
const description = `${relayName} Admission Fee for ${toBech32('npub')(pubkey)}`

src/handlers/event-message-handler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,15 @@ export class EventMessageHandler implements IMessageHandler {
195195
if (
196196
typeof limits.pubkey?.whitelist !== 'undefined' &&
197197
limits.pubkey.whitelist.length > 0 &&
198-
!limits.pubkey.whitelist.some((prefix) => event.pubkey.startsWith(prefix))
198+
!limits.pubkey.whitelist.includes(event.pubkey)
199199
) {
200200
return 'blocked: pubkey not allowed'
201201
}
202202

203203
if (
204204
typeof limits.pubkey?.blacklist !== 'undefined' &&
205205
limits.pubkey.blacklist.length > 0 &&
206-
limits.pubkey.blacklist.some((prefix) => event.pubkey.startsWith(prefix))
206+
limits.pubkey.blacklist.includes(event.pubkey)
207207
) {
208208
return 'blocked: pubkey not allowed'
209209
}
@@ -332,7 +332,7 @@ export class EventMessageHandler implements IMessageHandler {
332332

333333
const isApplicableFee = (feeSchedule: FeeSchedule) =>
334334
feeSchedule.enabled &&
335-
!feeSchedule.whitelists?.pubkeys?.some((prefix) => event.pubkey.startsWith(prefix)) &&
335+
!feeSchedule.whitelists?.pubkeys?.includes(event.pubkey) &&
336336
!feeSchedule.whitelists?.event_kinds?.some(isEventKindOrRangeMatch(event))
337337

338338
const feeSchedules = currentSettings.payments?.feeSchedules?.admission?.filter(isApplicableFee)

src/services/payments-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export class PaymentsService implements IPaymentsService {
164164
}
165165

166166
const isApplicableFee = (feeSchedule: FeeSchedule) =>
167-
feeSchedule.enabled && !feeSchedule.whitelists?.pubkeys?.some((prefix) => invoice.pubkey.startsWith(prefix))
167+
feeSchedule.enabled && !feeSchedule.whitelists?.pubkeys?.includes(invoice.pubkey)
168168
const admissionFeeSchedules = currentSettings.payments?.feeSchedules?.admission ?? []
169169
const admissionFeeAmount = admissionFeeSchedules.reduce((sum, feeSchedule) => {
170170
return sum + (isApplicableFee(feeSchedule) ? BigInt(feeSchedule.amount) : 0n)

test/unit/handlers/event-message-handler.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ describe('EventMessageHandler', () => {
478478
expect((handler as any).canAcceptEvent(event)).to.be.undefined
479479
})
480480

481-
it('returns undefined if pubkey is not blacklisted by prefix', () => {
481+
it('returns undefined if pubkey is not blacklisted if list entry is not exact match', () => {
482482
eventLimits.pubkey.blacklist = ['aa55']
483483
event.pubkey = 'aabbcc'
484484
expect((handler as any).canAcceptEvent(event)).to.be.undefined
@@ -490,10 +490,10 @@ describe('EventMessageHandler', () => {
490490
expect((handler as any).canAcceptEvent(event)).to.equal('blocked: pubkey not allowed')
491491
})
492492

493-
it('returns reason if pubkey is blacklisted by prefix', () => {
493+
it('returns undefined if pubkey extends a blacklist entry but is not an exact match', () => {
494494
eventLimits.pubkey.blacklist = ['aa55']
495495
event.pubkey = 'aa55ccddeeff'
496-
expect((handler as any).canAcceptEvent(event)).to.equal('blocked: pubkey not allowed')
496+
expect((handler as any).canAcceptEvent(event)).to.be.undefined
497497
})
498498
})
499499

@@ -509,10 +509,10 @@ describe('EventMessageHandler', () => {
509509
expect((handler as any).canAcceptEvent(event)).to.be.undefined
510510
})
511511

512-
it('returns undefined if pubkey is whitelisted by prefix', () => {
512+
it('returns reason if pubkey extends a whitelist entry but is not an exact match', () => {
513513
eventLimits.pubkey.whitelist = ['aa55']
514514
event.pubkey = 'aa55ccddeeff'
515-
expect((handler as any).canAcceptEvent(event)).to.be.undefined
515+
expect((handler as any).canAcceptEvent(event)).to.equal('blocked: pubkey not allowed')
516516
})
517517

518518
it('returns reason if pubkey is not whitelisted', () => {
@@ -521,7 +521,7 @@ describe('EventMessageHandler', () => {
521521
expect((handler as any).canAcceptEvent(event)).to.equal('blocked: pubkey not allowed')
522522
})
523523

524-
it('returns reason if pubkey is not whitelisted by prefix', () => {
524+
it('returns reason if pubkey is not whitelisted by exact match', () => {
525525
eventLimits.pubkey.whitelist = ['aa55']
526526
event.pubkey = 'aabbccddeeff'
527527
expect((handler as any).canAcceptEvent(event)).to.equal('blocked: pubkey not allowed')

test/unit/services/payments-service.spec.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,9 @@ describe('PaymentsService', () => {
395395
expect(userRepository.admitUser).not.to.have.been.called
396396
})
397397

398-
it('skips the fee for whitelisted pubkeys', async () => {
398+
it('skips the fee for whitelisted pubkeys(exact match)', async () => {
399399
settings.returns(makeSettings([
400-
{ enabled: true, amount: 1000n, whitelists: { pubkeys: ['whitelisted'] } },
400+
{ enabled: true, amount: 1000n, whitelists: { pubkeys: ['whitelistedpubkey'] } },
401401
]))
402402

403403
await service.confirmInvoice(makeCompletedInvoice({
@@ -410,6 +410,19 @@ describe('PaymentsService', () => {
410410
expect(userRepository.admitUser).not.to.have.been.called
411411
})
412412

413+
it('applies the fee when pubkey is not an exact whitelist match (prefix alone is insufficient)', async () => {
414+
settings.returns(makeSettings([
415+
{ enabled: true, amount: 1000n, whitelists: { pubkeys: ['whitelisted'] } },
416+
]))
417+
await service.confirmInvoice(makeCompletedInvoice({
418+
pubkey: 'whitelistedpubkey',
419+
unit: InvoiceUnit.MSATS,
420+
amountPaid: 5000n,
421+
}))
422+
expect(userRepository.admitUser).to.have.been.calledOnce
423+
})
424+
425+
413426
it('rolls back the transaction and re-throws on error', async () => {
414427
settings.returns(makeSettings([]))
415428
invoiceRepository.confirmInvoice.rejects(new Error('db error'))

0 commit comments

Comments
 (0)