0

我必须对在 Ruby On Rails 中开发的 API 进行更改,如下所示:

class V0::PythonsController < ApplicationController
  skip_before_action :authorize_request

  # POST v0/python/import
  def import
    result = { status: :error }
    data = eval(AwsTool.decrypt(params["code"])).first
    if data.class == Hash
      user = User.find_by(id: data[:ot_user_id])
      activity_type = ActivityType.find_by(id: data[:activity_type])
      if user.nil?
        result[:msg] = "user not found"
      elsif activity_type.nil?
        result[:msg] = "activity type not found"
      else...

我将“代码”参数中的一些数据传递给它,然后对其进行解密并进行探索。我想添加一个 if 子句,所以当我从不同的来源调用 API 时,不会发生加密和解密。所以我做了这个改变:

class V0::PythonsController < ApplicationController
  skip_before_action :authorize_request

  # POST v0/python/import
  def import
    result = { status: :error }
  if params["origin"] != 'trusted'
    data = eval(AwsTool.decrypt(params["code"])).first
  else
    data = params["code"]
  end
  if data.class == Hash
    user = User.find_by(id: data[:ot_user_id])
    activity_type = ActivityType.find_by(id: data[:activity_type])
    ...

问题是 data.class 不是一个 Hash 对象,它是一个字符串。我尝试了不同的解决方案来将对象从 String 转换为 Hash,例如 t_hash 和其他类似函数,但它们不起作用。我收到一些关于不允许参数的错误,我尝试将许可证添加到它们但仍然失败。

还有什么想法吗?

4

1 回答 1

1

它失败了,因为您忘记调用eval代码。做这个:

data = eval(params["code"])

顺便说一句,评估输入是非常危险的。我希望你相信任何使用这个 API 的人。

于 2019-02-28T02:14:13.087 回答