Skip to content

Commit

Permalink
fix: limit valid message size (#226)
Browse files Browse the repository at this point in the history
Add a hard limit of 10kb for a message to be considered valid
  • Loading branch information
achingbrain authored Jun 14, 2023
1 parent 3b587c2 commit 8a3e4f4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export const ERR_UNDEFINED_PARAMETER = 'ERR_UNDEFINED_PARAMETER'
export const ERR_INVALID_RECORD_DATA = 'ERR_INVALID_RECORD_DATA'
export const ERR_INVALID_EMBEDDED_KEY = 'ERR_INVALID_EMBEDDED_KEY'
export const ERR_MISSING_PRIVATE_KEY = 'ERR_MISSING_PRIVATE_KEY'
export const ERR_RECORD_TOO_LARGE = 'ERR_RECORD_TOO_LARGE'
9 changes: 9 additions & 0 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import type { PublicKey } from '@libp2p/interface-keys'

const log = logger('ipns:validator')

/**
* Limit valid IPNS record sizes to 10kb
*/
const MAX_RECORD_SIZE = 1024 * 10

/**
* Validates the given ipns entry against the given public key
*/
Expand Down Expand Up @@ -94,6 +99,10 @@ const validateCborDataMatchesPbData = (entry: IPNSEntry): void => {
}

export const ipnsValidator: ValidateFn = async (key, marshalledData) => {
if (marshalledData.byteLength > MAX_RECORD_SIZE) {
throw errCode(new Error('record too large'), ERRORS.ERR_RECORD_TOO_LARGE)
}

const peerId = peerIdFromRoutingKey(key)
const receivedEntry = unmarshal(marshalledData)

Expand Down
7 changes: 7 additions & 0 deletions test/validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,11 @@ describe('validator', function () {

await expect(ipnsValidator(key, marshalledData)).to.eventually.be.rejected().with.property('code', ERRORS.ERR_INVALID_EMBEDDED_KEY)
})

it('should limit the size of incoming records', async () => {
const marshalledData = new Uint8Array(1024 * 1024)
const key = new Uint8Array()

await expect(ipnsValidator(key, marshalledData)).to.eventually.be.rejected().with.property('code', ERRORS.ERR_RECORD_TOO_LARGE)
})
})

0 comments on commit 8a3e4f4

Please sign in to comment.