」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 前端和後端開發的關鍵安全原則

前端和後端開發的關鍵安全原則

發佈於2024-11-03
瀏覽:382

Key Security principles for Frontend and backend Development

Introduction:

Building a full-stack application is a rewarding challenge, but it's crucial to prioritize security from the very start. Have you considered the essential security principles needed to protect your product? How can we ensure that the application meets security standards effectively, and how easy is it to implement these principles?

In this article, I will explore fundamental security principles for both frontend and backend development. We’ll also discuss practical steps to implement them and ensure that your app adheres to industry security standards.

  1. Securing the frontend:
  • Input Validation and Sanitization:

-Ensure that all input from users is properly validated and sanitized to avoid injection attacks such as Cross-Site Scripting (XSS).

-Use libraries like DOMPurify for sanitizing user inputs that will be inserted into the DOM.

  • Authentication and Authorization:

-Implement secure authentication methods such as OAuth or the famous JWT method (JSON Web Tokens) to ensure that users are who they claim to be.
-Use role-based access control (RBAC) to restrict parts of the UI based on user permissions, for example part of the UI reserved to admin and other part for ordinary users.

// the code below show an example of how user 
//should be authenticated and depending 
//on his role the dashboard UI display
const createRoutes = (logged: boolean, role:string) =>
  createBrowserRouter([
{
          path: "/dashboard",
          loader: combinedDashboardLoader,

          element: (
            
              {role==="admin"}?:
          ),
          errorElement: ,
        }])

  • Secure Storage of sensitive Data:

-Avoid storing sensitive information like access tokens in localStorage or sessionStorage as they are vulnerable to XSS. Instead, use secure cookies with the HttpOnly and Secure flags enabled.

// in your server code you can write this code
//  inside your login controller function
// so you don't need to send your token to the frontend
res.cookie("accessToken", accessToken, {
      httpOnly: true,
      secure: process.env.NODE_ENV === "production",
      maxAge: expirationTime * 1000,
      sameSite: "lax",
    });

  • CORS (Cross-Origin Resource Sharing):

-Implement proper CORS policies to only allow specific origins to interact with your backend API. Ensure that you restrict methods and headers that are allowed.

  • Content Security Policy (CSP):

-Set up a robust CSP to prevent the execution of untrusted scripts on your frontend. This reduces the risk of XSS attacks by controlling where resources can be loaded from.
-_Implementation _:
the easiest way is to simply use meta tags in your head of the html page as shown below in the code , for more details you can refer to this link https://www.stackhawk.com/blog/react-content-security-policy-guide-what-it-is-and-how-to-enable-it/

  
    ...
    

-Alternatively you can a middleware in your EpressJS as below:

app.use((req, res, next) => {
  res.setHeader("Content-Security-Policy", "default-src 'self'; script-src 'self' https://trusted.cdn.com; object-src 'none';");
  next();
});

  • HTTPS:

Enforce HTTPS to encrypt communication between the client and the server, ensuring that data is not exposed during transit.

  1. Securing the backend:
  • Input Validation and Sanitization (Server-side):

-Always validate and sanitize user inputs on the server side to prevent SQL injection or other code injection attacks. Even if input validation is done on the frontend, it should always be performed on the backend as well.
below an example using express validator middleware, then simply apply this middleware before your registration controller function.

import { body, validationResult } from "express-validator";
const validateUserInput = [
    body("email").isEmail().withMessage("Please enter a valid email address"),
    body("name")
        .isLength({ min: 3 })
        .withMessage("Name must be at least 3 characters long"),
    body("password")
        .isLength({ min: 6 })
        .withMessage("Password must be longer than 5 characters"),

    // Middleware to check for validation errors
    (req, res, next) => {
        const errors = validationResult(req);
        if (!errors.isEmpty()) {
            return res.status(400).json({ errors: errors.array()[0].msg });
        }
        next();
    },
];
export default validateUserInput;
//in your router file apply it 
//register is the post request function to add new user
router.route("/register").post(validateUserInput, register);

  • Authentication and Authorization:

