Skip to content

Commit e45194e

Browse files
authored
Merge pull request #1695 from hackmdio/feat/csrf-export-user-data
Feat/csrf export user data
2 parents 5db11d2 + b8203ae commit e45194e

File tree

6 files changed

+62
-5
lines changed

6 files changed

+62
-5
lines changed

lib/homepage/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ exports.showIndex = async (req, res) => {
1616
errorMessage: req.flash('error'),
1717
privacyStatement: fs.existsSync(path.join(config.docsPath, 'privacy.md')),
1818
termsOfUse: fs.existsSync(path.join(config.docsPath, 'terms-of-use.md')),
19-
deleteToken: deleteToken
19+
deleteToken: deleteToken,
20+
csrfToken: req.csrfToken()
2021
}
2122

2223
if (!isLogin) {

lib/routes.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ const appRouter = Router()
1717

1818
// register route
1919

20+
const csurf = require('csurf')
21+
const csurfMiddleware = csurf({ cookie: true })
22+
2023
// get index
21-
appRouter.get('/', wrap(indexController.showIndex))
24+
appRouter.get('/', csurfMiddleware, wrap(indexController.showIndex))
2225

2326
// ----- error page -----
2427
// get 403 forbidden
@@ -52,7 +55,7 @@ appRouter.get('/me', wrap(userController.getMe))
5255
appRouter.get('/me/delete/:token?', wrap(userController.deleteUser))
5356

5457
// export the data of the authenticated user
55-
appRouter.get('/me/export', userController.exportMyData)
58+
appRouter.post('/me/export', urlencodedParser, csurfMiddleware, userController.exportMyData)
5659

5760
appRouter.get('/user/:username/avatar.svg', userController.getMyAvatar)
5861

package-lock.json

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"connect-session-sequelize": "~6.0.0",
5050
"cookie": "~0.4.0",
5151
"cookie-parser": "~1.4.4",
52+
"csurf": "~1.11.0",
5253
"deep-freeze": "~0.0.1",
5354
"ejs": "~2.6.2",
5455
"express": "~4.17.1",

public/js/cover.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function pageInit () {
9090
)
9191
}
9292

93-
$('.masthead-nav li').click(function () {
93+
$('.masthead-nav > li').click(function () {
9494
$(this).siblings().removeClass('active')
9595
$(this).addClass('active')
9696
})
@@ -428,3 +428,7 @@ $('.ui-use-tags').on('change', function () {
428428
$('.search').keyup(() => {
429429
checkHistoryList()
430430
})
431+
432+
$('.ui-export-user-data').click(function (e) {
433+
document.exportNoteData.submit()
434+
})

public/views/index/body.ejs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
<button class="btn btn-sm btn-success ui-signin" data-toggle="modal" data-target=".signin-modal"><%= __('Sign In') %></button>
2020
<% } %>
2121
</div>
22+
<form name="exportNoteData" action="<%- serverURL %>/me/export" method="post">
23+
<input type="hidden" name="_csrf" value="<%- csrfToken %>">
24+
</form>
2225
<div class="ui-signout" style="float: right; margin-top: 8px;<% if(!signin) { %> display: none;<% } %>">
2326
<a type="button" href="<%- serverURL %>/new" class="btn btn-sm btn-primary"><i class="fa fa-plus"></i> <%= __('New note') %></a>
2427
<span class="ui-profile dropdown pull-right">
@@ -27,7 +30,7 @@
2730
</button>
2831
<ul class="dropdown-menu" aria-labelledby="profileLabel">
2932
<li><a href="<%- serverURL %>/features"><i class="fa fa-dot-circle-o fa-fw"></i> <%= __('Features') %></a></li>
30-
<li><a href="<%- serverURL %>/me/export"><i class="fa fa-cloud-download fa-fw"></i> <%= __('Export user data') %></a></li>
33+
<li><a href="#" class="ui-export-user-data"><i class="fa fa-cloud-download fa-fw"></i> <%= __('Export user data') %></a></li>
3134
<li><a class="ui-delete-user" data-toggle="modal" data-target=".delete-user-modal"><i class="fa fa-trash fa-fw"></i> <%= __('Delete user') %></a></li>
3235
<li><a href="<%- serverURL %>/logout"><i class="fa fa-sign-out fa-fw"></i> <%= __('Sign Out') %></a></li>
3336
</ul>

0 commit comments

Comments
 (0)