-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_isolation.sh
More file actions
executable file
·478 lines (408 loc) · 14.6 KB
/
node_isolation.sh
File metadata and controls
executable file
·478 lines (408 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/bin/bash
# =============================================================================
# Node.js Bubblewrap Sandbox Installer
#
# This script sets up complete Node.js and npm isolation using bubblewrap
# for enhanced security when running potentially malicious npm packages.
#
# Features:
# - Isolates Node.js and npm in a secure sandbox
# - Protects SSH keys, browser data, and system files
# - Maintains development workflow capabilities
# - Provides network access for npm installs
# - Creates comprehensive test scripts for verification
#
# Author: Generated for secure Node.js development
# =============================================================================
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m' # No Color
# Create separator line (compatible with all shells)
create_separator() {
echo "============================================================"
}
# Print functions
print_header() {
echo -e "\n${CYAN}${BOLD}$(create_separator)${NC}"
echo -e "${CYAN}${BOLD}$1${NC}"
echo -e "${CYAN}${BOLD}$(create_separator)${NC}"
}
print_step() {
echo -e "\n${BLUE}${BOLD}➤ $1${NC}"
}
print_success() {
echo -e "${GREEN}✅ $1${NC}"
}
print_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
print_error() {
echo -e "${RED}❌ $1${NC}"
}
print_info() {
echo -e "${BLUE}ℹ️ $1${NC}"
}
# Check if running as root
check_not_root() {
if [ "$(id -u)" -eq 0 ]; then
print_error "This script should not be run as root!"
print_info "Please run as your regular user account."
exit 1
fi
}
# Check system requirements
check_requirements() {
print_step "Checking system requirements..."
# Check if bubblewrap is installed
if ! command -v bwrap >/dev/null 2>&1; then
print_error "bubblewrap is not installed!"
print_info "Install it with: sudo apt install bubblewrap"
exit 1
fi
print_success "bubblewrap found: $(which bwrap)"
# Check if Node.js is installed
if ! command -v node >/dev/null 2>&1; then
print_error "Node.js is not installed!"
print_info "Install it with: sudo apt install nodejs"
exit 1
fi
print_success "Node.js found: $(which node) ($(node --version))"
# Check if npm is installed
if ! command -v npm >/dev/null 2>&1; then
print_error "npm is not installed!"
print_info "Install it with: sudo apt install npm"
exit 1
fi
print_success "npm found: $(which npm) ($(npm --version))"
# Check bubblewrap permissions
BWRAP_PATH=$(which bwrap)
if [ ! -u "$BWRAP_PATH" ]; then
print_warning "bubblewrap needs setuid permissions for proper operation"
print_info "You may need to run: sudo chmod u+s $BWRAP_PATH"
printf "Do you want to set bubblewrap permissions now? (y/N): "
read -r REPLY
if [ "$REPLY" = "y" ] || [ "$REPLY" = "Y" ]; then
sudo chmod u+s "$BWRAP_PATH"
print_success "bubblewrap permissions set"
else
print_warning "Continuing without setting permissions - sandbox may not work properly"
fi
else
print_success "bubblewrap has proper permissions"
fi
# Check if user namespaces are enabled
if [ -f /proc/sys/kernel/unprivileged_userns_clone ]; then
USERNS_ENABLED=$(cat /proc/sys/kernel/unprivileged_userns_clone)
if [ "$USERNS_ENABLED" != "1" ]; then
print_warning "Unprivileged user namespaces are disabled"
print_info "You may need to run: sudo sysctl kernel.unprivileged_userns_clone=1"
printf "Do you want to enable user namespaces now? (y/N): "
read -r REPLY
if [ "$REPLY" = "y" ] || [ "$REPLY" = "Y" ]; then
sudo sysctl kernel.unprivileged_userns_clone=1
echo 'kernel.unprivileged_userns_clone=1' | sudo tee -a /etc/sysctl.conf
print_success "User namespaces enabled"
else
print_info "Continuing without enabling user namespaces - sandbox may not work"
fi
else
print_success "User namespaces are enabled"
fi
fi
}
# Create directory structure
setup_directories() {
print_step "Setting up sandbox directories..."
# Create local bin directory
mkdir -p "$HOME/.local/bin"
print_success "Created $HOME/.local/bin"
# Create sandbox home directory
SANDBOX_HOME="$HOME/.sandbox/node"
mkdir -p "$SANDBOX_HOME"
mkdir -p "$SANDBOX_HOME/tmp"
mkdir -p "$SANDBOX_HOME/.npm"
mkdir -p "$SANDBOX_HOME/.npm-global"
mkdir -p "$SANDBOX_HOME/.npm-global/bin"
mkdir -p "$SANDBOX_HOME/.npm-global/lib"
print_success "Created sandbox directory: $SANDBOX_HOME"
# Create npm configuration in sandbox
cat > "$SANDBOX_HOME/.npmrc" << EOF
prefix=$SANDBOX_HOME/.npm-global
cache=$SANDBOX_HOME/.npm
EOF
print_success "Created npm configuration"
}
# Create Node.js wrapper script
create_node_wrapper() {
print_step "Creating Node.js sandbox wrapper..."
cat > "$HOME/.local/bin/node" << 'EOF'
#!/bin/bash
# Sandboxed Node.js wrapper using bubblewrap
REAL_NODE="/usr/bin/node"
SANDBOX_HOME="$HOME/.sandbox/node"
# Ensure sandbox home exists
mkdir -p "$SANDBOX_HOME"
mkdir -p "$SANDBOX_HOME/tmp"
exec bwrap \
--unshare-all \
--share-net \
--clearenv \
--setenv HOME "$SANDBOX_HOME" \
--setenv USER "$USER" \
--setenv PATH "/usr/bin:/bin:/usr/local/bin:$SANDBOX_HOME/.npm-global/bin" \
--setenv PWD "$(pwd)" \
--ro-bind /usr /usr \
--ro-bind /lib /lib \
--ro-bind /lib64 /lib64 \
--ro-bind /bin /bin \
--ro-bind /sbin /sbin \
--ro-bind /etc/ld.so.cache /etc/ld.so.cache \
--ro-bind /etc/ld.so.conf /etc/ld.so.conf \
--ro-bind /etc/ld.so.conf.d /etc/ld.so.conf.d \
--ro-bind /etc/passwd /etc/passwd \
--ro-bind /etc/group /etc/group \
--ro-bind /etc/nsswitch.conf /etc/nsswitch.conf \
--ro-bind /etc/resolv.conf /etc/resolv.conf \
--ro-bind /etc/ssl /etc/ssl \
--ro-bind /etc/ca-certificates /etc/ca-certificates \
--bind "$SANDBOX_HOME" "$SANDBOX_HOME" \
--bind "$SANDBOX_HOME/tmp" /tmp \
--bind "$(pwd)" "$(pwd)" \
--proc /proc \
--dev /dev \
--tmpfs /run \
--tmpfs /var \
--die-with-parent \
--new-session \
"$REAL_NODE" "$@"
EOF
chmod +x "$HOME/.local/bin/node"
print_success "Created sandboxed node wrapper"
}
# Create npm wrapper script
create_npm_wrapper() {
print_step "Creating npm sandbox wrapper..."
cat > "$HOME/.local/bin/npm" << 'EOF'
#!/bin/bash
# Sandboxed npm wrapper using bubblewrap
REAL_NPM="/usr/bin/npm"
SANDBOX_HOME="$HOME/.sandbox/node"
# Ensure sandbox home exists
mkdir -p "$SANDBOX_HOME"
# Create npm directories in sandbox
mkdir -p "$SANDBOX_HOME/.npm"
mkdir -p "$SANDBOX_HOME/.npm-global"
mkdir -p "$SANDBOX_HOME/.npm-global/bin"
mkdir -p "$SANDBOX_HOME/.npm-global/lib"
# Create npm config if it doesn't exist
if [ ! -f "$SANDBOX_HOME/.npmrc" ]; then
cat > "$SANDBOX_HOME/.npmrc" << NPMEOF
prefix=$SANDBOX_HOME/.npm-global
cache=$SANDBOX_HOME/.npm
NPMEOF
fi
# Create a minimal /tmp inside sandbox
SANDBOX_TMP="$SANDBOX_HOME/tmp"
mkdir -p "$SANDBOX_TMP"
exec bwrap \
--unshare-all \
--share-net \
--clearenv \
--setenv HOME "$SANDBOX_HOME" \
--setenv USER "$USER" \
--setenv PATH "/usr/bin:/bin:/usr/local/bin:$SANDBOX_HOME/.npm-global/bin" \
--setenv PWD "$(pwd)" \
--ro-bind /usr /usr \
--ro-bind /lib /lib \
--ro-bind /lib64 /lib64 \
--ro-bind /bin /bin \
--ro-bind /sbin /sbin \
--ro-bind /etc/ld.so.cache /etc/ld.so.cache \
--ro-bind /etc/ld.so.conf /etc/ld.so.conf \
--ro-bind /etc/ld.so.conf.d /etc/ld.so.conf.d \
--ro-bind /etc/passwd /etc/passwd \
--ro-bind /etc/group /etc/group \
--ro-bind /etc/nsswitch.conf /etc/nsswitch.conf \
--ro-bind /etc/resolv.conf /etc/resolv.conf \
--ro-bind /etc/ssl /etc/ssl \
--ro-bind /etc/ca-certificates /etc/ca-certificates \
--bind "$SANDBOX_HOME" "$SANDBOX_HOME" \
--bind "$SANDBOX_TMP" /tmp \
--bind "$(pwd)" "$(pwd)" \
--proc /proc \
--dev /dev \
--tmpfs /run \
--tmpfs /var \
--die-with-parent \
--new-session \
"$REAL_NPM" "$@"
EOF
chmod +x "$HOME/.local/bin/npm"
print_success "Created sandboxed npm wrapper"
}
# Setup PATH in shell configuration
setup_path() {
print_step "Configuring PATH in shell profiles..."
PATH_EXPORT='export PATH="$HOME/.local/bin:$PATH"'
# Add to .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
if ! grep -q "$HOME/.local/bin" "$HOME/.bashrc"; then
echo "" >> "$HOME/.bashrc"
echo "# Node.js sandbox - added by Node.js sandbox installer" >> "$HOME/.bashrc"
echo "$PATH_EXPORT" >> "$HOME/.bashrc"
print_success "Added PATH to .bashrc"
else
print_info ".bashrc already contains .local/bin in PATH"
fi
fi
# Add to .zshrc if it exists
if [ -f "$HOME/.zshrc" ]; then
if ! grep -q "$HOME/.local/bin" "$HOME/.zshrc"; then
echo "" >> "$HOME/.zshrc"
echo "# Node.js sandbox - added by Node.js sandbox installer" >> "$HOME/.zshrc"
echo "$PATH_EXPORT" >> "$HOME/.zshrc"
print_success "Added PATH to .zshrc"
else
print_info ".zshrc already contains .local/bin in PATH"
fi
fi
# Add to .profile as fallback
if [ -f "$HOME/.profile" ]; then
if ! grep -q "$HOME/.local/bin" "$HOME/.profile"; then
echo "" >> "$HOME/.profile"
echo "# Node.js sandbox - added by Node.js sandbox installer" >> "$HOME/.profile"
echo "$PATH_EXPORT" >> "$HOME/.profile"
print_success "Added PATH to .profile"
else
print_info ".profile already contains .local/bin in PATH"
fi
fi
}
# Test installation
test_installation() {
print_step "Testing sandbox installation..."
# Export PATH for this session
export PATH="$HOME/.local/bin:$PATH"
# Test which commands are being used
NODE_WHICH=$(which node)
NPM_WHICH=$(which npm)
print_info "node command location: $NODE_WHICH"
print_info "npm command location: $NPM_WHICH"
if [ "$NODE_WHICH" = "$HOME/.local/bin/node" ]; then
print_success "node wrapper is active"
else
print_warning "node wrapper not in PATH - you may need to restart your shell"
fi
if [ "$NPM_WHICH" = "$HOME/.local/bin/npm" ]; then
print_success "npm wrapper is active"
else
print_warning "npm wrapper not in PATH - you may need to restart your shell"
fi
# Test basic functionality with system node first to avoid recursion
print_info "Testing Node.js version with system node..."
if /usr/bin/node --version >/dev/null 2>&1; then
print_success "System Node.js working"
else
print_error "System Node.js test failed"
return 1
fi
print_info "Testing npm version with system npm..."
if /usr/bin/npm --version >/dev/null 2>&1; then
print_success "System npm working"
else
print_error "System npm test failed"
return 1
fi
# Test sandbox functionality
print_info "Testing sandboxed Node.js version..."
if "$HOME/.local/bin/node" --version >/dev/null 2>&1; then
print_success "Sandboxed Node.js working"
else
print_error "Sandboxed Node.js test failed"
return 1
fi
# Run basic security test using the test script in current directory
print_info "Running basic security test..."
if [ -f "./test-sandbox.js" ]; then
if "$HOME/.local/bin/node" ./test-sandbox.js; then
print_success "Security test passed"
else
print_warning "Security test had issues"
fi
else
print_warning "Test script not found - skipping security test"
fi
}
# Create uninstaller
create_uninstaller() {
print_step "Creating uninstaller script..."
cat > "$HOME/.local/bin/uninstall-node-sandbox" << 'EOF'
#!/bin/bash
echo "🗑️ Uninstalling Node.js Sandbox..."
# Remove wrapper scripts
rm -f "$HOME/.local/bin/node"
rm -f "$HOME/.local/bin/npm"
rm -f "$HOME/.local/bin/uninstall-node-sandbox"
# Note: test-sandbox.js is part of the project and should not be removed
# Optionally remove sandbox directory
printf "Remove sandbox directory $HOME/.sandbox/node? (y/N): "
read -r REPLY
if [ "$REPLY" = "y" ] || [ "$REPLY" = "Y" ]; then
rm -rf "$HOME/.sandbox/node"
echo "✅ Sandbox directory removed"
else
echo "ℹ️ Sandbox directory preserved"
fi
echo "✅ Node.js sandbox uninstalled"
echo "Note: You may want to manually remove PATH entries from your shell config files"
echo "Look for lines containing: # Node.js sandbox - added by Node.js sandbox installer"
EOF
chmod +x "$HOME/.local/bin/uninstall-node-sandbox"
print_success "Created uninstaller: uninstall-node-sandbox"
}
# Main installation function
main() {
print_header "Node.js Bubblewrap Sandbox Installer"
print_info "This installer will set up a secure Node.js sandbox using bubblewrap."
print_info "The sandbox will protect your system from malicious npm packages."
print_info ""
printf "Do you want to continue with the installation? (Y/n): "
read -r REPLY
if [ "$REPLY" = "n" ] || [ "$REPLY" = "N" ]; then
print_info "Installation cancelled"
exit 0
fi
check_not_root
check_requirements
setup_directories
create_node_wrapper
create_npm_wrapper
setup_path
create_uninstaller
print_header "Testing Installation"
test_installation
print_header "Installation Complete!"
print_success "Node.js sandbox has been successfully installed!"
echo
print_info "Next steps:"
echo " 1. Restart your terminal or run: source ~/.bashrc"
echo " 2. Verify sandbox is active with: which node && which npm"
echo " (Should show: $HOME/.local/bin/node and $HOME/.local/bin/npm)"
echo " 3. Run security test: node test-sandbox.js"
echo " 4. For comparison, test with system node: /usr/bin/node test-sandbox.js"
echo
print_info "Your Node.js and npm commands are now sandboxed!"
print_info "Malicious packages cannot access your SSH keys, browser data, or system files."
echo
print_info "To uninstall: uninstall-node-sandbox"
echo
print_warning "PATH was added to your shell config - restart terminal or run 'source ~/.bashrc'!"
}
# Run main function
main "$@"