(app)
| 52 | |
| 53 | bootstrapSharify() |
| 54 | |
| 55 | export function initializeMiddleware(app) { |
| 56 | app.use(serverTimingHeaders) |
| 57 | |
| 58 | app.set("trust proxy", true) |
| 59 | |
| 60 | // Cookie parser |
| 61 | app.use(cookieParser()) |
| 62 | |
| 63 | // Ensure all responses are gzip compressed |
| 64 | app.use(compression()) |
| 65 | |
| 66 | // JSON Body Parser is required for getting the `_csurf` token for passport. |
| 67 | app.use(bodyParser.json()) |
| 68 | app.use(bodyParser.urlencoded({ extended: true })) |
| 69 | |
| 70 | // Denied IPs |
| 71 | if (IP_DENYLIST && IP_DENYLIST.length > 0) { |
| 72 | app.use(ipFilter(IP_DENYLIST)) |
| 73 | } |
| 74 | |
| 75 | // Timeout middleware |
| 76 | if (process.env.NODE_ENV === "production") { |
| 77 | app.use(timeout(APP_TIMEOUT || "29s")) |
| 78 | } |
| 79 | |
| 80 | // Make sure we're using SSL and prevent clickjacking |
| 81 | app.use(ensureSslMiddleware) |
| 82 | |
| 83 | // Use HSTS to prevent downgrade attacks. |
| 84 | app.use(hstsMiddleware) |
| 85 | |
| 86 | // Prevent frame nesting in development and production. |
| 87 | if (NODE_ENV !== "test") { |
| 88 | app.use(helmet.frameguard()) |
| 89 | } |
| 90 | |
| 91 | // Inject UUID for each request into the X-Request-Id header |
| 92 | app.use(addRequestId()) |
| 93 | |
| 94 | // Session cookie |
| 95 | app.use(sessionMiddleware()) |
| 96 | |
| 97 | // Initialize async context for the request |
| 98 | app.use(asyncLocalsMiddleware) |
| 99 | |
| 100 | // Inject sharify data and asset middleware before any app code so that when |
| 101 | // crashing errors occur we'll at least get a 500 error page. |
| 102 | // Attach sharify object to `locals` because almost all other middleware |
| 103 | // relies on it existing. |
| 104 | app.use(sharify) |
| 105 | |
| 106 | // Static asset routing, required for all legacy code and must come early |
| 107 | // because `local.asset` is required to render the error page. |
| 108 | app.use(assetMiddleware()) |
| 109 | |
| 110 | // Passport middleware for authentication. |
| 111 | app.use( |
no test coverage detected