NodeJS TypeError:验证器.escape不是函数-(express-validator@6.12.1包)

enyaitl3  于 2023-02-15  发布在  Node.js
关注(0)|答案(2)|浏览(178)
    • 编解码器视频:**link
    • 说明:**

作为我的Codecademy Back-End Engineer training的一部分,我必须在他们的平台之外做一个项目,这个项目的目标是确保一个节点应用程序免受常见的web攻击。
我面临的一个挑战是保护来自Cross-Site Scripting (XSS) attacks的代码。为了做到这一点,我使用了一个名为express-validator@6.12.1的包。代码使用了一个名为validator.escape的函数,该函数旨在防止任何恶意代码插入到输入表单中。然而,当我尝试使用它时,在控制台中得到了一个错误。

    • 终端输出:**
TypeError: validator.escape is not a function
    • 代码如下:**
const validator = require("express-validator");

app.post("/public_forum", function (request, response) {
  if (request.session.loggedin) {
    var comment = validator.escape(request.body.comment);
    var username = request.session.username;
    if (comment) {
      db.all(
        `INSERT INTO public_forum (username,message) VALUES ('${username}','${comment}')`,
        (err, rows) => {
          console.log(err);
        }
      );
      db.all(`SELECT username,message FROM public_forum`, (err, rows) => {
        console.log(rows);
        console.log(err);
        response.render("forum", { rows });
      });
    } else {
      db.all(`SELECT username,message FROM public_forum`, (err, rows) => {
        console.log(rows);
        console.log(err);
        response.render("forum", { rows });
      });
    }
    comment = "";
  } else {
    response.redirect("/");
  }
  comment = "";
  //response.end();
});

Codecademy的视频中,这个家伙使用了这个函数。

zbdgwd5y

zbdgwd5y1#

尝试:

const {check, validationResult} = require('express-validator');

app.post('/public_forum', async function (request, response) {
  if (request.session.loggedin) {
    await check('comment').trim().escape().run(req);
    const validationResult = await validationResult(req);
    if (validationResult.isEmpty()) {
      // Good to go...
      const { comment } = req.body;
    }
    ...

官方文档链接

f3temu5u

f3temu5u2#

我已经实现了您的代码。我尝试添加恶意和安全注解,但我的浏览器上显示错误消息“Port 4000 Not Found”。每次我运行该代码时,它都会杀死端口。因此,我根据您发送给我的代码实现了另一个运行良好的代码。

// This code defines a post request handler for the "/public_forum" endpoint.
app.post('/public_forum', async function (request, response) {
  // Check if the user is logged in by checking the session data.
  if (request.session.loggedin) {
    // Trim and escape the incoming comment.
    await check('comment').trim().escape().run(request);
    // Get the validation result of the incoming comment.
    const errors = validationResult(request);
    // If the validation result contains errors, return a 400 status with the errors in a JSON format.
    if (!errors.isEmpty()) {
      return response.status(400).json({ errors: errors.array() });
    }
    // Get the comment from the request body.
    const { comment } = request.body;
    // If a valid comment exists, insert it into the "public_forum" database table.
    if (comment) {
      db.run(
        `INSERT INTO public_forum (username,message) VALUES (?,?)`, [request.session.username, comment],
        (err) => {
          // If an error occurs while inserting the comment, log the error.
          if (err) {
            console.error(err);
          }
        }
      );
    }
    // Select all the rows from the "public_forum" table.
    db.all(`SELECT username,message FROM public_forum`, (err, rows) => {
      // If an error occurs while selecting the rows, log the error.
      if (err) {
        console.error(err);
      }
      // Log the selected rows.
      console.log(rows);
      // Render the "forum" template, passing in the selected rows as a parameter.
      response.render("forum", { rows });
    });
  } else {
    // If the user is not logged in, redirect them to the homepage.
    response.redirect("/");
  }
});

相关问题