5

我正在尝试在 TypeScript 项目中添加eslint-plugin-security 。但是,对于这些代码

import { promises as fsp } from 'fs';
import fs from 'fs';
import path from 'path';

const index = await fsp.readFile(path.resolve(__dirname, './index.html'), 'utf-8');
const key = fs.readFileSync(path.join(__dirname, './ssl.key'));
await fsp.writeFile(path.resolve(__dirname, './sitemap.xml'), sitemap);

我收到了很多这样的 ESLint 警告:

warning Found fs.readFile with non literal argument at index 0   security/detect-non-literal-fs-filename
warning Found fs.readFileSync with non literal argument at index 0  security/detect-non-literal-fs-filename
warning Found fs.writeFile with non literal argument at index 0  security/detect-non-literal-fs-filename

我在https://github.com/nodesecurity/eslint-plugin-security#detect-non-literal-fs-filename找到了有关此 ESLint 错误的文档

但我仍然不知道如何解决它。任何指南都会有所帮助!谢谢


更新

发现只要使用传递返回的路径path.join就会path.resolve出现这个ESLint问题。

如果我更改为绝对路径,ESLint 问题就消失了。path.join但是,这通过or失去了相对路径的好处path.resolve

fs.readFileSync('/Users/me/project/ssl.key');

寻找替代/更好的方法(如果存在)。

(可能是误报?在https://github.com/nodesecurity/eslint-plugin-security/issues/65询问)

4

0 回答 0