Skip to content

Commit

Permalink
feat: add length assertion for pod list size (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorShadurin authored Oct 20, 2023
1 parent a1f4f5d commit 0735ded
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/pod/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
PodsList,
} from './types'
import { Bee, Data, Utils } from '@ethersphere/bee-js'
import { bytesToString, stringToBytes, wordArrayToBytes } from '../utils/bytes'
import { assertMaxLength, bytesToString, stringToBytes, wordArrayToBytes } from '../utils/bytes'
import { utils } from 'ethers'
import { getRawDirectoryMetadataBytes } from '../directory/adapter'
import {
Expand Down Expand Up @@ -42,6 +42,7 @@ import { DEFAULT_DIRECTORY_PERMISSIONS, getDirectoryMode } from '../directory/ut
import { getCacheKey, setEpochCache } from '../cache/utils'
import { getWalletByIndex } from '../utils/cache/wallet'
import { getPodsList } from './cache/api'
import { CHUNK_SIZE } from '../account/utils'

export const META_VERSION = 2
export const MAX_PODS_COUNT = 65536
Expand Down Expand Up @@ -421,6 +422,7 @@ export async function createPod(
}

const allPodsData = podListToBytes(pods, sharedPods)
assertMaxLength(allPodsData.length, CHUNK_SIZE, `Exceeded pod list size by ${allPodsData.length - CHUNK_SIZE} bytes`)
await writeFeedData(connection, POD_TOPIC, allPodsData, userWallet, preparePrivateKey(userWallet.privateKey), epoch)

if (isPod(realPod)) {
Expand Down
12 changes: 12 additions & 0 deletions src/utils/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,15 @@ export function bytesToWordArray(data: Uint8Array): CryptoJS.lib.WordArray {
export function wordArrayToBytes(data: CryptoJS.lib.WordArray): Uint8Array {
return Utils.hexToBytes(CryptoJS.enc.Hex.stringify(data))
}

/**
* Asserts that length is less than or equal to max length
* @param currentLength currentLength
* @param maxLength max length
* @param customMessage custom error message
*/
export function assertMaxLength(currentLength: number, maxLength: number, customMessage?: string): void {
if (currentLength > maxLength) {
throw new Error(customMessage ? customMessage : `length ${currentLength} exceeds max length ${maxLength}`)
}
}
18 changes: 18 additions & 0 deletions test/integration/node/pod/pod-limit-error-message.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createFdp, generateRandomHexString, generateUser } from '../../../utils'
import { MAX_POD_NAME_LENGTH } from '../../../../src/pod/utils'

jest.setTimeout(400000)
it('Pod list size error message', async () => {
const fdp = createFdp()
generateUser(fdp)

for (let i = 0; i < 25; i++) {
const longPodName = generateRandomHexString(MAX_POD_NAME_LENGTH)

if (i === 24) {
await expect(fdp.personalStorage.create(longPodName)).rejects.toThrow('Exceeded pod list size by 46 bytes')
} else {
await fdp.personalStorage.create(longPodName)
}
}
})

0 comments on commit 0735ded

Please sign in to comment.