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

Deprecate passing in unsafe HTML into Govspeak #356

Merged
merged 11 commits into from
Jun 6, 2018
Merged

Conversation

tijmenb
Copy link
Contributor

@tijmenb tijmenb commented Jun 5, 2018

Currently, when passing in HTML into the Govspeak component the component will mark the input as "HTML safe" (using raw), meaning it will be rendered on the page without sanitisation.

A simple situation like this, where @bar is untrusted user input, will result in an XSS vulnerability.

<%= render 'govuk_publishing_components/components/govspeak', content:
"<p>Foo #{@bar}</p>" %>

We currently have 1 such vulnerability in email-alert-frontend, but it's not exploitable.

A better way to pass in input is to use a block, which will use sanitisation by default:

<%= render 'govuk_publishing_components/components/govspeak' do %>
  <p>Foo <%= @bar %></p>
<% end %>

This PR deprecates (but doesn't remove) the functionality to pass in untrusted input. See commits for details.

@tijmenb tijmenb temporarily deployed to govuk-publishing-compon-pr-356 June 5, 2018 13:32 Inactive
Copy link
Contributor

@andysellick andysellick left a comment

Choose a reason for hiding this comment

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

Can you update the component guide page for the govspeak component as well?

@@ -2,7 +2,7 @@

describe "All components" do
it "doesn't use `html_safe`" do
files_with_html_safe = `grep -rni "html_safe" app/views/govuk_publishing_components/components`.lines
files_with_html_safe = `grep -rni "html_safe " app/views/govuk_publishing_components/components`.lines
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lol I hoped you wouldn't notice. It's to avoid this test triggering on the deprecation message in the component.

@@ -14,6 +14,21 @@
<% if content.html_safe? %>
<%= content %>
<% else %>
<% puts "
Copy link
Contributor

Choose a reason for hiding this comment

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

I've not seen puts used like this in a template before. Will this render the text inside it as written, or will it all collapse into a single chunk?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like this in the output:

        You've passed in unsanitised HTML into the govspeak component as the
        `content` param.

        Passing in unsafe HTML is deprecated and will be removed in a future
        version. You need to pass in a block instead or use the `capture` helper.

        See the component guide for examples.

        If you're 100% sure there's no unsanitised user input in the string you
        could also call `.html_safe` on the string or use the `raw` helper before
        passing it in.

        Called from /var/lib/jenkins/workspace/components_govspeak-content-LXCJ5KGV3W4GAGTNRFJXBGJGVEBXPWFEIRVZXHMIVRN7HGSF5OKA/app/views/govuk_publishing_components/component_guide/show.html.erb:16:in `__var_lib_jenkins_workspace_components_govspeak_content______________________________________________________app_views_govuk_publishing_components_component_guide_show_html_erb___1824013196706400841_47344039424500'

https://ci.integration.publishing.service.gov.uk/job/govuk_publishing_components/job/govspeak-content/2/console

@tijmenb tijmenb temporarily deployed to govuk-publishing-compon-pr-356 June 5, 2018 15:37 Inactive
@tijmenb tijmenb temporarily deployed to govuk-publishing-compon-pr-356 June 5, 2018 15:45 Inactive
@tijmenb tijmenb temporarily deployed to govuk-publishing-compon-pr-356 June 5, 2018 15:49 Inactive
tijmenb added 11 commits June 5, 2018 16:49
The block will not have `raw` called on it, so it will be the safer
option to pass in content.
When the HTML has been declared safe (either by constructing it
properly, or using `html_safe`, or `raw) we won't mark it as safe
again. This will prevent accidental marking as safe of unsafe content.

Note that `raw foo` is the same as `foo.html_safe`.
When you pass in unsafe HTML into the component like this:

```
<%= render 'govuk_publishing_components/components/govspeak', content:
"<p>Foo</p>" %>
```

You will now see deprecation message with instructions on how to better
input content. I've chosen the message instead of just an error to
avoid having to update all the apps at once.
This silences the warnings.
We use `raw` because in some case the input is `nil` and will crash
with `html_safe`.
This will be passed into the govspeak component, so it needs to be HTML
safe.
This is the first example because it should be the main usage.
And skip govspeak for now.
@tijmenb
Copy link
Contributor Author

