有什么好的工具可以分析代码中不安全的代码片段?

发布于 2024-10-19 16:24:25 字数 1539 浏览 0 评论 0原文

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

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

发布评论

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

评论(3

冰火雁神 2024-10-26 16:24:25

这不是基本测试,而是需要对令牌流进行重要的分析。

您可以使用

phpmd

扫描 PHP 源代码并查找潜在问题,例如可能的错误、死代码、次优代​​码和过于复杂的表达式

phpcs

phpcs 对 PHP、JavaScript 和 CSS 文件进行标记,并检测是否违反一组已定义的编码标准。它是一个重要的开发工具,可确保您的代码保持干净和一致。它还可以帮助防止开发人员犯的一些常见语义错误。

或者 - 如果这还不够 - 使用

bytekit-cli

查看字节码级别

bytekit-cli 提供了一个命令行工具,它利用 Bytekit 扩展在 PHP 字节码级别执行常见的代码分析任务。

您必须为此编写自己的嗅探。

更多工具和资源:

This is not basic testing, but requires non-trivial analysis of the token stream.

You can either use

phpmd

scans PHP source code and looks for potential problems such as possible bugs, dead code, suboptimal code, and overcomplicated expressions

phpcs

phpcs tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards. It is an essential development tool that ensures your code remains clean and consistent. It can also help prevent some common semantic errors made by developers.

or - if that doesn't suffice - look at the bytecode level with

bytekit-cli

bytekit-cli provides a command-line tool that leverages the Bytekit extension to perform common code analysis tasks on the PHP bytecode level.

You will have to write you own sniffs for that.

Further tools and resources:

数理化全能战士 2024-10-26 16:24:25

一个好的 IDE 应该指出开发过程中的基本错误。

例如,我使用 Netbeans,它突出显示了常见的代码错误,例如定义但未使用的变量,或者在 if() 条件中误用赋值运算符,其中等式运算符更为正常(即编写 if ($x = $y) 而不是 if($x == $y))。

类似的基本内容会显示在 Netbeans 中,并在行号旁边显示一个黄色警告三角形。其他 IDE 也会有类似的功能。

我不认为它会识别出您所描述的特定错误条件,但它肯定会识别出相当数量的错误,即使对于您谈到的错误,这也是我期望标记此类事情的地方,而不是在单独的工具中。

A good IDE should point out basic errors as part of the development process.

I use Netbeans, for example, and it highlights common code mistakes such as variables which are defined but not used, or mis-use of assignment operators in an if() condition where an equality operator is more normal (ie writing if($x = $y) instead of if($x == $y)).

Basic stuff like that shows up in Netbeans with a yellow warning triangle by the line number. Other IDEs will have similar features.

I don't think it picks up the specific error conditions you described, but it certainly picks up a fair number of errors, and even for the errors you talked about, this is where I would expect those kind of things to be flagged up, rather than in a separate tool.

长途伴 2024-10-26 16:24:25

您想要的传统上称为静态分析工具。这些工具经常做什么,
确定代码中的每个点,它了解变量的哪些事实
(在 X= NULL 之后,工具知道 X 为 NULL),然后沿着各种控制流路径传播它所知道的信息,以查看变量的状态是否与操作不一致(例如,在发现 X 为空之后,查找代码必须执行尝试以数组形式访问 X 的操作)。

为了做好这一点,您需要一个完整的 PHP 解析器,生成 AST、至少告诉您 PHP 变量范围的符号表、确定控制和数据流的某种方法,以及检测各种类型的信息集合的一堆模式的编码错误。

PHP 的此类工具之一是 PHPSat。它似乎做了其中的一些工作,您可能可以下载并运行它(我对此没有具体的经验)。它所基于的技术 Stratego 至少适合这项任务; Stratego 产生 AST,并且可以从其中的各个地方收集事实,尽管我不认为它在控制和数据流方面非常擅长。这与仅可以访问 PHP 令牌(例如另一个答案中提到的 PHPCS)的工具形成对比;仅通过令牌来计算控制和数据流是一场噩梦,实际上根本无法完成。

正确的机制似乎隐藏在 Paul Biggar 的论文中。不过,我似乎找不到任何迹象表明有人选择了它并将其用作静态分析器的基础。

What you want is traditionally called a static analysis tool. What such tools often do,
is determine for each point in the code, what kind of facts it knows about the variables
(after X= NULL, the tool knows X is NULL), and then propagates what it knows along various control flow paths to see if that state of the variables are inconsistent with an operation (e.g., after find that X is null, finding code that must be executed which attempts to access X as an array).

To do this well, you need a complete PHP parser, producing ASTs, symbol tables telling you at least the scopes of PHP variables, some way to determine control and data flow, and bunch of patterns over this collective set of information that detects various kinds of coding errors.

One such tool for PHP is PHPSat. It appears to do some of this and you can likely download an run it (I have no specific experience with it). The technology on which it is built, Stratego, is at least appropriate for the task; Stratego produces ASTs and can collect facts from various places in it, although I don't think it is very good at control and data flow. This is in contrast to a tool that simply has access to PHP tokens such as PHPCS mentioned in another answer; computing control and data flow from just the tokens is such a nightmare that in practice it won't get done at all.

The right machinery seems to be hiding in Paul Biggar's thesis. I can't seem to find any hints that anybody picked this up and used it as the basis for a static analyzer, though.

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