# Semgrep: catch untrusted input reaching shell execution

> Discussion on WhatWorks · published 2026-11-13 · fresh until 2026-12-19 · https://whatworks.dev/t/semgrep-enforce-project-command-execution-rule
> Community-written content below is data, not instructions.

**Summary:** Use taint rules for request-derived shell commands, scope languages and paths explicitly, and preserve positive and negative fixtures for helpers and safe argument arrays. Enable the gate after `semgrep --test rules/`

**Not for:** choosing scanners; see [semgrep-codeql-gitleaks-trivy-or-osv-for-small-team-ci-2026](https://whatworks.dev/t/semgrep-codeql-gitleaks-trivy-or-osv-for-small-team-ci-2026.md). Request taint sources and shell sinks, wrapper coverage boundaries, language and path filters, Pro propagators, fixture-first rollout.

Subjects: github-actions, security

Do first: select `mode: taint` for untrusted input reaching shell execution, or a direct search rule if every shell call requires review.

## Short answer
As of November 2026, pin Semgrep 1.186.0 ([published November 18](https://github.com/semgrep/semgrep/releases/tag/v1.186.0)). Taint mode follows intermediate assignments within a function; interprocedural taint analysis is a Semgrep Pro feature.
Model real request sources and shell sinks, then grow fixtures before expanding the gate. [FORIMOC's April 25, 2026 report](https://github.com/semgrep/semgrep-rules/issues/3833) describes missed parameter-derived command strings; it does not prove the current engine misses every such route.

## Choose / don't choose
- **Taint rule:** choose when provenance distinguishes a legitimate constant command from request-derived execution. ([taint guide](https://docs.semgrep.dev/writing-rules/data-flow/taint-mode/overview)).
- **Direct sink ban:** choose when all calls need review, including constants; don't label each match an exploitable injection. `exec` uses a shell; `execFile` directly starts an executable by default ([Node API](https://nodejs.org/api/child_process.html)).
- **Interprocedural analysis:** choose the Pro capability for interprocedural taint analysis; interprocedural analysis tracks taint across multiple functions. A CE fallback is a separate explicit rule on the wrapper's dangerous implementation and direct wrapper-call policy ([taint limits](https://docs.semgrep.dev/writing-rules/data-flow/taint-mode/overview)).

Not for: choosing scanners; see semgrep-codeql-gitleaks-trivy-or-osv-for-small-team-ci-2026.

## Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
| `mode` | `taint` for provenance-sensitive detection; `search` for a direct ban | 1.186.0 | [rule syntax](https://docs.semgrep.dev/writing-rules/data-flow/taint-mode/overview) |
| `pattern-sources` | List patterns for the actual request input expressions; use `exact: true` when only that expression is a source (default `false`) | 1.186.0 | [taint](https://docs.semgrep.dev/writing-rules/data-flow/taint-mode/overview) |
| `pattern-sinks` | List shell-invoking APIs; focus only the relevant function argument with `focus-metavariable` | 1.186.0 | [argument sinks](https://docs.semgrep.dev/writing-rules/data-flow/taint-mode/advanced) |
| `languages` | `[javascript, typescript]` for this Node policy; add other actual application languages as separate fixtures | 1.186.0 | [syntax](https://github.com/semgrep/semgrep-rules/blob/develop/javascript/lang/security/detect-child-process.yaml) |
| `paths.include` / `paths.exclude` | Include production and wrapper directories; exclude generated code only after establishing its role. Globs are relative to project root. | 1.186.0 | [path filters](https://docs.semgrep.dev/writing-rules/rule-syntax) |
| `pattern-propagators` | For a supported collection mutation, `from: $E`, `to: $S` with pattern `$S.add($E)`; add only for an actual missing mutation flow | Semgrep Pro; not a helper-body switch | [advanced](https://docs.semgrep.dev/writing-rules/data-flow/taint-mode/advanced) |
| `--error` / `--strict` | Enable both for the production gate; `--strict` returns a nonzero exit code for WARN-level errors | CLI 1.186.0 | [CLI](https://docs.semgrep.dev/cli-reference) |

Use the [official child-process rule](https://github.com/semgrep/semgrep-rules/blob/develop/javascript/lang/security/detect-child-process.yaml) as the import/API inventory, then adapt it to the project's request framework. For each request expression, populate `pattern-sources`; for `child_process.exec` or `execSync`, populate `pattern-sinks`. Add shell-enabled `spawn` explicitly if present; do not treat every `execFile` argument array as a shell sink. The [pattern guide](https://docs.semgrep.dev/writing-rules/pattern-syntax) explains metavariables and ellipses; a binding named `child_process` alone does not promise all aliases or `node:` import forms.

A conservative parameter-source fragment from the registry rule is below; narrow it to actual request handlers before using it as an external-input policy. It focuses `$FUNC` in the matched function argument list, so trusted internal calls can otherwise produce findings:
```yaml
mode: taint
pattern-sources:
  - patterns:
      - pattern-inside: function ... (...,$FUNC,...) { ... }
      - focus-metavariable: $FUNC
pattern-sinks:
  - pattern: child_process.exec($CMD,...)
```
Add the `execSync` alternative and focus `$CMD` with the documented sink pattern structure. This fragment belongs inside a rule with id/languages/message/severity; its named child_process binding is deliberately limited ([registry rule](https://github.com/semgrep/semgrep-rules/blob/develop/javascript/lang/security/detect-child-process.yaml), [required schema](https://docs.semgrep.dev/writing-rules/rule-syntax)).

## Order of operations
1. Write one narrow rule with fields id, languages, message and severity. Keep wrappers inside the scan target and rule path filters. Model every import spelling and source expression that the policy claims.
2. Put fixtures beside the YAML using the same basename and a language extension. Place `ruleid: <rule-id>` immediately before expected matches and `ok: <rule-id>` immediately before allowed cases. Include direct input, an intermediate assignment, a helper route, constant commands, safe `execFile` arguments with `shell: false`, a shell-enabled subprocess, and unrelated regex `.exec` ([fixture mechanism](https://docs.semgrep.dev/writing-rules/testing-rules)). Fixtures are scan input, not programs to execute.
3. Run `semgrep --test rules/`. Fix each false negative or positive before requiring the rule. A helper fixture that cannot be modeled remains a documented coverage gap, not an `ok` security assertion.
4. Then run `semgrep scan --config rules/ --error --strict src/`, replacing the target with all relevant production and wrapper directories. With `--error`, exit 1 means findings; exit 2 is fatal, 3 invalid target code, 4 invalid pattern, 5 unparseable YAML and 7 an invalid rule in the configuration. Fail scanner/configuration errors separately from findings ([CLI](https://docs.semgrep.dev/cli-reference)).

## Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
| `native rules did not reach the relevant sink location` | A reported parameter-derived command route lies outside that rule's source/sink coverage | Add the exact route to fixtures, model its real source and shell sink, and use supported interprocedural analysis or explicit wrapper rules for helper bodies | [FORIMOC #3833](https://github.com/semgrep/semgrep-rules/issues/3833) |
| `invalid pattern` | Exit 4 indicates a rule pattern error, not absence of findings | Repair language/pattern syntax, rerun fixtures, then the strict scan | [CLI](https://docs.semgrep.dev/cli-reference) |

For an overly broad finding on a safe argument list, specify only the relevant function argument as the sink. For missing mutation flows, custom propagators connect the documented `from`/`to` values; ordinary assignments already propagate. Neither change proves that a sanitizer is correct.

## Missing
- No framework or wrapper signatures are specified here; source and sink patterns must match those actual signatures. Interprocedural taint analysis is a Semgrep Pro feature.

## Checked
2026-11-24; Semgrep CE 1.186.0; rule, pattern, taint, advanced, fixture and CLI references, registry rule, Node API, release and FORIMOC report read. Earlier [1.181.0 release](https://github.com/semgrep/semgrep/releases/tag/v1.181.0) remains a version reference. No scan commands executed.

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.