From 641cd614c8387f8a2cb25c0f36b28f02491ec8c2 Mon Sep 17 00:00:00 2001 From: kingbri Date: Sat, 2 Dec 2023 01:41:00 -0500 Subject: [PATCH 1/2] Server: Fix CORS proxy with POST requests and status codes POST requests always sent an undefined body without the bodyParser middleware which isn't included with express by default. Add this as a new dependency and inject it into the express app. This middleware will also be useful for any other middleware that we might create. Also properly send status codes and messages to the client so they can be handled properly. Signed-off-by: kingbri --- package-lock.json | 62 +++++++++++++++++++++++++++++++++++++++++------ package.json | 1 + server.js | 7 +++++- 3 files changed, 61 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 459e0da18..f012118d5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,6 +14,7 @@ "@agnai/web-tokenizers": "^0.1.3", "@dqbd/tiktoken": "^1.0.2", "bing-translate-api": "^2.9.1", + "body-parser": "^1.20.2", "command-exists": "^1.2.9", "compression": "^1", "cookie-parser": "^1.4.6", @@ -990,12 +991,12 @@ "integrity": "sha512-vHdS19CnY3hwiNdkaqk93DvjVLfbEcI8mys4UjuWrlX1haDmroo8o4xCzh4wD6DGV6HxRCyauwhHRqMTfERtjw==" }, "node_modules/body-parser": { - "version": "1.20.1", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", - "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "version": "1.20.2", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", + "integrity": "sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==", "dependencies": { "bytes": "3.1.2", - "content-type": "~1.0.4", + "content-type": "~1.0.5", "debug": "2.6.9", "depd": "2.0.0", "destroy": "1.2.0", @@ -1003,7 +1004,7 @@ "iconv-lite": "0.4.24", "on-finished": "2.4.1", "qs": "6.11.0", - "raw-body": "2.5.1", + "raw-body": "2.5.2", "type-is": "~1.6.18", "unpipe": "1.0.0" }, @@ -1742,6 +1743,37 @@ "node": ">= 0.10.0" } }, + "node_modules/express/node_modules/body-parser": { + "version": "1.20.1", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", + "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "dependencies": { + "bytes": "3.1.2", + "content-type": "~1.0.4", + "debug": "2.6.9", + "depd": "2.0.0", + "destroy": "1.2.0", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "on-finished": "2.4.1", + "qs": "6.11.0", + "raw-body": "2.5.1", + "type-is": "~1.6.18", + "unpipe": "1.0.0" + }, + "engines": { + "node": ">= 0.8", + "npm": "1.2.8000 || >= 1.4.16" + } + }, + "node_modules/express/node_modules/bytes": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", + "integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==", + "engines": { + "node": ">= 0.8" + } + }, "node_modules/express/node_modules/cookie": { "version": "0.5.0", "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz", @@ -1750,6 +1782,20 @@ "node": ">= 0.6" } }, + "node_modules/express/node_modules/raw-body": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", + "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "dependencies": { + "bytes": "3.1.2", + "http-errors": "2.0.0", + "iconv-lite": "0.4.24", + "unpipe": "1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/express/node_modules/safe-buffer": { "version": "5.2.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", @@ -3520,9 +3566,9 @@ } }, "node_modules/raw-body": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", - "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.2.tgz", + "integrity": "sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==", "dependencies": { "bytes": "3.1.2", "http-errors": "2.0.0", diff --git a/package.json b/package.json index 057d1d339..63f3094ab 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "@agnai/web-tokenizers": "^0.1.3", "@dqbd/tiktoken": "^1.0.2", "bing-translate-api": "^2.9.1", + "body-parser": "^1.20.2", "command-exists": "^1.2.9", "compression": "^1", "cookie-parser": "^1.4.6", diff --git a/server.js b/server.js index f2ff69142..c00e283a9 100644 --- a/server.js +++ b/server.js @@ -25,6 +25,7 @@ const compression = require('compression'); const cookieParser = require('cookie-parser'); const multer = require("multer"); const responseTime = require('response-time'); +const bodyParser = require('body-parser'); // net related library imports const net = require("net"); @@ -106,6 +107,7 @@ process.chdir(directory); const app = express(); app.use(compression()); app.use(responseTime()); +app.use(bodyParser.json()); // impoort from statsHelpers.js @@ -336,7 +338,10 @@ if (getConfigValue('enableCorsProxy', false) === true || cliArguments.corsProxy body: bodyMethods.includes(req.method) ? JSON.stringify(req.body) : undefined, }); - response.body.pipe(res); // pipe the response to the proxy response + // Copy over relevant response params to the proxy response + res.statusCode = response.status; + res.statusMessage = response.statusText; + response.body.pipe(res); } catch (error) { res.status(500).send('Error occurred while trying to proxy to: ' + url + ' ' + error); From a1754c7192ed3ceab97e1161a3def78f4dac1d3d Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sat, 2 Dec 2023 15:32:09 +0200 Subject: [PATCH 2/2] Update server.js --- server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index c00e283a9..66a633130 100644 --- a/server.js +++ b/server.js @@ -25,7 +25,6 @@ const compression = require('compression'); const cookieParser = require('cookie-parser'); const multer = require("multer"); const responseTime = require('response-time'); -const bodyParser = require('body-parser'); // net related library imports const net = require("net"); @@ -107,7 +106,6 @@ process.chdir(directory); const app = express(); app.use(compression()); app.use(responseTime()); -app.use(bodyParser.json()); // impoort from statsHelpers.js @@ -308,6 +306,8 @@ app.use(function (req, res, next) { }); if (getConfigValue('enableCorsProxy', false) === true || cliArguments.corsProxy === true) { + const bodyParser = require('body-parser'); + app.use(bodyParser.json()); console.log('Enabling CORS proxy'); app.use('/proxy/:url(*)', async (req, res) => {