GWM : helmet
This commit is contained in:
parent
fd55167a21
commit
0c8de40ae5
31
app/utils/sanitizeInput.js
Normal file
31
app/utils/sanitizeInput.js
Normal file
@ -0,0 +1,31 @@
|
||||
const sanitize = require("sanitize-html");
|
||||
|
||||
function sanitizeValue(value) {
|
||||
if (typeof value === "string") {
|
||||
return sanitize(value, {
|
||||
allowedTags: [],
|
||||
allowedAttributes: {},
|
||||
});
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(item => sanitizeValue(item));
|
||||
}
|
||||
|
||||
if (value !== null && typeof value === "object") {
|
||||
const sanitizedObj = {};
|
||||
for (const key in value) {
|
||||
sanitizedObj[key] = sanitizeValue(value[key]);
|
||||
}
|
||||
return sanitizedObj;
|
||||
}
|
||||
|
||||
return value; // numbers, booleans, null
|
||||
}
|
||||
|
||||
module.exports = function sanitizeInput(req, res, next) {
|
||||
if (req.body) {
|
||||
req.body = sanitizeValue(req.body);
|
||||
}
|
||||
next();
|
||||
};
|
||||
13
server.js
13
server.js
@ -8,15 +8,22 @@ const routes = require("./app/routes/routes");
|
||||
const db = require("./app/models");
|
||||
const path = require("path");
|
||||
const cookieParser = require("cookie-parser");
|
||||
const sanitizeInput = require("./app/utils/sanitizeInput");
|
||||
require("dotenv").config();
|
||||
|
||||
const app = express();
|
||||
|
||||
|
||||
// GLOBAL MIDDLEWARE (body parsing)
|
||||
app.use(express.json());
|
||||
app.use(helmet());
|
||||
app.use(morgan("dev"));
|
||||
app.use(cookieParser());
|
||||
app.use(
|
||||
helmet({
|
||||
contentSecurityPolicy: false,
|
||||
})
|
||||
);
|
||||
app.use(morgan("dev"));
|
||||
app.use(sanitizeInput);
|
||||
|
||||
// app.use(cors());
|
||||
const allowedOrigins = [
|
||||
"http://localhost:5173/", //local
|
||||
|
||||
Loading…
Reference in New Issue
Block a user