Node.js 輸入驗(yàn)證

2021-06-01 09:50 更新

1.1.1【必須】按類型進(jìn)行數(shù)據(jù)校驗(yàn)

  • 所有程序外部輸入的參數(shù)值,應(yīng)進(jìn)行數(shù)據(jù)校驗(yàn)。校驗(yàn)內(nèi)容包括但不限于:數(shù)據(jù)長(zhǎng)度、數(shù)據(jù)范圍、數(shù)據(jù)類型與格式。校驗(yàn)不通過(guò),應(yīng)拒絕。

  1. // bad:未進(jìn)行輸入驗(yàn)證
  2. Router.get("/vulxss", (req, res) => {
  3. const { txt } = req.query;
  4. res.set("Content-Type", "text/html");
  5. res.send({
  6. data: txt,
  7. });
  8. });
  9. // good:按數(shù)據(jù)類型,進(jìn)行輸入驗(yàn)證
  10. const Router = require("express").Router();
  11. const validator = require("validator");
  12. Router.get("/email_with_validator", (req, res) => {
  13. const txt = req.query.txt || "";
  14. if (validator.isEmail(txt)) {
  15. res.send({
  16. data: txt,
  17. });
  18. } else {
  19. res.send({ err: 1 });
  20. }
  21. });

關(guān)聯(lián)漏洞:縱深防護(hù)措施 - 安全性增強(qiáng)特性

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)