{"record":{"id":"0de1313f8358cc02","repo":"sipeed/picoclaw","slug":"invalid-padding-size-d","errorCode":null,"errorMessage":"invalid padding size %d","messagePattern":"invalid padding size (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/channels/wecom/media.go","lineNumber":127,"sourceCode":"\t\treturn nil, fmt.Errorf(\"ciphertext length %d is not a multiple of block size\", len(ciphertext))\n\t}\n\tblock, err := aes.NewCipher(key)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"create cipher: %w\", err)\n\t}\n\tplaintext := make([]byte, len(ciphertext))\n\tiv := key[:aes.BlockSize]\n\tcipher.NewCBCDecrypter(block, iv).CryptBlocks(plaintext, ciphertext)\n\treturn pkcs7Unpad(plaintext)\n}\n\nfunc pkcs7Unpad(data []byte) ([]byte, error) {\n\tif len(data) == 0 {\n\t\treturn nil, fmt.Errorf(\"empty plaintext\")\n\t}\n\tpadding := int(data[len(data)-1])\n\tif padding == 0 || padding > 32 || padding > len(data) {\n\t\treturn nil, fmt.Errorf(\"invalid padding size %d\", padding)\n\t}\n\tfor i := 0; i < padding; i++ {\n\t\tif data[len(data)-1-i] != byte(padding) {\n\t\t\treturn nil, fmt.Errorf(\"invalid padding byte\")\n\t\t}\n\t}\n\treturn data[:len(data)-padding], nil\n}\n\nfunc inferMediaExt(contentType, fallback string) string {\n\tcontentType = normalizeWeComContentType(contentType)\n\tswitch contentType {\n\tcase \"image/jpeg\", \"image/jpg\":\n\t\treturn \".jpg\"\n\tcase \"image/png\":\n\t\treturn \".png\"\n\tcase \"image/gif\":\n\t\treturn \".gif\"","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/sipeed/picoclaw/blob/49183d7e8daed0dba89ddbb6fcb60089401d9680/pkg/channels/wecom/media.go#L109-L145","documentation":"Guard in pkcs7Unpad: the decrypted plaintext's final byte declares a padding length that is 0, exceeds 32, or exceeds the data length — not a legal PKCS7 padding value. This almost always means the AES decryption used the wrong key/IV (garbage plaintext), since correct decryption yields valid padding.","triggerScenarios":"Thrown at pkg/channels/wecom/media.go:127 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Verify the AES key matches the one used to encrypt the media (regenerate from WeCom admin)","Confirm the IV derivation matches WeCom's convention (here IV is the first block of the key)","Check the ciphertext was not truncated or reordered before decryption"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"49183d7e8daed0dba89ddbb6fcb60089401d9680","analyzedAt":"2026-08-15T21:55:41.315Z","contentChangedAt":"2026-08-15T21:55:41.315Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}