From 1c13a759707f4d43bfa6bbc352f2bb8fa23ef98d Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Fri, 13 Oct 2023 02:50:10 +0800 Subject: [PATCH] Fix #3868 postgres monitor could possibly crash Uptime Kuma (#3880) * Bump pg * Handle uncaughtException * Fix parsing issue of postgres connection and fix the query example --- package-lock.json | 36 +++++++++++++++++++++++++----------- package.json | 5 +++-- server/server.js | 6 ++++-- server/util-server.js | 21 +++++++++++++++++---- src/pages/EditMonitor.vue | 2 +- 5 files changed, 50 insertions(+), 20 deletions(-) diff --git a/package-lock.json b/package-lock.json index 973e95835..6ba2b32ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -57,8 +57,8 @@ "notp": "~2.0.3", "openid-client": "^5.4.2", "password-hash": "~1.2.2", - "pg": "~8.8.0", - "pg-connection-string": "~2.5.0", + "pg": "~8.11.3", + "pg-connection-string": "~2.6.2", "playwright-core": "~1.35.1", "prom-client": "~13.2.0", "prometheus-api-metrics": "~3.2.1", @@ -13392,6 +13392,11 @@ "node": "^12.20.0 || >=14" } }, + "node_modules/knex/node_modules/pg-connection-string": { + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.5.0.tgz", + "integrity": "sha512-r5o/V/ORTA6TmUnyWZR9nCj1klXCO2CEKNRlVuJptZe85QuhFayC7WeMic7ndayT5IRIR0S0xFxFi2ousartlQ==" + }, "node_modules/knex/node_modules/resolve-from": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-5.0.0.tgz", @@ -15221,21 +15226,24 @@ "dev": true }, "node_modules/pg": { - "version": "8.8.0", - "resolved": "https://registry.npmjs.org/pg/-/pg-8.8.0.tgz", - "integrity": "sha512-UXYN0ziKj+AeNNP7VDMwrehpACThH7LUl/p8TDFpEUuSejCUIwGSfxpHsPvtM6/WXFy6SU4E5RG4IJV/TZAGjw==", + "version": "8.11.3", + "resolved": "https://registry.npmjs.org/pg/-/pg-8.11.3.tgz", + "integrity": "sha512-+9iuvG8QfaaUrrph+kpF24cXkH1YOOUeArRNYIxq1viYHZagBxrTno7cecY1Fa44tJeZvaoG+Djpkc3JwehN5g==", "dependencies": { "buffer-writer": "2.0.0", "packet-reader": "1.0.0", - "pg-connection-string": "^2.5.0", - "pg-pool": "^3.5.2", - "pg-protocol": "^1.5.0", + "pg-connection-string": "^2.6.2", + "pg-pool": "^3.6.1", + "pg-protocol": "^1.6.0", "pg-types": "^2.1.0", "pgpass": "1.x" }, "engines": { "node": ">= 8.0.0" }, + "optionalDependencies": { + "pg-cloudflare": "^1.1.1" + }, "peerDependencies": { "pg-native": ">=3.0.1" }, @@ -15245,10 +15253,16 @@ } } }, + "node_modules/pg-cloudflare": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/pg-cloudflare/-/pg-cloudflare-1.1.1.tgz", + "integrity": "sha512-xWPagP/4B6BgFO+EKz3JONXv3YDgvkbVrGw2mTo3D6tVDQRh1e7cqVGvyR3BE+eQgAvx1XhW/iEASj4/jCWl3Q==", + "optional": true + }, "node_modules/pg-connection-string": { - "version": "2.5.0", - "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.5.0.tgz", - "integrity": "sha512-r5o/V/ORTA6TmUnyWZR9nCj1klXCO2CEKNRlVuJptZe85QuhFayC7WeMic7ndayT5IRIR0S0xFxFi2ousartlQ==" + "version": "2.6.2", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.6.2.tgz", + "integrity": "sha512-ch6OwaeaPYcova4kKZ15sbJ2hKb/VP48ZD2gE7i1J+L4MspCtBMAx8nMgz7bksc7IojCIIWuEhHibSMFH8m8oA==" }, "node_modules/pg-int8": { "version": "1.0.1", diff --git a/package.json b/package.json index bc95de954..21f01fe39 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "simple-dns-server": "node extra/simple-dns-server.js", "simple-mqtt-server": "node extra/simple-mqtt-server.js", "simple-mongo": "docker run --rm -p 27017:27017 mongo", + "simple-postgres": "docker run --rm -p 5432:5432 -e POSTGRES_PASSWORD=postgres postgres", "update-language-files": "cd extra/update-language-files && node index.js && cross-env-shell eslint ../../src/languages/$npm_config_language.js --fix", "ncu-patch": "npm-check-updates -u -t patch", "release-final": "node ./extra/test-docker.js && node extra/update-version.js && npm run build-docker && node ./extra/press-any-key.js && npm run upload-artifacts && node ./extra/update-wiki-version.js", @@ -121,8 +122,8 @@ "notp": "~2.0.3", "openid-client": "^5.4.2", "password-hash": "~1.2.2", - "pg": "~8.8.0", - "pg-connection-string": "~2.5.0", + "pg": "~8.11.3", + "pg-connection-string": "~2.6.2", "playwright-core": "~1.35.1", "prom-client": "~13.2.0", "prometheus-api-metrics": "~3.2.1", diff --git a/server/server.js b/server/server.js index c8f826b02..e8132cb90 100644 --- a/server/server.js +++ b/server/server.js @@ -1887,8 +1887,10 @@ gracefulShutdown(server.httpServer, { }); // Catch unexpected errors here -process.addListener("unhandledRejection", (error, promise) => { +let unexpectedErrorHandler = (error, promise) => { console.trace(error); UptimeKumaServer.errorLog(error, false); console.error("If you keep encountering errors, please report to https://github.com/louislam/uptime-kuma/issues"); -}); +}; +process.addListener("unhandledRejection", unexpectedErrorHandler); +process.addListener("uncaughtException", unexpectedErrorHandler); diff --git a/server/util-server.js b/server/util-server.js index 0cb63ad00..9e4946e60 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -395,6 +395,9 @@ exports.mssqlQuery = async function (connectionString, query) { try { pool = new mssql.ConnectionPool(connectionString); await pool.connect(); + if (!query) { + query = "SELECT 1"; + } await pool.request().query(query); pool.close(); } catch (e) { @@ -415,12 +418,22 @@ exports.postgresQuery = function (connectionString, query) { return new Promise((resolve, reject) => { const config = postgresConParse(connectionString); - if (config.password === "") { - // See https://github.com/brianc/node-postgres/issues/1927 - return reject(new Error("Password is undefined.")); + // Fix #3868, which true/false is not parsed to boolean + if (typeof config.ssl === "string") { + config.ssl = config.ssl === "true"; } - const client = new Client({ connectionString }); + if (config.password === "") { + // See https://github.com/brianc/node-postgres/issues/1927 + reject(new Error("Password is undefined.")); + return; + } + const client = new Client(config); + + client.on("error", (error) => { + log.debug("postgres", "Error caught in the error event handler."); + reject(error); + }); client.connect((err) => { if (err) { diff --git a/src/pages/EditMonitor.vue b/src/pages/EditMonitor.vue index 7a90b5752..1f64172d9 100644 --- a/src/pages/EditMonitor.vue +++ b/src/pages/EditMonitor.vue @@ -374,7 +374,7 @@