Rails app return 422 response (Can't verify CSRF token authenticity) when served in iframe

Viewed 259

I serve a specific rails app in an iFrame on a certain platform (to embed a web game). In some cases/browsers, but not all, submitting a form in impossible because of something going wrong with CSRF.

I have found that this might be fixed by setting a p3p header, so I did the following in application_controller.rb with no result:

before_action :set_p3p

private 
  
def set_p3p
  headers['P3P'] = 'CP="ALL DSP COR CURa ADMa DEVa OUR IND COM NAV"'
end

The error that shows up in the logs:

W, [2020-09-02T10:28:03.574095 #21884]  WARN -- : [a12fc8c6-8d21-450e-9555-4673e8317662] Can't verify CSRF token authenticity.
I, [2020-09-02T10:28:03.575387 #21884]  INFO -- : [a12fc8c6-8d21-450e-9555-4673e8317662] Completed 422 Unprocessable Entity in 2ms (ActiveRecord: 0.0ms | Allocations: 888)
F, [2020-09-02T10:28:03.576961 #21884] FATAL -- : [a12fc8c6-8d21-450e-9555-4673e8317662]   
[a12fc8c6-8d21-450e-9555-4673e8317662] ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):

0 Answers
Related