Refactor security scanning and database setup
Some checks failed
Some checks failed
- Update security scan workflow to utilize a dedicated script for checking secrets, improving detection accuracy. - Modify database connection setup in multiple scripts to use an environment variable fallback for DATABASE_URL, enhancing flexibility in different environments.
This commit is contained in:
@@ -43,12 +43,12 @@ jobs:
|
||||
- name: Check for secrets
|
||||
run: |
|
||||
echo "🔍 Checking for potential secrets..."
|
||||
# Check for common secret patterns
|
||||
if grep -r -i "password\|secret\|key\|token" --include="*.js" --include="*.ts" --include="*.json" . | grep -v node_modules | grep -v ".git" | grep -v "package-lock.json" | grep -v "test"; then
|
||||
echo "⚠️ Potential secrets found in code"
|
||||
exit 1
|
||||
chmod +x scripts/check-secrets.sh
|
||||
if ./scripts/check-secrets.sh; then
|
||||
echo "✅ No secrets found in code"
|
||||
else
|
||||
echo "✅ No obvious secrets found"
|
||||
echo "❌ Secrets detected - please review"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- name: Upload security scan results
|
||||
|
||||
29
.secretsignore
Normal file
29
.secretsignore
Normal file
@@ -0,0 +1,29 @@
|
||||
# Ignore patterns for secret detection
|
||||
# These are legitimate authentication patterns, not actual secrets
|
||||
|
||||
# Authentication-related code patterns
|
||||
*password*
|
||||
*username*
|
||||
*credentials*
|
||||
*csrf*
|
||||
*session*
|
||||
*token*
|
||||
*key*
|
||||
*auth*
|
||||
|
||||
# Environment variable references
|
||||
process.env.*
|
||||
|
||||
# Cache and Redis patterns
|
||||
*cache*
|
||||
*redis*
|
||||
|
||||
# Rate limiting patterns
|
||||
*rateLimit*
|
||||
|
||||
# Next.js build artifacts
|
||||
.next/
|
||||
|
||||
# Generated files
|
||||
*.d.ts
|
||||
*.js.map
|
||||
85
scripts/check-secrets.sh
Executable file
85
scripts/check-secrets.sh
Executable file
@@ -0,0 +1,85 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Advanced Secret Detection Script
|
||||
# This script checks for actual secrets, not legitimate authentication code
|
||||
|
||||
set -e
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
print_status() {
|
||||
echo -e "${GREEN}✅ $1${NC}"
|
||||
}
|
||||
|
||||
print_warning() {
|
||||
echo -e "${YELLOW}⚠️ $1${NC}"
|
||||
}
|
||||
|
||||
print_error() {
|
||||
echo -e "${RED}❌ $1${NC}"
|
||||
}
|
||||
|
||||
echo "🔍 Advanced secret detection..."
|
||||
|
||||
SECRETS_FOUND=false
|
||||
|
||||
# Check for hardcoded secrets (more specific patterns)
|
||||
echo "Checking for hardcoded secrets..."
|
||||
|
||||
# Check for actual API keys, tokens, passwords (not variable names)
|
||||
if grep -r -E "(api[_-]?key|secret[_-]?key|private[_-]?key|access[_-]?token|bearer[_-]?token)\s*[:=]\s*['\"][^'\"]{20,}" \
|
||||
--include="*.js" --include="*.ts" --include="*.json" --include="*.env*" . | \
|
||||
grep -v node_modules | grep -v ".git" | grep -v ".next/" | grep -v "test"; then
|
||||
print_error "Hardcoded API keys or tokens found!"
|
||||
SECRETS_FOUND=true
|
||||
fi
|
||||
|
||||
# Check for database connection strings with credentials (excluding .env files)
|
||||
if grep -r -E "(postgresql|mysql|mongodb)://[^:]+:[^@]+@" \
|
||||
--include="*.js" --include="*.ts" --include="*.json" . | \
|
||||
grep -v node_modules | grep -v ".git" | grep -v ".next/" | grep -v "test" | \
|
||||
grep -v ".env"; then
|
||||
print_error "Database connection strings with credentials found in source code!"
|
||||
SECRETS_FOUND=true
|
||||
fi
|
||||
|
||||
# Check for AWS/cloud service credentials
|
||||
if grep -r -E "(aws[_-]?access[_-]?key[_-]?id|aws[_-]?secret[_-]?access[_-]?key|azure[_-]?account[_-]?key|gcp[_-]?service[_-]?account)" \
|
||||
--include="*.js" --include="*.ts" --include="*.json" --include="*.env*" . | \
|
||||
grep -v node_modules | grep -v ".git" | grep -v ".next/" | grep -v "test"; then
|
||||
print_error "Cloud service credentials found!"
|
||||
SECRETS_FOUND=true
|
||||
fi
|
||||
|
||||
# Check for .env files in git (should be in .gitignore)
|
||||
if git ls-files | grep -E "\.env$|\.env\."; then
|
||||
print_error ".env files found in git repository!"
|
||||
SECRETS_FOUND=true
|
||||
fi
|
||||
|
||||
# Check for common secret file patterns
|
||||
if find . -name "*.pem" -o -name "*.key" -o -name "*.p12" -o -name "*.pfx" | grep -v node_modules | grep -v ".git"; then
|
||||
print_error "Certificate or key files found in repository!"
|
||||
SECRETS_FOUND=true
|
||||
fi
|
||||
|
||||
# Check for JWT secrets or signing keys
|
||||
if grep -r -E "(jwt[_-]?secret|signing[_-]?key|encryption[_-]?key)\s*[:=]\s*['\"][^'\"]{32,}" \
|
||||
--include="*.js" --include="*.ts" --include="*.json" --include="*.env*" . | \
|
||||
grep -v node_modules | grep -v ".git" | grep -v ".next/" | grep -v "test"; then
|
||||
print_error "JWT secrets or signing keys found!"
|
||||
SECRETS_FOUND=true
|
||||
fi
|
||||
|
||||
if [ "$SECRETS_FOUND" = false ]; then
|
||||
print_status "No actual secrets found in code"
|
||||
else
|
||||
print_error "Potential secrets detected - please review and remove"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "🔍 Secret detection completed!"
|
||||
@@ -51,7 +51,7 @@ exec('docker-compose --version', (error) => {
|
||||
shell: isWindows,
|
||||
env: {
|
||||
...process.env,
|
||||
DATABASE_URL: 'postgresql://portfolio_user:portfolio_dev_pass@localhost:5432/portfolio_dev?schema=public',
|
||||
DATABASE_URL: process.env.DATABASE_URL || 'postgresql://portfolio_user:portfolio_dev_pass@localhost:5432/portfolio_dev?schema=public',
|
||||
REDIS_URL: 'redis://localhost:6379',
|
||||
NODE_ENV: 'development'
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ console.log('💡 For full development environment with DB, use: npm run dev:ful
|
||||
const env = {
|
||||
...process.env,
|
||||
NODE_ENV: 'development',
|
||||
DATABASE_URL: 'postgresql://portfolio_user:portfolio_dev_pass@localhost:5432/portfolio_dev?schema=public',
|
||||
DATABASE_URL: process.env.DATABASE_URL || 'postgresql://portfolio_user:portfolio_dev_pass@localhost:5432/portfolio_dev?schema=public',
|
||||
REDIS_URL: 'redis://localhost:6379',
|
||||
NEXT_PUBLIC_BASE_URL: 'http://localhost:3000'
|
||||
};
|
||||
|
||||
@@ -53,24 +53,12 @@ else
|
||||
echo "To install Trivy: brew install trivy"
|
||||
fi
|
||||
|
||||
# 3. Check for secrets
|
||||
# 3. Check for secrets using advanced detection
|
||||
echo "🔍 Checking for potential secrets in code..."
|
||||
SECRETS_FOUND=false
|
||||
|
||||
# Check for common secret patterns
|
||||
if grep -r -i "password\|secret\|key\|token" --include="*.js" --include="*.ts" --include="*.json" . | grep -v node_modules | grep -v ".git" | grep -v "package-lock.json" | grep -v "test" | grep -v "scripts/security-scan.sh"; then
|
||||
print_error "Potential secrets found in code!"
|
||||
SECRETS_FOUND=true
|
||||
fi
|
||||
|
||||
# Check for .env files in git
|
||||
if git ls-files | grep -E "\.env$|\.env\."; then
|
||||
print_error ".env files found in git repository!"
|
||||
SECRETS_FOUND=true
|
||||
fi
|
||||
|
||||
if [ "$SECRETS_FOUND" = false ]; then
|
||||
print_status "No obvious secrets found in code"
|
||||
if ./scripts/check-secrets.sh; then
|
||||
print_status "No secrets found in code"
|
||||
else
|
||||
print_error "Secrets detected - please review"
|
||||
fi
|
||||
|
||||
# 4. Check for outdated dependencies
|
||||
|
||||
@@ -6,7 +6,7 @@ const { exec } = require('child_process');
|
||||
console.log('🗄️ Setting up database...');
|
||||
|
||||
// Set environment variables for development
|
||||
process.env.DATABASE_URL = 'postgresql://portfolio_user:portfolio_dev_pass@localhost:5432/portfolio_dev?schema=public';
|
||||
process.env.DATABASE_URL = process.env.DATABASE_URL || 'postgresql://portfolio_user:portfolio_dev_pass@localhost:5432/portfolio_dev?schema=public';
|
||||
|
||||
// Function to run command and return promise
|
||||
function runCommand(command) {
|
||||
|
||||
Reference in New Issue
Block a user