import mysql from 'mysql2/promise'; import { randomBytes } from 'node:crypto'; const port = Number(process.env.BHYLU_MYSQL_PORT); if ( process.env.BHYLU_NODE_DB_LAB !== 'YES' || !Number.isInteger(port) || port < 1024 || port > 65535 || port === 3306 || !process.env.BHYLU_MYSQL_USER || !process.env.BHYLU_MYSQL_PASSWORD ) throw new Error('Use the dedicated disposable database lab'); const connection = await mysql.createConnection({ host: '127.0.0.1', port, user: process.env.BHYLU_MYSQL_USER, password: process.env.BHYLU_MYSQL_PASSWORD, multipleStatements: false, }); const schema = 'bhylu_node_' + randomBytes(8).toString('hex'); let created = false; try { await connection.query('CREATE DATABASE ' + schema); created = true; await connection.query('USE ' + schema); await connection.query( 'CREATE TABLE kits (id INT PRIMARY KEY, name VARCHAR(40) NOT NULL, available INT NOT NULL CHECK (available >= 0))', ); await connection.execute('INSERT INTO kits VALUES (?, ?, ?), (?, ?, ?), (?, ?, ?)', [ 701, 'tripod', 3, 702, 'binoculars', 2, 703, 'torch', 0, ]); const [good] = await connection.execute( 'SELECT id FROM kits WHERE name = ? ORDER BY id', ['tripod'], ); const [untrusted] = await connection.execute( 'SELECT id FROM kits WHERE name = ? ORDER BY id', ["tripod' OR 1=1 --"], ); console.log(good.map((row) => row.id).join(',')); console.log(untrusted.length); } finally { try { if (created) await connection.query('DROP DATABASE ' + schema); } finally { await connection.end(); } }