Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Do not concatenate an array if passed to escapeLiteral #3489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
brianc merged 1 commit into master from bmc/fix-escape-literal
Jun 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/pg/lib/utils.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ const escapeLiteral = function (str) {
let hasBackslash = false
let escaped = "'"

if (str == null) {
return "''"
}

if (typeof str !== 'string') {
return "''"
Copy link
Collaborator

@charmander charmander Jun 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about throwing a TypeError? Seems about the same level of breaking change, but potentially less confusing.

Copy link
Owner Author

@brianc brianc Jun 14, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's what i was going to do at first, but after writing a bunch of tests for previously indeterminate behavior, almost everything returned "''" when passed to this function - numbers, boolean, date, object. The only problem is with things with a .length property which aren't strings (namely: array). It actually kinda feels more "escapey" to me to just turn your "not a string" into an empty string in the query if you're concatenating in there. But I was definitely only the fence.

Copy link
Collaborator

@charmander charmander Jun 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that you mention those examples... how about casting everything to String? pg 9?

brianc reacted with thumbs up emoji
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that isn't a good idea - def breakage, though subtle. tbh whenever I, very rarely, use these functions to concatenate some form of user-input into a query directly I do a ton of external sanitization first. Check types, if its a number make sure its in an expected range, etc. It's scary & should be a last resort in most cases.

}

for (let i = 0; i < str.length; i++) {
const c = str[i]
if (c === "'") {
Expand Down
45 changes: 28 additions & 17 deletions packages/pg/test/unit/utils-tests.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -232,35 +232,46 @@ test('prepareValue: can safely be used to map an array of values including those
})

const testEscapeLiteral = function (testName, input, expected) {
test(testName, function () {
test(`escapeLiteral: ${testName}`, function () {
const actual = utils.escapeLiteral(input)
assert.equal(expected, actual)
})
}
testEscapeLiteral('escapeLiteral: no special characters', 'hello world', "'hello world'")

testEscapeLiteral('escapeLiteral: contains double quotes only', 'hello " world', "'hello \" world'")
testEscapeLiteral('no special characters', 'hello world', "'hello world'")

testEscapeLiteral('escapeLiteral: contains single quotes only', "hello ' world", "'hello '' world'")
testEscapeLiteral('contains double quotes only', 'hello " world', "'hello \" world'")

testEscapeLiteral('escapeLiteral: contains backslashes only', 'hello \\ world', " E'hello \\\\ world'")
testEscapeLiteral('contains single quotes only', "hello ' world", "'hello '' world'")

testEscapeLiteral('escapeLiteral: contains single quotes and double quotes', 'hello \' " world', "'hello '' \" world'")
testEscapeLiteral('contains backslashes only', 'hello \\ world', " E'hello \\\\ world'")

testEscapeLiteral(
'escapeLiteral: contains double quotes and backslashes',
'hello \\ " world',
" E'hello \\\\ \" world'"
)
testEscapeLiteral('contains single quotes and double quotes', 'hello \' " world', "'hello '' \" world'")

testEscapeLiteral(
'escapeLiteral: contains single quotes and backslashes',
"hello \\ ' world",
" E'hello \\\\ '' world'"
)
testEscapeLiteral('date', new Date(), "''")

testEscapeLiteral('null', null, "''")

testEscapeLiteral('undefined', undefined, "''")

testEscapeLiteral('boolean', false, "''")

testEscapeLiteral('number', 1, "''")

testEscapeLiteral('number', 1, "''")

testEscapeLiteral('boolean', true, "''")

testEscapeLiteral('array', [1, 2, 3], "''")

testEscapeLiteral('object', { x: 42 }, "''")

testEscapeLiteral('contains double quotes and backslashes', 'hello \\ " world', " E'hello \\\\ \" world'")

testEscapeLiteral('contains single quotes and backslashes', "hello \\ ' world", " E'hello \\\\ '' world'")

testEscapeLiteral(
'escapeLiteral: contains single quotes, double quotes, and backslashes',
'contains single quotes, double quotes, and backslashes',
'hello \\ \' " world',
" E'hello \\\\ '' \" world'"
)
Expand Down

AltStyle によって変換されたページ (->オリジナル) /