函数或调用者应该负责输入验证吗?

发布于 2024-09-05 04:48:54 字数 174 浏览 9 评论 0原文

我正在对一个相当大的 php 应用程序进行安全审核,并且想知道应该在哪里包含用户输入验证。

我应该验证数据,然后将干净的数据发送到后端函数,还是应该依赖每个函数来进行自己的验证?或者两者兼而有之?

这类事情有什么标准或最佳实践吗?

目前,该应用程序的表现不一致,我想让事情变得更加一致。

I'm doing a security audit on a fairly large php application and was wondering where I should include my user-input validation.

Should I validate the data, then send the clean data off to the back-end functions or should I rely on each function to do it's own validation? Or even both?

Is there any standard or best-practice for this sort of thing?

Currently the app does both inconsistently and I'll like to make things more consistent.

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

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

发布评论

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

评论(5

追风人 2024-09-12 04:48:54

您绝对应该尽快验证来自外部的数据。根据架构的不同,负责函数内部的后端验证可能是第二步,但不依赖于后端验证,而是在数据进入应用程序时验证数据。

函数内部验证作为对先前验证的补充的优点是,维护系统更容易(也更安全),因为(更草率的)开发人员在您无法破坏应用程序之后。如果您的应用程序支持插件,例如第三方插件,那么安全功能也是必须的。

You should definitely validate the data from the outside as soon as possible. Depending on the architecture, backend validation inside the responsible functions can be a second step, but don't depend on backend validation but validate the data when it comes in to your application.

The pros with validation inside functions as a complement to the previous validation is that it's easier (and safer) to maintain the system because (sloppier) developers after you can't break the application. If you have an application with plugin support, e.g. for third party plugins, safe functions is a must also.

温折酒 2024-09-12 04:48:54

两者都是更好的答案。数据验证应该在每个处理数据的函数中进行,以避免希望驱动开发的问题(

Both is the better answer. Data validation should happen in every function that will be handling the data to avoid the problem of Hope Driven Development (HDD)

伪装你 2024-09-12 04:48:54

我认为如果你可以两者都做,并且时间/资源不是问题,为什么不呢?

I think if you can do both, and time / resources are not an issue, why not?

空城旧梦 2024-09-12 04:48:54

在后端进行验证就像在乘客登机后对其进行筛选一样。验证的全部目的是防止注入可能阻塞您的应用程序的元素。所以进门前必须先进行验证:)

Validating at the backend is like screening passengers after they have boarded the plane. The whole point of validation is to prevent injecting elements that might choke up your app. So you must validate before you enter the gate :)

晒暮凉 2024-09-12 04:48:54

这取决于应用程序的范围/定义。但传统上,您的函数在许多地方使用 $object->doSomething() 就是这样做的。通过依赖那里的验证,你可以阻止你自己的协议的 doSomething() 的能力,你知道吗?

同样,如果您将验证保留在外部,则可以轻松管理它。无需在特定的内部函数中寻找它。保持 OOP,但更像

$data = $validator->sanitizeSomething($data);
$object->doSomething($data);

这使您的验证规则以及内部功能保持独立且易于管理。

详细地说,假设您有一个 db 对象,它将一个数组添加到表中:

class db {
   function addRow($table, $associativeArray) {
      // primitive i know, just an example 
   }
}

您希望在其中进行验证吗?

function addRow($table, $associativeArray) {
    if( isset( $assiciativeArray['description'] ) {
       // validate
    }
}

会很愚蠢 - 你会希望它出现在你正在使用的对象中

class product {
   function update() {
       if( $this->validate() ) {
          $this->db->addRow($this->toArray()); // or something, you get the idea, ya?
       }
   }
   function validate() {
      if( $this->description != "") {
         return true;
      }
      return false;
   }
}

It depends on the scope/definition of the application. But traditionally, your functions are used in may places $object->doSomething() does just that. By relying on validation in there, you prevent the ability to doSomething() of your OWN acccord, ya know?

Too, if you keep validation outside you can easily manage it. No need to hunt it down in that particular internal function. Keep it OOP, but more like

$data = $validator->sanitizeSomething($data);
$object->doSomething($data);

this keeps your validation rules separate and easy to manaage as well as your internal functions.

To elaborate, say you have a db object that adds an array to the table:

class db {
   function addRow($table, $associativeArray) {
      // primitive i know, just an example 
   }
}

would you want your validation in there?

function addRow($table, $associativeArray) {
    if( isset( $assiciativeArray['description'] ) {
       // validate
    }
}

would be silly - you'd want that in the object you're working in

class product {
   function update() {
       if( $this->validate() ) {
          $this->db->addRow($this->toArray()); // or something, you get the idea, ya?
       }
   }
   function validate() {
      if( $this->description != "") {
         return true;
      }
      return false;
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文