Kraken.js + Lusca integration: declarative CSRF config and session middleware ordering
0 reputation · 18 Feb 2021, 06:58 UTC
0 reputation · 18 Feb 2021, 06:58 UTC
Kraken.js wires Lusca's security modules declaratively through the lusca key in config.json, rather than through imperative app.use() calls. My goal is to enable Lusca's CSRF protection in a Kraken app while keeping the guarantee that the session middleware it depends on is registered before Lusca in the middleware chain.
The uncertainty is at the integration boundary: Kraken controls middleware registration order based on its configuration and medley of registered handlers, so it is not obvious how to confirm — or enforce — that session-backed CSRF tokens are actually validated on POST routes. A misordered chain could fail silently, leaving forms unprotected without any startup error. I am also unsure which Lusca modules (CSP, X-Frame-Options, HSTS) are enabled by a default Kraken config versus which must be opted into per environment file.
Assume current Express 4.x with the latest published Kraken.js and Lusca releases; compatibility should be verified given Lusca's low maintenance activity.
production.json?35275 reputation · 18 Feb 2021, 08:24 UTC
lusca key in the environment config file after its core middleware and before user‑defined routes, preserving the order they appear in the config object.req.session being present; if session middleware runs after Lusca CSRF, validation is silently skipped.Because Kraken controls the middleware stack declaratively, you can only guarantee CSRF runs after session by ensuring the session middleware appears earlier in the Kraken‑managed stack (e.g., via middleware array in config.json or by registering it before Kraken boots). If the session middleware is added imperatively after Kraken starts, the order cannot be guaranteed from config alone.
production.json) and confirm the lusca object contains a csrf entry.config.json you can add a middleware array with an entry like { "name": "express-session", "args": [...] } or ensure you call app.use(session(...)) before require(' kraken-js')(app, config).app.use((req, res, next)=>{ console.log('layer', this.name || this.handle.name); next(); })) before defining routes. Look at the console output: the session layer should appear before any layer whose name contains “lusca” or “csrf”.X‑CSRF‑Token or that a test endpoint returns req.csrfToken() as a function._csrf field. Expect a 403 Forbidden response. A 2xx indicates CSRF enforcement is not active.If you want the app to fail to start when the order is wrong, add a boot‑script that runs after Kraken has finished middleware registration:
// bootstrap.js const assert = require('assert'); module.exports = function (app) { const stack = app._router.stack; let sessionIdx = -1, csrfIdx = -1; stack.forEach((layer, i) => { const name = (layer.handle.name || '').toLowerCase(); if (name.includes('session')) sessionIdx = i; if (name.includes('lusca') && name.includes('csrf')) csrfIdx = i; }); if (sessionIdx === -1) throw new Error('No session middleware found'); if (csrfIdx === -1) throw new Error('Lusca CSRF middleware not found'); if (csrfIdx < sessionIdx) throw new Error('Lusca CSRF must run after session middleware'); };Register this script in
config.jsonunder thebootarray (Kraken will execute it after loading middleware). If the assertion fails, the process exits with an error, preventing a silent misconfiguration.Missing diagnostic detail
To give a definitive ordering check, confirm which session middleware you are using (e.g.,
express-sessionor a custom store). If you are using a custom session implementation that does not include the word “session” in its middleware name, adjust the boot‑script’s name‑matching accordingly.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.