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
3131whitelist:
3232 - ::1
3333 - 127.0.0.1
34-# HTML displayed when a connection is blocked. Use "{{ipDetails}}" to print the client's IP.
35-whitelistErrorMessage: "<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>"
3634# Toggle basic authentication for endpoints
3735basicAuthMode: false
3836# 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');
1111const enableForwardedWhitelist = getConfigValue('enableForwardedWhitelist', false);
1212let whitelist = getConfigValue('whitelist', []);
1313let knownIPs = new Set();
14-
14+const forbiddenWebpage = Handlebars.compile(fs.readFileSync('./public/error/forbidden-by-whitelist.html', 'utf-8'));
15-const 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-
18-const errorMessage = Handlebars.compile(
19- getConfigValue(
20- 'whitelistErrorMessage',
21- DEFAULT_WHITELIST_ERROR_MESSAGE,
22- ),
23-);
2415
2516if (fs.existsSync(whitelistPath)) {
2617 try {
@@ -95,7 +86,7 @@ export default function whitelistMiddleware(whitelistMode, listen) {
9586 `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`,
9687 ),
9788 );
9889 return res.status(403).send(errorMessageforbiddenWebpage({ ipDetails }));
9990 }
10091 next();
10192 };