diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 9bd6b831a..4f8ffbaf0 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -64,6 +64,7 @@ class ApplicationController < ActionController::Base
 
   rescue_from ::Unauthorized, :with => :deny_access
   rescue_from ::ActionView::MissingTemplate, :with => :missing_template
+  rescue_from ActionController::InvalidCrossOriginRequest, with: :render_unauthorized
 
   include Redmine::Search::Controller
   include Redmine::MenuManager::MenuController
@@ -563,6 +564,12 @@ class ApplicationController < ActionController::Base
     end
   end
 
+  def render_unauthorized
+    @project = nil
+    render_error({:message => :notice_not_authorized, :status => 403})
+    return false
+  end
+
   def render_403(options={})
     @project = nil
     render_error({:message => :notice_not_authorized, :status => 403}.merge(options))
diff --git a/test/functional/projects_controller_test.rb b/test/functional/projects_controller_test.rb
index 1042ad973..01b23add0 100644
--- a/test/functional/projects_controller_test.rb
+++ b/test/functional/projects_controller_test.rb
@@ -333,6 +333,18 @@ class ProjectsControllerTest < Redmine::ControllerTest
     assert_equal 'text/javascript', response.media_type
   end
 
+  def test_requesting_protected_javascript_should_rescue_invalid_cross_origin_request
+    assert_nothing_raised do
+      get(
+        :autocomplete,
+        :params => {
+          :format => 'js'
+        }
+      )
+      assert_response :forbidden
+    end
+  end
+
   test "#index by non-admin user with view_time_entries permission should show overall spent time link" do
     @request.session[:user_id] = 3
     get :index
