Sentinel Guard - Bảo Vệ Bảo Mật 🛡️
Extension Visual Studio Code phát hiện lỗ hổng bảo mật realtime khi bạn lập trình, giúp code của bạn an toàn hơn ngay từ khi viết.
✨ Tính năng
🔍 Phát hiện bảo mật realtime
- Quét code tự động khi bạn gõ (debounce 350ms)
- Hiển thị lỗi ngay trong editor và Problems panel
- Hỗ trợ JavaScript, TypeScript, Python
🛠️ Tự động sửa lỗi (Auto-Fix)
- Quick Fix (Ctrl+.) cho các lỗi có thể sửa tự động
- Auto-Fix on Save (tùy chọn, mặc định TẮT)
- An toàn: chỉ áp dụng các fixes xác định được
📊 Giao diện trực quan
- Status Bar: Hiển thị số lượng vấn đề bảo mật
- Tree View: Danh sách tất cả lỗi theo file
- Output Channel: Log chi tiết các hoạt động
- Thông báo: Cảnh báo cho lỗi HIGH/CRITICAL
🔐 Các vấn đề được phát hiện
JavaScript / TypeScript
- ⚠️ Hardcoded Secrets: AWS keys, passwords, API keys
- 🎲 Insecure Random:
Math.random() cho mục đích bảo mật
- 💥 Dangerous eval/Function: Code injection risks
- 💉 Command Injection:
child_process.exec với string concatenation
- 💉 SQL Injection: String concatenation trong SQL queries
Python
- ⚠️ eval/exec: Nguy cơ Remote Code Execution
- 💉 subprocess shell=True: Command injection
- 🔐 Hardcoded Credentials: Passwords, secrets trong code
Chung (Mọi ngôn ngữ)
- 📝 Console.log Secrets: Log vô tình thông tin nhạy cảm
- 🔓 Weak Hashing: MD5/SHA1 cho password hashing
🚀 Cài đặt và Sử dụng
Cài đặt
- Clone repo hoặc copy folder
sentinel-guard
- Mở folder trong VS Code
- Chạy
npm install để cài dependencies
- Nhấn F5 để mở Extension Development Host
- Trong cửa sổ mới, mở bất kỳ file
.js, .ts, hoặc .py nào
Sử dụng
Extension tự động kích hoạt khi mở file JavaScript, TypeScript hoặc Python.
Commands (Ctrl+Shift+P)
Sentinel Guard: Quét File Hiện Tại - Quét lại file đang mở
Sentinel Guard: Bật/Tắt Tự Động Sửa - Toggle auto-fix on save
Sentinel Guard: Hiển Thị Output - Xem log chi tiết
Sử dụng Quick Fix
- Khi thấy gạch chân đỏ/vàng, đặt con trỏ tại đó
- Nhấn
Ctrl+. (hoặc click biểu tượng bóng đèn 💡)
- Chọn fix phù hợp
⚙️ Cấu hình
Mở Settings (Ctrl+,) và tìm "Sentinel Guard":
{
// Tự động sửa lỗi khi lưu file
"sentinelGuard.autoFixOnSave": false,
// Hiển thị thông báo cho lỗi HIGH/CRITICAL
"sentinelGuard.notifyOnHigh": true,
// Danh sách ngôn ngữ cần quét
"sentinelGuard.scanLanguages": [
"javascript",
"typescript",
"javascriptreact",
"typescriptreact",
"python"
],
// Kích thước tối đa file để quét (KB)
"sentinelGuard.maxFileSizeKB": 512,
// Thời gian debounce (ms)
"sentinelGuard.debounceMs": 350
}
🏗️ Kiến trúc
sentinel-guard/
├── src/
│ ├── extension.ts # Entry point, event handlers
│ ├── config.ts # Settings management
│ ├── engine/
│ │ ├── types.ts # Interfaces (Rule, Issue, Severity...)
│ │ └── scanner.ts # Scanner engine + debounce
│ ├── rules/
│ │ ├── index.ts # Tổng hợp tất cả rules
│ │ ├── jsRules.ts # JavaScript/TypeScript rules
│ │ ├── pyRules.ts # Python rules
│ │ └── commonRules.ts # Common rules (mọi ngôn ngữ)
│ └── providers/
│ ├── diagnostics.ts # Cập nhật Problems panel
│ ├── codeActionProvider.ts # Quick Fixes
│ └── treeViewProvider.ts # Tree View issues
├── package.json
├── tsconfig.json
└── README.md
📝 Thêm Rule mới
Để thêm rule bảo mật mới:
- Tạo rule object trong file tương ứng (
jsRules.ts, pyRules.ts, hoặc commonRules.ts):
export const myNewRule: Rule = {
id: 'my-new-rule',
name: 'Tên Rule',
description: 'Mô tả chi tiết',
severity: Severity.HIGH,
languages: ['javascript', 'typescript'],
check(text: string, document: vscode.TextDocument): Issue[] {
const issues: Issue[] = [];
const pattern = /pattern-của-bạn/g;
let match: RegExpMatchArray | null;
while ((match = pattern.exec(text)) !== null) {
issues.push(createIssueFromMatch(
this.id,
'Thông báo lỗi',
this.severity,
match,
document,
{
description: 'Mô tả fix',
replacement: 'code-thay-thế',
isDeterministic: true // true nếu fix an toàn
}
));
}
return issues;
}
};
- Export rule trong
rules/index.ts:
import { myNewRule } from './jsRules';
export const allRules: Rule[] = [
// ... existing rules
myNewRule
];
- Reload extension (Ctrl+Shift+P > "Developer: Reload Window")
🧪 Kế hoạch kiểm thử
Test Case 1: Phát hiện hardcoded password
Input:
const password = "MySecretPass123";
Expected: Gạch chân đỏ, severity HIGH, message về hardcoded password
Test Case 2: Phát hiện Math.random() cho token
Input:
const sessionToken = Math.random().toString();
Expected: Gạch chân vàng, severity MEDIUM, gợi ý dùng crypto
Test Case 3: Quick Fix
Input: Code có lỗi với fix có sẵn
Action: Ctrl+. tại vị trí lỗi
Expected: Hiển thị Quick Fix, áp dụng được
Test Case 4: Auto-Fix on Save
Setup: Bật sentinelGuard.autoFixOnSave
Input: File có lỗi có thể tự sửa
Action: Ctrl+S
Expected: Lỗi được sửa tự động, hiển thị thông báo
Test Case 5: Python subprocess
Input:
subprocess.call(cmd, shell=True)
Expected: Gạch chân đỏ, severity HIGH, warning về command injection
Test Case 6: Status Bar
Action: Mở file có 3 lỗi (1 HIGH, 2 MEDIUM)
Expected: Status bar hiển thị "3 vấn đề (1 nghiêm trọng)", màu đỏ
Test Case 7: Tree View
Action: Mở 2 files có lỗi
Expected: Tree view hiển thị 2 files, click vào issue sẽ nhảy đến đúng vị trí
📦 Build và Packaging
Development
npm install
npm run compile
# Nhấn F5 để debug
Production Build
npm run compile
Đóng gói thành VSIX (optional)
npm install -g vsce
vsce package
# Tạo file sentinel-guard-1.0.0.vsix
🔄 Changelog
Xem CHANGELOG.md
📄 License
MIT License - Tự do sử dụng và chỉnh sửa
🤝 Đóng góp
Contributions are welcome! Please feel free to submit issues or pull requests.
⚠️ Lưu ý quan trọng:
- Auto-Fix mặc định TẮT để tránh thay đổi code không mong muốn
- Extension chỉ quét files < 512KB để đảm bảo hiệu năng
- Các rule pattern có thể có false positives - luôn review trước khi apply fix
Made with ❤️ for secure coding