Mass Assignment 防止Hacked

本文探讨了Rails应用中如何防止批量赋值攻击,介绍了使用白名单和黑名单两种方式来限制敏感字段的更新,并给出了具体的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

涉及到一个安全问题,可能有人会利用这个问题进行Hack,加以注意可以避免。

官方说明地址:http://guides.rubyonrails.org/security.html#mass-assignment

Railscasts-china视频地址:http://railscasts-china.com/episodes/mass-assignment?autoplay=true 由Terry Tai进行讲解,个人觉得讲得很不错。学习很多。

这篇文章介绍的方法很不错:http://excid3.com/blog/rails-tip-7-mass-assignment-security/。通过白名单 + params[:user].delete :is_admin 的方式来满足admin对 is_admin这个属性的操作

这里涉及到的文件有:

  1. config/application.rb
    config.active_record.whiltelist_attributes = true 
  2. Model 里的 attr_accessible 与 attr_protected使用
    # 黑名单
    attr_protected :is_admin
    
    # 白名单
    attr_accessible :email, :username 
  3. Controller里的 params[:user]使用

问题的产生:

有一个名为User的Model,属性如上所述, 以及相对应的Contorller:UsersContoller。如果把 is_admin属性写在 accessible里,则在填写表单时,在console里把user[email] 改为 user[is_admin],然后填入非0数据,就可以把is_admin的值设置成为 true 。因为在Contoller里我们是这样写的:

File: users_controller.rb

def update    
    if @user.update_attributes(params[:user])
      redirect_to @user, notice: 'User was successfully updated.'
    else
      render action: "edit"
    end
end

 

这样也就会全盘地接受传过来的值,导致把 is_admin的值也被更改。

解决办法

综合视频以及其他一些博客给出的方法,有以下几种:

  1. 黑名单:在Model里,把 is_admin 用 attr_protected进行保护,不被访问到,即黑名单。坏处是不够灵活,会影响到Model的其他的行为
    class User < ActiveRecord::Base
      attr_protected :is_admin
    end 
  2. 白名单:不把is_admin写在attr_accessible里面,这样也不会被访问到
    class User < ActiveRecord::Base
      attr_accessible :email, :username
    end  
  3. 在Controller里,对传入的参数进行过滤选择: params[:user].slice(:email, :username),这时Model里面不要把 is_admin 用attr_accessible 来控制。
    class UsersController < ApplicationController
      def create    
        @user = User.find(params[id])
        if @user.update_attributes(user_params)
          redirect_to @user, notice: 'User was successfully updated.'
        else
          render action: "new"
        end
      end
    
      private
      def user_params
        params[:user].slice(:email, :username)
      end
    
    end

     通过加入一个 user_params 方法,来对传入的参数进行筛选,把 is_admin 排除在外。这种做法好处在于可以灵活定制,但会Repeat Yourself。 

     

    总体来说,个人觉得还是在Controller里进行一个安全过滤比较靠谱。

    关于白名单:在 config/application.rb 里有一行内容为“config.active_record.whiltelist_attributes = true ”,就是说明,在每一个Model里都要显式地声明一个 attr_accessible,否则会出错。如果不想使用这功能,可以把true 设置为 false

转载于:https://www.cnblogs.com/peterzd/archive/2012/11/27/2791552.html

防止Mass Assignment漏洞,也就是防止未经授权的数据注入,有几种常见的方法在Spring Boot中实施: 1. **使用`@RestControllerAdvice`和`@ModelAttribute`**:通过创建一个全局的`@RestControllerAdvice`,你可以定义一个方法拦截所有`@ModelAttribute`绑定的请求,并手动验证数据是否来自可信来源。 ```java @RestControllerAdvice public class GlobalExceptionHandler { @InitBinder public void initBinder(WebDataBinder binder) { binder.addValidators(new UniqueValidator<>()); } @ExceptionHandler(MassAssignmentException.class) public ResponseEntity<Error> handleMassAssignment(MassAssignmentException e) { return ResponseEntity.badRequest().body(new Error(e.getMessage())); } } ``` 2. **启用`Thymeleaf`模板引擎的安全模式**:如果你使用Thymeleaf作为视图技术,可以开启其安全模式来避免直接绑定JavaScript表达式中的变量。 3. **控制HTTP头信息**:在生产环境中,你可以设置`X-XSS-Protection`等头部信息,提示客户端不要尝试将请求体中的数据赋值给不可信的属性。 4. **使用`@JsonInclude(JsonInclude.Include.NON_NULL)`**:当序列化模型对象时,仅包含非空字段,防止意外的属性注入。 5. **`@JsonAutoDetect`注解**:在Spring MVC中,可以使用`@JsonAutoDetect(fieldVisibility= JsonAutoDetect.Visibility.ANY)`来控制JSON序列化时可见的字段,隐藏不必要的敏感信息。 6. **启用Spring Security的`WebMvcSecurityConfigurerAdapter`**:在Spring Security配置中,明确指定哪些属性是可以转换成方法参数的,如上述例子所示。 重要的是,始终保持警惕并只允许必需的数据访问,避免直接暴露业务层的敏感信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值