Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to Specify Code Assumptions #10169

Open
andrew-konstantinov opened this issue Apr 26, 2024 · 1 comment
Open

Ability to Specify Code Assumptions #10169

andrew-konstantinov opened this issue Apr 26, 2024 · 1 comment
Labels

Comments

@andrew-konstantinov
Copy link

Is your feature request related to a problem? Please describe.
Production code often depends on runtime feature flags, external services and so on. As a result many code branches are essentially unreachable under normal conditions, however AFAIK currently there is no way to express this in Semgrep, which leads to a high number of false positives. A very simple example could be:

fun test1() {
    val id = source()

    val shouldDoSanitization = isSanitizationEnabled(id)
    if (shouldDoSanitization) {
        id = sanitize(id)
    } else {
        // ok: sanitization-behind-a-feature-flag
        sink(id)
    }
}

In this case I'd like to express that isSanitizationEnabled is going to return true, but I couldn't find a way to do this in Semgrep documentation.

Describe the solution you'd like
Introduce a mechanism to specify code assumptions, such as:

  • return values (in the example above express that isSanitizationEnabled always returns true here)
  • variable values (in the example above express that shouldDoSanitization becomes true here)
  • control flow: assume a certain branch is always taken or always unreachable

Describe alternatives you've considered
Multiple sanitization rules could be defined as a workaround, in order to account for various code patterns, but these are dependent on heuristics and therefore less precise, also currently this approach does not work in Semgrep due to another bug (#10167 (comment)).

Use case

  1. Reduce amount of False Positives by making Semgrep understand sanitization patterns in production code
  2. Reduce amount of False Negatives by streamlining and simplifying Semgrep rules due to relying more on Taint Mode & Cross-file Analysis instead of heuristics
@ievans ievans added the enhancement New feature or request label Apr 30, 2024
@navhits
Copy link

navhits commented May 13, 2024

This is nice to have

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

5 participants