百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

angular_rails_csrf

> 编程语言
开源

针对 AngularJS 风格的 CSRF 防护的 Rails 集成

181 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

针对 AngularJS 风格的 CSRF 防护的 Rails 集成

AngularJS-style CSRF Protection for Rails

This gem is in passive maintenance mode:

James and then me (@bodrovis) have been supporting this gem since 2013. It has been downloaded more than 10 million times and we're glad you guys found it useful. However, after discussing privately we've decided to put angular_rails_csrf under passive maintenance starting from June 2025.

In modern frontend–backend architectures (e.g., Angular, React, Vue + Rails APIs), CSRF protection is typically handled via token-based authentication (JWT, OAuth) and not via CSRF cookies. This gem remains relevant only for Rails monoliths that:

  • Use cookie-based session auth
  • Serve frontend via Rails
  • Expect XSRF-TOKEN / X-XSRF-TOKEN pattern

The AngularJS ng.$http service has built-in CSRF protection. By default, it looks for a cookie named XSRF-TOKEN and, if found, writes its value into an X-XSRF-TOKEN header, which the server compares with the CSRF token saved in the user's session.

This project adds direct support for this scheme to your Rails application without requiring any changes to your AngularJS application. It also doesn't require the use of csrf_meta_tags to write a CSRF token into your page markup, so it works for pure JSON API applications.

Note that there is nothing AngularJS specific here, and this will work with any other front-end that implements the same scheme.

Check version compatibility to learn which Rails/Rubies are currently supported.

Installation

Add this line to your application's Gemfile:

ruby
gem 'angular_rails_csrf'

And then execute:

bash
$ bundle

That's it!

Configuration

Cookie Name

The default cookie's name is XSRF-TOKEN but it can be configured with the angular_rails_csrf_cookie_name setting:

ruby
# application.rb
class Application < Rails::Application
  #...
  config.angular_rails_csrf_cookie_name = 'CUSTOM_NAME'
end

Cookie Domain

Starting from version 3, you may set domain for the XSRF cookie:

ruby
# application.rb
class Application < Rails::Application
  #...
  config.angular_rails_csrf_domain = :all
end

If angular_rails_csrf_domain is not set, it defaults to nil.

Secure Cookie

To set a "secure" flag for the cookie, set the angular_rails_csrf_secure option to true:

ruby
# application.rb
class Application < Rails::Application
  #...
  config.angular_rails_csrf_secure = true
end

angular_rails_csrf_secure defaults to false.

SameSite

The SameSite attribute defaults to :lax. You can override this in the config:

ruby
# application.rb
class Application < Rails::Application
  #...
  config.angular_rails_csrf_same_site = :strict
end

NOTE: When using config.angular_rails_csrf_same_site = :none, this gem automatically sets the cookie to Secure (config.angular_rails_csrf_secure = true) to comply with the specifications.

Please note that Safari is known to have issues with SameSite attribute set to :none.

HttpOnly Cookie

To set the "httponly" flag for your cookie, set the angular_rails_csrf_httponly option to true:

ruby
# application.rb
class Application < Rails::Application
  #...
  config.angular_rails_csrf_httponly = true
end

angular_rails_csrf_httponly defaults to false.

Exclusions

Sometimes you will want to skip setting the XSRF token for certain controllers (for example, when using SSE or ActionCable, as discussed here):

ruby
class ExclusionsController < ApplicationController
  exclude_xsrf_token_cookie

  # your actions here...
end

Testing

Run

bash
$ bundle install

and then

bash
$ rake test

License

Licensed under the MIT License.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言