Skip to content

Commit

Permalink
15-06-adding-csrf
Browse files Browse the repository at this point in the history
  • Loading branch information
leo41271 committed Jul 1, 2024
1 parent a6aa2b1 commit 13ee591
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 56 deletions.
9 changes: 9 additions & 0 deletions 15 Authentication/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const session = require('express-session');
const MongoDBStore = require('connect-mongodb-session')(session);
const csrf = require('csurf');

This comment has been minimized.

Copy link
@leo41271

leo41271 Jul 9, 2024

Author Owner

CSRF 攻击和防御 - Web 安全常识
csurf Example (npm but package is deprecated)


const errorController = require('./controllers/error');
const User = require('./models/user');
Expand All @@ -20,6 +21,7 @@ const store = new MongoDBStore({
uri: MONGODB_URI,
collection: 'sessions',
});
const csrfProtection = csrf();

app.set('view engine', 'ejs');
app.set('views', 'views');
Expand All @@ -38,6 +40,7 @@ app.use(
store: store,
})
);
app.use(csrfProtection);

app.use((req, res, next) => {
if (!req.session.user) {
Expand All @@ -51,6 +54,12 @@ app.use((req, res, next) => {
.catch((err) => console.log(err));
});

app.use((req, res, next) => {
res.locals.isAuthenticated = req.session.isLoggedIn;
res.locals.csrfToken = req.csrfToken();
next();
});

app.use('/admin', adminRoutes);
app.use(shopRoutes);
app.use(authRoutes);
Expand Down
1 change: 0 additions & 1 deletion 15 Authentication/controllers/shop.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ exports.getIndex = (req, res, next) => {
prods: products,
pageTitle: 'Shop',
path: '/',
isAuthenticated: req.session.isLoggedIn,
});
})
.catch((err) => {
Expand Down
1 change: 1 addition & 0 deletions 15 Authentication/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"bcryptjs": "^2.4.3",
"body-parser": "^1.20.2",
"connect-mongodb-session": "^5.0.0",
"csurf": "^1.11.0",

This comment has been minimized.

Copy link
@leo41271

leo41271 Jul 9, 2024

Author Owner

This package has been deprecated Udemy used : scurf
npm search for csrf Package

"ejs": "^3.1.9",
"express": "^4.19.2",
"express-session": "^1.18.0",
Expand Down
1 change: 1 addition & 0 deletions 15 Authentication/views/admin/edit-product.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<input type="hidden" value="<%= product._id %>" name="productId">
<% } %>

<input type="hidden" name="_csrf" value="<%= csrfToken %>" />

This comment has been minimized.

Copy link
@leo41271

leo41271 Jul 9, 2024

Author Owner

type="hidden" 在 input 中 不顯示 但會跟著一起夾帶資料。
name="_csrf" 夾帶的資料名稱叫做 _csrf。
value="<%= csrfToken %>" 用於給定值 在驗證表單時使用。

伺服器接收到表單提交後,會從請求中提取出 _csrf 字段的值,並與存儲在會話或 Cookie 中的 CSRF 令牌進行比對。
如果兩者匹配,則表單提交是合法的,伺服器會繼續處理請求。
如果不匹配,則表單提交被認為是非法的,伺服器會拒絕請求,通常會返回一個錯誤響應。

<button class="btn" type="submit"><% if (editing) { %>Update Product<% } else { %>Add Product<% } %></button>
</form>
</main>
Expand Down
1 change: 1 addition & 0 deletions 15 Authentication/views/admin/products.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<a href="/admin/edit-product/<%= product._id %>?edit=true" class="btn">Edit</a>
<form action="/admin/delete-product" method="POST">
<input type="hidden" value="<%= product._id %>" name="productId">
<input type="hidden" name="_csrf" value="<%= csrfToken %>" />
<button class="btn" type="submit">Delete</button>
</form>
Expand Down
1 change: 1 addition & 0 deletions 15 Authentication/views/auth/login.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<label for="password">Password</label>
<input type="password" name="password" id="password">
</div>
<input type="hidden" name="_csrf" value="<%= csrfToken %>" />
<button class="btn" type="submit">Login</button>
</form>
</main>
Expand Down
1 change: 1 addition & 0 deletions 15 Authentication/views/auth/signup.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<label for="confirmPassword">Confirm Password</label>
<input type="password" name="confirmPassword" id="confirmPassword">
</div>
<input type="hidden" name="_csrf" value="<%= csrfToken %>" />
<button class="btn" type="submit">Login</button>
</form>
</main>
Expand Down
5 changes: 3 additions & 2 deletions 15 Authentication/views/includes/add-to-cart.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<form action="/cart" method="post">
<button class="btn" type="submit">Add to Cart</button>
<input type="hidden" name="productId" value="<%= product._id %>">
</form>
<input type="hidden" name="_csrf" value="<%= csrfToken %>" />
<input type="hidden" name="productId" value="<%= product._id %>" />
</form>
105 changes: 52 additions & 53 deletions 15 Authentication/views/includes/navigation.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,37 @@
<a class="<%= path === '/products' ? 'active' : '' %>" href="/products">Products</a>
</li>
<% if (isAuthenticated) { %>
<li class="main-header__item">
<a class="<%= path === '/cart' ? 'active' : '' %>" href="/cart">Cart</a>
</li>
<li class="main-header__item">
<a class="<%= path === '/orders' ? 'active' : '' %>" href="/orders">Orders</a>
</li>
<li class="main-header__item">
<a class="<%= path === '/admin/add-product' ? 'active' : '' %>" href="/admin/add-product">Add Product
</a>
</li>
<li class="main-header__item">
<a class="<%= path === '/admin/products' ? 'active' : '' %>" href="/admin/products">Admin Products
</a>
</li>
<li class="main-header__item">
<a class="<%= path === '/cart' ? 'active' : '' %>" href="/cart">Cart</a>
</li>
<li class="main-header__item">
<a class="<%= path === '/orders' ? 'active' : '' %>" href="/orders">Orders</a>
</li>
<li class="main-header__item">
<a class="<%= path === '/admin/add-product' ? 'active' : '' %>" href="/admin/add-product"
>Add Product
</a>
</li>
<li class="main-header__item">
<a class="<%= path === '/admin/products' ? 'active' : '' %>" href="/admin/products">Admin Products </a>
</li>
<% } %>
</ul>
<ul class="main-header__item-list">
<% if (!isAuthenticated) { %>
<li class="main-header__item">
<a class="<%= path === '/login' ? 'active' : '' %>" href="/login">Login</a>
</li>
<li class="main-header__item">
<a class="<%= path === '/signup' ? 'active' : '' %>" href="/signup">Signup</a>
</li>
<li class="main-header__item">
<a class="<%= path === '/login' ? 'active' : '' %>" href="/login">Login</a>
</li>
<li class="main-header__item">
<a class="<%= path === '/signup' ? 'active' : '' %>" href="/signup">Signup</a>
</li>
<% } else { %>
<li class="main-header__item">
<form action="/logout" method="post">
<button type="submit">Logout</button>
</form>
</li>
<li class="main-header__item">
<form action="/logout" method="post">
<input type="hidden" name="_csrf" value="<%= csrfToken %>" />
<button type="submit">Logout</button>
</form>
</li>
<% } %>
</ul>
</nav>
Expand All @@ -54,34 +55,32 @@
<a class="<%= path === '/products' ? 'active' : '' %>" href="/products">Products</a>
</li>
<% if (isAuthenticated) { %>
<li class="mobile-nav__item">
<a class="<%= path === '/cart' ? 'active' : '' %>" href="/cart">Cart</a>
</li>
<li class="mobile-nav__item">
<a class="<%= path === '/orders' ? 'active' : '' %>" href="/orders">Orders</a>
</li>
<li class="mobile-nav__item">
<a class="<%= path === '/admin/add-product' ? 'active' : '' %>" href="/admin/add-product">Add Product
</a>
</li>
<li class="mobile-nav__item">
<a class="<%= path === '/admin/products' ? 'active' : '' %>" href="/admin/products">Admin Products
</a>
</li>
<% } %>
<% if (!isAuthenticated) { %>
<li class="mobile-nav__item">
<a class="<%= path === '/login' ? 'active' : '' %>" href="/login">Login</a>
</li>
<li class="mobile-nav__item">
<a class="<%= path === '/signup' ? 'active' : '' %>" href="/signup">Signup</a>
</li>
<li class="mobile-nav__item">
<a class="<%= path === '/cart' ? 'active' : '' %>" href="/cart">Cart</a>
</li>
<li class="mobile-nav__item">
<a class="<%= path === '/orders' ? 'active' : '' %>" href="/orders">Orders</a>
</li>
<li class="mobile-nav__item">
<a class="<%= path === '/admin/add-product' ? 'active' : '' %>" href="/admin/add-product">Add Product </a>
</li>
<li class="mobile-nav__item">
<a class="<%= path === '/admin/products' ? 'active' : '' %>" href="/admin/products">Admin Products </a>
</li>
<% } %> <% if (!isAuthenticated) { %>
<li class="mobile-nav__item">
<a class="<%= path === '/login' ? 'active' : '' %>" href="/login">Login</a>
</li>
<li class="mobile-nav__item">
<a class="<%= path === '/signup' ? 'active' : '' %>" href="/signup">Signup</a>
</li>
<% } else { %>
<li class="mobile-nav__item">
<form action="/logout" method="post">
<button type="submit">Logout</button>
</form>
</li>
<li class="mobile-nav__item">
<form action="/logout" method="post">
<input type="hidden" name="_csrf" value="<%= csrfToken %>" />
<button type="submit">Logout</button>
</form>
</li>
<% } %>
</ul>
</nav>
</nav>
2 changes: 2 additions & 0 deletions 15 Authentication/views/shop/cart.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<h2>Quantity: <%= p.quantity %></h2>
<form action="/cart-delete-item" method="POST">
<input type="hidden" value="<%= p.productId._id %>" name="productId">
<input type="hidden" name="_csrf" value="<%= csrfToken %>" />
<button class="btn danger" type="submit">Delete</button>
</form>
</li>
Expand All @@ -21,6 +22,7 @@
<hr>
<div class="centered">
<form action="/create-order" method="POST">
<input type="hidden" name="_csrf" value="<%= csrfToken %>" />
<button type="submit" class="btn">Order Now!</button>
</form>
</div>
Expand Down

0 comments on commit 13ee591

Please sign in to comment.