mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Fix order of middleware application
Previously: CSRF => auth => whitelist Now: auth => whitelist => CSRF
This commit is contained in:
68
server.js
68
server.js
@ -186,40 +186,6 @@ function setAdditionalHeaders(request, args, server) {
|
|||||||
const SETTINGS_FILE = './public/settings.json';
|
const SETTINGS_FILE = './public/settings.json';
|
||||||
const { DIRECTORIES, UPLOADS_PATH, PALM_SAFETY, TEXTGEN_TYPES, CHAT_COMPLETION_SOURCES, AVATAR_WIDTH, AVATAR_HEIGHT } = require('./src/constants');
|
const { DIRECTORIES, UPLOADS_PATH, PALM_SAFETY, TEXTGEN_TYPES, CHAT_COMPLETION_SOURCES, AVATAR_WIDTH, AVATAR_HEIGHT } = require('./src/constants');
|
||||||
|
|
||||||
// CSRF Protection //
|
|
||||||
if (!cliArguments.disableCsrf) {
|
|
||||||
const CSRF_SECRET = crypto.randomBytes(8).toString('hex');
|
|
||||||
const COOKIES_SECRET = crypto.randomBytes(8).toString('hex');
|
|
||||||
|
|
||||||
const { generateToken, doubleCsrfProtection } = doubleCsrf({
|
|
||||||
getSecret: () => CSRF_SECRET,
|
|
||||||
cookieName: 'X-CSRF-Token',
|
|
||||||
cookieOptions: {
|
|
||||||
httpOnly: true,
|
|
||||||
sameSite: 'strict',
|
|
||||||
secure: false,
|
|
||||||
},
|
|
||||||
size: 64,
|
|
||||||
getTokenFromRequest: (req) => req.headers['x-csrf-token'],
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get('/csrf-token', (req, res) => {
|
|
||||||
res.json({
|
|
||||||
'token': generateToken(res, req),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
app.use(cookieParser(COOKIES_SECRET));
|
|
||||||
app.use(doubleCsrfProtection);
|
|
||||||
} else {
|
|
||||||
console.warn('\nCSRF protection is disabled. This will make your server vulnerable to CSRF attacks.\n');
|
|
||||||
app.get('/csrf-token', (req, res) => {
|
|
||||||
res.json({
|
|
||||||
'token': 'disabled',
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// CORS Settings //
|
// CORS Settings //
|
||||||
const CORS = cors({
|
const CORS = cors({
|
||||||
origin: 'null',
|
origin: 'null',
|
||||||
@ -273,6 +239,40 @@ app.use(function (req, res, next) {
|
|||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// CSRF Protection //
|
||||||
|
if (!cliArguments.disableCsrf) {
|
||||||
|
const CSRF_SECRET = crypto.randomBytes(8).toString('hex');
|
||||||
|
const COOKIES_SECRET = crypto.randomBytes(8).toString('hex');
|
||||||
|
|
||||||
|
const { generateToken, doubleCsrfProtection } = doubleCsrf({
|
||||||
|
getSecret: () => CSRF_SECRET,
|
||||||
|
cookieName: 'X-CSRF-Token',
|
||||||
|
cookieOptions: {
|
||||||
|
httpOnly: true,
|
||||||
|
sameSite: 'strict',
|
||||||
|
secure: false,
|
||||||
|
},
|
||||||
|
size: 64,
|
||||||
|
getTokenFromRequest: (req) => req.headers['x-csrf-token'],
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get('/csrf-token', (req, res) => {
|
||||||
|
res.json({
|
||||||
|
'token': generateToken(res, req),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
app.use(cookieParser(COOKIES_SECRET));
|
||||||
|
app.use(doubleCsrfProtection);
|
||||||
|
} else {
|
||||||
|
console.warn('\nCSRF protection is disabled. This will make your server vulnerable to CSRF attacks.\n');
|
||||||
|
app.get('/csrf-token', (req, res) => {
|
||||||
|
res.json({
|
||||||
|
'token': 'disabled',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (getConfigValue('enableCorsProxy', false) || cliArguments.corsProxy) {
|
if (getConfigValue('enableCorsProxy', false) || cliArguments.corsProxy) {
|
||||||
const bodyParser = require('body-parser');
|
const bodyParser = require('body-parser');
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
|
Reference in New Issue
Block a user