mirror of
https://github.com/Cameri/nostream.git
synced 2025-03-18 05:41:49 +01:00
feat: reject events with long content field
Signed-off-by: Ricardo Arturo Cabral Mejía <me@ricardocabral.io>
This commit is contained in:
parent
2089c9fd61
commit
9364412677
@ -63,6 +63,10 @@ export class EventMessageHandler implements IMessageHandler {
|
||||
protected canAcceptEvent(event: Event): string | undefined {
|
||||
const now = Math.floor(Date.now()/1000)
|
||||
const limits = this.settings().limits.event
|
||||
if (limits.content?.maxLength > 0 && event.content.length > limits.content.maxLength) {
|
||||
return `rejected: content is longer than ${limits.content.maxLength} bytes`
|
||||
}
|
||||
|
||||
if (limits.createdAt.maxPositiveDelta > 0 && event.created_at > now + limits.createdAt.maxPositiveDelta) {
|
||||
return `rejected: created_at is more than ${limits.createdAt.maxPositiveDelta} seconds in the future`
|
||||
}
|
||||
|
@ -177,6 +177,9 @@ describe('EventMessageHandler', () => {
|
||||
blacklist: [],
|
||||
whitelist: [],
|
||||
},
|
||||
content: {
|
||||
maxLength: 0,
|
||||
},
|
||||
}
|
||||
settings = {
|
||||
limits: {
|
||||
@ -235,6 +238,55 @@ describe('EventMessageHandler', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('content', () => {
|
||||
describe('maxLength', () => {
|
||||
it('returns undefined if maxLength is zero', () => {
|
||||
eventLimits.content.maxLength = 0
|
||||
|
||||
expect(
|
||||
(handler as any).canAcceptEvent(event)
|
||||
).to.be.undefined
|
||||
})
|
||||
|
||||
it('returns undefned if content is not too long', () => {
|
||||
eventLimits.content.maxLength = 100
|
||||
event.content = 'x'.repeat(100)
|
||||
|
||||
expect(
|
||||
(handler as any).canAcceptEvent(event)
|
||||
).to.be.undefined
|
||||
})
|
||||
|
||||
it('returns reason if content is too long', () => {
|
||||
eventLimits.content.maxLength = 100
|
||||
event.content = 'x'.repeat(101)
|
||||
|
||||
expect(
|
||||
(handler as any).canAcceptEvent(event)
|
||||
).to.equal('rejected: content is longer than 100 bytes')
|
||||
})
|
||||
})
|
||||
|
||||
describe('maxNegativeDelta', () => {
|
||||
it('returns undefined if maxNegativeDelta is zero', () => {
|
||||
eventLimits.createdAt.maxNegativeDelta = 0
|
||||
|
||||
expect(
|
||||
(handler as any).canAcceptEvent(event)
|
||||
).to.be.undefined
|
||||
})
|
||||
|
||||
it('returns reason if createdDate is too far in the past', () => {
|
||||
eventLimits.createdAt.maxNegativeDelta = 100
|
||||
event.created_at -= 101
|
||||
|
||||
expect(
|
||||
(handler as any).canAcceptEvent(event)
|
||||
).to.equal('rejected: created_at is more than 100 seconds in the past')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('eventId', () => {
|
||||
describe('minLeadingZeroBits', () => {
|
||||
it('returns undefined if minLeadingZeroBits is zero', () => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user