Skip to content

Instantly share code, notes, and snippets.

Last active January 1, 2016 13:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jvennix-r7/8151392 to your computer and use it in GitHub Desktop.
Universal patch for rails RJS XSS issue (see The only downside here is that your app will break for users behind proxies that strip referers. Additionally, this patch will not work for you if you plan on serving cross-domain javascripts (e.g. for a hosted javascript…
# This patch adds a before_filter to all controllers that prevents xdomain
# .js requests from being rendered successfully.
module RemoteJavascriptRefererCheck
extend ActiveSupport::Concern
included do
require 'uri'
before_filter :check_rjs_referer, :if => ->(controller) { controller.request.format.js? }
# prevent generated rjs scripts from being exfiltrated by remote sites
# see
def check_rjs_referer
referer_uri = begin
rescue URI::InvalidURIError
# if request comes from a cross domain document
if referer_uri.blank? or
( and != or
(request.port.present? and referer_uri.port != request.port)
head :unauthorized
# shove the check into the base controller so it gets hit on every route
ApplicationController.class_eval do
include RemoteJavascriptRefererCheck
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment