我在网上销售一些课程,因此对于付款购物车,我实现了一种方法,该方法通过URL中的查询从Firestore获取课程数据,例如:localhost:5000/?product=course1
。
所以,我想知道它在注入或其他漏洞方面有多安全
我实现了简单的验证。但没什么大不了的。
这是产品发布请求:
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
}
});
});
如果有任何漏洞以及如何修复它们,请告诉我。如果您没有发现任何漏洞,请发表评论。谢谢!
我在您显示的代码中没有看到任何漏洞。通常,为了避免任何类型的注入,您应该正确验证所有输入。也就是说,如果你希望一个product
是一个数字,你应该检查它是否是一个数字。此外,避免SQL注入的一般做法是不对SQL查询使用字符串插值(并且您不👍这样做(,而是使用预准备语句或/和ORM。
因此,在 URL 查询中发送参数与在采取所有预防措施时发送任何其他类型的参数一样安全。