TypeScript Secure Coding

Secure coding practices for TypeScript applications.


XSS Prevention

 1// ❌ Vulnerable
 2function displayMessage(message: string) {
 3    document.getElementById('output')!.innerHTML = message;
 4}
 5
 6// βœ… Secure
 7function displayMessage(message: string) {
 8    const element = document.getElementById('output')!;
 9    element.textContent = message;
10}
11
12// βœ… With sanitization
13import DOMPurify from 'dompurify';
14function displayMessage(message: string) {
15    const clean = DOMPurify.sanitize(message);
16    document.getElementById('output')!.innerHTML = clean;
17}

SQL Injection Prevention (Node.js)

1// ❌ Vulnerable
2const username = req.body.username;
3const query = `SELECT * FROM users WHERE username = '${username}'`;
4db.query(query);
5
6// βœ… Secure
7const username = req.body.username;
8const query = 'SELECT * FROM users WHERE username = ?';
9db.query(query, [username]);

Command Injection Prevention

 1// ❌ Vulnerable
 2import { exec } from 'child_process';
 3const filename = req.query.file;
 4exec(`cat ${filename}`, (error, stdout) => {
 5    res.send(stdout);
 6});
 7
 8// βœ… Secure
 9import { execFile } from 'child_process';
10const filename = req.query.file as string;
11if (!/^[a-zA-Z0-9_.-]+$/.test(filename)) {
12    throw new Error('Invalid filename');
13}
14execFile('cat', [filename], (error, stdout) => {
15    res.send(stdout);
16});

Secure Password Hashing

1// ❌ Insecure
2import crypto from 'crypto';
3const hash = crypto.createHash('md5').update(password).digest('hex');
4
5// βœ… Secure
6import bcrypt from 'bcrypt';
7const saltRounds = 12;
8const hash = await bcrypt.hash(password, saltRounds);
9const match = await bcrypt.compare(password, hash);

Secure Random Generation

1// ❌ Insecure
2const token = Math.random().toString(36).substring(2);
3
4// βœ… Secure
5import crypto from 'crypto';
6const token = crypto.randomBytes(32).toString('hex');

Related Snippets

  • C/C++ Code Smells
    Common code smells in C/C++ and how to fix them. Memory Leaks 1// ❌ Bad 2void …
  • C/C++ Secure Coding
    Secure coding practices for C/C++ applications. Buffer Overflow Prevention 1// ❌ …
  • C/C++ Vulnerability Checks
    Tools for checking vulnerabilities in C/C++ code. Valgrind 1# Install 2sudo apt …
  • Common Antipatterns
    Common software antipatterns to avoid across all languages and architectures. …
  • Common Code Smells
    Common code smells to watch for during code reviews with examples and fixes. …
  • Developer Pre-Submission Checklist
    Comprehensive checklist for developers before submitting a pull request. Code …
  • Go Code Smells
    Common code smells in Go and how to fix them. Ignoring Errors 1// ❌ Bad 2result, …
  • Go Secure Coding
    Secure coding practices for Go applications. SQL Injection Prevention 1// ❌ …
  • Go Vulnerability Checks
    Tools for checking vulnerabilities in Go code. Govulncheck 1# Install 2go …
  • Haskell Code Smells
    Common code smells in Haskell and how to fix them. Partial Functions 1-- ❌ Bad: …
  • Haskell Secure Coding
    Secure coding practices for Haskell applications. SQL Injection Prevention 1-- ❌ …
  • Haskell Vulnerability Checks
    Tools for checking vulnerabilities in Haskell code. Cabal Outdated 1# Check …
  • Python Code Smells
    Common code smells in Python and how to fix them. Mutable Default Arguments 1# ❌ …
  • Python Secure Coding
    Secure coding practices for Python applications. SQL Injection Prevention 1# ❌ …
  • Python Vulnerability Checks
    Tools for checking vulnerabilities in Python code. Safety - Dependency Scanner …
  • Reviewer Checklist
    Comprehensive checklist for code reviewers to ensure thorough and constructive …
  • Rust Code Smells
    Common code smells in Rust and how to fix them. Unwrap/Expect Abuse 1// ❌ Bad …
  • Rust Secure Coding
    Secure coding practices for Rust applications. SQL Injection Prevention 1// ❌ …
  • Rust Vulnerability Checks
    Tools for checking vulnerabilities in Rust code. Cargo Audit 1# Install 2cargo …
  • TypeScript Code Smells
    Common code smells in TypeScript and how to fix them. Using any 1// ❌ Bad …
  • TypeScript Vulnerability Checks
    Tools for checking vulnerabilities in TypeScript/JavaScript code. npm audit 1# …