-Use robust authentication mechanisms, such as OAuth, and secure tokens like JWT for session management.

-Implement role-based access control (RBAC) and avoid hardcoding permissions directly in your application.

  • Password security:

-Store passwords securely using strong hashing algorithms like bcrypt or Argon2. Never store plain text passwords.

-If needed implement multi-factor authentication (MFA) for an additional layer of security.

the code below shows how the process of securing password and using JWT (authentication).

// hashing password inside your registration function  
const register = async (req: Request, res: Response) => {
  const { email, password, name } = req.body;
const hashedPassword = await bcrypt.hash(password, 10);
createUser(email,name,hashedpassword) //a function to add new user into your DB
}

// Compare the password to the hashed one during the login request
const login = async (req: Request, res: Response) => {
  const { email, password } = req.body;
const existingUser = await query(findUserByEmail, [email]);
    if (!existingUser[0])
      return res.status(404).json({
        message: `${email} not found! register if you don't have an account`,
      });
    const matchPassword = await bcrypt.compare(
      password,
      existingUser[0].password
    );
//generate token JWT
    const expirationTime = 180;
    const accessToken = jwt.sign(
      { id: existingUser[0].iduser },
      process.env.ACCESS_TOKEN_SECRET!,
      {
        expiresIn: expirationTime   "s",
      }
    );}

  • Rate Limiting and Throttling: To prevent brute-force attacks, implement rate limiting and IP-based throttling on sensitive endpoints such as login routes.

Rate Limiting:
It restricts the number of requests a user (identified by IP, API key, etc.) can make to the server in a given time window (e.g., 100 requests per minute). After the limit is reached, further requests are blocked or delayed.

const express = require('express');
const rateLimit = require('express-rate-limit');
const app = express();

// Define rate limiting rule: 100 requests per 15 minutes
const limiter = rateLimit({
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 100, // Limit each IP to 100 requests 
  message: 'Too many requests from this IP, please try again later.', // Custom message 
  standardHeaders: true,  `RateLimit-*` headers
  legacyHeaders: false, 
});

// Apply the rate limiting middleware to all requests

app.use(limiter);

//but we can also apply it to a specific route 
app.post('/login', limiter, (req, res) => {
  res.send('Login route');
});


app.listen(3000, () => {
  console.log('Server is running on port 3000');
});


Throttling:
Similar to rate limiting, but instead of blocking requests entirely, it slows down the processing of requests after a certain threshold is reached. This can avoid service overloads.

Example:
If a user makes more than 10 API requests per second, you slow down the response to every subsequent request to ensure the system isn’t overwhelmed.

  • Data encryption:

Use encryption for sensitive data both at rest and in transit. Ensure that databases and other storage mechanisms encrypt sensitive information.

the code below shows an implementation of data encryption of a credit card number before storing it in the database.

const crypto = require('crypto');

// Encryption settings
const encryptionKey = crypto.randomBytes(32); // AES-256 key
const iv = crypto.randomBytes(16); // Initialization vector

// Encrypt function
function encrypt(text) {
  const cipher = crypto.createCipheriv('aes-256-cbc', encryptionKey, iv);
  let encrypted = cipher.update(text, 'utf8', 'hex');
  encrypted  = cipher.final('hex');
  return `${iv.toString('hex')}:${encrypted}`; // Store both IV and encrypted text
}

// Post request
app.post('/user', (req, res) => {
  const { creditCard, email } = req.body;

  // Encrypt the name
  const encryptedCard = encrypt(creditCard);

  // Save encrypted  to the database

  const user = new User({
    creditCard: encryptedCard ,
    email: email
  });// example of sequelize queries

  user.save().then(() => {
    res.send('User saved successfully');
  }).catch(err => {
    res.status(500).send('Error saving user');
  });
});


