Skip to content

Commit c897656

Browse files
committed
switched to cookie-based CSRF
1 parent ff3ec40 commit c897656

4 files changed

Lines changed: 7 additions & 16 deletions

File tree

app/assets/javascripts/qpixel_api.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ const validators = [];
77
let popped_modals_ct = 0;
88

99
window.QPixel = {
10-
csrfToken: () => {
11-
const token = $('meta[name="csrf-token"]').attr('content');
12-
QPixel.csrfToken = () => token;
13-
return token;
14-
},
15-
1610
createNotification: function (type, message) {
1711
// Some messages include a date stamp, `append_date` governs that.
1812
let append_date = false;
@@ -320,7 +314,6 @@ window.QPixel = {
320314

321315
fetch: async (uri, init) => {
322316
const defaultHeaders = {
323-
'X-CSRF-Token': QPixel.csrfToken(),
324317
// X-Requested-With is necessary for request.xhr? to work
325318
'X-Requested-With': 'XMLHttpRequest',
326319
'Content-Type': 'application/json',

app/controllers/application_controller.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
class ApplicationController < ActionController::Base
55
# Prevent CSRF attacks by raising an exception.
66
# For APIs, you may want to use :null_session instead.
7-
protect_from_forgery with: :exception
7+
protect_from_forgery with: :exception, store: :cookie
88
before_action :configure_permitted_parameters, if: :devise_controller?
99
before_action :set_globals
1010
before_action :enforce_signed_in, unless: :devise_controller?
@@ -417,4 +417,10 @@ def require_sudo
417417
redirect_to user_sudo_path
418418
end
419419
end
420+
421+
# default request_authenticity_tokens only checks form tokens and request.x_csrf_token
422+
# for some reason, even if cookie-based strategy is officially supported, it's not checked here
423+
def request_authenticity_tokens
424+
super << csrf_token_storage_strategy.fetch(request)
425+
end
420426
end

app/views/layouts/_head.html.erb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@
7171
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
7272
<% end %>
7373

74-
<%= csrf_meta_tags %>
75-
7674
<%= yield(:head) %>
7775

7876
<% if content_for? :twitter_card_meta %>

global.d.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,6 @@ interface QPixel {
411411
*/
412412
addPrePostValidation?: (callback: PostValidator) => void;
413413

414-
/**
415-
* Get the current CSRF anti-forgery token. Should be passed as the X-CSRF-Token header when
416-
* making AJAX POST requests.
417-
*/
418-
csrfToken?: () => string;
419-
420414
/**
421415
* Create a notification popup - not an inbox notification.
422416
* @param type the type to apply to the popup - warning, danger, etc.

0 commit comments

Comments
 (0)