-
Notifications
You must be signed in to change notification settings - Fork 2k
harden: add CSRF protection in server.js... #4413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,13 +16,16 @@ | |
|
|
||
| // [START gae_update_web_server_app] | ||
| const express = require('express'); | ||
| const path = require('path'); | ||
| const cookieParser = require('cookie-parser'); | ||
| const csrf = require('csurf'); | ||
|
|
||
| const app = express(); | ||
|
|
||
| // [START gae_enable_parser] | ||
| // This middleware is available in Express v4.16.0 onwards | ||
| app.use(cookieParser()); | ||
| app.use(express.urlencoded({extended: true})); | ||
| app.use(csrf({cookie: true})); | ||
| // [END gae_enable_parser] | ||
|
|
||
| app.get('/', (req, res) => { | ||
|
|
@@ -31,7 +34,8 @@ app.get('/', (req, res) => { | |
|
|
||
| // [START gae_add_display_form] | ||
| app.get('/submit', (req, res) => { | ||
| res.sendFile(path.join(__dirname, '/views/form.html')); | ||
| const token = req.csrfToken(); | ||
| res.send(`<!DOCTYPE html><html><head><title>My App Engine App</title></head><body><h2>Create a new post</h2><form method="POST" action="/submit"><input type="hidden" name="_csrf" value="${token}"><div><input type="text" name="name" placeholder="Name"></div><div><textarea name="message" placeholder="Message"></textarea></div><div><button type="submit">Submit</button></div></form></body></html>`); | ||
| }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoding the HTML form directly in app.get('/submit', async (req, res, next) => {
try {
const token = req.csrfToken();
const template = await fs.readFile(path.join(__dirname, '/views/form.html'), 'utf-8');
const html = template.replace(
'<form method="POST" action="/submit">',
`<form method="POST" action="/submit"><input type="hidden" name="_csrf" value="${token}">`
);
res.send(html);
} catch (err) {
next(err);
}
});References
|
||
| // [END gae_add_display_form] | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid hardcoding the HTML form in
server.jsand keep using the existingviews/form.htmltemplate, we need to import thefsandpathmodules. This allows us to read the HTML file asynchronously and inject the CSRF token dynamically.References
fs.promisesAPI when working withasync/await.