Skip to content

Commit 8a3e4f4

Browse files
authored
fix: limit valid message size (#226)
Add a hard limit of 10kb for a message to be considered valid
1 parent 3b587c2 commit 8a3e4f4

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

src/errors.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export const ERR_UNDEFINED_PARAMETER = 'ERR_UNDEFINED_PARAMETER'
1010
export const ERR_INVALID_RECORD_DATA = 'ERR_INVALID_RECORD_DATA'
1111
export const ERR_INVALID_EMBEDDED_KEY = 'ERR_INVALID_EMBEDDED_KEY'
1212
export const ERR_MISSING_PRIVATE_KEY = 'ERR_MISSING_PRIVATE_KEY'
13+
export const ERR_RECORD_TOO_LARGE = 'ERR_RECORD_TOO_LARGE'

src/validator.ts

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import type { PublicKey } from '@libp2p/interface-keys'
1111

1212
const log = logger('ipns:validator')
1313

14+
/**
15+
* Limit valid IPNS record sizes to 10kb
16+
*/
17+
const MAX_RECORD_SIZE = 1024 * 10
18+
1419
/**
1520
* Validates the given ipns entry against the given public key
1621
*/
@@ -94,6 +99,10 @@ const validateCborDataMatchesPbData = (entry: IPNSEntry): void => {
9499
}
95100

96101
export const ipnsValidator: ValidateFn = async (key, marshalledData) => {
102+
if (marshalledData.byteLength > MAX_RECORD_SIZE) {
103+
throw errCode(new Error('record too large'), ERRORS.ERR_RECORD_TOO_LARGE)
104+
}
105+
97106
const peerId = peerIdFromRoutingKey(key)
98107
const receivedEntry = unmarshal(marshalledData)
99108

test/validator.spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,11 @@ describe('validator', function () {
8080

8181
await expect(ipnsValidator(key, marshalledData)).to.eventually.be.rejected().with.property('code', ERRORS.ERR_INVALID_EMBEDDED_KEY)
8282
})
83+
84+
it('should limit the size of incoming records', async () => {
85+
const marshalledData = new Uint8Array(1024 * 1024)
86+
const key = new Uint8Array()
87+
88+
await expect(ipnsValidator(key, marshalledData)).to.eventually.be.rejected().with.property('code', ERRORS.ERR_RECORD_TOO_LARGE)
89+
})
8390
})

0 commit comments

Comments
 (0)