Visitar URL original
Ruby: flag up `protect_from_forgery` calls without an exception strategy by alexrford · Pull Request #7611 · github/codeql · GitHub
The Wayback Machine - https://web.archive.org/web/20260105055448/https://github.com/github/codeql/pull/7611
Skip to content

Conversation

@alexrford
Copy link
Contributor

Calling protect_from_forgery without specifying a protection strategy defaults to nulling the session. However, if parts of the session are memoized (e.g. in previous before_actions), then this may not be sufficient to avoid CSRF attacks, as this memoization can take place before the session is nulled.

See https://www.veracode.com/blog/managing-appsec/when-rails-protectfromforgery-fails for details

@github-actions
Copy link
Contributor

QHelp previews:

ruby/ql/src/queries/security/cwe-352/CSRFProtectionDisabled.qhelp

CSRF protection weakened or disabled

Cross-site request forgery (CSRF) is a type of vulnerability in which an attacker is able to force a user carry out an action that the user did not intend.

The attacker tricks an authenticated user into submitting a request to the web application. Typically this request will result in a state change on the server, such as changing the user's password. The request can be initiated when the user visits a site controlled by the attacker. If the web application relies only on cookies for authentication, or on other credentials that are automatically included in the request, then this request will appear as legitimate to the server.

A common countermeasure for CSRF is to generate a unique token to be included in the HTML sent from the server to a user. This token can be used as a hidden field to be sent back with requests to the server, where the server can then check that the token is valid and associated with the relevant user session.

Recommendation

In many web frameworks, CSRF protection is enabled by default. In these cases, using the default configuration is sufficient to guard against most CSRF attacks.

Example

The following example shows a case where CSRF protection is disabled by skipping token verification.

class UsersController < ApplicationController
  skip_before_action :verify_authenticity_token
end

Verification can be re-enabled by removing the call to skip_before_action.

Care should be taken when using the Rails protect_from_forgery method to prevent CSRF. The default behaviour of this method is to null the session when an invalid CSRF token is provided. This may not be sufficient to avoid a CSRF vulnerability - for example if parts of the session are memoized. Calling protect_from_forgery with: :exception can help to avoid this by raising an exception on an invalid CSRF token instead.

References

@github-actions
Copy link
Contributor

QHelp previews:

ruby/ql/src/queries/security/cwe-352/CSRFProtectionDisabled.qhelp

CSRF protection weakened or disabled

Cross-site request forgery (CSRF) is a type of vulnerability in which an attacker is able to force a user carry out an action that the user did not intend.

The attacker tricks an authenticated user into submitting a request to the web application. Typically this request will result in a state change on the server, such as changing the user's password. The request can be initiated when the user visits a site controlled by the attacker. If the web application relies only on cookies for authentication, or on other credentials that are automatically included in the request, then this request will appear as legitimate to the server.

A common countermeasure for CSRF is to generate a unique token to be included in the HTML sent from the server to a user. This token can be used as a hidden field to be sent back with requests to the server, where the server can then check that the token is valid and associated with the relevant user session.

Recommendation

In many web frameworks, CSRF protection is enabled by default. In these cases, using the default configuration is sufficient to guard against most CSRF attacks.

Example

The following example shows a case where CSRF protection is disabled by skipping token verification.

class UsersController < ApplicationController
  skip_before_action :verify_authenticity_token
end

Verification can be re-enabled by removing the call to skip_before_action.

Care should be taken when using the Rails protect_from_forgery method to prevent CSRF. The default behaviour of this method is to null the session when an invalid CSRF token is provided. This may not be sufficient to avoid a CSRF vulnerability - for example if parts of the session are memoized. Calling protect_from_forgery with: :exception can help to avoid this by raising an exception on an invalid CSRF token instead.

References

@alexrford alexrford marked this pull request as ready for review January 17, 2022 19:07
@alexrford alexrford requested a review from a team as a code owner January 17, 2022 19:07
def user_authored_article?
@article.author_id = current_user.id
end
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this file here to demonstrate the danger of having memoised values (current_user) and weakened CSRF settings? As far as I can tell the tests don't actually depend on this file, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, the file is technically not necessary. It's just for some context into how the non-with: :exception calls can be vulnerable.


