(修复方案)CVE-2023-26111: node-static 路径遍历漏洞
- [1. 升级或替换受影响模块](#1. 升级或替换受影响模块)
- [2. 增强路径验证(若必须使用 node-static)](#2. 增强路径验证(若必须使用 node-static))
1. 升级或替换受影响模块
受影响模块(该漏洞利用 ../ 等父目录访问绕过目录限制):
node-static@nubosoftware/node-static
替换为维护良好的静态服务器模块:
- serve-static(Express 官方推荐)
- http-server(Node 社区维护)
2. 增强路径验证(若必须使用 node-static)
如果暂时无法替换模块,可以在应用层增加路径检查:
例如:
js
const nodeStatic = require('node-static');
const path = require('path');
const http = require('http');
const fileServer = new nodeStatic.Server('./public');
http.createServer((req, res) => {
// 构造绝对路径
const requestedPath = path.normalize(req.url);
// 拒绝访问父目录
if (requestedPath.includes('..')) {
res.writeHead(403, {'Content-Type': 'text/plain'});
res.end('Access Denied');
return;
}
fileServer.serve(req, res);
}).listen(3000);