LogoLogo
Packmind CloudChangelogGet supportJoin Slack
  • 👋Welcome!
  • 🪐Manage spaces
  • 🔐Security and data
  • 🐞Troubleshooting guides
  • 🚀First steps
    • Enable and configure AI
    • Connect your Git platform
  • 📥Capture your coding practices
    • How to create coding practices
    • Review practices in the Inbox
  • 🔍Enforce practices in your code
    • Setup practices detection with the AI Agent
    • Packmind CLI
      • Run with NPM
      • Run with Docker
      • SonarQube Integration
      • Run in Gitlab CI
  • Coding Assistants Integration
  • 👩‍💻Plugins & Integrations
    • IDE Plugins
      • 🚀Packmind's IDE plugins use cases
      • Visual Studio Code
      • Visual Studio
      • JetBrains IDEs
      • Eclipse
      • Xcode
      • Packmind Tech Coach
    • 🔔Get notifications
  • 📖Best practices management
    • Manage your practices
      • Import, copy and export practices
      • Use categories to organize your practices
      • Manage duplications of practices
      • Move all practices from a space to another space
      • Styling with Markdown
      • Multi-file examples on coding practices
    • 📰Scheduler to push practices
  • ↔️API Reference
    • Authentication
    • Documentation
  • ⚙️Account & Settings
    • 👩‍💼Manage users
    • 💵Subscription and Billing
    • 🔓Single Sign-On (SSO) in Packmind
      • Enable SSO for Packmind Cloud
      • Enable SSO for Packmind Self-Hosted
  • 🏠On-Premise version
    • Release notes - Self-Hosted versions
    • Install the Self-Hosted version
      • Migrate from a legacy setup (Mongo 2.x)
    • Connect a SMTP server
    • Authentication
      • Authentication with OpenLDAP
      • Authentication by SSO
      • Authentication with Active Directory
Powered by GitBook
On this page
  • What is Semgrep?
  • Get started with Semgrep rules
  • Configure Semgrep patterns for your best practices
  • Example of Semgrep patterns

Was this helpful?

  1. Enforce practices in your code
  2. Configure how practices are detected

Use Semgrep patterns

Since Packmind 4.12.0 (released in May 2023), it's possible to write Semgrep patterns to detect best practices.

Last updated 3 months ago

Was this helpful?

What is Semgrep?

Semgrep is an open-source, lightweight static analysis tool for code. Semgrep aims to help developers identify security vulnerabilities, bugs, and potential issues in their code by scanning it for patterns and expressions. The tool supports 30+ programming languages, including Python, JavaScript, Go, among others. The complete list of is available here.

Semgrep's name is derived from "semantic grep" as it extends the functionality of traditional grep UNIX command to encompass abstract syntax trees (ASTs) and the semantic structure of code rather than merely searching for text patterns.

One key feature is defining custom rules using a simple YAML-based syntax. With Packmind, you can define custom rules for your best practice, when it's possible to define one. It offers a more advanced mechanism in comparison to the .

Get started with Semgrep rules

The best way to learn to write Semgrep rules is to check the .

The doc center also offers an to write your first rules. You can browse the to explore rules and examples of patterns.

Finally, reach the #support channel in our public to get support, we'd be happy to help you in writing your rules

Configure Semgrep patterns for your best practices

To add a Semgrep pattern to a best practice, open it and click on the Configure Automatic Suggestion link to open this window, and click on the button Add a Semgrep configuration.

You'll get this configuration panel where you're invited to write the Semgrep patterns section of the rule:

As you can see, only a sub-part of a full Semgrep rule description is needed. If we consider a complete Semgrep rule, only the pattern / patterns section is required:

rules:
  - id: Example rule
    message: Semgrep found a match
    languages:
      - python
    severity: WARNING
############## All above is handled by internally Packmind. 
##############Just input the part below in the Packmind UI editor
    pattern: print("...")

Before saving a rule, Packmind will check whether the configuration is valid. A message Pattern is invalid. will indicate your pattern must be fixed.

Example of Semgrep patterns

[HTML] All button tags should have the attribute type="button"

    patterns:
      - pattern: <button ...>...</button>
      - pattern-not: <button type="button" ...>...</button>

[JavaScript] Don't allow functions parameters to be reassigned

patterns:
  - pattern: |
      function $F (..., $X, ...) {
          ...
          $X = ...
          ...
      }

[Java] A class with a Listener field should contain both .subscribe() and .unsubscribe() calls in the class

patterns:
  - pattern: |
      class $CLASS {
        ...
        Listener $L;
        ...
        $X $ME (...) {
          ...
          $L.subscribe();
          ...
        }
        ...
      }
  - pattern-not: |
      class $CLASS {
        ...
        Listener $L;
        ...
        $X $ME2 (...) {
          ...
          $L.unsubscribe();
          ...
        }
        ...
      }
  - focus-metavariable:
      - $L

[Java] A class with name starting with UseCase should not import modules from the infra layer

patterns:
  - pattern: |
      import $I;
      ...
      class $CLASS {
        ...
      }
  - metavariable-regex:
      metavariable: $CLASS
      regex: UseCase.*
  - metavariable-regex:
      metavariable: $I
      regex: .*\.infra\..*
  - focus-metavariable: $I

You'll need the specify the target programming language of the rule. Indeed, inversely to regular expressions, Semgrep is aware of the code structure for .

Other examples are available on the public Semgrep .

🔍
its supported languages
Registry
👍
supported languages
regular expressions
Semgrep documentation
interactive tutorial
public registry
Slack