Fix import null bytes error (#5036)

fix: sanitize null bytes in import data from SQLite
This commit is contained in:
Ong Chung Yau 2025-08-07 18:11:53 +08:00 committed by GitHub
parent 141c49013a
commit 9e743e4aa1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 0 deletions

View File

@ -26,6 +26,7 @@ import marketplacesService from '../marketplaces'
import toolsService from '../tools'
import variableService from '../variables'
import { Platform } from '../../Interface'
import { sanitizeNullBytes } from '../../utils/sanitize.util'
type ExportInput = {
agentflow: boolean
@ -753,6 +754,8 @@ const importData = async (importData: ExportData, orgId: string, activeWorkspace
importData = await replaceDuplicateIdsForVariable(queryRunner, importData, importData.Variable)
}
importData = sanitizeNullBytes(importData)
await queryRunner.startTransaction()
if (importData.AgentFlow.length > 0) await queryRunner.manager.save(ChatFlow, importData.AgentFlow)

View File

@ -0,0 +1,32 @@
export function sanitizeNullBytes(obj: any): any {
const stack = [obj]
while (stack.length) {
const current = stack.pop()
if (Array.isArray(current)) {
for (let i = 0; i < current.length; i++) {
const val = current[i]
if (typeof val === 'string') {
// eslint-disable-next-line no-control-regex
current[i] = val.replace(/\u0000/g, '')
} else if (val && typeof val === 'object') {
stack.push(val)
}
}
} else if (current && typeof current === 'object') {
for (const key in current) {
if (!Object.hasOwnProperty.call(current, key)) continue
const val = current[key]
if (typeof val === 'string') {
// eslint-disable-next-line no-control-regex
current[key] = val.replace(/\u0000/g, '')
} else if (val && typeof val === 'object') {
stack.push(val)
}
}
}
}
return obj
}