defer 403 HTML to file

928487985d56043a1db3bb54d41657db2966c1b3

Spappz <34202141+Spappz@users.noreply.github.com>

Signed
3 files changed, +24 -13Showing whitespace changes
default/config.yaml+0 -2
@@ -31,8 +31,6 @@ enableForwardedWhitelist: true
31whitelist:31whitelist:
32 - ::132 - ::1
33 - 127.0.0.133 - 127.0.0.1
34# HTML displayed when a connection is blocked. Use "{{ipDetails}}" to print the client's IP.
35whitelistErrorMessage: "<h1>Forbidden</h1><p>If you are the system administrator, add your IP address to the whitelist or disable whitelist mode by editing <code>config.yaml</code> in the root directory of your installation.</p><hr /><p><em>Connection from {{ipDetails}} has been blocked. This attempt has been logged.</em></p>"
36# Toggle basic authentication for endpoints34# Toggle basic authentication for endpoints
37basicAuthMode: false35basicAuthMode: false
38# Basic authentication credentials36# Basic authentication credentials
default/public/error/forbidden-by-whitelist.html+22 -0
@@ -0,0 +1,22 @@
1<!DOCTYPE html>
2<html>
3
4<head>
5 <title>Forbidden</title>
6</head>
7
8<body>
9 <h1>Forbidden</h1>
10 <p>
11 If you are the system administrator, add your IP address to the
12 whitelist or disable whitelist mode by editing
13 <code>config.yaml</code> in the root directory of your installation.
14 </p>
15 <hr />
16 <p>
17 <em>Connection from {{ipDetails}} has been blocked. This attempt
18 has been logged.</em>
19 </p>
20</body>
21
22</html>
src/middleware/whitelist.js+2 -11
@@ -11,16 +11,7 @@ const whitelistPath = path.join(process.cwd(), './whitelist.txt');
11const enableForwardedWhitelist = getConfigValue('enableForwardedWhitelist', false);11const enableForwardedWhitelist = getConfigValue('enableForwardedWhitelist', false);
12let whitelist = getConfigValue('whitelist', []);12let whitelist = getConfigValue('whitelist', []);
13let knownIPs = new Set();13let knownIPs = new Set();
1414const forbiddenWebpage = Handlebars.compile(fs.readFileSync('./public/error/forbidden-by-whitelist.html', 'utf-8'));
15const DEFAULT_WHITELIST_ERROR_MESSAGE =
16 '<h1>Forbidden</h1><p>If you are the system administrator, add your IP address to the whitelist or disable whitelist mode by editing <code>config.yaml</code> in the root directory of your installation.</p><hr /><p><em>Connection from {{ipDetails}} has been blocked. This attempt has been logged.</em></p>';
17
18const errorMessage = Handlebars.compile(
19 getConfigValue(
20 'whitelistErrorMessage',
21 DEFAULT_WHITELIST_ERROR_MESSAGE,
22 ),
23);
2415
25if (fs.existsSync(whitelistPath)) {16if (fs.existsSync(whitelistPath)) {
26 try {17 try {
@@ -95,7 +86,7 @@ export default function whitelistMiddleware(whitelistMode, listen) {
95 `Blocked connection from ${clientIp}; User Agent: ${userAgent}\n\tTo allow this connection, add its IP address to the whitelist or disable whitelist mode by editing config.yaml in the root directory of your SillyTavern installation.\n`,86 `Blocked connection from ${clientIp}; User Agent: ${userAgent}\n\tTo allow this connection, add its IP address to the whitelist or disable whitelist mode by editing config.yaml in the root directory of your SillyTavern installation.\n`,
96 ),87 ),
97 );88 );
98 return res.status(403).send(errorMessage({ ipDetails }));89 return res.status(403).send(forbiddenWebpage({ ipDetails }));
99 }90 }
100 next();91 next();
101 };92 };