# BAD: `protect_from_forgery` without `with: :exception` can expose an
# application to CSRF attacks in some circumstances
protect_from_forgery
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be good to have an example controller with protect_from_forgery with: :exception to confirm that the query doesn't flag that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a case of this (with a new action) to the ArticlesController.

Copy link
Contributor

@hmac hmac left a comment

Choose a reason for hiding this comment

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

This looks good to me. Just had a couple of comments. Since we're expanding the range of this query, I guess we might as well add a change note too?

Ping me when you're ready for a re-review

@github-actions
Copy link
Contributor

QHelp previews:

ruby/ql/src/queries/security/cwe-352/CSRFProtectionDisabled.qhelp

CSRF protection weakened or disabled

Cross-site request forgery (CSRF) is a type of vulnerability in which an attacker is able to force a user carry out an action that the user did not intend.

The attacker tricks an authenticated user into submitting a request to the web application. Typically this request will result in a state change on the server, such as changing the user's password. The request can be initiated when the user visits a site controlled by the attacker. If the web application relies only on cookies for authentication, or on other credentials that are automatically included in the request, then this request will appear as legitimate to the server.

A common countermeasure for CSRF is to generate a unique token to be included in the HTML sent from the server to a user. This token can be used as a hidden field to be sent back with requests to the server, where the server can then check that the token is valid and associated with the relevant user session.

Recommendation

In many web frameworks, CSRF protection is enabled by default. In these cases, using the default configuration is sufficient to guard against most CSRF attacks.

Example

The following example shows a case where CSRF protection is disabled by skipping token verification.

class UsersController < ApplicationController
  skip_before_action :verify_authenticity_token
end

Verification can be re-enabled by removing the call to skip_before_action.

Care should be taken when using the Rails protect_from_forgery method to prevent CSRF. The default behaviour of this method is to null the session when an invalid CSRF token is provided. This may not be sufficient to avoid a CSRF vulnerability - for example if parts of the session are memoized. Calling protect_from_forgery with: :exception can help to avoid this by raising an exception on an invalid CSRF token instead.

References

@alexrford
Copy link
Contributor Author

@hmac thanks for the review - updated.

@github-actions
Copy link
Contributor

QHelp previews:

ruby/ql/src/queries/security/cwe-352/CSRFProtectionDisabled.qhelp

CSRF protection weakened or disabled

Cross-site request forgery (CSRF) is a type of vulnerability in which an attacker is able to force a user carry out an action that the user did not intend.

The attacker tricks an authenticated user into submitting a request to the web application. Typically this request will result in a state change on the server, such as changing the user's password. The request can be initiated when the user visits a site controlled by the attacker. If the web application relies only on cookies for authentication, or on other credentials that are automatically included in the request, then this request will appear as legitimate to the server.

A common countermeasure for CSRF is to generate a unique token to be included in the HTML sent from the server to a user. This token can be used as a hidden field to be sent back with requests to the server, where the server can then check that the token is valid and associated with the relevant user session.

Recommendation

In many web frameworks, CSRF protection is enabled by default. In these cases, using the default configuration is sufficient to guard against most CSRF attacks.

Example

The following example shows a case where CSRF protection is disabled by skipping token verification.

class UsersController < ApplicationController
  skip_before_action :verify_authenticity_token
end

Verification can be re-enabled by removing the call to skip_before_action.

Care should be taken when using the Rails protect_from_forgery method to prevent CSRF. The default behaviour of this method is to null the session when an invalid CSRF token is provided. This may not be sufficient to avoid a CSRF vulnerability - for example if parts of the session are memoized. Calling protect_from_forgery with: :exception can help to avoid this by raising an exception on an invalid CSRF token instead.

References

Copy link
Contributor

@hmac hmac left a comment

Choose a reason for hiding this comment

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

Nice!

@alexrford alexrford merged commit 9613ff7 into main Jan 20, 2022
@alexrford alexrford deleted the ruby/protect_from_forgery-without-exception branch January 20, 2022 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants