Commit Graph

7460 Commits

Author SHA1 Message Date
Javi Martín
db97f9d08c Add and apply rubocop rules for empty lines
We were very inconsistent regarding these rules.

Personally I prefer no empty lines around blocks, clases, etc... as
recommended by the Ruby style guide [1], and they're the default values
in rubocop, so those are the settings I'm applying.

The exception is the `private` access modifier, since we were leaving
empty lines around it most of the time. That's the default rubocop rule
as well. Personally I don't have a strong preference about this one.


[1] https://rubystyle.guide/#empty-lines-around-bodies
2019-10-24 17:11:47 +02:00
Javier Martín
b6750e8f17 Merge pull request #3792 from consul/rubocop_find_by
Apply rubocop FindBy rules
2019-10-24 16:47:21 +02:00
Javi Martín
49e55b4dc4 Apply Rails/DynamicFindBy rubocop rule
We were already using `find_by` most of the time.

Since there are false positives related to our `find_by_slug_or_id!` and
`find_by_manger_login` methods, which cannot be replaced with `find_by`,
I'm adding it indicating the "refactor" severity.
2019-10-23 20:05:40 +02:00
Javi Martín
93c6347b45 Apply Rails/FindBy rubocop rule
We were already using it in most places.
2019-10-23 18:29:09 +02:00
Javi Martín
69e3e67c85 Avoid "Overwriting existing method open" warning
In Ruby, the Kernel class defined the `open` method, which is available
for (almost) every object. So creating a scope with the name `open`
generates a warning indicating we are overwriting the existing `open`
method.

While this warning is pretty much harmless and we could ignore it, it
generates a lot of noise in the logs. So I'm "undefining" the method
before generating the scope, so we don't get the warning all the time.
2019-10-23 18:07:50 +02:00
Javi Martín
26e050b9f2 Remove redundant scopes
These scopes were already defined by the `enum :task` method.
2019-10-23 18:03:53 +02:00
Javi Martín
49c3402833 Use update! instead of update_attributes!
It's what we use almost everywhere, and it's shorter.
2019-10-23 14:39:31 +02:00
Javi Martín
7ca55c44e0 Apply Rails/SaveBang rubocop rule
Having exceptions is better than having silent bugs.

There are a few methods I've kept the same way they were.

The `RelatedContentScore#score_with_opposite` method is a bit peculiar:
it creates scores for both itself and the opposite related content,
which means the opposite related content will try to create the same
scores as well.

We've already got a test to check `Budget::Ballot#add_investment` when
creating a line fails ("Edge case voting a non-elegible investment").

Finally, the method `User#send_oauth_confirmation_instructions` doesn't
update the record when the email address isn't already present, leading
to the test "Try to register with the email of an already existing user,
when an unconfirmed email was provided by oauth" fo fail if we raise an
exception for an invalid user. That's because updating a user's email
doesn't update the database automatically, but instead a confirmation
email is sent.

There are also a few false positives for classes which don't have bang
methods (like the GraphQL classes) or destroying attachments.

For these reasons, I'm adding the rule with a "Refactor" severity,
meaning it's a rule we can break if necessary.
2019-10-23 14:39:31 +02:00
Javi Martín
431074c99f Add save! method to ActiveModel models
This way we make it clear we expect records to be valid when we save
them, just like we do with ActiveRecord models.
2019-10-23 14:32:42 +02:00
Javi Martín
35bbd87093 Fix adding blank comments to existing annotations
We were using the `present?` method, which will always return true for a
comment object.
2019-10-23 04:28:08 +02:00
Javi Martín
38b7307450 Use respond_to? instead of try
Usually when we use `try` we actually mean `try!`, which is the same as
the safe navigation operator. However, there are a few cases where we
actually mean to execute a method if the object responds to that method.

