Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
LUISDASARTIMANHAS committed Feb 3, 2024
1 parent 330ed12 commit 8973a1d
Show file tree
Hide file tree
Showing 16 changed files with 3,546 additions and 373 deletions.
67 changes: 67 additions & 0 deletions modules/checkHeaderMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import fs from "fs";
import path from "path";
import xss from "xss";
const filesServer = __dirname + "/../src/";
const path_pages = filesServer + "pages/";
const forbiddenFilePath = path.join(path_pages, "forbidden.html");

const checkHeaderMiddleware = (req, res, next) => {
const origin = req.headers.referer || req.headers.referrer;
const keyHeader = req.headers["authorization"];
const blockedRoutes = JSON.parse(
fs.readFileSync("data/blockedRoutes.json", "utf8")
);
const blockRoutesPresent = blockedRoutes.some((route) => {
// Trata rotas com curingas
const regex = new RegExp(`^${route.replace(/\*/g, ".*")}$`);
return regex.test(req.path);
});
const payload = JSON.stringify(req.body, null, 2);
const keys = [
"snve072509ç$",
"snve072509ç$",
"snve072509&Aplication"
];
const validKey = keys.some((key) => keyHeader === key);
const auth = blockRoutesPresent && !validKey;

console.log("-------------------------");
console.log("SISTEMA <CHECK> <OBTER>: " + req.url);
console.log("SISTEMA <ORIGEM>: " + origin);
console.log("SISTEMA <PAYLOAD>: " + payload);
keys.forEach((key) => {
const auth = keyHeader === key;
print(keyHeader, key, auth);
});
for (const key in req.body) {
req.body[key] = xss(req.body[key]);
}
if (auth) {
// Se estiver solicitando das rotas bloqueadas E não conter key, bloquea a solicitação
forbidden(res);
} else {
// Cabeçalho "solicitador" presente ou rota não bloqueada, permite o acesso
next();
}
};

function forbidden(res) {
res.status(403);
res.sendFile(forbiddenFilePath);
}

function conversorSimEnao(value) {
if (value) {
return "✔Voce foi autorizado, esta tudo correto";
}
return "⚠Esta faltando algo ou não foi autorizado!";
}

// functions basicas
function print(keyHeader, key, auth) {
console.log("SISTEMA <VERIFICAÇÃO>: " + keyHeader + " == " + key);
console.log("SISTEMA <AUTORIZAÇÃO>: " + conversorSimEnao(!auth));
console.log("----------------------------");
}

export default checkHeaderMiddleware;
19 changes: 19 additions & 0 deletions modules/httpsSecurity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import cors from "cors"
import helmet from "helmet"

const httpsSecurityMiddleware = (req, res, next) => {
const corsOptions = {
origin: [/^https:\/\/.+/],
methods: "GET,PUT,POST,DELETE",
optionsSuccessStatus: 204,
};

cors(corsOptions)(req, res, () => { }); // Executa o middleware cors
helmet.hsts({
maxAge: 365 * 24 * 60 * 60,
includeSubDomains: true,
preload: true,
})(req, res, next); // Executa o middleware helmet
};

export default httpsSecurityMiddleware;
Loading

0 comments on commit 8973a1d

Please sign in to comment.