Enterprise Security Setup
This project allows you to scaffold Node.js microservices with enterprise-grade security hardening out of the box. This feature is designed to meet the rigorous security standards of top-tier tech companies.
🛡️ Features Included
When you select "Yes" for Enterprise Security Hardening during project initialization, the following tools and configurations are added:
1. Snyk (SCA - Software Composition Analysis)
- Tool: Snyk
- Purpose: Automatically scans your
node_modulesfor known vulnerabilities in third-party dependencies. - Integration:
- A dedicated GitHub Actions workflow (
.github/workflows/security.yml). - GitLab CI and Jenkins stages for automated scanning.
- Fail-safe: CI will fail if "High" or "Critical" vulnerabilities are detected.
- A dedicated GitHub Actions workflow (
2. SonarCloud / SonarQube (SAST - Static Application Security Testing)
- Tool: SonarCloud
- Purpose: Deep-dive static analysis to detect code smells, bugs, and security hotspots (e.g., SQL Injection, XSS patterns).
- Integration:
- Pre-configured
sonar-project.properties. - Quality Gates enforced in CI/CD pipelines.
- Pre-configured
3. Husky (Pre-commit Quality Gates)
- Tool: Husky & lint-staged
- Purpose: Automatically runs linting and formatting on changed files before every commit to ensure code quality.
- Integration:
- Automatically initialized during
npm install. - Prevents "bad code" from being committed to the repository.
- Automatically initialized during
To fully activate these features in your generated project, follow these detailed steps to obtain your authentication tokens and configure your CI/CD environment.
🛡️ 1. Snyk Integration (SCA)
Snyk monitors your dependencies for known vulnerabilities. To set it up:
- Create an Account: Sign up for a free account at Snyk.io.
- Get your Auth Token:
- Click on your profile avatar (bottom left) -> Account Settings.
- Find the Auth Token section.
- Click Click to show and copy your token.
- Alternatively, for Organizational use, go to Settings -> Service Accounts to create a non-personal token.
- Configure GitHub Secrets:
- Go to your GitHub Repository -> Settings -> Secrets and variables -> Actions.
- Click New repository secret.
- Name:
SNYK_TOKEN - Value: (Paste your token here).
🔍 2. SonarCloud Integration (SAST)
SonarCloud performs deep static analysis and tracks code quality.
- Sign Up: Log in to SonarCloud.io using your GitHub account.
- Create/Import Project:
IMPORTANT
You must manually import your project in the SonarCloud UI before the CI/CD pipeline can scan it.
- Click the "+" icon in the top right -> Analyze new project.
- Select your GitHub organization and import the specific repository.
- Configure Analysis Method:
- Once imported, go to Administration -> Analysis Method.
- Turn OFF "SonarCloud Automatic Analysis".
- Select GitHub Actions as your primary analysis method. This provides you with the correct
sonar-project.propertiesvalues.
- Generate a Token:
- Click your profile icon -> My Account -> Security.
- Generate a new token and copy it.
- Get your Project & Organization Details:
- On your SonarCloud project dashboard, look for the Project Key and Organization Key in the bottom right corner of the "Information" section.
- Note: The Project Key often looks like
your-org_your-repo. - Update your local
sonar-project.properties:sonar.projectKey=exact-key-from-sonarcloudsonar.organization=exact-org-from-sonarcloud
- Configure GitHub Secrets:
- Add
SONAR_TOKEN: (Paste the token you just generated). - Add
SONAR_HOST_URL:- If using SonarCloud: Set this to
https://sonarcloud.io. - If using Self-hosted SonarQube: Set this to your instance URL (e.g.,
http://your-server-ip:9000).
- If using SonarCloud: Set this to
- Add
TIP
Most users should use https://sonarcloud.io as it is the official free cloud service for open-source and small projects!
🐳 3. Snyk Container Scanning
In addition to dependency scanning, the generator includes Container Security audits to check your Dockerfile and base image.
- Requirement: Ensure your
Dockerfileis present at the root. - CI/CD: The
security.yml(GitHub) orsecuritystage (GitLab/Jenkins) automatically builds your image and runssnyk container test. - Local Check:bash
docker build -t my-app . snyk container test my-app --file=Dockerfile --severity-threshold=high
⚡ 4. Husky & Lint-Staged
IMPORTANT
You must run git init before running npm install for Husky to set up the hooks correctly.
- Initialize Git:
git init - Install:
npm install(This automatically triggershusky install). - Usage: Just try to commit! Husky will automatically run
lint-staged.
Troubleshooting: If you see an error like .husky/pre-commit: line 2: .husky/_/husky.sh: No such file, it means Husky wasn't initialized correctly (usually because git init was skipped). To fix it:
npx husky install4. Running Scans Locally
You can run a security audit at any time using:
npm run security:checkThis command runs npm audit and snyk test in sequence.