fix(templates): add helmet middleware to Express backend templates#248
Open
Alisha-21-cloud wants to merge 1 commit into
Open
fix(templates): add helmet middleware to Express backend templates#248Alisha-21-cloud wants to merge 1 commit into
Alisha-21-cloud wants to merge 1 commit into
Conversation
Author
|
@JoeCelaster I've opened a PR for Issue #247 — adding helmet middleware to all Express-based backend templates scaffolded by Celtrix. Would love to get a review when you get the chance! Happy to make any changes based on your feedback. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Walkthrough: Securing Express APIs using Helmet
I've successfully implemented the changes outlined in (Issue #247) to secure the Express API templates scaffolded by Celtrix.
Changes Made
1. Template
package.jsonUpdatesAll relevant template
package.jsonfiles have been updated (or verified) to includehelmet:"helmet": "^8.1.0"inpackage.jsonfor Express templates (e.g.mevn+tailwind+auth,mern/Ts-Backend)."@types/helmet"fromtemplates/mern/Ts-Backend/package.json, ashelmetv8+ includes its own TypeScript types.package.jsonis generated dynamically by Celtrix (vianpm init -y), verified thatutils/installer.jsandutils/customGenerators/backendGenerator.jsalready successfully injecthelmetinto the dependencies array.2. Template Server Middleware Configuration
Added
helmetto the top of the middleware stack for all Express-basedserver.js/server.ts/app.tsfiles inside thetemplates/directory:templates/mern/server/server.jstemplates/mean/server/server.jstemplates/mevn/server/server.jstemplates/mean+tailwind+auth/server/server.jstemplates/mern+tailwind+auth/server/server.jstemplates/mevn+tailwind+auth/server/server.jstemplates/mevn+tailwind+auth/javascript/server/server.jstemplates/mevn+tailwind+auth/typescript/server/server.jsImportant
In all these files,
app.use(helmet())has been correctly positioned as the very first middleware, beforeexpress.json()andcors(), ensuring HTTP headers are set immediately.3. README Updates
Added a
Securitysection to the existing backend README file (templates/mern/Ts-Backend/readme.md) briefly detailing the inclusion of Helmet and what it protects against.Verification
You can verify the functionality by running a standard Celtrix scaffold for an Express-based stack and inspecting the API response headers:
Expected headers in the response:
Content-Security-Policy: default-src 'self';base-uri 'self';font-src 'self' https: data:;form-action 'self';frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requestsX-Frame-Options: SAMEORIGINX-Content-Type-Options: nosniffStrict-Transport-Security: max-age=31536000; includeSubDomainsX-Download-Options: noopenX-DNS-Prefetch-Control: offReferrer-Policy: no-referrer(Note: It also successfully removes the default
X-Powered-By: Expressheader to prevent information disclosure).Closes #247