@theoptimalpartner/npm-security-kit 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/install.sh ADDED
@@ -0,0 +1,324 @@
1
+ #!/bin/bash
2
+
3
+ # NPM Security Kit - Installation Script
4
+ # Installs security infrastructure in any Node.js project
5
+
6
+ set -e
7
+
8
+ # Colors
9
+ RED='\033[0;31m'
10
+ GREEN='\033[0;32m'
11
+ YELLOW='\033[1;33m'
12
+ BLUE='\033[0;34m'
13
+ NC='\033[0m' # No Color
14
+
15
+ # Configuration
16
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
17
+ TARGET_DIR="${1:-.}"
18
+ UPDATE_MODE=false
19
+
20
+ # Parse arguments
21
+ for arg in "$@"; do
22
+ case $arg in
23
+ --update)
24
+ UPDATE_MODE=true
25
+ shift
26
+ ;;
27
+ --help)
28
+ echo "Usage: $0 [target-directory] [--update]"
29
+ echo ""
30
+ echo "Options:"
31
+ echo " target-directory Directory where to install (default: current directory)"
32
+ echo " --update Update existing installation (preserves config)"
33
+ echo " --help Show this help message"
34
+ exit 0
35
+ ;;
36
+ esac
37
+ done
38
+
39
+ echo -e "${BLUE}========================================"
40
+ echo "NPM Security Kit - Installation"
41
+ echo -e "========================================${NC}"
42
+ echo ""
43
+
44
+ # Check if target directory exists
45
+ if [ ! -d "$TARGET_DIR" ]; then
46
+ echo -e "${RED}✗ Target directory does not exist: $TARGET_DIR${NC}"
47
+ exit 1
48
+ fi
49
+
50
+ # Check if it's a Node.js project
51
+ if [ ! -f "$TARGET_DIR/package.json" ]; then
52
+ echo -e "${YELLOW}⚠ Warning: package.json not found in $TARGET_DIR${NC}"
53
+ echo -e "${YELLOW} This might not be a Node.js project.${NC}"
54
+ read -p "Continue anyway? (y/n) " -n 1 -r
55
+ echo
56
+ if [[ ! $REPLY =~ ^[Yy]$ ]]; then
57
+ exit 1
58
+ fi
59
+ fi
60
+
61
+ cd "$TARGET_DIR"
62
+
63
+ echo -e "${GREEN}✓ Target directory: $(pwd)${NC}"
64
+ echo ""
65
+
66
+ # Check for required tools
67
+ echo "Checking required tools..."
68
+ MISSING_TOOLS=()
69
+
70
+ if ! command -v jq &> /dev/null; then
71
+ MISSING_TOOLS+=("jq")
72
+ fi
73
+
74
+ if ! command -v npm &> /dev/null; then
75
+ MISSING_TOOLS+=("npm")
76
+ fi
77
+
78
+ if [ ${#MISSING_TOOLS[@]} -gt 0 ]; then
79
+ echo -e "${RED}✗ Missing required tools: ${MISSING_TOOLS[*]}${NC}"
80
+ echo ""
81
+ echo "Install them with:"
82
+ for tool in "${MISSING_TOOLS[@]}"; do
83
+ case $tool in
84
+ jq)
85
+ echo " - Ubuntu/Debian: sudo apt-get install jq"
86
+ echo " - macOS: brew install jq"
87
+ ;;
88
+ npm)
89
+ echo " - Visit: https://nodejs.org/"
90
+ ;;
91
+ esac
92
+ done
93
+ exit 1
94
+ fi
95
+
96
+ echo -e "${GREEN}✓ All required tools available${NC}"
97
+ echo ""
98
+
99
+ # Create directories
100
+ echo "Creating directory structure..."
101
+ mkdir -p .security
102
+ mkdir -p scripts
103
+ mkdir -p .github/workflows
104
+
105
+ echo -e "${GREEN}✓ Directories created${NC}"
106
+ echo ""
107
+
108
+ # Copy security files
109
+ echo "Installing security files..."
110
+
111
+ # 1. Blacklist
112
+ if [ -f "$SCRIPT_DIR/template/.security/malicious-packages-blacklist.json" ]; then
113
+ cp "$SCRIPT_DIR/template/.security/malicious-packages-blacklist.json" .security/
114
+ echo -e "${GREEN} ✓ Blacklist installed${NC}"
115
+ else
116
+ # Fallback: copy from auth-service if available
117
+ if [ -f "$SCRIPT_DIR/../auth-service/.security/malicious-packages-blacklist.json" ]; then
118
+ cp "$SCRIPT_DIR/../auth-service/.security/malicious-packages-blacklist.json" .security/
119
+ echo -e "${GREEN} ✓ Blacklist installed (from auth-service)${NC}"
120
+ else
121
+ echo -e "${YELLOW} ⚠ Blacklist not found, creating empty template${NC}"
122
+ cat > .security/malicious-packages-blacklist.json <<'EOF'
123
+ {
124
+ "lastUpdated": "2025-10-20",
125
+ "source": "Socket.dev",
126
+ "blacklistedPackages": [],
127
+ "patterns": [],
128
+ "maliciousDomains": [],
129
+ "suspiciousNpmAccounts": []
130
+ }
131
+ EOF
132
+ fi
133
+ fi
134
+
135
+ # 2. Config
136
+ if [ "$UPDATE_MODE" = false ] || [ ! -f ".security/config.json" ]; then
137
+ cat > .security/config.json <<'EOF'
138
+ {
139
+ "projectName": "my-project",
140
+ "projectType": "node",
141
+ "excludeDirs": ["node_modules", "dist", "build", ".next"],
142
+ "strictMode": true,
143
+ "notifications": {
144
+ "enabled": false,
145
+ "email": "",
146
+ "slack": ""
147
+ },
148
+ "autofix": {
149
+ "enabled": false,
150
+ "level": "moderate"
151
+ }
152
+ }
153
+ EOF
154
+ echo -e "${GREEN} ✓ Config created${NC}"
155
+ else
156
+ echo -e "${BLUE} → Config preserved (update mode)${NC}"
157
+ fi
158
+
159
+ # 3. Scripts
160
+ if [ -f "$SCRIPT_DIR/template/scripts/check-malicious-packages.sh" ]; then
161
+ cp "$SCRIPT_DIR/template/scripts/check-malicious-packages.sh" scripts/
162
+ else
163
+ cp "$SCRIPT_DIR/../auth-service/scripts/check-malicious-packages.sh" scripts/
164
+ fi
165
+
166
+ if [ -f "$SCRIPT_DIR/template/scripts/security-check.sh" ]; then
167
+ cp "$SCRIPT_DIR/template/scripts/security-check.sh" scripts/
168
+ else
169
+ cp "$SCRIPT_DIR/../auth-service/scripts/security-check.sh" scripts/
170
+ fi
171
+
172
+ chmod +x scripts/*.sh
173
+ echo -e "${GREEN} ✓ Scripts installed${NC}"
174
+
175
+ # 4. GitHub Actions
176
+ if [ -f "$SCRIPT_DIR/template/.github/workflows/security-audit.yml" ]; then
177
+ cp "$SCRIPT_DIR/template/.github/workflows/security-audit.yml" .github/workflows/
178
+ else
179
+ cp "$SCRIPT_DIR/../auth-service/.github/workflows/security-audit.yml" .github/workflows/
180
+ fi
181
+
182
+ echo -e "${GREEN} ✓ GitHub Actions workflow installed${NC}"
183
+
184
+ # 5. Dependabot
185
+ if [ -f "$SCRIPT_DIR/template/.github/dependabot.yml" ]; then
186
+ cp "$SCRIPT_DIR/template/.github/dependabot.yml" .github/
187
+ else
188
+ cp "$SCRIPT_DIR/../auth-service/.github/dependabot.yml" .github/
189
+ fi
190
+
191
+ echo -e "${GREEN} ✓ Dependabot config installed${NC}"
192
+
193
+ # 6. SECURITY.md
194
+ if [ "$UPDATE_MODE" = false ] || [ ! -f "SECURITY.md" ]; then
195
+ cat > SECURITY.md <<'EOF'
196
+ # Security Documentation
197
+
198
+ ## Quick Reference
199
+
200
+ ### Security Commands
201
+
202
+ ```bash
203
+ # Check for malicious packages
204
+ npm run security:check-malicious
205
+
206
+ # Full security audit
207
+ npm run security:full
208
+
209
+ # npm audit only
210
+ npm run security:audit
211
+
212
+ # Auto-fix vulnerabilities
213
+ npm run security:audit:fix
214
+
215
+ # Check outdated packages
216
+ npm run security:outdated
217
+
218
+ # Verify licenses
219
+ npm run security:licenses
220
+ ```
221
+
222
+ ## Current Security Status
223
+
224
+ **Last Audit**: Run `npm run security:full` to check
225
+
226
+ ## Automated Protection
227
+
228
+ - ✅ Blacklist of 175+ known malicious packages
229
+ - ✅ GitHub Actions (weekly scans)
230
+ - ✅ Dependabot (automatic updates)
231
+ - ✅ Pre-deployment checks
232
+
233
+ ## Reporting Security Issues
234
+
235
+ If you discover a security vulnerability, please report it to the security team.
236
+
237
+ ## Resources
238
+
239
+ - NPM Security Kit: https://github.com/tu-org/npm-security-kit
240
+ - Socket.dev: https://socket.dev
241
+ - npm Security: https://docs.npmjs.com/security
242
+ EOF
243
+ echo -e "${GREEN} ✓ SECURITY.md created${NC}"
244
+ else
245
+ echo -e "${BLUE} → SECURITY.md preserved (update mode)${NC}"
246
+ fi
247
+
248
+ echo ""
249
+
250
+ # Add npm scripts
251
+ echo "Adding npm scripts to package.json..."
252
+
253
+ if [ -f "package.json" ]; then
254
+ # Check if scripts already exist
255
+ if grep -q '"security:check-malicious"' package.json; then
256
+ echo -e "${BLUE} → Scripts already exist, skipping${NC}"
257
+ else
258
+ # Backup package.json
259
+ cp package.json package.json.backup
260
+
261
+ # Add scripts using jq
262
+ jq '.scripts += {
263
+ "security:check": "./scripts/security-check.sh",
264
+ "security:check-malicious": "./scripts/check-malicious-packages.sh",
265
+ "security:audit": "npm audit --audit-level=moderate",
266
+ "security:audit:fix": "npm audit fix",
267
+ "security:outdated": "npm outdated",
268
+ "security:licenses": "npm list --json | jq -r '\''.dependencies | to_entries[] | \"\\(.key): \\(.value.license)\"'\''",
269
+ "security:full": "npm run security:audit && npm run security:check"
270
+ }' package.json > package.json.tmp
271
+
272
+ mv package.json.tmp package.json
273
+ echo -e "${GREEN} ✓ Scripts added to package.json${NC}"
274
+ echo -e "${BLUE} → Backup saved as package.json.backup${NC}"
275
+ fi
276
+ else
277
+ echo -e "${YELLOW} ⚠ package.json not found, skipping script installation${NC}"
278
+ fi
279
+
280
+ echo ""
281
+
282
+ # Run initial check
283
+ echo "Running initial security check..."
284
+ if npm run security:check-malicious > /dev/null 2>&1; then
285
+ echo -e "${GREEN}✓ Initial check passed - No malicious packages detected${NC}"
286
+ else
287
+ echo -e "${YELLOW}⚠ Check completed with warnings${NC}"
288
+ fi
289
+
290
+ echo ""
291
+ echo -e "${BLUE}========================================"
292
+ echo "Installation Complete!"
293
+ echo -e "========================================${NC}"
294
+ echo ""
295
+ echo "Files installed:"
296
+ echo " ✓ .security/malicious-packages-blacklist.json"
297
+ echo " ✓ .security/config.json"
298
+ echo " ✓ scripts/check-malicious-packages.sh"
299
+ echo " ✓ scripts/security-check.sh"
300
+ echo " ✓ .github/workflows/security-audit.yml"
301
+ echo " ✓ .github/dependabot.yml"
302
+ echo " ✓ SECURITY.md"
303
+ echo " ✓ package.json (scripts added)"
304
+ echo ""
305
+ echo "Next steps:"
306
+ echo ""
307
+ echo " 1. Verify installation:"
308
+ echo " npm run security:check-malicious"
309
+ echo ""
310
+ echo " 2. Run full audit:"
311
+ echo " npm run security:full"
312
+ echo ""
313
+ echo " 3. Customize config:"
314
+ echo " Edit .security/config.json"
315
+ echo ""
316
+ echo " 4. Commit files:"
317
+ echo " git add .security scripts .github SECURITY.md package.json"
318
+ echo " git commit -m \"security: add npm security kit\""
319
+ echo ""
320
+ echo " 5. Push to enable GitHub Actions:"
321
+ echo " git push"
322
+ echo ""
323
+ echo -e "${GREEN}Your project is now protected!${NC} 🛡️"
324
+ echo ""
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@theoptimalpartner/npm-security-kit",
3
+ "version": "1.0.0",
4
+ "description": "Portable security kit to protect Node.js/npm projects against malicious packages and vulnerabilities",
5
+ "main": "src/index.js",
6
+ "bin": {
7
+ "npm-security-install": "./bin/install.js",
8
+ "npm-security-check": "./bin/check.js"
9
+ },
10
+ "scripts": {
11
+ "test": "echo \"No tests specified - package verified with npm pack\"",
12
+ "prepare": "chmod +x bin/*.js"
13
+ },
14
+ "keywords": [
15
+ "security",
16
+ "npm",
17
+ "malicious-packages",
18
+ "supply-chain",
19
+ "vulnerability",
20
+ "audit",
21
+ "phishing",
22
+ "typosquatting",
23
+ "socket.dev",
24
+ "dependabot",
25
+ "github-actions"
26
+ ],
27
+ "author": "The Optimal",
28
+ "license": "MIT",
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "https://bitbucket.org/RoutingUC/npm-security-kit.git"
32
+ },
33
+ "engines": {
34
+ "node": ">=14.0.0",
35
+ "npm": ">=6.0.0"
36
+ },
37
+ "files": [
38
+ "bin/",
39
+ "src/",
40
+ "template/",
41
+ "README.md",
42
+ "LICENSE",
43
+ "QUICK_START.md",
44
+ "EXAMPLES.md"
45
+ ],
46
+ "dependencies": {},
47
+ "devDependencies": {},
48
+ "peerDependencies": {},
49
+ "publishConfig": {
50
+ "access": "public"
51
+ }
52
+ }
package/src/index.js ADDED
@@ -0,0 +1,71 @@
1
+ /**
2
+ * NPM Security Kit
3
+ * Main entry point for programmatic usage
4
+ */
5
+
6
+ const path = require('path');
7
+ const fs = require('fs');
8
+
9
+ /**
10
+ * Get path to template files
11
+ */
12
+ function getTemplatePath() {
13
+ return path.join(__dirname, '..', 'template');
14
+ }
15
+
16
+ /**
17
+ * Get path to blacklist file
18
+ */
19
+ function getBlacklistPath() {
20
+ return path.join(getTemplatePath(), '.security', 'malicious-packages-blacklist.json');
21
+ }
22
+
23
+ /**
24
+ * Load malicious packages blacklist
25
+ */
26
+ function loadBlacklist() {
27
+ const blacklistPath = getBlacklistPath();
28
+ if (!fs.existsSync(blacklistPath)) {
29
+ throw new Error('Blacklist file not found');
30
+ }
31
+ return JSON.parse(fs.readFileSync(blacklistPath, 'utf8'));
32
+ }
33
+
34
+ /**
35
+ * Check if a package is in the blacklist
36
+ * @param {string} packageName - Name of the package to check
37
+ * @returns {boolean} - True if package is malicious
38
+ */
39
+ function isMalicious(packageName) {
40
+ const blacklist = loadBlacklist();
41
+ return blacklist.blacklistedPackages.includes(packageName);
42
+ }
43
+
44
+ /**
45
+ * Check if a package matches malicious patterns
46
+ * @param {string} packageName - Name of the package to check
47
+ * @returns {boolean} - True if package matches a malicious pattern
48
+ */
49
+ function matchesMaliciousPattern(packageName) {
50
+ const blacklist = loadBlacklist();
51
+ return blacklist.patterns.some(p => {
52
+ const regex = new RegExp(p.pattern);
53
+ return regex.test(packageName);
54
+ });
55
+ }
56
+
57
+ /**
58
+ * Get installation path
59
+ */
60
+ function getInstallPath() {
61
+ return path.join(__dirname, '..', 'bin', 'install.sh');
62
+ }
63
+
64
+ module.exports = {
65
+ getTemplatePath,
66
+ getBlacklistPath,
67
+ loadBlacklist,
68
+ isMalicious,
69
+ matchesMaliciousPattern,
70
+ getInstallPath
71
+ };
@@ -0,0 +1,63 @@
1
+ version: 2
2
+ updates:
3
+ # Enable version updates for npm dependencies
4
+ - package-ecosystem: "npm"
5
+ directory: "/"
6
+ schedule:
7
+ interval: "weekly"
8
+ day: "monday"
9
+ time: "09:00"
10
+ # Limit the number of open pull requests
11
+ open-pull-requests-limit: 10
12
+ # Add reviewers
13
+ reviewers:
14
+ - "diego"
15
+ # Add labels
16
+ labels:
17
+ - "dependencies"
18
+ - "security"
19
+ - "automerge-candidate"
20
+ # Group updates
21
+ groups:
22
+ aws-sdk:
23
+ patterns:
24
+ - "@aws-sdk/*"
25
+ typescript:
26
+ patterns:
27
+ - "typescript"
28
+ - "@types/*"
29
+ - "ts-node"
30
+ - "tsx"
31
+ testing:
32
+ patterns:
33
+ - "jest"
34
+ - "@types/jest"
35
+ - "c8"
36
+ serverless:
37
+ patterns:
38
+ - "serverless*"
39
+ # Security updates configuration
40
+ versioning-strategy: increase
41
+ # Allow both direct and indirect updates
42
+ allow:
43
+ - dependency-type: "all"
44
+ # Ignore major version updates for now (require manual review)
45
+ ignore:
46
+ - dependency-name: "*"
47
+ update-types: ["version-update:semver-major"]
48
+ # Automatically rebase PRs
49
+ rebase-strategy: "auto"
50
+ # Commit message prefix
51
+ commit-message:
52
+ prefix: "chore"
53
+ prefix-development: "chore(dev)"
54
+ include: "scope"
55
+
56
+ # Monitor GitHub Actions
57
+ - package-ecosystem: "github-actions"
58
+ directory: "/"
59
+ schedule:
60
+ interval: "weekly"
61
+ labels:
62
+ - "ci"
63
+ - "security"