如何在 git repo 中查找双向 Unicode 控制字符?

发布于 2025-01-16 14:16:46 字数 557 浏览 2 评论 0原文

我在 NPM 上有一个包,显示它包含 socket.dev 报告的“双向 unicode 控制字符”

我找到了这个问题的答案 如何更新 GitHub Actions CI 以检测木马代码提交(恶意[双向] unicode 字符,python)< /a>.

我使用过:

git grep -oP "[^\x00-\x7F]*"

它在二进制文件中找到了一些匹配项,因此我从 .gitattributes 中删除了所有二进制标志,现在我只有 __tests__ 目录中包含 ANSI 文件和一个图像的文件,但是那些未发布到 NPM。

找到这些“双向 unicode 控制字符”的正确方法是什么?

I have a package on NPM that shows that it contain "Bidirectional unicode control characters" reported by socket.dev.

I've found answer to this question How to update GitHub Actions CI to detect Trojan Code commits (malicious [bidirectional] unicode chars, python).

I've used:

git grep -oP "[^\x00-\x7F]*"

It found some matches in binary files, so I've removed all binary flags from .gitattributes and now I only have files from __tests__ directory that has ANSI files and one image, but those are not published to NPM.

What is a proper way to find those: "Bidirectional unicode control characters"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尛丟丟 2025-01-23 14:16:46

perl oneliner

perl -CSD -ne 'print "$ARGV: $_" if /\p{Bidi_Control}/' file1 file2 ...

将打印出给定 UTF-8 编码文件中包含双向控制字符的行。

不幸的是,git grep -P 使用的 PCRE 不支持 perl 正则表达式所支持的 unicode 属性级别。不过,您可以显式搜索控制字符:

nbsp;git clone https://github.com/jcubic/jquery.terminal.git
$ cd jquery.terminal
$ git grep -IP '(*UTF)[\x{061C}\x{200E}\x{200F}\x{202A}-\x{202E}\x{2066}-\x{2069}]'
js/jquery.terminal-src.js:        "‎": "<U+200E>",
js/jquery.terminal-src.js:        "‏": "<U+200F>",

-I 选项会跳过二进制文件。

(控制字符列表取自 perluniprops)。

The perl one liner

perl -CSD -ne 'print "$ARGV: $_" if /\p{Bidi_Control}/' file1 file2 ...

will print out lines of the given UTF-8 encoded files that have bidirectional control characters in them.

Unfortunately, PCRE, which git grep -P uses, doesn't support anywhere near the level of unicode properties that perl regular expressions do. You can search for the control characters explicitly, though:

$ git clone https://github.com/jcubic/jquery.terminal.git
$ cd jquery.terminal
$ git grep -IP '(*UTF)[\x{061C}\x{200E}\x{200F}\x{202A}-\x{202E}\x{2066}-\x{2069}]'
js/jquery.terminal-src.js:        "‎": "<U+200E>",
js/jquery.terminal-src.js:        "‏": "<U+200F>",

The -I option skips binary files.

(List of control characters taken from perluniprops).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文