Skip to content

Commit 00a10b2

Browse files
committed
Fix escaping for helpers and listen
fixes porsager#157 and porsager#57 and porsager#97 and porsager#111 and porsager#71
1 parent c3b6531 commit 00a10b2

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

lib/types.js

+1-18
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,7 @@ function typeHandlers(types) {
6666
}
6767

6868
module.exports.escape = function escape(str) {
69-
let result = ''
70-
let q = str[0] < 10 || str[0] === '$'
71-
let last = 0
72-
let c
73-
74-
for (let i = 0; i < str.length; i++) {
75-
c = str[i].charCodeAt(0)
76-
if (str[i] === '"') {
77-
q = true
78-
result += str.slice(last, i) + '"'
79-
last = i
80-
} else if (c === 96 || (c !== 36 && c <= 47) || (c >= 58 && c <= 64)
81-
|| (c >= 91 && c <= 94) || (c >= 123 && c <= 128)) {
82-
q = true
83-
}
84-
}
85-
86-
return (q ? '"' : '') + (q ? result + str.slice(last, str.length) : str) + (q ? '"' : '')
69+
return '"' + str.replace(/"/g, '""') + '"'
8770
}
8871

8972
const type = {

tests/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,13 @@ t('listen and notify with weird name', async() => {
578578
)]
579579
})
580580

581+
t('listen and notify with upper case', async() =>
582+
['works', await new Promise(async resolve => {
583+
await sql.listen('withUpperChar', resolve)
584+
sql.notify('withUpperChar', 'works')
585+
})]
586+
)
587+
581588
t('listen reconnects', async() => {
582589
const listener = postgres(options)
583590
, xs = []
@@ -731,6 +738,18 @@ t('sql().finally throws not tagged error', async() => {
731738
return ['NOT_TAGGED_CALL', error]
732739
})
733740

741+
t('little bobby tables', async() => {
742+
const name = 'Robert\'); DROP TABLE students;--'
743+
744+
await sql`create table students (name text, age int)`
745+
await sql`insert into students (name) values (${ name })`
746+
747+
return [
748+
name, (await sql`select name from students`)[0].name,
749+
await sql`drop table students`
750+
]
751+
})
752+
734753
t('dynamic column name', async() => {
735754
return ['!not_valid', Object.keys((await sql`select 1 as ${ sql('!not_valid') }`)[0])[0]]
736755
})

0 commit comments

Comments
 (0)