{"record":{"id":"d712f8d0b66cf5fb","repo":"siyuan-note/siyuan","slug":"encrypted-asset-metadata-is-too-large","errorCode":null,"errorMessage":"encrypted asset metadata is too large","messagePattern":"encrypted asset metadata is too large","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/crypto.go","lineNumber":2268,"sourceCode":"\tmetadata, err := json.Marshal(&encryptedAssetMetadata{\n\t\tSpec:         encryptedAssetSpec,\n\t\tContainerID:  containerID,\n\t\tOriginalName: originalName,\n\t\tSize:         int64(len(plaintext)),\n\t\tChunks:       chunkCount,\n\t})\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tassetKey := util.DeriveSubKey(dek, \"siyuan/asset\")\n\tdefer zeroAndClear(assetKey)\n\taadPrefix := \"siyuan:asset:\" + boxID + \":assets/\" + diskName\n\tencryptedMetadata, err := util.EncryptWithAAD(assetKey, metadata, []byte(aadPrefix+\":metadata\"))\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tif len(encryptedMetadata) > encryptedAssetMetadataMaxSize {\n\t\treturn nil, errors.New(\"encrypted asset metadata is too large\")\n\t}\n\tret := bytes.NewBuffer(make([]byte, 0, len(plaintext)+len(encryptedMetadata)+int(chunkCount)*64+12))\n\tret.Write(encryptedAssetMagic)\n\tif err = binary.Write(ret, binary.BigEndian, uint32(len(encryptedMetadata))); err != nil {\n\t\treturn nil, err\n\t}\n\tret.Write(encryptedMetadata)\n\tfor chunkIndex := uint64(0); chunkIndex < chunkCount; chunkIndex++ {\n\t\tstart := int(chunkIndex) * encryptedAssetChunkSize\n\t\tend := start + encryptedAssetChunkSize\n\t\tif end > len(plaintext) {\n\t\t\tend = len(plaintext)\n\t\t}\n\t\tencryptedChunk, encryptErr := util.EncryptWithAAD(\n\t\t\tassetKey,\n\t\t\tplaintext[start:end],\n\t\t\tencryptedAssetChunkAAD(aadPrefix, containerID, chunkIndex),\n\t\t)","sourceCodeStart":2250,"sourceCodeEnd":2286,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/crypto.go#L2250-L2286","documentation":"EncryptAsset builds an encrypted asset container whose metadata (original name, size, chunk count, container ID) is encrypted as one AAD-bound blob and length-prefixed with a uint32. The kernel throws this error when the encrypted metadata exceeds encryptedAssetMetadataMaxSize (1 MiB), which would make the length prefix and parsing invariants unsatisfiable.","triggerScenarios":"Calling model.EncryptAsset with a metadata payload whose ciphertext is larger than 1 MiB — practically only via abnormally huge original-name or metadata fields, since the struct is small; tampered/oversized metadata input to the encryption helper.","commonSituations":"Uploading an asset whose recorded metadata was extended or crafted to exceed the limit; future format extensions inflating metadata; pathological file names from a modified client.","solutions":["Reduce the metadata content (shorter original name, no extra fields) below the 1 MiB encrypted-size cap","Check len(plaintext metadata) before calling EncryptAsset and reject inputs that could exceed encryptedAssetMetadataMaxSize after encryption overhead (~16-byte tag + nonce)","If larger metadata is genuinely needed, bump the format limit in a versioned container-format change, not ad hoc"],"exampleFix":"// before\nenc, err := model.EncryptAsset(boxID, diskName, originalName, data)\n// after\nif len(originalName) > 255 { return errors.New(\"original name too long\") }\nenc, err := model.EncryptAsset(boxID, diskName, originalName, data)","handlingStrategy":"validation","validationCode":"if len(metadataJSON) > 1<<20-64 {\n    return errors.New(\"asset metadata would exceed the 1 MiB encrypted limit\")\n}\nenc, err := model.EncryptAsset(boxID, diskName, name, data)","typeGuard":null,"tryCatchPattern":"enc, err := model.EncryptAsset(boxID, diskName, name, data)\nif err != nil && strings.Contains(err.Error(), \"metadata is too large\") {\n    return shrinkMetadataAndRetry(name, data)\n}","preventionTips":["Keep asset original names short (e.g. cap at 255 bytes) before encryption","Never append custom fields to the encrypted metadata struct without checking the size budget","Remember ciphertext adds nonce+tag overhead (~28+ bytes) to plaintext size"],"tags":["encryption","payload-too-large","go"],"backgroundTag":"payload-too-large","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}