When you need to display the real data (in this case the card number) , you can use decrypt function inside your GET request.

// Decrypt function
function decrypt(encryptedData) {
  const [ivHex, encryptedText] = encryptedData.split(':');
  const ivBuffer = Buffer.from(ivHex, 'hex');
  const decipher = crypto.createDecipheriv('aes-256-cbc', encryptionKey, ivBuffer);
  let decrypted = decipher.update(encryptedText, 'hex', 'utf8');
  decrypted  = decipher.final('utf8');
  return decrypted;
}

//GET request
app.get('/user/:id', (req, res) => {
  const userId = req.params.id;
 User.findById(userId).then(user => {
    if (!user) return res.status(404).send('User not found');

    // Decrypt the user's card 
    const decryptedCard = decrypt(user.creditCard);

 res.json({
      creditCard: decryptedCard ,
      email: user.email
    });
  }).catch(err => {
    res.status(500).send('Error retrieving user');
  });
});
  • Security Headers:

-Apply HTTP security headers like Strict-Transport-Security, X-Frame-Options, X-XSS-Protection, and X-Content-Type-Options to protect against various common vulnerabilities.

The simplest way to implement it is by using the helmet middleware in express after installing it npm install helmet.

const express = require('express');
const helmet = require('helmet');
const app = express();

app.use(helmet());

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

The alternative implementation is to manually setting Headers without Helmet as shown as below.

app.use((req, res, next) => {
  res.setHeader('Content-Security-Policy', "default-src 'self'; script-src 'self' https://trusted.cdn.com");
  res.setHeader('X-Frame-Options', 'DENY');
  res.setHeader('X-Content-Type-Options', 'nosniff');
  res.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
  res.setHeader('X-XSS-Protection', '1; mode=block');
  res.setHeader('Referrer-Policy', 'no-referrer');
  res.setHeader('Permissions-Policy', 'geolocation=(self), microphone=()');
  next();
});
  • CSRF Protection:

-Implement Cross-Site Request Forgery (CSRF) protection mechanisms such as CSRF tokens to prevent unauthorized actions in your app.

How CSRF Attacks Work:

-The user logs into your app, and the server sets a session cookie or an authentication token.
-The user then visits a malicious site while still logged in to your app in the background.
-The malicious site sends a request to your app using the logged-in user's credentials (session cookie) to perform unintended actions like transferring money or changing account details.

To prevent CSRF attacks, you need to ensure that requests made from your frontend are authenticated and originate from a trusted source. The most common way to protect against CSRF is by using CSRF tokens.

Implementation is easy by using cookie parser and express.urlencoded middlewares
first install them npm install csurf cookie-parser

const express = require('express');
const cookieParser = require('cookie-parser');
const csrf = require('csurf');
const app = express();

// Use cookie-parser for CSRF token storage (if using cookie-based tokens)
app.use(cookieParser());

// Initialize the CSRF middleware
const csrfProtection = csrf({ cookie: true });

// Use body-parser to parse form data
app.use(express.urlencoded({ extended: false }));

// routes here after applying the middlewares

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});
  • Logging and monitoring:

-Ensure proper logging of security-related events such as failed login attempts and potential XSS attacks. Also, set up monitoring and alerts for abnormal behavior.

Conclusion:

This article aimed to highlight and summarize the key security principles that should be considered when developing full-stack JavaScript applications. We also explored practical ways to implement these principles efficiently. Additionally, these insights can be valuable when preparing for job interviews, especially when discussing various security measures and best practices in app development.

