【问题标题】:Security aspects of ES6 Import - Using on the client side of MeteorES6 导入的安全方面 - 在 Meteor 的客户端使用
【发布时间】:2018-07-29 23:10:14
【问题描述】:

在官方Meteor guide 中说;

在服务器上运行的代码是可以信任的。其他一切:代码 在客户端上运行,通过 Method 和发布发送的数据 论据等是不可信的。

还有;

应用中的秘密业务逻辑应位于以下代码中 只加载在服务器上。这意味着它位于 server/ 目录中 您的应用程序,在仅包含在服务器上的包中,或在 仅在服务器上加载的包中的文件。

敏感的方法/算法等必须放在服务器端。我的第一个问题是,我们如何从客户端安全地调用服务器端的敏感方法,比如 createUser()?

我的第二个问题;在安全性方面使用Meteor.method 和Validated-Method 有什么区别吗?调用标准 Meteor 方法时不需要使用 import 语句,但如果调用 Validated-Method 则需要导入它。对于同一个 createUser() 示例,最好在 Meteor 方法中定义它以提高安全性?

【问题讨论】:

  • import 不调用服务器端方法。
  • 您是否尝试过将在服务器上声明的函数导入到位于客户端文件夹中的文件中?
  • @Jankapunkt 服务器文件对 Meteor 中的客户端文件不可见,即使您导入它们也是如此。
  • 正确,这就是为什么您无法将 Meteor 方法导入客户端的原因。因此,唯一的漏洞在于方法的设计(参数、方法内执行的代码、权限检查等)
  • @Jankapunkt 感谢您的指出,我编辑了问题以消除歧义。

标签: javascript meteor import ecmascript-6 es6-modules


【解决方案1】:

在官方 Meteor 指南中说;

流星指南想说的是:

Meteor 是一个完整的堆栈框架,可以通过多种方式解决许多产品需求(在服务器和客户端中分配代码)。假设您想在每次购买商品时向客户收取 20% 的费用。

解决方案 1:向客户收取 20% 的费用

Template.yourTemplate.events({
// ... other events
'click .buyme': function(event, template) {
   // Suppose you have product id in element's id attr
   let productId = event.target.id,
       product = Products.findOne({_id: productId}),
       charge = Math.ceil(product.price * 0.2);

   // Add a order
   Order.insert({
     charge,
     productId,
     userId: Meteor.userId()
   })
},
// ... other events
})

解决方案 1:在服务器上收取 20% 的费用

Meteor.methods({
// ... other methods
'order': function(productId) {
   // Suppose you have product id in element's id attr
   let product = Products.findOne({_id: productId}),
       charge = Math.ceil(product.price * 0.2);

   // Add a order
   Order.insert({
     charge,
     productId,
     userId: Meteor.userId()
   })
},
// ... other methods
})

从服务器调用方法。

您现在必须清楚,我们不能相信解决方案 1,对吧?

在安全性方面使用 Meteor.method 和 Validated-Method 有什么区别吗?

不,当然不是。请参阅https://github.com/meteor/validated-method 以了解有关验证方法的更多信息。您会看到两者之间的主要区别在于Metor.Methods 依赖于magic string 来访问方法,但另一方面validated-method 提供了一个用于访问方法的对象。而且,这就是为什么我们需要导入而不只是 Method.call()。

【讨论】:

    猜你喜欢
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 2011-08-22
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多