Skip to content

Commit 3eaca7c

Browse files
authored
Merge pull request #1327 from vinodkiran/BUGFIX/XSS
Bugfix/xss
2 parents 1989007 + c06c25b commit 3eaca7c

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

packages/components/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"@upstash/redis": "^1.22.1",
3737
"@zilliz/milvus2-sdk-node": "^2.2.24",
3838
"apify-client": "^2.7.1",
39-
"axios": "^0.27.2",
39+
"axios": "1.6.2",
4040
"cheerio": "^1.0.0-rc.12",
4141
"chromadb": "^1.5.11",
4242
"cohere-ai": "^6.2.0",

packages/server/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"dependencies": {
4848
"@oclif/core": "^1.13.10",
4949
"async-mutex": "^0.4.0",
50-
"axios": "^0.27.2",
50+
"axios": "1.6.2",
5151
"cors": "^2.8.5",
5252
"crypto-js": "^4.1.1",
5353
"dotenv": "^16.0.0",
@@ -61,6 +61,7 @@
6161
"mysql": "^2.18.1",
6262
"pg": "^8.11.1",
6363
"reflect-metadata": "^0.1.13",
64+
"sanitize-html": "^2.11.0",
6465
"socket.io": "^4.6.1",
6566
"sqlite3": "^5.1.6",
6667
"typeorm": "^0.3.6",
@@ -71,6 +72,7 @@
7172
"@types/cors": "^2.8.12",
7273
"@types/crypto-js": "^4.1.1",
7374
"@types/multer": "^1.4.7",
75+
"@types/sanitize-html": "^2.9.5",
7476
"concurrently": "^7.1.0",
7577
"nodemon": "^2.0.15",
7678
"oclif": "^3",

packages/server/src/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import { CachePool } from './CachePool'
5858
import { ICommonObject, IMessage, INodeOptionsValue } from 'flowise-components'
5959
import { createRateLimiter, getRateLimiter, initializeRateLimiter } from './utils/rateLimit'
6060
import { addAPIKey, compareKeys, deleteAPIKey, getApiKey, getAPIKeys, updateAPIKey } from './utils/apiKey'
61+
import { sanitizeMiddleware } from './utils/XSS'
6162
import axios from 'axios'
6263
import { Client } from 'langchainhub'
6364
import { parsePrompt } from './utils/hub'
@@ -118,9 +119,15 @@ export class App {
118119
// Allow access from *
119120
this.app.use(cors())
120121

122+
// Switch off the default 'X-Powered-By: Express' header
123+
this.app.disable('x-powered-by')
124+
121125
// Add the expressRequestLogger middleware to log all requests
122126
this.app.use(expressRequestLogger)
123127

128+
// Add the sanitizeMiddleware to guard against XSS
129+
this.app.use(sanitizeMiddleware)
130+
124131
if (process.env.FLOWISE_USERNAME && process.env.FLOWISE_PASSWORD) {
125132
const username = process.env.FLOWISE_USERNAME
126133
const password = process.env.FLOWISE_PASSWORD
@@ -967,6 +974,12 @@ export class App {
967974
// Download file from assistant
968975
this.app.post('/api/v1/openai-assistants-file', async (req: Request, res: Response) => {
969976
const filePath = path.join(getUserHome(), '.flowise', 'openai-assistant', req.body.fileName)
977+
//raise error if file path is not absolute
978+
if (!path.isAbsolute(filePath)) return res.status(500).send(`Invalid file path`)
979+
//raise error if file path contains '..'
980+
if (filePath.includes('..')) return res.status(500).send(`Invalid file path`)
981+
//only return from the .flowise openai-assistant folder
982+
if (!(filePath.includes('.flowise') && filePath.includes('openai-assistant'))) return res.status(500).send(`Invalid file path`)
970983
res.setHeader('Content-Disposition', 'attachment; filename=' + path.basename(filePath))
971984
const fileStream = fs.createReadStream(filePath)
972985
fileStream.pipe(res)

packages/server/src/utils/XSS.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Request, Response, NextFunction } from 'express'
2+
import sanitizeHtml from 'sanitize-html'
3+
4+
export function sanitizeMiddleware(req: Request, res: Response, next: NextFunction): void {
5+
// decoding is necessary as the url is encoded by the browser
6+
const decodedURI = decodeURI(req.url)
7+
req.url = sanitizeHtml(decodedURI)
8+
for (let p in req.query) {
9+
req.query[p] = sanitizeHtml(req.query[p] as string)
10+
}
11+
12+
next()
13+
}

0 commit comments

Comments
 (0)