In those cases using `try` would actually be OK, but in order to avoid
confusion as to whether we mean to check for `respond_to?` or we mean to
use safe navigation, I'm removing all usages of `try`.
2019-10-22 17:37:51 +02:00
Javi Martín
1004ac01f8 Add and apply Style/SafeNavigation rubocop rule
We were already using it most of the time, but not always.
2019-10-22 17:37:51 +02:00
Javi Martín
6ceca143b4 Remove redundant check deleting content blocks
The `find` method raises an exception if nothing is found, so there's no
need to check if it found something.
2019-10-22 17:37:51 +02:00
Javi Martín
68ca29fa8b Convert markdown to HTML on demand
We were converting markdown to HTML every time we saved a record, which
has the same problems as sanitizing HTML before saving it to the
database, particularly because the body of a legislation draft is stored
in a translations table.

Performance-wise this isn't a problem: converting a text with more than
200_000 characters takes about a milisecond on my machine.

Note we need to modify a migration generated by globalize, since the
method `create_translation_table!` would fail now that we don't define
`translates :body_html` in the model.
2019-10-21 21:32:43 +02:00
Javi Martín
7bf4e4d611 Sanitize descriptions in the views
Sanitizing descriptions before saving a record has a few drawbacks:

1. It makes the application rely on data being safe in the database. If
somehow dangerous data enters the database, the application will be
vulnerable to XSS attacks
2. It makes the code complicated
3. It isn't backwards compatible; if we decide to disallow a certain
HTML tag in the future, we'd need to sanitize existing data.

On the other hand, sanitizing the data in the view means we don't need
to triple-check dangerous HTML has already been stripped when we see the
method `auto_link_already_sanitized_html`, since now every time we use
it we sanitize the text in the same line we call this method.

We could also sanitize the data twice, both when saving to the database
and when displaying values in the view. However, doing so wouldn't make
the application safer, since we sanitize text introduced through
textarea fields but we don't sanitize text introduced through input
fields.

Finally, we could also overwrite the `description` method so it
sanitizes the text. But we're already introducing Globalize which
overwrites that method, and overwriting it again is a bit too confusing
in my humble opinion. It can also lead to hard-to-debug behaviour.
2019-10-21 21:32:02 +02:00
Javi Martín
ae2576020e Extract method to use WYSIWYGSanitizer in views
This is similar to methods we use like `sanitize` or `markdown`.
2019-10-21 21:32:02 +02:00
Javier Martín
a8713793a5 Merge pull request #3779 from consul/jquery_xss
Use jQuery's text() instead of html() where safer
2019-10-21 21:18:47 +02:00
Javi Martín
7f1bfc6bd7 Avoid using html() to set languages description
The jQuery html() function does not filter <script> tags, so if somehow
an attacker introduced a <script> in the translation, we would be
vulnerable to a XSS attack.

Note using $.parseHTML wouldn't solve the problem, since it doesn't
filter attributes in image tags.

Since changing the text of the part which doesn't have the count wasn't
very clean, I've added another <span> tag for the part with the
description, and so we can use jQuery's text() function to replace it.
2019-10-21 20:24:50 +02:00
Javi Martín
d61e8cb6a6 Use text() instead of html()
Using html() makes it possible to insert <script> tags in the DOM, and
in this case we aren't supposed to be inserting any HTML.

I haven't found a way to focus on a field with Capybara, then add a
character, and focus on another field. So I've manually triggered the
change event in the test.
2019-10-21 20:24:50 +02:00
Javi Martín
31c2379a4e Don't sanitize <span> tags in HTML attributes
Doing so will cause the `<span>` tag to be rendered in the document,
instead of being rendered as a data attribute.
2019-10-21 20:24:50 +02:00
Javi Martín
9340d189cb Fix investments search with numbers in their title 2019-10-21 19:27:16 +02:00
Javi Martín
8aa4c630d7 Make search_by_title_or_id behave like a scope
There's no need to pass the collection of results when we use methods
like `where`.
2019-10-21 19:27:15 +02:00
Javier Martín
2f9995f566 Merge pull request #3766 from consul/relative_urls
Use relative URLs where possible
2019-10-20 18:33:13 +02:00
Javier Martín
c1d6cd4e4b Merge pull request #2151 from consul/feature-flag-api
Feature flag api
2019-10-20 18:21:28 +02:00
Javi Martín
27468b0b7b Use relative URLs where possible
In general, we always use relative URLs (using `_path`), but sometimes
we were accidentally using absolute URLs (using `_url`). It's been
reported i might cause some isuses if accepting both HTTP and HTTPS
connections, although we've never seen the case.