tijmenb commented Jun 5, 2018

Updated this PR to add tests, fix a problem with govspeak-htmlpub and fix the deprecated usage in the component guide.

@tijmenb tijmenb merged commit 4c5426a into master Jun 6, 2018
@tijmenb tijmenb deleted the govspeak-content branch June 6, 2018 11:26
tijmenb added a commit to alphagov/email-alert-frontend that referenced this pull request Jun 8, 2018
The Govspeak component has moved from Static to the gem. This switches
this app to use the gem-based component.

To make the transition easier I've added a `govspeak` helper that takes
a block. This cleans up the views a lot and makes it easier to read the
HTML. It also makes sure that we're using Rails' HTML escaping
mechanism by default, which is needed because the component will in the
future no longer call `.html_safe` on the input
(alphagov/govuk_publishing_components#356).
This fixes at least one (unexploitable) [XSS vulnerability in this
app][1] (`@address` is add to the page unescaped).

[1]:
https://github.com/alphagov/email-alert-frontend/blob/73dc8d4db0174b215f
c61c42fa8ad992f2d8f6d2/app/views/authentication/request_sign_in_token.ht
ml.erb#L17

https://trello.com/c/uwcWXXNp
tijmenb added a commit to alphagov/government-frontend that referenced this pull request Jun 8, 2018
This makes sure that we only pass in safe HTML to the govspeak
component, because in the future the component won't call `html_safe`
on it for us
(alphagov/govuk_publishing_components#356).

In some cases I've used `raw` because the input may be nil.
tijmenb added a commit to alphagov/government-frontend that referenced this pull request Jun 8, 2018
This makes sure that we only pass in safe HTML to the govspeak
component, because in the future the component won't call `html_safe`
on it for us
(alphagov/govuk_publishing_components#356).

In some cases I've used `raw` because the input may be nil.
tijmenb added a commit to alphagov/government-frontend that referenced this pull request Jun 8, 2018
This makes sure that we only pass in safe HTML to the govspeak
component, because in the future the component won't call `html_safe`
on it for us
(alphagov/govuk_publishing_components#356).

In some cases I've used `raw` because the input may be nil.
tijmenb added a commit to alphagov/email-alert-frontend that referenced this pull request Jun 11, 2018
The Govspeak component has moved from Static to the gem. This switches
this app to use the gem-based component.

To make the transition easier I've added a `govspeak` helper that takes
a block. This cleans up the views a lot and makes it easier to read the
HTML. It also makes sure that we're using Rails' HTML escaping
mechanism by default, which is needed because the component will in the
future no longer call `.html_safe` on the input
(alphagov/govuk_publishing_components#356).
This fixes at least one (unexploitable) [XSS vulnerability in this
app][1] (`@address` is add to the page unescaped).

[1]:
https://github.com/alphagov/email-alert-frontend/blob/73dc8d4db0174b215f
c61c42fa8ad992f2d8f6d2/app/views/authentication/request_sign_in_token.ht
ml.erb#L17

https://trello.com/c/uwcWXXNp
@NickColley NickColley mentioned this pull request Jul 31, 2018
NickColley added a commit that referenced this pull request Jul 31, 2018
To see what led to this decision see:

- [an incident involving multiple components](#305)
- [a potential vulnerability with the govspeak component](#356).
NickColley added a commit that referenced this pull request Jul 31, 2018
To see what led to this decision see:

- [an incident involving multiple components](#305)
- [a potential vulnerability with the govspeak component](#356).
NickColley added a commit that referenced this pull request Jul 31, 2018
To see what led to this decision see:

- [an incident involving multiple components](#305)
- [a potential vulnerability with the govspeak component](#356).
NickColley added a commit that referenced this pull request Jul 31, 2018
To see what led to this decision see:

- [an incident involving multiple components](#305)
- [a potential vulnerability with the govspeak component](#356).
NickColley added a commit that referenced this pull request Jul 31, 2018
To see what led to this decision see:

- [an incident involving multiple components](#305)
- [a potential vulnerability with the govspeak component](#356).
NickColley added a commit that referenced this pull request Aug 1, 2018
To see what led to this decision see:

- [an incident involving multiple components](#305)
- [a potential vulnerability with the govspeak component](#356).
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.

3 participants