angularjs - 应在 MEAN 堆栈中的哪个位置创建验证规则?

标签 angularjs node.js mongodb express mean-stack

我正在使用 MEAN 堆栈创建一个应用程序,该堆栈有大量表单数据条目,这些数据条目会从经过身份验证的用户和匿名用户保存到数据库中。

我应该在堆栈的哪个位置创建所有验证规则?它们应该在 AngularJS 中吗?但我希望我的服务器端 API 是安全的,所以也许它们应该在 Express 中,然后冒泡到 AngularJS?或者它们应该一直在 MongoDB 层(我将使用 Mongoose,因此很容易在那里创建验证)。

目前我把它们散布在各处,并且发现自己在重复规则。我想避免这种情况并在一个地方创建规则。那么在 MVW 应用程序中定义验证的一般规则是什么,以及最好将它们放在哪一层(特别是对于 MEAN 应用程序)?

最佳答案

根据 OWASP 建议

Where to include validation Validation must be performed on every tier. However, validation should be performed as per the function of the server executing the code. For example, the web / presentation tier should validate for web related issues, persistence layers should validate for persistence issues such as SQL / HQL injection, directory lookups should check for LDAP injection, and so on.

您需要到处验证。

在 Angular 部分中,您在客户端验证问题,而无需一路返回服务器,因此,您应该尽快防止此类问题。

在 Express 上,您需要进行验证,因为您无法信任您的前端。

在 Mongo 上,您需要验证访问、权限、要插入的数据等。

为什么要验证?

The most common web application security weakness is the failure to properly validate input from the client or environment. This weakness leads to almost all of the major vulnerabilities in applications, such as Interpreter Injection, locale/Unicode attacks, file system attacks and buffer overflows. Data from the client should never be trusted for the client has every possibility to tamper with the data.

来自同一来源:

https://www.owasp.org/index.php/Data_Validation

关于angularjs - 应在 MEAN 堆栈中的哪个位置创建验证规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33398077/

相关文章:

javascript - 我的 NodeJS 初始化给出了一个奇怪的错误并且没有更新

javascript - Gulp 命令在记事本中打开 gulp.js 而不是运行它

json - 无法迭代 Node 、express 应用程序中的 json 对象

javascript - AngularJS Karma Jasmine 指令测试不呈现 html

AngularJS |使用 $http.get 方法时设置路径参数

angularjs - 带有 ngModelController 格式化程序的 Angular 指令

javascript - 如何在 nodejs 上使用对象 jSON 数据进行嵌套循环

node.js - 如何将 $split 运算符与聚合结合使用

node.js - nodejs + azure 函数应用程序 - 处理数据库连接的最佳实践?

javascript - 使用 $resource 并在多个 Controller 之间共享结果的最佳实践是什么?