{"record":{"id":"738159b333f8c7dd","repo":"danny-avila/LibreChat","slug":"invalid-base64-string","errorCode":null,"errorMessage":"Invalid base64 string","messagePattern":"Invalid base64 string","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"api/server/services/Files/process.js","lineNumber":1243,"sourceCode":"    logger.debug(`[retrieveAndProcessFile] Non-image file type detected: ${basename}`);\n    return await processOpenAIFile({ ...processArgs, saveFile: true });\n  }\n}\n\n/**\n * Converts a base64 string to a buffer.\n * @param {string} base64String\n * @returns {Buffer<ArrayBufferLike>}\n */\nfunction base64ToBuffer(base64String) {\n  try {\n    const typeMatch = base64String.match(/^data:([A-Za-z-+/]+);base64,/);\n    const type = typeMatch ? typeMatch[1] : '';\n\n    const base64Data = base64String.replace(/^data:([A-Za-z-+/]+);base64,/, '');\n\n    if (!base64Data) {\n      throw new Error('Invalid base64 string');\n    }\n\n    return {\n      buffer: Buffer.from(base64Data, 'base64'),\n      type,\n    };\n  } catch (error) {\n    throw new Error(`Failed to convert base64 to buffer: ${error.message}`);\n  }\n}\n\nasync function saveBase64Image(\n  url,\n  { req, file_id: _file_id, filename: _filename, endpoint, context, resolution },\n) {\n  const appConfig = req.config;\n  const effectiveResolution = resolution ?? appConfig.fileConfig?.imageGeneration ?? 'high';\n  const file_id = _file_id ?? v4();","sourceCodeStart":1225,"sourceCodeEnd":1261,"githubUrl":"https://github.com/danny-avila/LibreChat/blob/5ff282f9006c436e561de1afd39a481bea1ef0d8/api/server/services/Files/process.js#L1225-L1261","documentation":"Thrown by base64ToBuffer after stripping the optional `data:<type>;base64,` prefix if the remaining string is empty. This catches inputs that were only the prefix (`data:image/png;base64,` with no payload) or an empty string after stripping. The function returns buffer+type, so an empty payload has nothing to decode.","triggerScenarios":"A data-URL with an empty base64 body; a string that is just the MIME prefix; calling base64ToBuffer('') or base64ToBuffer('data:image/png;base64,') — anything where the regex strips the content down to nothing.","commonSituations":"Frontend paste handler that captured the data URL header but not the body due to truncation; a copy/paste that dropped the payload; a generated data URL from a failed canvas.toDataURL (rare); upstream serialization that split the string.","solutions":["Ensure the full base64 payload is included after the `data:<type>;base64,` prefix.","Validate on the client that the data URL body is non-empty before submit.","If the input is a plain (non-data-URL) base64 string, note the regex only strips the optional prefix — a plain empty string still trips this.","Check the producer of the data URL (e.g., FileReader.readAsDataURL) completes before the value is sent."],"exampleFix":"// before\nawait saveBase64Image('data:image/png;base64,', ctx);\n\n// after: ensure the body is captured\nconst dataUrl = await fileToDataUrl(file); // resolves full data URL\nif (!dataUrl.split(',')[1]) throw new Error('Empty image payload');\nawait saveBase64Image(dataUrl, ctx);","handlingStrategy":"validation","validationCode":"function assertBase64Body(dataUrl) {\n  const body = typeof dataUrl === 'string'\n    ? dataUrl.replace(/^data:[^;]+;base64,/, '')\n    : '';\n  if (!body) throw new Error('Empty base64 payload');\n  return body;\n}","typeGuard":null,"tryCatchPattern":"try { base64ToBuffer(s); }\ncatch (e) {\n  if (/Invalid base64 string/.test(e.message)) return res.status(400).json({ error: 'Image payload is empty' });\n  throw e;\n}","preventionTips":["Ensure FileReader.readAsDataURL / canvas.toDataURL completes before using the value.","Validate the data URL body is non-empty on the client before submit.","Distinguish empty-payload (this error) from invalid characters (error 138)."],"tags":["base64","input-validation","data-url"],"backgroundTag":null,"analyzedSha":"5ff282f9006c436e561de1afd39a481bea1ef0d8","analyzedAt":"2026-08-12T21:38:08.145Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}