This commit is contained in:
Benjamin Toby 2024-11-18 10:25:28 +01:00
parent 7ffa5c0da5
commit aaa604c66b
7 changed files with 47 additions and 112 deletions

View File

@ -51,7 +51,7 @@ async function localGet({ options, dbSchema }) {
(query.match(
/^alter|^delete|information_schema|databases|^create/i
) ||
!query.match(/^select/i))
!query.match(/^select|^\( ?select/i))
) {
return { success: false, msg: "Wrong Input" };
}

62
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "@moduletrace/datasquirel",
"version": "2.6.5",
"version": "2.6.7",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@moduletrace/datasquirel",
"version": "2.6.5",
"version": "2.6.7",
"license": "ISC",
"dependencies": {
"@types/ace": "^0.0.52",
@ -15,7 +15,6 @@
"dotenv": "^16.3.1",
"generate-password": "^1.7.1",
"lodash": "^4.17.21",
"mariadb": "^3.4.0",
"mysql": "^2.18.1",
"nodemailer": "^6.9.14",
"sanitize-html": "^2.13.1",
@ -37,12 +36,6 @@
"integrity": "sha512-YPF9S7fzpuyrxru+sG/rrTpZkC6gpHBPF14W3x70kqVOD+ks6jkYLapk4yceh36xej7K4HYxcyz9ZDQ2lTvwgQ==",
"license": "MIT"
},
"node_modules/@types/geojson": {
"version": "7946.0.14",
"resolved": "https://registry.npmjs.org/@types/geojson/-/geojson-7946.0.14.tgz",
"integrity": "sha512-WCfD5Ht3ZesJUsONdhvm84dmzWOiOzOAqOncN0++w0lBw1o8OuDNJF2McvvCef/yBqb/HYRahp1BYtODFQ8bRg==",
"license": "MIT"
},
"node_modules/@types/jquery": {
"version": "3.5.32",
"resolved": "https://registry.npmjs.org/@types/jquery/-/jquery-3.5.32.tgz",
@ -72,6 +65,7 @@
"version": "22.7.5",
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.7.5.tgz",
"integrity": "sha512-jML7s2NAzMWc//QSJ1a3prpk78cOPchGvXJsC3C6R6PSMoooztvRVQEz89gmBTBY1SPMaqo5teB4uNHPdetShQ==",
"devOptional": true,
"license": "MIT",
"dependencies": {
"undici-types": "~6.19.2"
@ -136,15 +130,6 @@
"node": ">=0.10.0"
}
},
"node_modules/denque": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz",
"integrity": "sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==",
"license": "Apache-2.0",
"engines": {
"node": ">=0.10"
}
},
"node_modules/dom-serializer": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-2.0.0.tgz",
@ -260,18 +245,6 @@
"entities": "^4.4.0"
}
},
"node_modules/iconv-lite": {
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz",
"integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==",
"license": "MIT",
"dependencies": {
"safer-buffer": ">= 2.1.2 < 3.0.0"
},
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/inherits": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
@ -297,28 +270,6 @@
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==",
"license": "MIT"
},
"node_modules/lru-cache": {
"version": "10.4.3",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-10.4.3.tgz",
"integrity": "sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ==",
"license": "ISC"
},
"node_modules/mariadb": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/mariadb/-/mariadb-3.4.0.tgz",
"integrity": "sha512-hdRPcAzs+MTxK5VG1thBW18gGTlw6yWBe9YnLB65GLo7q0fO5DWsgomIevV/pXSaWRmD3qi6ka4oSFRTExRiEQ==",
"license": "LGPL-2.1-or-later",
"dependencies": {
"@types/geojson": "^7946.0.14",
"@types/node": "^22.5.4",
"denque": "^2.1.0",
"iconv-lite": "^0.6.3",
"lru-cache": "^10.3.0"
},
"engines": {
"node": ">= 14"
}
},
"node_modules/mysql": {
"version": "2.18.1",
"resolved": "https://registry.npmjs.org/mysql/-/mysql-2.18.1.tgz",
@ -424,12 +375,6 @@
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
},
"node_modules/safer-buffer": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==",
"license": "MIT"
},
"node_modules/sanitize-html": {
"version": "2.13.1",
"resolved": "https://registry.npmjs.org/sanitize-html/-/sanitize-html-2.13.1.tgz",
@ -485,6 +430,7 @@
"version": "6.19.8",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz",
"integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==",
"devOptional": true,
"license": "MIT"
},
"node_modules/util-deprecate": {

View File

@ -3,19 +3,15 @@
const mysql = require("serverless-mysql");
const grabDbSSL = require("../grabDbSSL");
// const mariadb = require("mariadb");
// const pool = mariadb.createPool({
// host: process.env.DSQL_DB_HOST,
// user: process.env.DSQL_DB_USERNAME,
// password: process.env.DSQL_DB_PASSWORD,
// database: process.env.DSQL_DB_NAME,
// port: process.env.DSQL_DB_PORT
// ? Number(process.env.DSQL_DB_PORT)
// : undefined,
// charset: "utf8mb4",
// ssl: true,
// });
/**
* DSQL user read-only DB handler
* @param {object} params
* @param {string} params.paradigm
* @param {string} params.database
* @param {string} params.queryString
* @param {string[]} [params.queryValues]
*/
async function LOCAL_DB_HANDLER(/** @type {any[]} */ ...args) {
const MASTER = mysql({
config: {
host: process.env.DSQL_DB_HOST,
@ -30,30 +26,18 @@ const MASTER = mysql({
timeout: 1000,
connectTimeout: 1000,
},
onConnect: () => {
console.log("Connection Successful!");
},
onConnectError: (/** @type {any} */ err) => {
console.log("Connection Error", err.message);
},
onError: (/** @type {any} */ err) => {
console.log("Client Error", err.message);
},
});
/**
* DSQL user read-only DB handler
* @param {object} params
* @param {string} params.paradigm
* @param {string} params.database
* @param {string} params.queryString
* @param {string[]} [params.queryValues]
*/
async function LOCAL_DB_HANDLER(/** @type {any[]} */ ...args) {
MASTER.config({
host: process.env.DSQL_DB_HOST,
user: process.env.DSQL_DB_USERNAME,
password: process.env.DSQL_DB_PASSWORD,
database: process.env.DSQL_DB_NAME,
port: process.env.DSQL_DB_PORT
? Number(process.env.DSQL_DB_PORT)
: undefined,
charset: "utf8mb4",
ssl: grabDbSSL(),
timeout: 1000,
connectTimeout: 1000,
});
console.log("Querying ...");
try {
const results = await MASTER.query(...args);
@ -61,7 +45,7 @@ async function LOCAL_DB_HANDLER(/** @type {any[]} */ ...args) {
return JSON.parse(JSON.stringify(results));
} catch (/** @type {any} */ error) {
console.log("DB Error =>", error);
console.log("DB Error =>", error.message);
return {
success: false,
error: error.message,

View File

@ -8,9 +8,6 @@ const fs = require("fs");
module.exports = function grabDbSSL() {
const SSL_DIR = process.env.DSQL_SSL_DIR;
if (!SSL_DIR?.match(/./)) {
// console.log(
// "No SSL certificate provided. Query will run in normal mode. To add SSL add an env path dir `DSQL_SSL_DIR` with a file named `ca-cert.pem`"
// );
return undefined;
}

View File

@ -1,6 +1,6 @@
{
"name": "@moduletrace/datasquirel",
"version": "2.6.6",
"version": "2.6.7",
"description": "Cloud-based SQL data management tool",
"main": "index.js",
"bin": {
@ -36,7 +36,6 @@
"dotenv": "^16.3.1",
"generate-password": "^1.7.1",
"lodash": "^4.17.21",
"mariadb": "^3.4.0",
"mysql": "^2.18.1",
"nodemailer": "^6.9.14",
"sanitize-html": "^2.13.1",

View File

@ -19,10 +19,11 @@ const grabHostNames = require("../package-shared/utils/grab-host-names");
* @param {string} params.query - SQL Query
* @param {string[]} [params.queryValues] - An array of query values if using "?" placeholders
* @param {string} [params.tableName] - Name of the table to query
* @param {boolean} [params.useLocal] - Whether to use a remote database instead of API
*
* @returns { Promise<import("../package-shared/types").GetReturn> } - Return Object
*/
async function get({ key, db, query, queryValues, tableName }) {
async function get({ key, db, query, queryValues, tableName, useLocal }) {
const { host, port, scheme } = grabHostNames();
/**
@ -37,7 +38,8 @@ async function get({ key, db, query, queryValues, tableName }) {
DSQL_DB_HOST?.match(/./) &&
DSQL_DB_USERNAME?.match(/./) &&
DSQL_DB_PASSWORD?.match(/./) &&
DSQL_DB_NAME?.match(/./)
DSQL_DB_NAME?.match(/./) &&
useLocal
) {
/** @type {import("../package-shared/types").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;

View File

@ -1,7 +1,5 @@
// @ts-check
const http = require("http");
const https = require("https");
const path = require("path");
const fs = require("fs");
const localPost = require("../engine/query/post");
@ -17,10 +15,18 @@ const grabHostNames = require("../package-shared/utils/grab-host-names");
* @param {import("../package-shared/types").PostDataPayload | string} params.query - SQL query String or Request Object
* @param {any[]} [params.queryValues] - Query Values if using "?" placeholders
* @param {string} [params.tableName] - Name of the table to query
* @param {boolean} [params.useLocal] - Whether to use a remote database instead of API
*
* @returns { Promise<import("../package-shared/types").PostReturn> } - Return Object
*/
async function post({ key, query, queryValues, database, tableName }) {
async function post({
key,
query,
queryValues,
database,
tableName,
useLocal,
}) {
const { host, port, scheme } = grabHostNames();
/**
@ -35,7 +41,8 @@ async function post({ key, query, queryValues, database, tableName }) {
DSQL_DB_HOST?.match(/./) &&
DSQL_DB_USERNAME?.match(/./) &&
DSQL_DB_PASSWORD?.match(/./) &&
DSQL_DB_NAME?.match(/./)
DSQL_DB_NAME?.match(/./) &&
useLocal
) {
/** @type {import("../package-shared/types").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;