Skip to content

Commit 0a84e48

Browse files
committed
fixed topic delete not post method security problem.
1 parent b422857 commit 0a84e48

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

controllers/topic.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -696,33 +696,33 @@ exports.delete = function (req, res, next) {
696696
//刪除topic_tag,標簽topic_count減1
697697
//刪除topic_collect,用戶collect_topic_count減1
698698
if (!req.session.user || !req.session.user.is_admin) {
699-
res.redirect('home');
700-
return;
699+
return res.send({ success: false, message: '無權限' });
701700
}
702701

703702
var topic_id = req.params.tid;
704703

705704
if (topic_id.length !== 24) {
706-
res.render('notify/notify', {error: '此話題不存在或已被刪除。'});
705+
res.send({success: false, message: '此話題不存在或已被刪除。'});
707706
return;
708707
}
709708

710709
get_topic_by_id(topic_id, function (err, topic, tags, author) {
710+
if (err) {
711+
return res.send({ success: false, message: err.message });
712+
}
713+
711714
if (!topic) {
712-
res.render('notify/notify', {error: '此話題不存在或已被刪除。'});
715+
res.send({success: false, message: '此話題不存在或已被刪除。'});
713716
return;
714717
}
715718
var proxy = new EventProxy();
716719
var render;
717720

718-
render = function () {
719-
res.render('notify/notify', {success: '話題已被刪除。'});
720-
return;
721-
};
722-
723-
proxy.assign('topic_removed', render);
724721
topic.remove(function (err) {
725-
proxy.emit('topic_removed');
722+
if (err) {
723+
return res.send({ success: false, message: err.message });
724+
}
725+
res.send({success: true, message: '話題已被刪除。'});
726726
});
727727
});
728728
};

routes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ module.exports = function (app) {
8282
app.get('/topic/:tid', topic.index);
8383
app.get('/topic/:tid/top/:is_top?', topic.top);
8484
app.get('/topic/:tid/edit', topic.edit);
85-
app.get('/topic/:tid/delete', topic.delete);
85+
app.post('/topic/:tid/delete', topic.delete);
8686
app.post('/topic/create', topic.create);
8787
app.post('/topic/:tid/edit', topic.edit);
8888
app.post('/topic/collect', topic.collect);

views/topic/index.html

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ <h3><% if(topic.top){%>[置頂]<% } %><%= topic.title %></h3>
9797
<a href='/topic/<%= topic._id %>/top/1'><img class='user_icon' src='<%- config.site_static_host %>/images/star_fav_icon&16.png' title='置頂' /></a>
9898
<% } %>
9999
<a href='/topic/<%= topic._id %>/edit'><img class='user_icon' src='<%- config.site_static_host %>/images/doc_edit_icon&16.png' title='編輯' /></a>
100-
<a href='/topic/<%= topic._id %>/delete' class='delete_topic_btn'><img class='user_icon' src='<%- config.site_static_host %>/images/trash_icon&16.png' title='刪除' /></a>
100+
<a href='javascript:;' data-id='<%= topic._id %>' class='delete_topic_btn'><img class='user_icon' src='<%- config.site_static_host %>/images/trash_icon&16.png' title='刪除' /></a>
101101
<% } else { %>
102102
<% if (current_user._id.toString() === topic.author_id.toString()) { %>
103103
<span class='sp10'></span>
@@ -138,7 +138,7 @@ <h3><% if(topic.top){%>[置頂]<% } %><%= topic.title %></h3>
138138
<div id='wmd-preview' class='wmd-preview reply-wmd-preview'></div>
139139
</div>
140140
</div>
141-
<input type='hidden' name='_csrf' value='<%= csrf %>' />
141+
<input type='hidden' name='_csrf' id='_csrf' value='<%= csrf %>' />
142142
</div>
143143
<div class='sep10'></div>
144144
<button id='submit_btn' class='btn'>回覆</button>
@@ -305,8 +305,15 @@ <h3><% if(topic.top){%>[置頂]<% } %><%= topic.title %></h3>
305305
});
306306

307307
$('.delete_topic_btn').click(function() {
308+
var topicId = $(this).data('id');
308309
if(confirm('確定要刪除此話題嗎?')) {
309-
window.location.href = $(this).attr('href');
310+
$.post('/topic/' + topicId + '/delete', { _csrf: $('#_csrf').val() }, function (result) {
311+
if (!result.success) {
312+
alert(result.message);
313+
} else {
314+
window.location.href = '/';
315+
}
316+
});
310317
}
311318
return false;
312319
});

0 commit comments

Comments
 (0)