In any case, this change makes the code more consistent and makes the
generated HTML cleaner.
2019-10-20 17:26:14 +02:00
Javi Martín
11e52dbe98 Remove kaminari_path
The main reason to use it was the `rel` attribute for previous/next
pages not being indexed correctly by certain search engines when using a
relative URL. However, AFAIK that only applied to `<link>` tags, not to
`<a>` tags, and only if a `<base>` tag was defined.

In any case, it looks like the same search engines don't use the `rel`
attribute for previous/next to index pages anymore.
2019-10-20 17:26:14 +02:00
Javi Martín
15c49e0c10 Remove obsolete URL helpers
We now use `polymorphic_hierarchy_path` instead.
2019-10-20 17:23:59 +02:00
Juanjo Bazán
0063e7b4d8 Add feature flag for the GraphQL API 2019-10-20 14:52:07 +02:00
Javi Martín
f5b60e03e1 Don't let valuators update investments
There were some confusing definitions regarding the valuation of budget
investments.

In the controller, `CommentableActions` was included, which includes the
update action.

In the abilities, a valuator was given permission to update an
investment.

However, the action to update an investment didn't work because there is
no route defined to do so.

The ability was defined so valuators could access the "edit" action,
which will not call the "update" action but the "valuate" action. Since
internally "edit" and "update" use the same permission, it worked.

But then we added permission for regular users to update budget
investments, and these permissions were allowing valuators to update
another user's investment.

