返回提示词目录第 1333 页

编写自定义静态安全扫描规则

根据危险代码模式生成扫描规则、正反测试用例及运行命令,说明匹配逻辑与检测局限。

完整提示词

<context>
Generic rulesets catch generic bugs. The highest-value static analysis rules are the custom ones that encode a team's own lessons: "never call `render_raw` with request data", "always go through `db.safe_query`", "this crypto helper is deprecated". They fail when the pattern is too literal (misses a renamed variable or a keyword argument), too broad (flags the safe helper itself), or has no tests, so it silently breaks on the next refactor. A rule earns its place in CI only with a clear message that tells the developer what to do instead, and a test file that proves both what it flags and what it leaves alone.
</context>

<task>
Write a Semgrep rule in {{language}} for this pattern:

<pattern_description>
{{pattern_description}}
</pattern_description>
{{#examples}}

<examples>
{{examples}}
</examples>
{{/examples}}

Rule style requested: {{rule_style}}.

1. If the description does not say what makes the code dangerous or what the safe alternative is, ask those two questions and stop.
2. Choose the approach. Use `mode: taint` with `pattern-sources`, `pattern-sinks` and `pattern-sanitizers` when the danger is untrusted data reaching a sink across assignments or function calls; use search mode with `patterns`, `pattern-either`, `pattern-not`, `pattern-inside` and `pattern-not-inside` when the danger is a code shape. Explain the choice in two sentences.
3. Write the rule in YAML: `id` (kebab-case, specific), `languages: [{{language}}]`, `severity` (ERROR, WARNING or INFO), a `message` that names the risk and the safe alternative in one or two sentences, `metadata` with `cwe`, `category: security`, `confidence` and `references` only if supplied, and the matching logic. Use metavariables (`$X`, `$...ARGS`) and the ellipsis operator so the rule survives renamed variables, extra arguments and keyword arguments. Narrow with `metavariable-regex` or `metavariable-pattern` where useful.
4. Add a `fix:` only when the rewrite is mechanical and always correct; otherwise put the fix in the message.
5. Write a test file in {{language}} with each case annotated on the line above it: `ruleid: <rule-id>` for code that must be flagged and `ok: <rule-id>` for code that must not, using the comment syntax of the language. Cover at least three true positives (including one variant shape such as an alias or a keyword argument) and three true negatives (the safe helper, a sanitised value, and the closest legitimate look-alike).
6. Trace every test case through the rule and state which match. Fix the rule until the trace agrees with the annotations.
</task>

<constraints>
- Match the language's real syntax; do not use pattern operators or keys that do not exist. If unsure whether an operator is supported for {{language}}, say so.
- Prefer fewer false positives over completeness for a rule that will block CI; if broad coverage is needed, propose a second WARNING-level rule.
- Do not paste secrets, internal URLs or customer data from the examples into the rule or tests; replace them with neutral names.
- Do not claim the rule has been run.
{{> output/uncertainty}}
</constraints>

<output_format>
## Approach
Search or taint, and why, in two sentences.

## Rule
One fenced `yaml` block.

## Test file
One fenced block in {{language}} with `ruleid:` and `ok:` annotations, followed by a table: Case | Expected | Matches when traced.

## How to run
The commands to run the tests (`semgrep --test` against the folder holding the rule and test file) and to scan the repository with the rule, plus where to add it in CI.

## Limits
What the rule will miss (cross-file flows, reflection, dynamic calls) and when to revisit it.
</output_format>

可替换内容

  • {{language}}
  • {{pattern_description}}
  • {{#examples}}
  • {{examples}}
  • {{/examples}}
  • {{rule_style}}
  • {{> output/uncertainty}}
  • [{{language}}]

分类与标签

编程开发、静态代码分析、安全规则、测试用例、持续集成

前往 AI 图片创作