Previously, the user's session cookie was being checked against the
database for all non-static requests. However, this is not really
needed and wastes resources (and is slow).
For most page views (e.g. index, channel page), just parsing the value
of the cookie is sufficient:
* The cookies are already HMAC signed, so tampering with them ought to
be for all reasonable purposes, impossible.
* Assuming the worst case, all a nefarious user could manage to do is
change the text of the "Welcome, {user}" and cause a (non-functional)
ACP link to appear clientside, both of which are already possible by
using the Inspect Element tool.
For authenticated pages (currently, the ACP, and anything under
/account/), the full database check is still performed (for now).
67 lines
1.9 KiB
JavaScript
67 lines
1.9 KiB
JavaScript
var pug = require("pug");
|
|
var fs = require("fs");
|
|
var path = require("path");
|
|
var Config = require("../config");
|
|
var templates = path.join(__dirname, "..", "..", "templates");
|
|
var cache = {};
|
|
|
|
/**
|
|
* Merges locals with globals for pug rendering
|
|
*/
|
|
function merge(locals, res) {
|
|
var _locals = {
|
|
siteTitle: Config.get("html-template.title"),
|
|
siteDescription: Config.get("html-template.description"),
|
|
siteAuthor: "Calvin 'calzoneman' 'cyzon' Montgomery",
|
|
loginDomain: Config.get("https.enabled") ? Config.get("https.full-address")
|
|
: Config.get("http.full-address"),
|
|
csrfToken: typeof res.req.csrfToken === 'function' ? res.req.csrfToken() : '',
|
|
baseUrl: getBaseUrl(res),
|
|
channelPath: Config.get("channel-path"),
|
|
};
|
|
if (typeof locals !== "object") {
|
|
return _locals;
|
|
}
|
|
for (var key in locals) {
|
|
_locals[key] = locals[key];
|
|
}
|
|
return _locals;
|
|
}
|
|
|
|
function getBaseUrl(res) {
|
|
var req = res.req;
|
|
return req.realProtocol + "://" + req.header("host");
|
|
}
|
|
|
|
/**
|
|
* Renders and serves a pug template
|
|
*/
|
|
function sendPug(res, view, locals) {
|
|
if (!locals) {
|
|
locals = {};
|
|
}
|
|
locals.loggedIn = nvl(locals.loggedIn, res.locals.loggedIn);
|
|
locals.loginName = nvl(locals.loginName, res.locals.loginName);
|
|
locals.superadmin = nvl(locals.superadmin, res.locals.superadmin);
|
|
|
|
if (!(view in cache) || Config.get("debug")) {
|
|
var file = path.join(templates, view + ".pug");
|
|
var fn = pug.compile(fs.readFileSync(file), {
|
|
filename: file,
|
|
pretty: !Config.get("http.minify")
|
|
});
|
|
cache[view] = fn;
|
|
}
|
|
var html = cache[view](merge(locals, res));
|
|
res.send(html);
|
|
}
|
|
|
|
function nvl(a, b) {
|
|
if (typeof a === 'undefined') return b;
|
|
return a;
|
|
}
|
|
|
|
module.exports = {
|
|
sendPug: sendPug
|
|
};
|