@galadrim/galadmin 0.1.236 → 0.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/main.yml +34 -3
- package/README.md +65 -0
- package/dist/api.d.ts +16 -0
- package/dist/api.js +240 -189
- package/dist/api.js.map +1 -1
- package/dist/index.js +25 -3
- package/dist/index.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/config.d.ts +6 -1
- package/dist/utils/dbManager.d.ts +5 -0
- package/dist/utils/dbManager.js +146 -11
- package/dist/utils/dbManager.js.map +1 -1
- package/dist/utils/imageDimensions.d.ts +5 -0
- package/dist/utils/imageDimensions.js +98 -0
- package/dist/utils/imageDimensions.js.map +1 -0
- package/jest.config.js +43 -0
- package/jest.integration.config.js +16 -0
- package/package.json +30 -10
- package/src/api.ts +284 -217
- package/src/front/src/App.test.js +28 -0
- package/src/front/src/components/OutsideClickHandler.test.js +53 -0
- package/src/front/src/components/RedirectToFirstView.test.js +38 -0
- package/src/front/src/utils/checkRequiredColumns.test.js +46 -0
- package/src/front/src/utils/evalExpression.test.js +29 -0
- package/src/index.ts +26 -3
- package/src/types/config.ts +13 -1
- package/src/utils/dbManager.ts +243 -102
- package/src/utils/imageDimensions.ts +112 -0
- package/tests/__mocks__/@aws-sdk/client-s3.js +6 -0
- package/tests/__mocks__/@aws-sdk/s3-request-presigner.js +3 -0
- package/tests/__mocks__/@google-cloud/storage.js +7 -0
- package/tests/__mocks__/argon2.js +3 -0
- package/tests/__mocks__/axios.js +7 -0
- package/tests/__mocks__/connect-pg-simple.js +24 -0
- package/tests/__mocks__/express-mysql-session.js +24 -0
- package/tests/__mocks__/mysql2/promise.js +3 -0
- package/tests/__mocks__/pg.js +3 -0
- package/tests/__mocks__/rollup.js +5 -0
- package/tests/__mocks__/ssh2-sftp-client.js +10 -0
- package/tests/api-helpers.test.ts +376 -0
- package/tests/dbManager.init.test.ts +129 -0
- package/tests/dbManager.test.ts +265 -0
- package/tests/edge-cases.test.ts +167 -0
- package/tests/helpers/app.ts +69 -0
- package/tests/helpers/fakeDb.ts +66 -0
- package/tests/helpers/fakePool.ts +76 -0
- package/tests/helpers/fixtures.ts +68 -0
- package/tests/imageDimensions.test.ts +111 -0
- package/tests/index.sessionStore.test.ts +19 -0
- package/tests/index.test.ts +412 -0
- package/tests/integration/__mocks__/axios.js +3 -0
- package/tests/integration/dbHarness.ts +381 -0
- package/tests/integration/dbManager.integration.test.ts +69 -0
- package/tests/integration/globalSetup.ts +43 -0
- package/tests/integration/globalTeardown.ts +42 -0
- package/tests/integration/routes.authorization.integration.test.ts +319 -0
- package/tests/integration/routes.config.integration.test.ts +189 -0
- package/tests/integration/routes.crud.integration.test.ts +805 -0
- package/tests/integration/routes.custom.integration.test.ts +376 -0
- package/tests/integration/routes.foreign.integration.test.ts +306 -0
- package/tests/integration/routes.importExport.integration.test.ts +404 -0
- package/tests/integration/routes.integration.test.ts +366 -0
- package/tests/integration/routes.postgresql.integration.test.ts +88 -0
- package/tests/integration/routes.records.integration.test.ts +634 -0
- package/tests/integration/routes.upload.integration.test.ts +425 -0
- package/tests/setup/silence-console.ts +15 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { render, fireEvent } from "@testing-library/react";
|
|
2
|
+
import OutsideClickHandler from "./OutsideClickHandler";
|
|
3
|
+
|
|
4
|
+
describe("OutsideClickHandler", () => {
|
|
5
|
+
it("renders its children", () => {
|
|
6
|
+
const { getByText } = render(
|
|
7
|
+
<OutsideClickHandler onOutsideClick={() => {}}>
|
|
8
|
+
<span>inner content</span>
|
|
9
|
+
</OutsideClickHandler>,
|
|
10
|
+
);
|
|
11
|
+
expect(getByText("inner content")).toBeTruthy();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("does not fire when clicking inside", () => {
|
|
15
|
+
const onOutsideClick = jest.fn();
|
|
16
|
+
const { getByText } = render(
|
|
17
|
+
<OutsideClickHandler onOutsideClick={onOutsideClick}>
|
|
18
|
+
<span>inner</span>
|
|
19
|
+
</OutsideClickHandler>,
|
|
20
|
+
);
|
|
21
|
+
fireEvent.mouseDown(getByText("inner"));
|
|
22
|
+
expect(onOutsideClick).not.toHaveBeenCalled();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("fires on a mousedown outside of the wrapper", () => {
|
|
26
|
+
const onOutsideClick = jest.fn();
|
|
27
|
+
const { getByText } = render(
|
|
28
|
+
<div>
|
|
29
|
+
<div data-testid="outside">outside</div>
|
|
30
|
+
<OutsideClickHandler onOutsideClick={onOutsideClick}>
|
|
31
|
+
<span>inner</span>
|
|
32
|
+
</OutsideClickHandler>
|
|
33
|
+
</div>,
|
|
34
|
+
);
|
|
35
|
+
fireEvent.mouseDown(getByText("outside"));
|
|
36
|
+
expect(onOutsideClick).toHaveBeenCalledTimes(1);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("stops listening after unmount", () => {
|
|
40
|
+
const onOutsideClick = jest.fn();
|
|
41
|
+
const { unmount, getByText } = render(
|
|
42
|
+
<div>
|
|
43
|
+
<div>outside</div>
|
|
44
|
+
<OutsideClickHandler onOutsideClick={onOutsideClick}>
|
|
45
|
+
<span>inner</span>
|
|
46
|
+
</OutsideClickHandler>
|
|
47
|
+
</div>,
|
|
48
|
+
);
|
|
49
|
+
unmount();
|
|
50
|
+
fireEvent.mouseDown(document.body);
|
|
51
|
+
expect(onOutsideClick).not.toHaveBeenCalled();
|
|
52
|
+
});
|
|
53
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { render } from "@testing-library/react";
|
|
2
|
+
import { MemoryRouter, Route, Switch } from "react-router-dom";
|
|
3
|
+
import RedirectToFirstView from "./RedirectToFirstView";
|
|
4
|
+
import { ConfigContext } from "../contexts/ConfigContext";
|
|
5
|
+
|
|
6
|
+
function renderWithViews(views) {
|
|
7
|
+
return render(
|
|
8
|
+
<ConfigContext.Provider value={{ views }}>
|
|
9
|
+
<MemoryRouter initialEntries={["/"]}>
|
|
10
|
+
<Switch>
|
|
11
|
+
<Route exact path="/" component={RedirectToFirstView} />
|
|
12
|
+
<Route path="/records/0" render={() => <div>LANDING-records</div>} />
|
|
13
|
+
<Route path="/iframe/0" render={() => <div>LANDING-iframe</div>} />
|
|
14
|
+
<Route
|
|
15
|
+
path="/customView/0"
|
|
16
|
+
render={() => <div>LANDING-customView</div>}
|
|
17
|
+
/>
|
|
18
|
+
</Switch>
|
|
19
|
+
</MemoryRouter>
|
|
20
|
+
</ConfigContext.Provider>,
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe("RedirectToFirstView", () => {
|
|
25
|
+
it.each([
|
|
26
|
+
["table", "LANDING-records"],
|
|
27
|
+
["iframe", "LANDING-iframe"],
|
|
28
|
+
["custom", "LANDING-customView"],
|
|
29
|
+
])("redirects a %s first view to its route", (type, landing) => {
|
|
30
|
+
const { getByText } = renderWithViews([{ type, label: "First" }]);
|
|
31
|
+
expect(getByText(landing)).toBeTruthy();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("renders nothing when the config has no views", () => {
|
|
35
|
+
const { container } = renderWithViews([]);
|
|
36
|
+
expect(container.innerHTML).toBe("");
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import checkRequiredColumns from "./checkRequiredColumns";
|
|
2
|
+
|
|
3
|
+
describe("checkRequiredColumns", () => {
|
|
4
|
+
beforeEach(() => {
|
|
5
|
+
global.window.swal = jest.fn();
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
const columns = [
|
|
9
|
+
{ label: "Nom", name: "fruitName", required: true },
|
|
10
|
+
{ label: "Note", name: "note", required: true },
|
|
11
|
+
{ label: "Acidité", name: "acidity" },
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
it("accepts data where every required column is filled", () => {
|
|
15
|
+
expect(
|
|
16
|
+
checkRequiredColumns(columns, { fruitName: "pomme", note: "ok" }),
|
|
17
|
+
).toBe(true);
|
|
18
|
+
expect(global.window.swal).not.toHaveBeenCalled();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("rejects data missing a required column and shows a message", () => {
|
|
22
|
+
expect(checkRequiredColumns(columns, { fruitName: "pomme", note: "" })).toBe(
|
|
23
|
+
false,
|
|
24
|
+
);
|
|
25
|
+
expect(global.window.swal).toHaveBeenCalledWith(
|
|
26
|
+
expect.objectContaining({
|
|
27
|
+
text: "Le champ requis Note n'a pas été rempli",
|
|
28
|
+
}),
|
|
29
|
+
);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("uses the plural message when several required columns are missing", () => {
|
|
33
|
+
expect(checkRequiredColumns(columns, {})).toBe(false);
|
|
34
|
+
expect(global.window.swal).toHaveBeenCalledWith(
|
|
35
|
+
expect.objectContaining({
|
|
36
|
+
text: "Les champs requis Nom, Note n'ont pas été remplis",
|
|
37
|
+
}),
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("treats null as unfilled and optional columns as ignorable", () => {
|
|
42
|
+
expect(
|
|
43
|
+
checkRequiredColumns(columns, { fruitName: "pomme", note: "n", acidity: null }),
|
|
44
|
+
).toBe(true);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import evalExpression from "./evalExpression";
|
|
2
|
+
|
|
3
|
+
describe("evalExpression", () => {
|
|
4
|
+
it("evaluates an arithmetic expression over the data", () => {
|
|
5
|
+
expect(evalExpression("a + b", { a: 1, b: 2 })).toBe(3);
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
it("interpolates string values", () => {
|
|
9
|
+
expect(
|
|
10
|
+
evalExpression("fruitName + ' !'", { fruitName: "pomme" }),
|
|
11
|
+
).toBe("pomme !");
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("escapes single quotes in string data", () => {
|
|
15
|
+
expect(
|
|
16
|
+
evalExpression("name.toUpperCase()", { name: "l'orange" }),
|
|
17
|
+
).toBe("L'ORANGE");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("serializes object values", () => {
|
|
21
|
+
expect(evalExpression("obj.x", { obj: { x: 42 } })).toBe(42);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("returns 'Error' for a broken expression instead of throwing", () => {
|
|
25
|
+
const consoleError = jest.spyOn(console, "error").mockImplementation();
|
|
26
|
+
expect(evalExpression("a +++", { a: 1 })).toBe("Error");
|
|
27
|
+
consoleError.mockRestore();
|
|
28
|
+
});
|
|
29
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -51,7 +51,7 @@ export default (config: Config): any => {
|
|
|
51
51
|
}),
|
|
52
52
|
createTableIfMissing: true,
|
|
53
53
|
tableName: 'galadmin_sessions'
|
|
54
|
-
}) : new SessionStore({
|
|
54
|
+
}) : new (SessionStore(session))({
|
|
55
55
|
schema: { tableName: 'galadmin_sessions' }
|
|
56
56
|
}, mysql2.createPool({
|
|
57
57
|
host: config.mysql.host,
|
|
@@ -65,6 +65,26 @@ export default (config: Config): any => {
|
|
|
65
65
|
name: 'galadmin.sid'
|
|
66
66
|
}));
|
|
67
67
|
|
|
68
|
+
// trustedAuth: the host app (e.g. behind its own SSO) says who the user is, on every request.
|
|
69
|
+
// galadmin then has no login of its own: /api/login is refused, config.auth and authRoles are unused.
|
|
70
|
+
if (config.trustedAuth) {
|
|
71
|
+
app.use(async (req, res, next) => {
|
|
72
|
+
try {
|
|
73
|
+
const user = await config.trustedAuth(req);
|
|
74
|
+
if (user) {
|
|
75
|
+
if (JSON.stringify(req.session.user) !== JSON.stringify(user)) {
|
|
76
|
+
req.session.user = user;
|
|
77
|
+
}
|
|
78
|
+
} else if (req.session.user) {
|
|
79
|
+
delete req.session.user;
|
|
80
|
+
}
|
|
81
|
+
next();
|
|
82
|
+
} catch (err) {
|
|
83
|
+
next(err);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
68
88
|
const passwordMatch = async (password: string, hash: string, useArgon2: boolean) => {
|
|
69
89
|
if (password === hash || bcrypt.compareSync(password, hash)) {
|
|
70
90
|
return true;
|
|
@@ -81,7 +101,10 @@ export default (config: Config): any => {
|
|
|
81
101
|
}
|
|
82
102
|
|
|
83
103
|
app.post('/api/login', async (req, res) => {
|
|
84
|
-
if (
|
|
104
|
+
if (config.trustedAuth) {
|
|
105
|
+
return res.send({ success: false })
|
|
106
|
+
}
|
|
107
|
+
if (config.auth && req.body.username === config.auth.username && req.body.password === config.auth.password) {
|
|
85
108
|
req.session.user = { role: "root" }
|
|
86
109
|
res.send({ success: true })
|
|
87
110
|
} else if (config.authRoles) {
|
|
@@ -126,7 +149,7 @@ export default (config: Config): any => {
|
|
|
126
149
|
|
|
127
150
|
app.use("/api", async (req, res, next) => {
|
|
128
151
|
if (req.method === "OPTIONS" || req.session.user) {
|
|
129
|
-
if (req.session?.user?.userId) {
|
|
152
|
+
if (req.session?.user?.userId && !config.trustedAuth) {
|
|
130
153
|
// we check that the user still exists
|
|
131
154
|
let stillExists = false;
|
|
132
155
|
for (const authRole of config.authRoles) {
|
package/src/types/config.ts
CHANGED
|
@@ -428,6 +428,11 @@ export type AuthRole = {
|
|
|
428
428
|
useArgon2?: boolean;
|
|
429
429
|
};
|
|
430
430
|
|
|
431
|
+
export type TrustedUser = {
|
|
432
|
+
role: string;
|
|
433
|
+
userId?: string | number;
|
|
434
|
+
};
|
|
435
|
+
|
|
431
436
|
export type Config = {
|
|
432
437
|
/** Ne pas renseigner !!! */
|
|
433
438
|
enriched?: boolean;
|
|
@@ -440,8 +445,15 @@ export type Config = {
|
|
|
440
445
|
|
|
441
446
|
useMysql2?: boolean;
|
|
442
447
|
|
|
448
|
+
/**
|
|
449
|
+
* Authentification déléguée à l'application hôte (par exemple derrière son SSO) : appelée à chaque
|
|
450
|
+
* requête, renvoie l'utilisateur (`role`, et `userId` pour la traçabilité) ou null. Quand elle est
|
|
451
|
+
* définie, galadmin n'a plus de login propre : `auth` et `authRoles` ne servent plus.
|
|
452
|
+
*/
|
|
453
|
+
trustedAuth?: (req: any) => TrustedUser | null | Promise<TrustedUser | null>;
|
|
454
|
+
|
|
443
455
|
/** Identifiants permettant d'accéder au back-office */
|
|
444
|
-
auth
|
|
456
|
+
auth?: {
|
|
445
457
|
/** Nom d'utilisateur */
|
|
446
458
|
username: string;
|
|
447
459
|
|
package/src/utils/dbManager.ts
CHANGED
|
@@ -1,132 +1,273 @@
|
|
|
1
1
|
import * as mysql from "mysql";
|
|
2
2
|
import * as mysql2 from "mysql2";
|
|
3
|
-
import * as pg from
|
|
3
|
+
import * as pg from "pg";
|
|
4
4
|
|
|
5
5
|
export function quoteIdentifier(name: string, dbType: string): string {
|
|
6
|
-
const quoteChar = dbType === "POSTGRESQL" ? '"' :
|
|
6
|
+
const quoteChar = dbType === "POSTGRESQL" ? '"' : "`";
|
|
7
7
|
return quoteChar + name + quoteChar;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
function
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
10
|
+
function endOfLiteralOrComment(query: string, start: number): number {
|
|
11
|
+
const ch = query.charAt(start);
|
|
12
|
+
const next = query.charAt(start + 1);
|
|
13
|
+
|
|
14
|
+
if (ch === '-' && next === '-') {
|
|
15
|
+
const newline = query.indexOf('\n', start);
|
|
16
|
+
return newline === -1 ? query.length : newline;
|
|
17
|
+
}
|
|
18
|
+
if (ch === '/' && next === '*') {
|
|
19
|
+
const close = query.indexOf('*/', start + 2);
|
|
20
|
+
return close === -1 ? query.length : close + 2;
|
|
21
|
+
}
|
|
22
|
+
if (ch !== "'") {
|
|
23
|
+
return -1;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const prefix = query.charAt(start - 1);
|
|
27
|
+
const backslashEscapes = (prefix === 'E' || prefix === 'e') && !/[\w$]/.test(query.charAt(start - 2));
|
|
28
|
+
|
|
29
|
+
let index = start + 1;
|
|
30
|
+
while (index < query.length) {
|
|
31
|
+
const current = query.charAt(index);
|
|
32
|
+
if (backslashEscapes && current === '\\') {
|
|
33
|
+
index += 2;
|
|
34
|
+
} else if (current === "'" && query.charAt(index + 1) === "'") {
|
|
35
|
+
index += 2;
|
|
36
|
+
} else if (current === "'") {
|
|
37
|
+
return index + 1;
|
|
27
38
|
} else {
|
|
28
|
-
|
|
29
|
-
charIndex++; // Go to next character
|
|
39
|
+
index++;
|
|
30
40
|
}
|
|
31
41
|
}
|
|
32
|
-
|
|
33
|
-
return {
|
|
34
|
-
pgQuery,
|
|
35
|
-
pgParams,
|
|
36
|
-
};
|
|
42
|
+
return query.length;
|
|
37
43
|
}
|
|
38
44
|
|
|
39
|
-
function
|
|
40
|
-
|
|
45
|
+
export function convertQuery(mysqlQuery, params) {
|
|
46
|
+
let pgQuery = "";
|
|
47
|
+
let pgParams = [];
|
|
41
48
|
|
|
42
|
-
|
|
49
|
+
let paramIndex = 1;
|
|
50
|
+
let charIndex = 0;
|
|
43
51
|
|
|
44
|
-
|
|
45
|
-
|
|
52
|
+
// Placeholders (`?` / `??`) only carry meaning OUTSIDE string literals:
|
|
53
|
+
// a literal `?` inside '…' must survive verbatim instead of being turned
|
|
54
|
+
// into a $N placeholder that would consume a parameter slot.
|
|
55
|
+
while (charIndex < mysqlQuery.length) {
|
|
56
|
+
const ch = mysqlQuery.charAt(charIndex);
|
|
57
|
+
const next = mysqlQuery.charAt(charIndex + 1);
|
|
58
|
+
const verbatimEnd = endOfLiteralOrComment(mysqlQuery, charIndex);
|
|
46
59
|
|
|
47
|
-
|
|
60
|
+
if (verbatimEnd !== -1) {
|
|
61
|
+
pgQuery += mysqlQuery.slice(charIndex, verbatimEnd);
|
|
62
|
+
charIndex = verbatimEnd;
|
|
63
|
+
} else if (ch === "?" && next === "?") {
|
|
64
|
+
// Replace "??"
|
|
65
|
+
pgQuery += params.shift();
|
|
66
|
+
charIndex += 2; // Skip two characters
|
|
67
|
+
} else if (ch === "?") {
|
|
68
|
+
// Replace "?"
|
|
69
|
+
pgQuery += `$${paramIndex++}`;
|
|
70
|
+
pgParams.push(params.shift());
|
|
71
|
+
charIndex++; // Skip one character
|
|
72
|
+
} else {
|
|
73
|
+
pgQuery += ch;
|
|
74
|
+
charIndex++; // Go to next character
|
|
75
|
+
}
|
|
76
|
+
}
|
|
48
77
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if (part.split(/['"]/).length % 2 === 0) {
|
|
55
|
-
inString = !inString;
|
|
56
|
-
}
|
|
78
|
+
return {
|
|
79
|
+
pgQuery,
|
|
80
|
+
pgParams,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
57
83
|
|
|
58
|
-
|
|
84
|
+
export function quotePostgresQuery(query) {
|
|
85
|
+
query = query.replace(/`/g, '"');
|
|
59
86
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
87
|
+
const reservedWords = [
|
|
88
|
+
"1",
|
|
89
|
+
"COUNT",
|
|
90
|
+
"*",
|
|
91
|
+
"UPDATE",
|
|
92
|
+
"SELECT",
|
|
93
|
+
"SET",
|
|
94
|
+
"=",
|
|
95
|
+
"INSERT",
|
|
96
|
+
"INTO",
|
|
97
|
+
"DELETE",
|
|
98
|
+
"FROM",
|
|
99
|
+
"WHERE",
|
|
100
|
+
"AND",
|
|
101
|
+
"OR",
|
|
102
|
+
"JOIN",
|
|
103
|
+
"ON",
|
|
104
|
+
"AS",
|
|
105
|
+
"IN",
|
|
106
|
+
"ILIKE",
|
|
107
|
+
"LIKE",
|
|
108
|
+
"EXISTS",
|
|
109
|
+
"ALL",
|
|
110
|
+
"ANY",
|
|
111
|
+
"NOT",
|
|
112
|
+
"NULL",
|
|
113
|
+
"TRUE",
|
|
114
|
+
"FALSE",
|
|
115
|
+
"GROUP",
|
|
116
|
+
"BY",
|
|
117
|
+
"ORDER",
|
|
118
|
+
"LIMIT",
|
|
119
|
+
"OFFSET",
|
|
120
|
+
"DESC",
|
|
121
|
+
"VALUES",
|
|
122
|
+
"LEFT",
|
|
123
|
+
"RIGHT",
|
|
124
|
+
"USING",
|
|
125
|
+
"CONCAT",
|
|
126
|
+
"CONCAT_WS",
|
|
127
|
+
"CASE",
|
|
128
|
+
"WHEN",
|
|
129
|
+
"THEN",
|
|
130
|
+
"ELSE",
|
|
131
|
+
"END",
|
|
132
|
+
"ASC",
|
|
133
|
+
"IS",
|
|
134
|
+
"INTERVAL",
|
|
135
|
+
"CURRENT_DATE",
|
|
136
|
+
"SUM",
|
|
137
|
+
"COALESCE",
|
|
138
|
+
"HAVING",
|
|
139
|
+
"WITH",
|
|
140
|
+
"-",
|
|
141
|
+
"ROUND",
|
|
142
|
+
"DATE_TRUNC",
|
|
143
|
+
"<>",
|
|
144
|
+
"+",
|
|
145
|
+
"DISTINCT",
|
|
146
|
+
"MAX",
|
|
147
|
+
"MIN",
|
|
148
|
+
"AVG",
|
|
149
|
+
"BIT_AND",
|
|
150
|
+
"BIT_OR",
|
|
151
|
+
"BOOL_AND",
|
|
152
|
+
"BOOL_OR",
|
|
153
|
+
"XMLAGG",
|
|
154
|
+
"STRING_AGG",
|
|
155
|
+
"EVERY",
|
|
156
|
+
"CORR",
|
|
157
|
+
"COVAR_POP",
|
|
158
|
+
"COVAR_SAMP",
|
|
159
|
+
"VAR_POP",
|
|
160
|
+
"VAR_SAMP",
|
|
161
|
+
"VARIANCE",
|
|
162
|
+
"REGR_SLOPE",
|
|
163
|
+
"REGR_INTERCEPT",
|
|
164
|
+
"REGR_COUNT",
|
|
165
|
+
"REGR_R2",
|
|
166
|
+
"REGR_AVGX",
|
|
167
|
+
"REGR_AVGY",
|
|
168
|
+
"REGR_SXX",
|
|
169
|
+
"REGR_SXY",
|
|
170
|
+
"REGR_SYY",
|
|
171
|
+
"CONFLICT",
|
|
172
|
+
"DO",
|
|
173
|
+
"EXCLUDED",
|
|
174
|
+
"RETURNING",
|
|
175
|
+
];
|
|
176
|
+
|
|
177
|
+
const wordsToAddQuotes = new Set();
|
|
178
|
+
const parts = query.split(/[ ,\(\)]/);
|
|
179
|
+
|
|
180
|
+
let inString = false;
|
|
181
|
+
|
|
182
|
+
for (let i = 0; i < parts.length; i++) {
|
|
183
|
+
const part = parts[i].trim();
|
|
184
|
+
if (!part) {
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (part.split(/['"]/).length % 2 === 0) {
|
|
188
|
+
inString = !inString;
|
|
67
189
|
}
|
|
68
190
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
191
|
+
if (
|
|
192
|
+
inString ||
|
|
193
|
+
part.startsWith("$") ||
|
|
194
|
+
part.includes("'") ||
|
|
195
|
+
part.includes('"') ||
|
|
196
|
+
reservedWords.includes(part.toUpperCase())
|
|
197
|
+
) {
|
|
198
|
+
} else {
|
|
199
|
+
for (let x of part.split(".")) {
|
|
200
|
+
if (isNaN(x)) {
|
|
201
|
+
wordsToAddQuotes.add(x);
|
|
73
202
|
}
|
|
203
|
+
}
|
|
74
204
|
}
|
|
205
|
+
}
|
|
75
206
|
|
|
76
|
-
|
|
77
|
-
|
|
207
|
+
let newQuery = query;
|
|
208
|
+
for (let word of wordsToAddQuotes) {
|
|
209
|
+
if (word !== "*") {
|
|
210
|
+
newQuery = newQuery.replace(
|
|
211
|
+
new RegExp(`(?<!["\'])\\b${word}\\b(?!["\'])`, "g"),
|
|
212
|
+
`"${word}"`,
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
78
216
|
|
|
79
|
-
|
|
217
|
+
newQuery = newQuery.replace(/\) LIKE /g, ") ILIKE ");
|
|
218
|
+
newQuery = newQuery.replace(/ LIKE /g, "::text ILIKE ");
|
|
219
|
+
|
|
220
|
+
return newQuery;
|
|
80
221
|
}
|
|
81
222
|
|
|
82
223
|
export default class dbManager {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
init(config) {
|
|
87
|
-
if (config.mysql) {
|
|
88
|
-
this._db = (config.useMysql2 ? mysql2 : mysql).createPool({
|
|
89
|
-
connectionLimit: 10,
|
|
90
|
-
host: config.mysql.host,
|
|
91
|
-
user: config.mysql.user,
|
|
92
|
-
password: config.mysql.password,
|
|
93
|
-
database: config.mysql.database,
|
|
94
|
-
port: config.mysql.port || 3306,
|
|
95
|
-
charset: 'UTF8MB4_GENERAL_CI',
|
|
96
|
-
dateStrings: true,
|
|
97
|
-
// SECURITY: multi-statement queries let any SQL-injection escalate to stacked
|
|
98
|
-
// writes / DROP. Keep OFF by default; opt in explicitly only if a config truly
|
|
99
|
-
// relies on `;`-separated statements (none of galadmin's internal queries do).
|
|
100
|
-
multipleStatements: config.mysql.multipleStatements === true
|
|
101
|
-
});
|
|
102
|
-
this.dbType = "MYSQL";
|
|
103
|
-
} else if (config.postgresql) {
|
|
104
|
-
this._db = new pg.Pool({
|
|
105
|
-
host: config.postgresql.host,
|
|
106
|
-
user: config.postgresql.user,
|
|
107
|
-
password: config.postgresql.password,
|
|
108
|
-
database: config.postgresql.database,
|
|
109
|
-
port: config.postgresql.port || 5432,
|
|
110
|
-
ssl: config.postgresql.ssl
|
|
111
|
-
});
|
|
112
|
-
this.dbType = "POSTGRESQL";
|
|
113
|
-
}
|
|
114
|
-
}
|
|
224
|
+
_db;
|
|
225
|
+
dbType;
|
|
115
226
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
227
|
+
init(config) {
|
|
228
|
+
if (config.mysql) {
|
|
229
|
+
this._db = (config.useMysql2 ? mysql2 : mysql).createPool({
|
|
230
|
+
connectionLimit: 10,
|
|
231
|
+
host: config.mysql.host,
|
|
232
|
+
user: config.mysql.user,
|
|
233
|
+
password: config.mysql.password,
|
|
234
|
+
database: config.mysql.database,
|
|
235
|
+
port: config.mysql.port || 3306,
|
|
236
|
+
charset: "UTF8MB4_GENERAL_CI",
|
|
237
|
+
dateStrings: true,
|
|
238
|
+
// SECURITY: multi-statement queries let any SQL-injection escalate to stacked
|
|
239
|
+
// writes / DROP. Keep OFF by default; opt in explicitly only if a config truly
|
|
240
|
+
// relies on `;`-separated statements (none of galadmin's internal queries do).
|
|
241
|
+
multipleStatements: config.mysql.multipleStatements === true,
|
|
242
|
+
});
|
|
243
|
+
this.dbType = "MYSQL";
|
|
244
|
+
} else if (config.postgresql) {
|
|
245
|
+
this._db = new pg.Pool({
|
|
246
|
+
host: config.postgresql.host,
|
|
247
|
+
user: config.postgresql.user,
|
|
248
|
+
password: config.postgresql.password,
|
|
249
|
+
database: config.postgresql.database,
|
|
250
|
+
port: config.postgresql.port || 5432,
|
|
251
|
+
ssl: config.postgresql.ssl,
|
|
252
|
+
});
|
|
253
|
+
this.dbType = "POSTGRESQL";
|
|
131
254
|
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
async query(queryString, args = []): Promise<any> {
|
|
258
|
+
return new Promise((resolve, reject) => {
|
|
259
|
+
if (this.dbType === "POSTGRESQL") {
|
|
260
|
+
const result = convertQuery(queryString, args);
|
|
261
|
+
queryString = quotePostgresQuery(result.pgQuery);
|
|
262
|
+
args = result.pgParams;
|
|
263
|
+
}
|
|
264
|
+
this._db.query(queryString, args, (a, b, c) => {
|
|
265
|
+
if (this.dbType === "MYSQL") {
|
|
266
|
+
resolve({ err: a, rows: b, fields: c });
|
|
267
|
+
} else if (this.dbType === "POSTGRESQL") {
|
|
268
|
+
resolve({ err: a, rows: b?.rows, fields: b?.fields });
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
}
|
|
132
273
|
}
|