CSP Violation on OTP QR-code #231
Replies: 2 comments 4 replies
-
I think the preferred solution would be to modify rqrcode to support an approach that doesn't require setting inline styles. You could try doing that and sending a pull request to rqrcode to see if they would be open to such an approach. |
Beta Was this translation helpful? Give feedback.
-
There seems to have been some discussion about this topic for rqrcode gem: whomwah/rqrcode#51. But not implemented or code was removed later. In this discussion a possible solution was mentioned that I was not aware of: allow specific inline styles by adding a hash to the CSP. When changing code for CSP like this: # Allow inline style (fill:#000) on QR-code svg
# Hash calculated as: Base64::encode64(Digest::SHA256.digest('fill:#000')).strip
csp.style_src :self, :unsafe_hashes, [:sha256, 'hXLriEz22xKNlZLBZHXTURsF5XZHj1Bkjo1s7SNTzSI='] This works in Chrome, no more CSP violations. In Firefox there is still an error but the qr-code is displayed okay. Also making this change to have the qr-code rendered as a path, instead of separate elements, which is more compact: auth_class_eval do
def otp_qr_code
RQRCode::QRCode.new(otp_provisioning_uri).as_svg(module_size: 8, viewbox: true, use_path: true)
end
end |
Beta Was this translation helpful? Give feedback.
-
When using the OTP feature and displaying the otp-setup page I'm getting a CSP violation, both on Firefox and Chrome. Reason is the qr-code svg image, all 'pixels' have a style attribute. This is hardcoded in gem 'rqrcode'.
It is possible to prevent the csp violation in two ways:
:unsafe_inline
to the content security policy's 'style_src' element.gsub('style="fill:#000"', '')
to the generated svg string. This should always work since the default fill color of an svg rectangle is black, adding a white background color to the element containing the svg is necessaryAre there any other solutions? What would be the preferred solution?
Beta Was this translation helpful? Give feedback.
All reactions