javascript - 在 url 中传递查询的安全性如何

标签 javascript node.js security url google-cloud-firestore

我正在网上销售一些类(class),因此对于付款购物车,我实现了一种通过 URL 中的查询从 firestore 获取类(class)数据的方法,例如:localhost:5000/?product=course1.

所以,我想知道它在注入(inject)或其他漏洞方面有多安全

我实现了简单的验证。但没什么大不了的。

这是产品发布请求:

router.post("/courses", (req, res) => {
  const product = req.body.product;

  res.redirect("/payment?product=" + product);
});

这是付款页面:

router.get("/payment", async (req, res) => {
  console.log(req.query.product);

  const snapshot = await db.collection("products").get();
  const products = await snapshot.docs.map(doc => {
    return {
      name: doc.id,
      price: doc.data().price
    };
  });
  thisProduct = products.find(product => {
    return req.query.product === product.name;
  });
  console.log(thisProduct);
  if (typeof thisProduct == "undefined") {
    return res.send("product not found");
  }

  res.render("payment", {
    key: "pk_test_fVJwSNZpMoCwrF7Zs48PsLR100zpmBhXrc",
    user: true,
    title: "Pay for a course",
    product: {
      name: thisProduct.name,
      price: thisProduct.price
    }
  });
});

如果存在任何漏洞,请告诉我以及如何修复它们。 如果您没有发现任何漏洞,请发表评论。 谢谢!

最佳答案

我在您展示的代码中没有发现任何漏洞。一般来说,为了避免任何类型的注入(inject),您应该正确验证所有输入。 IE。如果您希望产品是一个数字,您应该检查它是否是一个数字。此外,避免 SQL 注入(inject)的一般做法是不对 SQL 查询使用字符串插值(而且您也不会这样做👍),而是使用准备好的语句或/和 ORM。

因此,在采取一切预防措施的情况下,在 URL 查询中发送参数与发送任何其他类型的参数一样安全。

关于javascript - 在 url 中传递查询的安全性如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57236806/

相关文章:

javascript - redux 中的应用程序与本地状态

javascript - 在 Laravel 4 上使用循环将数据数组存储到单个数组中

node.js - 使用 Sinon 对 Mongoose 模型进行 Stub

security - Azure云服务Web应用程序未找到-404

javascript - 中止多个 xhr 请求

php - 简单的 php/mysql 登录表单安全性

javascript - console.log() 没有按预期工作

javascript - 如何为点击事件实现 Omniture 自定义链接跟踪?

javascript - 错过预定事件后如何执行代码?

javascript - 无法在 Riot JS 中的服务器端请求标签