This lesson covers advanced security practices including code review techniques, compliance reporting, custom security linters, and dependency analysis. Learn how to use Copilot for thorough security assessments.
Location: demos/auth-api/
Files:
server.js- Production-ready authentication APImiddleware/security.js- Comprehensive security middleware
Security Features:
- Rate limiting with exponential backoff
- Account lockout protection
- Secure session management
- Input validation and sanitization
- Comprehensive audit logging
- CSRF protection
- Security headers
Run Demo:
node demos/auth-api/server.js
# API available at http://localhost:3000Location: scripts/compliance-report.js
Features:
- OWASP Top 10 coverage analysis
- PCI-DSS requirement mapping
- CWE categorization
- HTML and JSON report generation
- Remediation tracking
Copilot Prompt:
Generate compliance report for OWASP Top 10 and PCI-DSS requirements
Run:
node scripts/compliance-report.jsLocation: linters/security-linter.js
Detection Rules:
- SQL injection (string concatenation)
- XSS (innerHTML, document.write)
- Command injection (exec, spawn)
- Weak cryptography (MD5, SHA1, ECB mode)
- Hardcoded credentials
- Path traversal
- Information disclosure
Run:
node linters/security-linter.js ./src
# Or with SARIF output:
node linters/security-linter.js ./src --sarif > results.sarifLocation: scripts/dependency-analyzer.js
Features:
- Vulnerability detection
- License compliance checking
- Outdated package detection
- Dependency tree visualization
Run:
node scripts/dependency-analyzer.js .
# Or JSON output:
node scripts/dependency-analyzer.js . --json > analysis.json| Middleware | Purpose |
|---|---|
requestId |
Unique request ID for tracing |
securityHeaders |
CSP, HSTS, X-Frame-Options |
secureCors |
Secure CORS configuration |
RateLimiter |
Sliding window rate limiting |
sanitizeInput |
Input sanitization |
CsrfProtection |
CSRF token validation |
ipFilter |
IP whitelist/blacklist |
securityLogger |
Security-focused logging |
const {
requestId,
securityHeaders,
RateLimiter,
sanitizeInput
} = require('./middleware/security');
const app = express();
app.use(requestId);
app.use(securityHeaders());
app.use(sanitizeInput());
const limiter = new RateLimiter({
windowMs: 60000,
maxRequests: 100
});
app.use(limiter.middleware());| ID | Category | Detection |
|---|---|---|
| A01 | Broken Access Control | Auth tests, IDOR scanning |
| A02 | Cryptographic Failures | Weak crypto linting |
| A03 | Injection | SQL, XSS, Command injection |
| A04 | Insecure Design | Threat modeling |
| A05 | Security Misconfiguration | Header validation |
| A06 | Vulnerable Components | Dependency analysis |
| A07 | Auth Failures | Auth API testing |
| A08 | Integrity Failures | Dependency signing |
| A09 | Logging Failures | Audit log review |
| A10 | SSRF | URL validation |
const customRules = {
'no-custom-issue': {
severity: 'error',
message: 'Description of the issue',
cwe: 'CWE-XXX',
patterns: [
/pattern-to-match/g
]
}
};
const linter = new SecurityLinter({
rules: customRules
});| Property | Type | Description |
|---|---|---|
| severity | string | 'error' or 'warning' |
| message | string | Issue description |
| cwe | string | CWE identifier |
| patterns | RegExp[] | Detection patterns |
| exceptions | string[] | Strings to skip |
| check | function | Custom check function |
Generated at reports/compliance-report.html:
- Visual dashboard
- OWASP coverage chart
- Findings list with severity
- Remediation guidance
Generated at reports/compliance-report.json:
- Machine-readable format
- CI/CD integration ready
- Full finding details
- Score calculations
- name: Run Security Linter
run: node linters/security-linter.js ./src --sarif > results.sarif
- name: Upload SARIF
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: results.sarif
- name: Check Dependencies
run: node scripts/dependency-analyzer.js .Review this authentication code for security vulnerabilities
Check for OWASP Top 10 issues in this API endpoint
Analyze this code for race conditions and timing attacks
Create STRIDE threat model for this authentication flow
Identify attack vectors for this microservice architecture
- Regular Audits: Run security linters in CI/CD pipeline
- Dependency Updates: Monitor and update vulnerable packages
- Compliance Tracking: Generate reports for each release
- Code Review: Use Copilot for security-focused reviews
- Threat Modeling: Document threats during design phase