Skip to content
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

fix: limit valid message size #226

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})