Skip to content

Numeric options should be converted to Number before being used #622

@paulovieira

Description

@paulovieira

Example 1:

Supposed I have

// at index.js

let sql = Postgres({ max: process.env.PGMAXCONNECTIONS });

and PGMAXCONNECTIONS is set as

PGMAXCONNECTIONS=20 node index.js

Then we don't get 20 connections as expected, but only 1! The problem is that options.max will be the string "20" instead of the number 20, so the logic to create the array of connections won't work:

const connections = [...Array(options.max)].map(...)

Example 2:

This kind of issue might also happen with the idle_timeout, max_lifetime or connect_timeout options. They end up being used in the timer utility, in connection.js (which calls setTimeout). A numeric string will work well because setTimeout will coerce the string to a number. However suppose there is some typo like this:

# notice the comma after 5
PGIDLE_TIMEOUT=60, node index

When the numeric string can't be coerced to a number, setTimeout will still work but using 0 instead! This will obviously cause unexpected behaviors. In this case we would never see any idle connections when inspecting pg_stat_activity.


There might be other options where this sort of problems happen, I didn't check all of them.

Overall I think this kind of issues could be easily catched if some simple validations were done when parsing the options.

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions