Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement RequireScriptNonce #201

Merged
merged 5 commits into from
Feb 22, 2021

Conversation

almostwhitehat
Copy link

This new linter allows us to look for script tags with missing CSP nonce attributes

This new linter allows us to look for script tags with missing CSP nonce attributes
@almostwhitehat
Copy link
Author

👋 Hello @exterm and @jhottenstein - what are your thoughts on rolling this check into erb-lint? If you don't think it's in line with the goals of the linter, I can just extract it to a module for my own use :)

context 'usage of javascript_tag helper with a well formed nonce tag' do
let(:file) { <<~FILE }
<br />
<%= javascript_tag do nonce: true %>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not this be:

Suggested change
<%= javascript_tag do nonce: true %>
<%= javascript_tag nonce: true do %>

?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch - updated!

context 'usage of javascript_include_tag helper with a well formed nonce tag' do
let(:file) { <<~FILE }
<br />
<%= javascript_include_tag "script" nonce: true %>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a syntax error here. I was expecting this to fail

Suggested change
<%= javascript_include_tag "script" nonce: true %>
<%= javascript_include_tag "script", nonce: true %>

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Not sure why the parser didn't choke on this one, but it is updated now :)

# type attribute = something other than text/javascript
next if type_attribute &&
type_attribute.value_node.present? &&
type_attribute.value_node.to_a[1] != 'text/javascript'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check "application/javascript" as well?

We should also extract this check to a private method with a meaningful name

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All set! application/javascript has been added, and the logic here (and below) has been extracted to private methods.

nonce_attribute = tag.attributes['nonce']
nonce_present = nonce_attribute.present? && nonce_attribute.value_node.present?

next if nonce_present
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here to a private metod

send_node&.method_name?(:javascript_include_tag) ||
send_node&.method_name?(:javascript_pack_tag)

next if source.include?("nonce: true")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"nonce" => true is also valid so we probably should check that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point - instead of handling all of the possible syntaxes (hash rockets, symbols, single & double quotes, etc) we made it a bit more generic and pass if the word nonce is present in any form.

@rafaelfranca rafaelfranca merged commit f4b9380 into Shopify:master Feb 22, 2021
@almostwhitehat
Copy link
Author

👋 Hey @rafaelfranca thanks for merging this! Would you possibly be able to cut a release for this?

@almostwhitehat almostwhitehat deleted the adding-require-nonce branch May 13, 2021 14:48
@shopify-shipit shopify-shipit bot temporarily deployed to production July 26, 2021 18:44 Inactive
@shopify-shipit shopify-shipit bot temporarily deployed to production July 28, 2021 18:01 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants