{"record":{"id":"769a5ff92d6b728b","repo":"CherryHQ/cherry-studio","slug":"invalid-mcp-package-upload-file-name-cannot-conta","errorCode":null,"errorMessage":"Invalid MCP package upload: file name cannot contain leading or trailing whitespace","messagePattern":"Invalid MCP package upload: file name cannot contain leading or trailing whitespace","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"src/main/ai/mcp/McpPackageService.ts","lineNumber":291,"sourceCode":"\n  return resolvedEnv\n}\n\nexport function validatePackageUploadPayload(\n  fileBuffer: ArrayBuffer | NodeJS.ArrayBufferView,\n  fileName: string,\n  packageFormat: McpPackageFormat\n): Buffer {\n  if (typeof fileName !== 'string') {\n    throw new Error('Invalid MCP package upload: file name must be a string')\n  }\n\n  const trimmedFileName = fileName.trim()\n  if (!trimmedFileName) {\n    throw new Error('Invalid MCP package upload: file name cannot be empty')\n  }\n  if (trimmedFileName !== fileName) {\n    throw new Error('Invalid MCP package upload: file name cannot contain leading or trailing whitespace')\n  }\n  if (trimmedFileName.includes('\\0') || /[/\\\\]/.test(trimmedFileName)) {\n    throw new Error('Invalid MCP package upload: file name cannot contain path separators')\n  }\n  if (!/^[A-Za-z0-9._ ()@+-]+$/.test(trimmedFileName)) {\n    throw new Error('Invalid MCP package upload: file name contains unsupported characters')\n  }\n  if (path.extname(trimmedFileName).toLowerCase() !== `.${packageFormat}`) {\n    throw new Error(`Invalid MCP package upload: expected a .${packageFormat} file`)\n  }\n\n  let buffer: Buffer\n  if (fileBuffer instanceof ArrayBuffer) {\n    buffer = Buffer.from(fileBuffer)\n  } else if (ArrayBuffer.isView(fileBuffer)) {\n    buffer = Buffer.from(fileBuffer.buffer, fileBuffer.byteOffset, fileBuffer.byteLength)\n  } else {\n    throw new Error('Invalid MCP package upload: file buffer must be an ArrayBuffer')","sourceCodeStart":273,"sourceCodeEnd":309,"githubUrl":"https://github.com/CherryHQ/cherry-studio/blob/726446b54cd69ffe51a276638672f6d95ca0768c/src/main/ai/mcp/McpPackageService.ts#L273-L309","documentation":"Thrown by validatePackageUploadPayload when fileName !== fileName.trim(), i.e. the value has leading or trailing whitespace. The service rejects this because (a) the temp-file path is built from the filename and surrounding whitespace causes confusing path errors, and (b) it almost always indicates a frontend string-handling bug rather than an intentional filename.","triggerScenarios":"Renderer sent fileName = \" pkg.mcpb\" or \"pkg.dxt\\n\". Common when the filename was concatenated with a separator and not trimmed, or when copy-pasted from a label that included padding spaces.","commonSituations":"The filename was read from a UI label or template literal that introduced spaces; a user typed the filename manually with stray spaces; the IPC payload builder did `somePrefix + ' ' + file.name`.","solutions":["On the renderer, pass file.name directly rather than a constructed or user-typed string; do not trim there because the service wants the raw value to detect the bug.","Find the code path that added the whitespace (often a template literal or label concat) and remove it.","Retry the upload once the frontend sends the verbatim File.name."],"exampleFix":"// renderer - before\nconst fileName = `upload: ${file.name}`\n// after\nconst fileName = file.name","handlingStrategy":"validation","validationCode":"function hasNoSurroundingWhitespace(s: string): boolean {\n  return s === s.trim()\n}","typeGuard":"function isTrimmedString(s: unknown): s is string {\n  return typeof s === 'string' && s.length > 0 && s === s.trim()\n}","tryCatchPattern":null,"preventionTips":["Pass the verbatim File.name; do not construct filenames by concatenation with labels or prefixes.","If the filename is user-typed, trim is the renderer's responsibility — but the service intentionally rejects pre-trimmed whitespace as a bug signal, so send a clean value.","Add a renderer lint rule that flags template literals used to build filenames."],"tags":["mcp","upload","validation","ipc","whitespace"],"backgroundTag":null,"analyzedSha":"726446b54cd69ffe51a276638672f6d95ca0768c","analyzedAt":"2026-08-12T17:30:37.448Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}