版本聲明 本文轉載於:https://dev.to/oussamabouyahia/key-security-principales-for-frontend-and-backend-development-o14?1如有侵犯,請洽[email protected]刪除
最新教學 更多>
  • Firestore 如何優化社群網路時間軸以實現可擴充性?
    Firestore 如何優化社群網路時間軸以實現可擴充性?
    使用Firestore 優化社交網路時間軸在設計具有提要和關注功能的社交網路時,資料庫可擴展性對於處理潛在問題至關重要大型數據集。 Firebase 的即時資料庫帶來了可擴展性挑戰,特別是在儲存使用者時間軸的方法方面。要解決這些問題,請考慮過渡到 Firestore。 優化的資料庫結構Firesto...
    程式設計 發佈於2024-11-08
  • 如何解決將物件數組作為函數參數傳遞時的錯誤?
    如何解決將物件數組作為函數參數傳遞時的錯誤?
    類型提示:物件陣列將物件陣列作為參數傳遞給函數時,如果未指定參數類型。例如,考慮以下程式碼:class Foo {} function getFoo(Foo $f) {}嘗試將 Foo 物件陣列傳遞給 getFoo 將導致致命錯誤:Argument 1 passed to getFoo() must...
    程式設計 發佈於2024-11-08
  • 為什麼 iOS 裝置上缺少 CSS 捲軸?
    為什麼 iOS 裝置上缺少 CSS 捲軸?
    iOS上無法顯示有CSS Overflow的捲軸為iPad開發網站時,使用CSS屬性overflow: auto來啟用div內的捲軸可能無效。儘管兩指滾動手勢功能正常,但捲軸仍然隱藏。嘗試同時使用溢出:自動和溢出:滾動不會產生任何結果。 iOS行為不幸的是,溢位:自動和捲動都不會在iOS裝置上產生捲...
    程式設計 發佈於2024-11-08
  • Java中如何從執行緒操作傳回值?
    Java中如何從執行緒操作傳回值?
    執行緒操作回傳值在多執行緒程式設計中,執行緒之間的互動往往需要交換資料。常見的情況是嘗試檢索在單獨執行緒中執行的操作的結果。 請考慮下面的範例程式碼:public void test() { Thread uiThread = new HandlerThread("UIHandle...
    程式設計 發佈於2024-11-08
  • Python 簡介:)
    Python 簡介:)
    歷史 Python 由 Guido van Rossum 創建,首次發佈於 1991 年。它旨在優先考慮程式碼的可讀性和簡單性,從而提高開發人員的工作效率。 「Python」 的靈感來自 BBC 電視節目 「Monty Python's Flying Circus」,van...
    程式設計 發佈於2024-11-08
  • 學習 Go 結構最終如何讓我愛上編碼
    學習 Go 結構最終如何讓我愛上編碼
    「我仍然記得早期與代碼搏鬥的日子。 基本的東西?我正要到那裡。但後來出現了結構體,一切都變得模糊起來。我不斷地破壞東西,我的程式碼一團糟。我做錯了什麼? 直到我坐下來,學習了 Go 結構體的基礎知識,並開始有效地使用它們,事情才終於有了進展。那是轉捩點。突然間,程式碼變得更有組織、更有效率、更乾淨...
    程式設計 發佈於2024-11-08
  • MERN 堆疊仍然有效嗎?
    MERN 堆疊仍然有效嗎?
    Remember when the MERN stack was the Beyoncé of web development stacks? It was everywhere, and if you weren’t using it, you were probably missing out ...
    程式設計 發佈於2024-11-08
  • 什麼時候需要在 Tkinter 中呼叫 `mainloop()`?
    什麼時候需要在 Tkinter 中呼叫 `mainloop()`?
    在 Tkinter 應用程式中呼叫 mainloop在 Tkinter 中,mainloop 是實現視窗渲染和事件處理的基本功能。與流行的看法相反,並不總是需要在互動式 shell 環境中明確呼叫 mainloop。然而,這種便利性在 shell 之外並不適用。 mainloop 的角色mainlo...
    程式設計 發佈於2024-11-08
  • 如何解決將靜態 C 庫與 C++ 程式碼連結時出現「未定義的引用」錯誤?
    如何解決將靜態 C 庫與 C++ 程式碼連結時出現「未定義的引用」錯誤?
    對用C 程式碼連結靜態C 函式庫時的錯誤的未定義引用當嘗試用C 程式碼連結靜態C 函式庫時,您可以儘管修改了連結順序,但仍遇到「未定義的引用」錯誤。此問題是由 C 和 C 編譯創建的不同符號名稱(稱為“名稱修飾”)引起的。 在 C 中,連結器在錯誤訊息中顯示分解的符號名稱,這可能會造成混淆。使用“n...
    程式設計 發佈於2024-11-08
  • 書籍:學習 JavaScript 設計模式
    書籍:學習 JavaScript 設計模式
    本書探討了 JavaScript 中常見軟體設計模式的實作和使用。雖然根據最新的最佳實踐,一些示例可能稍微過時,但它們對於維護遺留系統的人來說仍然很有價值。 對於初學者: 它是對軟體設計模式的出色介紹。然而,對於那些程式設計經驗有限的人來說,這些模式解決的問題可能不太熟悉。 ...
    程式設計 發佈於2024-11-08
  • 了解命令式程式設計和聲明式程式設計之間的區別
    了解命令式程式設計和聲明式程式設計之間的區別
    當我剛開始學習React時,我的老師說:「JavaScript是命令式編程,而React是聲明式編程。」然而,一開始這對我來說不太有意義。因此,我決定將其分解以更好地理解其中的差異。 將命令式和聲明式程式設計與披薩進行比較? 為了更容易理解,讓我們來比較一下這兩種烹飪方法。 ...
    程式設計 發佈於2024-11-08
  • 如何在JavaScript中進行穩定排序以保持元素順序的一致性?
    如何在JavaScript中進行穩定排序以保持元素順序的一致性?
    JavaScript 中的穩定排序演算法對資料進行排序時,保留相等元素的原始順序對於穩定的排序演算法至關重要。在這種情況下,我們的目標是按給定順序對具有特定鍵的物件陣列進行排序,同時保持元素順序一致性。 穩定排序技術有趣的是,甚至非穩定排序函數可以實現穩定排序。透過在排序前捕捉每個元素的初始位置,我...
    程式設計 發佈於2024-11-08
  • npm 與 npx
    npm 與 npx
    如果您一直在使用 Node.js,您可能遇到過 npm 和 npx。 雖然它們聽起來很相似且都是 Node.js 生態系統不可或缺的一部分,但它們有不同的用途。這篇文章將探討 npm 和 npx 之間的差異,幫助您了解何時以及為何使用它們。 什麼是NPM? NPM 是 Node...
    程式設計 發佈於2024-11-08
  • Python 中的鍊式賦值如何運作?它們真的相當於多個順序分配嗎?
    Python 中的鍊式賦值如何運作?它們真的相當於多個順序分配嗎?
    理解Python 中的鍊式賦值Python 中的鍊式賦值,例如「x = y = somefunction()」這樣的表達式,引發了人們的關注關於它們與多個順序賦值的等價性的討論(“x = somefunction(); y = somefunction()”)。為了澄清這個問題,讓我們詳細探討一下鍊...
    程式設計 發佈於2024-11-08
  • 如何使用 Gorilla Websocket 在 Go Websocket 應用程式中向特定用戶端發送目標訊息?
    如何使用 Gorilla Websocket 在 Go Websocket 應用程式中向特定用戶端發送目標訊息?
    Go with Gorilla Websocket 中的特定客戶端訊息傳遞在websocket 通訊領域,向特定客戶端發送訊息的能力對於建立即時應用程式至關重要。然而,預設的 websocket 範例通常會示範同時向所有連線的用戶端廣播訊息。 為了解決這個問題,我們可以採用一種方法,為每個客戶端分配...
    程式設計 發佈於2024-11-08

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3