代码之家  ›  专栏  ›  技术社区  ›  San Diago

Rails:如何生成404或重定向不受欢迎的url攻击?

  •  2
  • San Diago  · 技术社区  · 14 年前

    我想隐藏的网址编辑用户和他们的档案背后更安全和有意义的网址。例如,我想 /user/13/edit 成为 /settings/account /user/13/profile/edit 成为 /settings/profile

    我设法做到了这一点,但为此我必须从 current_user 会话中的位。像这样:

    # users_controller
    def edit
      @user = current_user
    end
    
    # profiles_controller
    def edit
      @user = current_user
      @profile = @user.profile
    end
    

    但是现在,既然我不能比较 @user.id params 当前用户 在会话中,如何停止旧的URL( /用户/13/编辑 /用户/13/配置文件/编辑

    提前谢谢。

    2 回复  |  直到 14 年前
        1
  •  1
  •   Patrick Klingemann    14 年前

    首先,身份验证机制需要设置当前用户。

    map.account '/settings/account', :controller => 'user', :action => 'edit' 
    map.profile '/settings/profile', :controller => 'user', :action => 'edit_profile'
    
    map.resources :users, :only => [:edit, :update, :show],
                  :member => { :edit_profile => :get, :update_profile, :put }
    

    这将生成以下路由:

    /settings/account         (get)
    /settings/profile         (get)
    /users/:id                (get, put)
    /users/:id/edit           (get)
    /users/:id/edit_profile   (get)
    /users/:id/update_profile (put)
    

    before_filter :redirect_if_unauthorized
    
    def edit
      @user = current_user
    end
    
    # profiles_controller
    def edit
      @user = current_user
      @profile = @user.profile
    end
    
    protected
    
    def redirect_if_unauthorized
      redirect_to some_path if params[:id] or current_user.nil?
    end
    

    显然有些路径不存在,您必须创建一个页面/路径等来显示错误。

    使用此解决方案,您永远不会基于params[:id]显示/操作用户,而只显示由身份验证方案保存的当前用户。


    Github , Railscast )

        2
  •  0
  •   imightbeinatree at Cloudspace    14 年前