Skip to content

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
merged 1 commit into from
Jun 14, 2025

Conversation

brianc
Copy link
Owner

@brianc brianc commented Jun 14, 2025

  • do not throw on null or undefined
  • write more comprehensive tests
  • do not loop through any elements of something with a .length

Copy link

Deploying node-postgres with  Cloudflare Pages  Cloudflare Pages

Latest commit: bf59f44
Status: ✅  Deploy successful!
Preview URL: https://d55076a2.node-postgres.pages.dev
Branch Preview URL: https://bmc-fix-escape-literal.node-postgres.pages.dev

View logs

@brianc brianc merged commit 8608fb8 into master Jun 14, 2025
15 checks passed
}

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

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

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

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?

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.

@brianc brianc deleted the bmc/fix-escape-literal branch June 14, 2025 23:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants