Skip to content

Commit aa83ecc

Browse files
committed
Whitelisted comment parameters to avoid hijacking comments
1 parent 6c91237 commit aa83ecc

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

app/controllers/comments_controller.rb

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@ def index
1010
@comments = Comment.where('created_at > ?', 1.day.ago)
1111
end
1212

13-
def new
13+
def new ; end
1414

15-
end
16-
17-
def edit
18-
19-
end
15+
def edit ; end
2016

2117
def create
22-
redirect_to_signup_if_unauthenticated(request.referer + "?" + (params[:comment].try(:to_query) || ""), "You must signin/signup to add a comment") do
23-
@comment = @protip.comments.build(params[:comment])
18+
create_comment_params = params.require(:comment).permit(:comment)
19+
20+
redirect_to_signup_if_unauthenticated(request.referer + "?" + (create_comment_params.try(:to_query) || ""), "You must signin/signup to add a comment") do
21+
@comment = @protip.comments.build(create_comment_params)
2422
@comment.user = current_user
2523
respond_to do |format|
2624
if @comment.save
@@ -36,8 +34,10 @@ def create
3634
end
3735

3836
def update
37+
update_comment_params = params.require(:comment).permit(:comment)
38+
3939
respond_to do |format|
40-
if @comment.update_attributes(params[:comment])
40+
if @comment.update_attributes(update_comment_params)
4141
format.html { redirect_to protip_path(@comment.commentable.try(:public_id)) }
4242
format.json { head :ok }
4343
else
@@ -50,7 +50,6 @@ def update
5050
def destroy
5151
return head(:forbidden) if @comment.nil?
5252
@comment.destroy
53-
#record_event('destroyed comment')
5453
respond_to do |format|
5554
format.html { redirect_to @protip }
5655
format.json { head :ok }
@@ -68,17 +67,20 @@ def like
6867
end
6968

7069
private
70+
7171
def lookup_comment
72-
@comment = Comment.find(params[:id])
72+
id = params.permit(:id)[:id]
73+
@comment = Comment.find(id)
7374
lookup_protip
7475
end
7576

7677
def lookup_protip
77-
@protip = Protip.with_public_id(params[:protip_id])
78+
protip_id = params.permit(:protip_id)[:protip_id]
79+
@protip = Protip.with_public_id(protip_id)
7880
end
7981

8082
def verify_ownership
8183
lookup_comment
8284
redirect_to(root_url) unless (is_admin? or (@comment && @comment.authored_by?(current_user)))
8385
end
84-
end
86+
end

app/models/comment.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def commenting_on_own?
112112
end
113113

114114
private
115+
115116
def decrement_likes_cache(value)
116117
self.likes_cache -= 1
117118
self.likes_value_cache -= value

0 commit comments

Comments
 (0)