Skip to content

Commit 0f75598

Browse files
authored
Merge pull request #414 from jon4hz/main
fix: sanitize data before db insert
2 parents 2240a68 + 5b3b039 commit 0f75598

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

backend/routes/sync.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,41 @@ function getErrorLineNumber(error) {
3939
return lineNumber;
4040
}
4141

42+
function sanitizeNullBytes(obj) {
43+
if (typeof obj === 'string') {
44+
// Remove various forms of null bytes and control characters that cause Unicode escape sequence errors
45+
return obj
46+
.replace(/\u0000/g, '') // Remove null bytes
47+
.replace(/\\u0000/g, '') // Remove escaped null bytes
48+
.replace(/\x00/g, '') // Remove hex null bytes
49+
.replace(/[\u0000-\u001F\u007F-\u009F]/g, '') // Remove all control characters
50+
.trim(); // Remove leading/trailing whitespace
51+
}
52+
53+
if (Array.isArray(obj)) {
54+
return obj.map(sanitizeNullBytes);
55+
}
56+
57+
if (obj && typeof obj === 'object') {
58+
const sanitized = {};
59+
for (const [key, value] of Object.entries(obj)) {
60+
sanitized[key] = sanitizeNullBytes(value);
61+
}
62+
return sanitized;
63+
}
64+
65+
return obj;
66+
}
67+
4268
class sync {
4369
async getExistingIDsforTable(tablename) {
4470
return await db.query(`SELECT "Id" FROM ${tablename}`).then((res) => res.rows.map((row) => row.Id));
4571
}
4672

4773
async insertData(tablename, dataToInsert, column_mappings) {
48-
let result = await db.insertBulk(tablename, dataToInsert, column_mappings);
74+
const sanitizedData = sanitizeNullBytes(dataToInsert);
75+
76+
let result = await db.insertBulk(tablename, sanitizedData, column_mappings);
4977
if (result.Result === "SUCCESS") {
5078
// syncTask.loggedData.push({ color: "dodgerblue", Message: dataToInsert.length + " Rows Inserted." });
5179
} else {

0 commit comments

Comments
 (0)