Apache Thrift 是否验证参数?

发布于 2024-11-16 13:08:56 字数 211 浏览 5 评论 0原文

我正在考虑使用 Apache Thrift 作为实现 Web 服务的 PHP 服务器。

如果我的 Web 服务被定义为接受两个参数(字符串形式的用户名和整数形式的密码),则会节俭地验证这些类型的参数是否由客户端提供,或者我是否仍然需要执行验证服务器?

我在这里并不是为了清理输入,而是为了向客户端返回有意义的错误响应,以及如果使用不正确的参数调用服务,是否会向服务器发出请求。

I'm considering using Apache Thrift for a PHP server that implements web services.

If my web service were to be defined to accept two parameters - a user name as a string, and a password as an integer - would thrift validate that parameters of those types were supplied by the client, or do I still need to perform validation on the server?

I'm not asking here for the purposes of sanitising input, but rather for returning meaningful error responses to clients, and whether if a service is invoked with incorrect parameters requests will even be made to the server.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

饮惑 2024-11-23 13:08:56

据我了解,检查(如果有的话)是在“库级别”执行的,其中本地类型被转换为 Thrift 类型,代码生成发生在其中。换句话说,如果类型不受尊重,它将取决于您所使用的特定语言(PHP、Erlang 等等)的绑定,以引发有意义的或无意义的错误。但我必须再仔细研究一下。

To my understanding, the check - if any - is performed at "library level", where the local types are translated into Thrift types, where the code generation happens. In other words, it will depend on the bindings for the specific language you are using (PHP, Erlang, whatever), to raise meaningful - or not - errors if types are not respected. But I have to look into it a bit more.

过去的过去 2024-11-23 13:08:56

这基本上取决于我们正在谈论哪种验证。

如果需要设置参数,可以将它们设置为“必需”。在这种情况下,在反序列化期间,大多数语言实现(并非全部)都会检查字段,并且每当缺少 reuirwed 参数时就会抛出错误。然而,必需的代价是存在版本控制的潜在问题:一旦 API 发布,您就不能省略必填字段,否则会破坏兼容性。

通常可以通过 _isset 标志来检查正常(“可选”)值的存在,该标志正是为此目的而存在的。这是您自己的代码负责的事情 - Thrift 确实提供了消息传递机制,但当然不提供数据的解释。

如果您的情况下的验证意味着应检查数据格式(字符串和数字):因为客户端和服务器的代码都是从 Thrift IDL 文件生成的,所以实现将根据以下内容使用正确的字段序列化和反序列化方法: IDL 文件:

Service sample
{
  bool Login( 1: string Name, 2: i32 pwd)
}

如果您出于某种原因需要将密码从 i32 更改为字符串,从技术上讲,这会导致两个更改:

  1. 删除 ID 为 2 的密码字段,除非(如果该字段为“必填”)
  2. 添加一个新领域新的 ID 3,

因此它看起来像这样:

Service sample
{
  bool Login( 1: string Name, /*2: i32 _deprecated_pwd*/, 3: string pwd)
}

由于字段类型和字段用途绑定到数字字段 ID,因此强烈建议在这种情况下使用另一个字段 ID,该字段迄今为止在此范围内未使用。还建议保留 IDL 中的旧字段以指示过时的 ID。

关于“软版本控制”的内容以及“必填”字段的优缺点,可以在 Diwaker Gupta 的“Missing Guide”中找到一个很好的参考。

It basically depends on what kind of validation we are talking.

If the parameters need to be set, you could make them "required". In that case, the fields are checked in most language implementations (not in all) during deserialization and an error is thrown whenever a reuirwed Parameter is missing. However, required comes at the cost of potential problems with versioning: you can't omit a required field once the API has been published, or it will break compatibility otherwise.

The presence of normal ("optional") values can typically be checked by means of the _isset flags, which exist for exactly this purpose. This is something that your own code is responsible for - Thrift does provide the messaging mechanisms, but of course not the interpretation of your data.

If validation in your case means, that the data Format (string and number) should be checked: Because the code for both Client and Server is generated from the Thrift IDL file, the implementation will use the correct field serialization and deserialization method according to the IDL file:

Service sample
{
  bool Login( 1: string Name, 2: i32 pwd)
}

If you Need to Change for whatever reason the Password from i32 into string, technically this results in two changes:

  1. remove the Password field with the ID 2, except if the field is "required"
  2. add a new field with the new ID 3

so it looks this way:

Service sample
{
  bool Login( 1: string Name, /*2: i32 _deprecated_pwd*/, 3: string pwd)
}

As field type and field purpose are bound to the numeric field ID, it is highly recommended to use another field ID in such a case, which is so far unused within this scope. It is also recommended to leabve the old fields in the IDL to indicate outdated IDs.

A good reference about this "soft versioning" stuff and the pros and cons of "required" fields can be found in Diwaker Gupta's "Missing Guide".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文