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
2 changed files with 35 additions and 0 deletions
@@ -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
}