mirror of
https://github.com/UNITRONIX/BetterDesk.git
synced 2026-11-24 01:52:12 +00:00
e11f7865c6
Refs #513 A user denied by a panel section policy got Express's bare "Internal Server Error" instead of a 403 page, so the account, the login and the server all looked broken at once. Two independent causes, both fixed: - middleware order: panelAccessGuard renders errors/403 on a denial, but initI18n() was mounted after it, so res.locals.lang, appName and _() did not exist yet and the template threw - the error templates used lang, appName and _() unguarded, so the subsequent 500.ejs render threw the same way and Express fell back to its default page initI18n() now mounts before loadEffectivePermissions / loadEffectivePolicy / panelAccessGuard, which is safe because it still runs after cookieParser() and the session middleware. The three error templates compute defensive locals, so they render with no locals at all; 403.ejs falls back to the title/message the guard already passes, and 500.ejs still reveals details only under NODE_ENV=development. Covered by tests/errorTemplates.render.test.js, which renders each template with ejs.render(src, {}) so an unguarded local cannot come back. Reported-by: @mh254 Thanks: INSOLVE (Honorary); Marco Jakobs (@jacotec); MyNameisStitch (@MyNameisStitch); Redspin (@playerumpknow); muopc (@muopc)
87 lines
3.2 KiB
JavaScript
87 lines
3.2 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Error templates must render with no locals at all.
|
|
*
|
|
* Issue #513: panelAccessGuard renders errors/403 before initI18n() has run,
|
|
* so `lang`, `appName` and `_()` were undefined and the 403 page threw. The
|
|
* throw was then handled by the 500 handler, which threw the same way, and the
|
|
* browser got Express's bare "Internal Server Error".
|
|
*
|
|
* These tests render each template with an empty locals object so any
|
|
* reintroduced unguarded variable fails here instead of in production.
|
|
*/
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const ejs = require('ejs');
|
|
|
|
const VIEWS = path.join(__dirname, '..', 'views', 'errors');
|
|
|
|
function render(template, locals = {}) {
|
|
const source = fs.readFileSync(path.join(VIEWS, template), 'utf8');
|
|
return ejs.render(source, locals, { filename: path.join(VIEWS, template) });
|
|
}
|
|
|
|
describe('error templates render without i18n/branding locals', () => {
|
|
test.each(['403.ejs', '404.ejs', '500.ejs'])('%s renders with empty locals', (template) => {
|
|
const html = render(template);
|
|
expect(html).toContain('<html lang="en">');
|
|
expect(html).toContain('BetterDesk');
|
|
expect(html.length).toBeGreaterThan(300);
|
|
});
|
|
|
|
test.each([
|
|
['403.ejs', '403'],
|
|
['404.ejs', '404'],
|
|
['500.ejs', '500'],
|
|
])('%s keeps its status code visible', (template, code) => {
|
|
expect(render(template)).toContain(`error-code">${code}<`);
|
|
});
|
|
|
|
test('403 uses the title/message passed by panelAccessGuard', () => {
|
|
const html = render('403.ejs', {
|
|
title: 'Forbidden',
|
|
message: 'You do not have permission to access this panel section',
|
|
});
|
|
// With no _() available the English fallback keeps the server-provided
|
|
// message instead of a generic one.
|
|
expect(html).toContain('Forbidden');
|
|
});
|
|
|
|
test('403 prefers translations when an i18n helper is available', () => {
|
|
const html = render('403.ejs', {
|
|
lang: 'de',
|
|
appName: 'BetterDesk DE',
|
|
_: (key, fallback) => {
|
|
const dict = {
|
|
'errors.forbidden_title': 'Zugriff verweigert',
|
|
'errors.forbidden_message': 'Kein Zugriff',
|
|
'errors.go_home': 'Zur Startseite',
|
|
};
|
|
return dict[key] || fallback || key;
|
|
},
|
|
});
|
|
expect(html).toContain('<html lang="de">');
|
|
expect(html).toContain('BetterDesk DE');
|
|
expect(html).toContain('Zugriff verweigert');
|
|
});
|
|
|
|
test('500 survives a missing, null or string error local', () => {
|
|
for (const locals of [{}, { error: null }, { error: 'boom' }, { error: new Error('boom') }]) {
|
|
expect(() => render('500.ejs', locals)).not.toThrow();
|
|
}
|
|
});
|
|
|
|
test('500 does not leak error details outside development', () => {
|
|
const previous = process.env.NODE_ENV;
|
|
process.env.NODE_ENV = 'production';
|
|
try {
|
|
const html = render('500.ejs', { error: new Error('secret stack detail') });
|
|
expect(html).not.toContain('secret stack detail');
|
|
} finally {
|
|
process.env.NODE_ENV = previous;
|
|
}
|
|
});
|
|
});
|