Skip to content

Commit 2f3ad7f

Browse files
committed
logger done
1 parent b636068 commit 2f3ad7f

File tree

10 files changed

+101
-20
lines changed

10 files changed

+101
-20
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,12 @@ This setup leaks memory as memoryStore doesn't have a way to make sessions expir
9494
As per express recommendation, developers should use a suitable session storage library. See below one example with `connect-pg-simple`, which stores session data in a postgres database.
9595

9696
<https://github.com/RaihanSharif/Cyber_Security_base_2025_project1/blob/ac0608d4f1745a9a05558d0a938372693709fd9c/app.js#L22>
97-
In this example the session is also configured to be more secure.
98-
* `secure` - Ensures that the browser only sends cookies over HTTPS
99-
* `httpOnly` - Ensure the cookie is sent only over HTTP(S), no client JavaScript, which protects against cross-site scripting.
100-
* `maxAge` - Ensure that the cookie expires after some time.
97+
In this example the session is also configured to be more secure.
98+
99+
- `secure` - Ensures that the browser only sends cookies over HTTPS
100+
- `httpOnly` - Ensure the cookie is sent only over HTTP(S), no client JavaScript, which protects against cross-site scripting.
101+
- `maxAge` - Ensure that the cookie expires after some time.
101102

102103
### A09 Security Logging and Monitoring Failures
104+
105+
Logging key security event is vital to ensuring accountability. Meaning that in the event of a secuirty breach the security team is able to properly trace the source the breach, for example security misconfiguration. Logging is also important to be able to quickly see and respond to attacks. E.g. if a large number of login attempts are made in a short time, it should be logged so that appropriate action can be taken.

app.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ app.use(
1919
secret: process.env.SESSION_SECRET,
2020
resave: false,
2121
saveUninitialized: false,
22-
// store: new pgSession({
23-
// pool: pool,
24-
// createTableIfMissing: true,
25-
// }),
26-
// cookie: {
27-
// maxAge: 24 * 60 * 60 * 1000,
28-
// secure: true,
29-
// httpOnly: true,
30-
// }, // 1 day
22+
store: new pgSession({
23+
pool: pool,
24+
createTableIfMissing: true,
25+
}),
26+
cookie: {
27+
maxAge: 24 * 60 * 60 * 1000,
28+
// secure: true,
29+
httpOnly: true,
30+
}, // 1 day
3131
})
3232
);
3333

controllers/accountController.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ const postLogin = [
6262

6363
// TODO: fix auth failure
6464
function getAdminView(req, res) {
65-
console.log(req.query.admin === "true");
6665
if (req.query.admin === "true") {
6766
// if (req.user && req.user.is_admin) {
6867
res.render("adminPanel", { title: "admin panel" });

controllers/postController.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ async function getAllPosts(req, res, next) {
88
await pool.query(`SELECT message, username FROM post JOIN account ON
99
post.user_id=account.id;`);
1010

11-
console.log(rows);
1211
res.render("showPosts", { title: "posts", postList: rows });
1312
} catch (err) {
1413
return next(err);

package-lock.json

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"express": "^5.2.1",
2525
"express-session": "^1.18.2",
2626
"express-validator": "^7.3.1",
27+
"morgan": "^1.10.1",
2728
"passport": "^0.7.0",
2829
"passport-local": "^1.0.0",
2930
"pg": "^8.16.3"

public/script.js

Whitespace-only changes.

public/styles.css

Whitespace-only changes.

rootPath.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const path = require("node:path");
2+
3+
module.exports = {
4+
logPath: path.join(__dirname, "logs"),
5+
};

routes/authRoutes.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
const { Router } = require("express");
2-
32
const accountController = require("../controllers/accountController");
4-
3+
const morgan = require("morgan");
4+
const fs = require("fs");
5+
const path = require("node:path");
56
const authRouter = new Router();
67

8+
const { logPath } = require("../rootPath");
9+
10+
const logStream = fs.createWriteStream(
11+
path.join(logPath, "authentication.log"),
12+
{ flags: "a" }
13+
);
14+
15+
const logger = morgan("combined", {
16+
stream: logStream,
17+
});
18+
719
authRouter.get("/sign-up", accountController.getSingup);
820

921
authRouter.post("/sign-up", accountController.postSignup);
1022

11-
authRouter.post("/log-in", accountController.postLogin);
23+
authRouter.post("/log-in", [logger, accountController.postLogin]);
1224

13-
authRouter.post("/log-out", accountController.postLogout);
25+
authRouter.post("/log-out", [logger, accountController.postLogout]);
1426

15-
authRouter.get("/admin", accountController.getAdminView);
27+
authRouter.get("/admin", [logger, accountController.getAdminView]);
1628

1729
module.exports = authRouter;

0 commit comments

Comments
 (0)