在 Debian 上运行 Node.js 服务时,安全加固通常从系统层、Node 运行时、依赖与代码、运行与网络四个维度来做。下面给你一套比较实用、可落地的加固清单。
apt update && apt upgrade -y
useradd -m -s /bin/bash nodeapp
su - nodeapp
systemd 指定 User=ufw enable
ufw allow 22
ufw allow 443
ufw deny 3000 # 若只内网访问
PermitRootLogin no
node -v
nodesource 或 nvm 管理版本--unsafe-perm--experimental-*(生产环境)process.env.NODE_DISABLE_COLORS = 1
eval、Function--max-http-header-size 等限制防止头部攻击:
node --max-http-header-size=8192 app.js
npm ci
package-lock.jsonnpm install xxxnpm audit
npm audit fix
或使用:
snykowasp-dependency-checkpostinstall 脚本const helmet = require('helmet')
app.use(helmet())
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
[Service]
User=nodeapp
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
PrivateTmp=true
RestrictSUIDSGID=true
❌ root 运行 Node
❌ 开放 0.0.0.0:3000 到公网
❌ 忽略 npm audit
❌ 使用废弃模块(如 request)
❌ debug 模式上线
node -v
npm audit
ufw status
ps aux | grep node
cat /etc/ssh/sshd_config | grep PermitRoot
如果你愿意,我可以:
你目前是哪种部署方式?(裸机 / Docker / K8s)