After this change, everything seems to work properly since we check
authorization in the controller itself instead of using abilities.
2019-10-18 16:24:27 +02:00
denialtorres
bb627a7117 Edit Budget Investment only in accepting phase (#3716)
This way users who made a typo can fix it before the investment is reviewed.
2019-10-18 13:59:14 +02:00
Javier Martín
dd8bc5cea7 Merge pull request #3760 from consul/fix_milestone_published
Fix milestone publication date comparison
2019-10-10 20:58:22 +02:00
Javi Martín
6a6a8bf365 Fix milestone publication date comparison
We're storing the publication date as datetime in the database, and we
were comparing it to a date, meaning today's milestones were not being
found.
2019-10-10 02:35:20 +02:00
Javi Martín
56e62a41b6 Fix duplicate given order creating answers
It's possible to have a given order greater than the number of answers;
we don't have any validation rules for that. So the check for the number
of answers isn't enough.

Checking the maximum given order in the answers is safer. Another option
would be to reorder the answers every time we add a new one, but I'm not
sure whether that's the expected behaviour.

Note even after this change the action is not thread-safe, as it is
possible to create two questions with the same given order with two
simultaneous requests.
2019-10-10 00:36:57 +02:00
Javi Martín
6b1864fbcd Sanitize translations instead of using _html
Using the `_html` suffix in an i18n key is the same as using `html_safe`
on it, which means that translation could potentially be used for XSS
attacks.
2019-10-09 19:46:47 +02:00
Javi Martín
b66859945e Remove _html suffix from already sanitized texts
Using the `_html` suffix automatically marks texts as HTML safe, so
doing so on sanitized texts is redundant.

Note flash texts are not sanitized the moment they are generated, but
are sanitized when displayed in the view.
2019-10-09 19:46:47 +02:00
Javi Martín
391f58eb90 Sanitize dashboard action before displaying it
We were using `<%==`, which is the same as using `raw`.

Note ERB Lint doesn't warn us of this usage. Brakeman does warn us,
though.
2019-10-08 19:10:14 +02:00
Javi Martín
a20c0f078d Use safe_join instead of <%==
Using `<%==` is the same as using `raw`, and here we only want to mark
as safe a `<br>` tag.
2019-10-08 19:10:14 +02:00
Javi Martín
00a6f5b601 Remove <%== usage displaying settings
Using `<%==` is the same as using `raw`. I'm not sure if we meant
`sanitize` in this case, or it's just a typo. I'm assuming the latter
since we don't use anything similar in any other places.
2019-10-08 19:10:14 +02:00
Javi Martín
89402bdbf6 Use raw instead of html_safe
They do the exact same thing; however `html_safe` might confuse
developers into thinking it will make the HTML safe. Using `raw` makes
it clear that we're inserting the text without escaping it.
2019-10-08 19:10:13 +02:00
Javi Martín
9eee79f218 Sanitize markdown output
We were using the markdown renderer with the `filter_html` option set to
false, so we weren't removing hypothetical `<script>` tags.
2019-10-08 18:46:21 +02:00
Javi Martín
61bf9a5c73 Use sanitize instead of html_safe
The difference is `html_safe` allows every HTML tag, including the
`<script>` tag, while `sanitize` only allows tags which are considered
safe. In this case, we want to allow a `<span>` tag in a translation,
and links inside flash messages.
2019-10-08 18:46:21 +02:00
Javi Martín
928312e218 Use sanitize in translations with links
Sometimes we're interpolating a link inside a translation, and marking
the whole translations as HTML safe.

However, some translations added by admins to the database or through
crowdin are not entirely under our control.

Although AFAIK crowdin checks for potential cross-site scripting
attacks, it's a good practice to sanitize parts of a string potentially
out of our control before marking the string as HTML safe.
2019-10-08 18:46:21 +02:00
Javi Martín
56f690b8a9 Use attributes in translations with sanitize
There's a slight chance an attribute like an author's name might contain
an attempt to perform XSS attacks. So, instead of marking the whole text
as HTML safe, we can sanitize it.

Also note I'm removing the `_html` suffix in the i18n key, since it's
got the same effect as using `html_safe`.
2019-10-08 18:46:21 +02:00
Javi Martín
75a28fafcb Sanitize label texts automatically
This way we can remove all those `html_safe` calls and we avoid
potential XSS attacks in label texts.
2019-10-08 18:46:21 +02:00
Javi Martín
2586229e38 Remove duplication in TextWithLinksHelper
We were using `Rinku.auto_link` the same way twice. And it makes sense
that the method `sanitize_and_auto_link` first sanitizes the text and
then calls `auto_link_already_sanitized_text`.
2019-10-08 18:46:21 +02:00
Javi Martín
0b40865e61 Raise an exception when handling unsafe content
We were confused about what `.html_safe` did, and were automatically
marking as safe content which was not.
2019-10-08 18:46:20 +02:00
Javi Martín
2aabf79fb4 Rename methods to add auto links to HTML
The name `safe_html_with_links` was confusing and could make you think
it takes care of making the HTML safe. So I've renamed it in a way that
makes it a bit more intuitive that it expects its input to be already
sanitized.

I've changed `text_with_links` as well so now the two method names
complement each other.
2019-10-08 18:46:20 +02:00
Javi Martín
2ffbae890e Sanitize valuation explanations
If we don't sanitize them, valuators might attempt Cross-Site Scripting
attacks.
2019-10-08 18:46:20 +02:00
Javi Martín
8b73cfc019 Sanitize annotation context before displaying it
There's a case where we would face a Cross-Site Scripting attack. An
attacker could use the browser's developer tools to add (on their
browser) a `<code>` tag with a `<script>` tag inside in the text of the
draft version. After doing so, commenting on that text would result in
the attacker's JavaScript being executed.
2019-10-08 18:46:20 +02:00
Javi Martín
0f485308b7 Sanitize CKEditor content before displaying it
It's possible to create a newsletter or a proposed action with
<script> tags by filling in the body using a textarea instead of a
CKEditor. While we trust our administrators not to do so, it's better to
completely eliminate that possibility.
2019-10-08 18:46:20 +02:00