ForbesLindesay / atdatabases

TypeScript clients for databases that prevent SQL Injection
https://www.atdatabases.org
MIT License
598 stars 47 forks source link

Feature Request: SSL Connection Support for MySQL (@databases/mysql) #315

Open juvasquezg opened 7 months ago

juvasquezg commented 7 months ago

Feature Request: SSL Connection Support for MySQL

Description:

I would like to request support for SSL connections in the @databases/mysql package similar to how it is implemented in the mysql2 package. Currently, I need to use SSL connections for secure database connections, and having this feature in @databases/mysql would greatly benefit my use case.

Additional Context:

I am testing the code from the documentation:

const createConnectionPool = require('@databases/mysql');
const { sql } = require('@databases/mysql');

async function run() {
  // N.B. you will need to replace this connection
  // string with the correct string for your database.
  const db = createConnectionPool(
    "mysql://db_user:db_password@db_host:db_port/db_name",
  );

  const results = await db.query(sql`
    SELECT 1 + 1 as result;
  `);

  console.log(results);
  // => [{result: 2}]

  await db.dispose();
}

run().catch((err) => {
  console.error(err);
  process.exit(1);
});

However, I encountered the following error:

{
  uri: 'mysql://db_user:db_password@db_host:db_port/db_name,
  multipleStatements: true,
  timezone: 'local',
  typeCast: [Function: typeCast]
}
{
  maxSize: 10,
  maxUses: Infinity,
  idleTimeoutMilliseconds: 30000,
  queueTimeoutMilliseconds: 60000
}
Ignoring invalid configuration option passed to Connection: schema. This is currently a warning, but in future versions of MySQL2, an error will be thrown if you pass an invalid configuration option to a Connection
Error: Access denied for user 'db_user'@'db_host' (using password: YES)
...
{
  code: 'ER_ACCESS_DENIED_ERROR',
  errno: 1045,
  sqlState: '28000'
}

Providing the Certificate Authority (CA) in the configuration for mysql2 resolves any SSL-related issues.

```javascript
const mysql = require('mysql2/promise');

async function run() {
  const connection = await mysql.createConnection({
    host: 'db_host',
    user: 'db_user',
    password: 'db_password',
    database: 'db_name',
    port: 'db_port',
    ssl: {
      ca: require('fs').readFileSync('path/to/ca.pem'), // Provide the CA certificate path
    },
  });

  try {
    const [rows, fields] = await connection.execute('SELECT 1 + 1 as result');
    console.log(rows); // => [{result: 2}]
  } finally {
    connection.end();
  }
}

run().catch((err) => {
  console.error(err);
  process.exit(1);
});
tot-ra commented 1 week ago

+1 encountered similar issue when trying to connect to DigitalOcean from nodejs

So I wish I could use full mysql configuration object.

Unfortunately I can't do that because there is this check in between https://github.com/ForbesLindesay/atdatabases/blob/d49f86508f66bc30a679a021a9385f2541fe340a/packages/mysql/src/index.ts#L144