Created
November 30, 2013 22:06
-
-
Save javan/7725255 to your computer and use it in GitHub Desktop.
Prevent cross-origin js requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
before_filter :ensure_xhr | |
private | |
def ensure_xhr | |
if request.get? && request.format && (request.format.js? || request.format.json?) | |
head :forbidden unless request.xhr? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@homakov If the incoming request doesn't have .js in the path or a javascript Accept header,
request.format.js?
will be false. If you've made the decision to still return js in that scenario then the above solution will not protect you.