Semgrep:简单的 HTML 匹配(包括 MWE)

问题描述 投票:0回答:0

我知道 HTML 在 Semgrep 中是实验性的,但我希望它能在非常简单的匹配上工作(Bash 也是实验性的,它已经足够好了)。

如何匹配具有相同 ID 的任意两个元素?

<!-- this should match since both have same IDs -->
<div id="a"></div>
<div id="a"></div>

<!-- this should match since both have same IDs, even though spacing is different (valid HTML) -->
<div id="b"></div>
<div id = "b"></div>

<!-- this should match since both have same IDs, even though different tags -->
<div id="c"></div>
<div>Something in between should still work</div>
<span id="c"></div>

<!-- self-closing tags should match too -->
<link id="d" rel="stylesheet" href="styles.css"> 
<span id="d"></span>

尝试

在操场上试试

rules:
  - id: dupe_id
    message: duplicate IDs not allowed
    languages:
      - html
    severity: ERROR
    patterns:
      - pattern-either:
        # Both not self-closing
        - pattern: <$TAGA id="$X"></$TAGA><$TAGB id="$X"></$TAGB>
        # First self-closing
        - pattern: <$TAGA id="$X"><$TAGB id="$X"></$TAGB>
        # Second self-closing
        - pattern: <$TAGB id="$X"></$TAGB><$TAGA id="$X">

错误

尝试只给出了一场比赛,但预期是 4.

其他图案

  • Just
    <$TAGA id="$X"></$TAGA>
    与预期的 ID 匹配任何标签
  • <$TAGA id="$X"></$TAGA><$TAGB id="$X"></$TAGB>
    什么都不匹配(意外,应该匹配任何具有两个连续相同 ID 的东西)
    • 即使
      ...
      用在
    • 之间
html static-analysis semgrep
© www.soinside.com 2019 - 2024. All rights reserved.