gpt4 book ai didi

ruby-on-rails - 防止修改 ("hacking") 隐藏字段在 rails3 中的形式?

转载 作者:太空宇宙 更新时间:2023-11-03 17:26:40 25 4
gpt4 key购买 nike

假设我有一个用于提交新帖子的表单。

表单有一个隐藏字段,它指定了 category_id。我们也在该类别的展示 View 中。

我担心的是,有人使用诸如 firebug 之类的东西,可能只是在代码中编辑类别 ID,然后提交表单 - 为不同的类别创建帖子。

显然我的表单更复杂,场景也不同——但思路是一样的。我也无法在帖子的创建 Controller 中定义类别,因为每个显示 View 中的类别都不同...

有什么解决办法吗?

编辑:

这是一个更好的问题 - 如果它不在隐藏字段中,是否可以在帖子的创建 Controller 中获取类别 ID?

最佳答案

您的网站是否有关于类别本身的权限/访问控制列表的概念?如果用户可以访问其他类别,那么我会说这里不用担心,因为没有什么可以阻止他们转到其他类别并执行相同的操作。

如果您的类别在某种程度上受到限制,那么我建议将您的帖子嵌套在一个类别下(嵌套资源路由)并执行 before_filter 以确保您被授予访问适当类别的权限。

config/routes.rb

resources :categories do 
resources :posts
end

app/controllers/posts_controller

before_filter :ensure_category_access

def create
@post = @category.posts.new(params[:post])
...
end

private
def ensure_category_access
@category = Category.find(params[:category_id])
# do whatever you need to do. if you don't have to validate access, then I'm not sure I'd worry about this.
# If the user wants to change their category in their post instead of
# going to the other category and posting there, I don't think I see a concern?
end

URL 看起来像

得到/categories/1/posts/new邮政/categories/1/posts

关于ruby-on-rails - 防止修改 ("hacking") 隐藏字段在 rails3 中的形式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7536425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com