Skip to content

addPieces fails with "value.length" error when metadata entry value is undefined #606

@ishtails

Description

@ishtails

Description

Problem

During addPieces, the SDK can throw:

undefined is not an object (evaluating 'value.length')

Wrapped as:

StorageContext addPieces failed: Failed to add piece to data set - undefined is not an object (evaluating 'value.length')

Cause

In packages/synapse-core/src/utils/metadata.ts, both pieceMetadataObjectToEntry and datasetMetadataObjectToEntry do:

for (const entry of entries) {
  if (entry.key.length > METADATA_LIMITS.MAX_KEY_LENGTH) { ... }
  if (entry.value.length > METADATA_LIMITS.MAX_VALUE_LENGTH) {  // crashes if entry.value is undefined

MetadataObject is Record<string, string>, but that is not enforced at runtime. If any metadata entry has undefined as a value (e.g. from { key: undefined } or metadataInternal.ipfsRootCID being undefined), entry.value.length throws.

Impact

  • Unhandled promise rejection can crash the server
  • Upload to the storage provider succeeds, but the piece is not added to the data set
  • File remains in S3; users can still view/sign, but pieces are not registered on-chain

Suggested fix

Validate entry.value before using it:

Either add null/undefined check like this:

for (const entry of entries) {
  if (entry.key.length > METADATA_LIMITS.MAX_KEY_LENGTH) {
    throw new Error('Metadata key exceeds the maximum length')
  }
  if (entry.value == null || String(entry.value).length > METADATA_LIMITS.MAX_VALUE_LENGTH) {
    throw new Error('Metadata value exceeds the maximum length')
  }
}

Or filter invalid entries:

const entries = Object.entries(obj)
  .filter(([, value]) => value != null && typeof value === 'string')
  .sort((a, b) => a[0].localeCompare(b[0]))
  .map(([key, value]) => ({ key, value }))

Will have to apply the same logic in both pieceMetadataObjectToEntry and datasetMetadataObjectToEntry.


Environment


Reproduction

  1. Create a storage context with metadata: { filosign_user: walletAddress }
  2. Upload a piece with metadata: {}
  3. After the piece is uploaded to the SP, addPieces fails when processing the pending pieces batch
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    📌 Triage

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions