Skip to content

Commit 84d9421

Browse files
committed
Throw error on \0
1 parent 3ceb90b commit 84d9421

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

packages/pg/lib/client.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,21 @@ class Client extends EventEmitter {
444444

445445
// Ported from PostgreSQL 9.2.4 source code in src/interfaces/libpq/fe-exec.c
446446
escapeIdentifier(str) {
447-
return '"' + str.replace(/["\0]/g, '""') + '"'
447+
var escaped = '"'
448+
449+
for (var i = 0; i < str.length; i++) {
450+
var c = str[i]
451+
if (c === '"') {
452+
escaped += c + c
453+
} else if (c === '\0') {
454+
throw new Error("Identifier contains \\0 which is not allowed in PostgreSQL identifiers.");
455+
} else {
456+
escaped += c
457+
}
458+
}
459+
460+
escaped += '"'
461+
return escaped
448462
}
449463

450464
// Ported from PostgreSQL 9.2.4 source code in src/interfaces/libpq/fe-exec.c
@@ -460,7 +474,7 @@ class Client extends EventEmitter {
460474
escaped += c + c
461475
hasBackslash = true
462476
} else if (c === '\0') {
463-
// Ignore it
477+
throw new Error("Literal contains \\0 which is not allowed in PostgreSQL strings.");
464478
} else {
465479
escaped += c
466480
}

0 commit comments

Comments
 (0)