Skip to content

Commit ed4b973

Browse files
committed
prevent HTML/JS injection attacks
1 parent 9b4d0b6 commit ed4b973

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

middlewares/chat_backend.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
require 'faye/websocket'
22
require 'thread'
33
require 'redis'
4+
require 'json'
5+
require 'erb'
46

57
module ChatDemo
68
class ChatBackend
@@ -32,7 +34,7 @@ def call(env)
3234

3335
ws.on :message do |event|
3436
p [:message, event.data]
35-
@redis.publish(CHANNEL, event.data)
37+
@redis.publish(CHANNEL, sanitize(event.data))
3638
end
3739

3840
ws.on :close do |event|
@@ -48,5 +50,12 @@ def call(env)
4850
@app.call(env)
4951
end
5052
end
53+
54+
private
55+
def sanitize(message)
56+
json = JSON.parse(message)
57+
json.each {|key, value| json[key] = ERB::Util.html_escape(value) }
58+
JSON.generate(json)
59+
end
5160
end
5261
end

0 commit comments

Comments
 (0)