Skip to main content

libsast

Generic SAST for Security Engineers. Powered by tree-sitter AST pattern matcher, regex based pattern matcher, and semantic aware semgrep.

Made with Love in India Tweet

PyPI version platform License python Build

Support libsast

  • Donate via Paypal: Donate via Paypal
  • Sponsor the Project: Github Sponsors

Install

pip install libsast
# Optional Tree-sitter language grammar extras:
pip install "libsast[treesitter]"  # 165+ languages via tree-sitter-language-pack
pip install "libsast[mobile]"      # JS, TS/TSX, Java, Kotlin, Swift, ObjC, C#
pip install "libsast[js]"          # JavaScript and TypeScript/TSX
# Or install any individual tree-sitter-<lang> package alongside libsast:
pip install libsast tree-sitter-python
# Optional Semgrep support (macOS/Linux):
pip install semgrep==1.172.0

Pattern Matcher and Tree-sitter Matcher are cross-platform (Windows, macOS, and Linux). Semgrep supports macOS and Linux.

Command Line Options

$ libsast
usage: libsast [-h] [-o OUTPUT] [-p PATTERN_FILE] [-t TS_PATTERN_FILE]
               [-s SGREP_PATTERN_FILE]
               [--ts-file-extensions TS_FILE_EXTENSIONS [TS_FILE_EXTENSIONS ...]]
               [--sgrep-file-extensions SGREP_FILE_EXTENSIONS [SGREP_FILE_EXTENSIONS ...]]
               [--file-extensions FILE_EXTENSIONS [FILE_EXTENSIONS ...]]
               [--ignore-filenames IGNORE_FILENAMES [IGNORE_FILENAMES ...]]
               [--ignore-extensions IGNORE_EXTENSIONS [IGNORE_EXTENSIONS ...]]
               [--ignore-paths IGNORE_PATHS [IGNORE_PATHS ...]]
               [--show-progress] [--cpu-core CPU_CORE] [-v]
               [path ...]

positional arguments:
  path                  Path can be file(s) or directories

options:
  -h, --help            show this help message and exit
  -o OUTPUT, --output OUTPUT
                        Output filename to save JSON report.
  -p PATTERN_FILE, --pattern-file PATTERN_FILE
                        YAML pattern file, directory or url
  -t TS_PATTERN_FILE, --ts-pattern-file TS_PATTERN_FILE
                        Tree-sitter YAML pattern file, directory or url
  -s SGREP_PATTERN_FILE, --sgrep-pattern-file SGREP_PATTERN_FILE
                        sgrep rules directory
  --ts-file-extensions TS_FILE_EXTENSIONS [TS_FILE_EXTENSIONS ...]
                        File extensions that should be scanned with
                        tree-sitter matcher
  --sgrep-file-extensions SGREP_FILE_EXTENSIONS [SGREP_FILE_EXTENSIONS ...]
                        File extensions that should be scanned with semantic
                        grep
  --file-extensions FILE_EXTENSIONS [FILE_EXTENSIONS ...]
                        File extensions that should be scanned with pattern
                        matcher
  --ignore-filenames IGNORE_FILENAMES [IGNORE_FILENAMES ...]
                        File name(s) to ignore
  --ignore-extensions IGNORE_EXTENSIONS [IGNORE_EXTENSIONS ...]
                        File extension(s) to ignore in lower case
  --ignore-paths IGNORE_PATHS [IGNORE_PATHS ...]
                        Path(s) to ignore
  --show-progress       Show scan progress
  --cpu-core CPU_CORE   No of CPU cores to use. Use all cores by default
  -v, --version         Show libsast version

Example Usage

$ libsast -s tests/assets/rules/semantic_grep/ -p tests/assets/rules/pattern_matcher/ tests/assets/files/
{
  "pattern_matcher": {
    "test_regex": {
      "files": [
        {
          "file_path": "tests/assets/files/test_matcher.test",
          "match_lines": [
            28,
            28
          ],
          "match_position": [
            1141,
            1149
          ],
          "match_string": ".close()"
        }
      ],
      "metadata": {}
    },
    "test_regex_and": {
      "files": [
        {
          "file_path": "tests/assets/files/test_matcher.test",
          "match_lines": [
            3,
            3
          ],
          "match_position": [
            52,
            66
          ],
          "match_string": "webkit.WebView"
        },
        {
          "file_path": "tests/assets/files/test_matcher.test",
          "match_lines": [
            7,
            7
          ],
          "match_position": [
            194,
            254
          ],
          "match_string": ".loadUrl(\"file:/\" + Environment.getExternalStorageDirectory("
        }
      ],
      "metadata": {}
    },
    "test_regex_and_not": {
      "files": [
        {
          "file_path": "tests/assets/files/test_matcher.test",
          "match_lines": [
            42,
            42
          ],
          "match_position": [
            1415,
            1424
          ],
          "match_string": "WKWebView"
        },
        {
          "file_path": "tests/assets/files/test_matcher.test",
          "match_lines": [
            40,
            40
          ],
          "match_position": [
            1363,
            1372
          ],
          "match_string": "WKWebView"
        }
      ],
      "metadata": {}
    },
    "test_regex_and_or": {
      "files": [
        {
          "file_path": "tests/assets/files/test_matcher.test",
          "match_lines": [
            50,
            50
          ],
          "match_position": [
            1551,
            1571
          ],
          "match_string": "telephony.SmsManager"
        },
        {
          "file_path": "tests/assets/files/test_matcher.test",
          "match_lines": [
            58,
            58
          ],
          "match_position": [
            1973,
            1988
          ],
          "match_string": "sendTextMessage"
        }
      ],
      "metadata": {}
    },
    "test_regex_multiline_and_metadata": {
      "files": [
        {
          "file_path": "tests/assets/files/test_matcher.test",
          "match_lines": [
            52,
            52
          ],
          "match_position": [
            1586,
            1684
          ],
          "match_string": "public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {"
        },
        {
          "file_path": "tests/assets/files/test_matcher.test",
          "match_lines": [
            10,
            11
          ],
          "match_position": [
            297,
            368
          ],
          "match_string": "public static ForgeAccount add(Context context, ForgeAccount account) {"
        }
      ],
      "metadata": {
        "cwe": "CWE-1051 Initialization with Hard-Coded Network Resource Configuration Data",
        "description": "This is a rule to test regex",
        "foo": "bar",
        "masvs": "MSTG-STORAGE-3",
        "owasp-mobile": "M1: Improper Platform Usage",
        "owasp-web": "A10: Insufficient Logging & Monitoring",
        "severity": "info"
      }
    },
    "test_regex_or": {
      "files": [
        {
          "file_path": "tests/assets/files/test_matcher.test",
          "match_lines": [
            26,
            26
          ],
          "match_position": [
            1040,
            1067
          ],
          "match_string": "Context.MODE_WORLD_READABLE"
        }
      ],
      "metadata": {}
    }
  },
  "semantic_grep": {
    "errors": [
      {
        "code": 3,
        "level": "warn",
        "message": "Semgrep Core WARN - Lexical error in file tests/assets/files/test_matcher.test:40\n\tunrecognized symbols: !",
        "path": "tests/assets/files/test_matcher.test",
        "type": "Lexical error"
      },
    ],
    "matches": {
      "boto-client-ip": {
        "files": [
          {
            "file_path": "tests/assets/files/example_file.py",
            "match_lines": [
              4,
              4
            ],
            "match_position": [
              24,
              31
            ],
            "match_string": "c = boto3.client(host='8.8.8.8')"
          }
        ],
        "metadata": {
          "cwe": "CWE-1050 Excessive Platform Resource Consumption within a Loop",
          "description": "boto client using IP address",
          "owasp-web": "A8: Insecure Deserialization",
          "severity": "ERROR"
        }
      }
    }
  }
}

Python API

>>> from libsast import Scanner
>>> options = {'match_rules': '/Users/ajinabraham/Code/njsscan/njsscan/rules/pattern_matcher', 'sgrep_rules': '/Users/ajinabraham/Code/njsscan/njsscan/rules/semantic_grep', 'sgrep_extensions': {'', '.js'}, 'match_extensions': {'.hbs', '.sh', '.ejs', '.toml', '.mustache', '.tmpl', '.jade', '.json', '.ect', '.vue', '.yml', '.hdbs', '.tl', '.html', '.haml', '.dust', '.pug', '.tpl'}, 'ignore_filenames': {'bootstrap.min.js', '.DS_Store', 'bootstrap-tour.js', 'd3.min.js', 'tinymce.js', 'codemirror.js', 'tinymce.min.js', 'react-dom.production.min.js', 'react.js', 'jquery.min.js', 'react.production.min.js', 'codemirror-compressed.js', 'axios.min.js', 'angular.min.js', 'raphael-min.js', 'vue.min.js'}, 'ignore_paths': {'__MACOSX', 'jquery', 'fixtures', 'node_modules', 'bower_components', 'example', 'spec'}, 'show_progress': False}
>>> paths = ['../njsscan/tests/assets/dot_njsscan/']
>>> scanner = Scanner(options, paths)
>>> scanner.scan()

Write your own Static Analysis tool

With libsast, you can write your own static analysis tools. libsast provides three matching engines:

  1. Pattern Matcher (Regex)
  2. Tree-sitter Matcher (AST)
  3. Semantic Grep (Semgrep)

Pattern Matcher

Pattern Matcher supports any language.

Use Regex 101 to write simple Python Regex rule patterns.

A sample rule looks like

- id: test_regex_or
  message: This is a rule to test regex_or
  input_case: exact
  pattern:
  - MODE_WORLD_READABLE|Context\.MODE_WORLD_READABLE
  - openFileOutput\(\s*".+"\s*,\s*1\s*\)
  severity: error
  type: RegexOr
  metadata:
    owasp-web: a1
    reference: http://foo.bar
    foo: Some extra metadata

A rule consists of

  • id : A unique id for the rule.
  • message: A description for the rule.
  • input_case: It can be exact, upper or lower. Data will be converted to lower case/upper case/as it is before comparing with the regex.
  • pattern: List of patterns depends on type.
  • severity: It can be error, warning or info.
  • type: Pattern Matcher supports Regex, RegexAnd, RegexOr, RegexAndOr, RegexAndNot (as well as TreeSitter, TreeSitterAnd, TreeSitterOr, TreeSitterAndOr, TreeSitterAndNot).
  • metadata (optional): Define your own custom fields that you can use as metadata along with standard mappings.
1. Regex - if regex1 in input
2. RegexAnd - if regex1 in input and regex2 in input
3. RegexOr - if regex1 in input or regex2 in input
4. RegexAndOr -  if regex1 in input and (regex2 in input or regex3 in input)
5. RegexAndNot - if regex1 in input and not regex2 in input

Example: Pattern Matcher Rule

Test your pattern matcher rules

$ libsast -p tests/assets/rules/pattern_matcher/patterns.yaml tests/assets/files/

Tree-sitter Matcher

Tree-sitter Matcher provides language-agnostic, syntax-aware AST pattern matching across any language supported by Tree-sitter (JavaScript, TypeScript/TSX, HTML/Vue <script>, Java, Kotlin, Swift, Objective-C, Dart, C#, Python, Go, Rust, C/C++, etc.).

Tree-sitter rules can be placed inside --pattern-file (match_rules) alongside Regex rules or passed separately via -t / ts_rules:

- id: rn_webview_universal_file_access
  type: TreeSitter
  languages: [javascript, tsx]
  message: WebView allows universal access from file URLs
  severity: error
  pattern: |
    (jsx_opening_element
      name: (_) @_tag (#match? @_tag "WebView$")
      attribute: (jsx_attribute
        (property_identifier) @_prop (#eq? @_prop "allowUniversalAccessFromFileURLs")
        (_ (true)))) @match
  exclude_ancestor: |
    (if_statement condition: (_) @_c (#match? @_c "__DEV__"))
  metadata:
    cwe: cwe-200
    owasp-mobile: m1
    masvs: platform-2

Supported rule keys:

  • type: TreeSitter, TreeSitterAnd, TreeSitterOr, TreeSitterAndOr, TreeSitterAndNot.
  • pattern: Tree-sitter S-expression query (or list of queries) capturing @match.
  • languages (optional): List of Tree-sitter language names the rule applies to.
  • exclude_ancestor (optional): S-expression query; suppresses matches nested inside a matching ancestor node (e.g., if (__DEV__)).
  • within_ancestor (optional): S-expression query; requires @match to be nested inside a matching ancestor node.
  • not_within_node (optional): S-expression query; suppresses @match if the matched node itself contains a sub-node matching this query (e.g., missing-property checks).

Inbuilt Standard Mapping Support

Metadata fields also support libsast standard mapping.

For example, the metadata field owasp-web: a1 will get expanded at runtime as owasp-web: 'A1: Injection'.

Currently Supports

Semantic Grep

Semantic Grep uses semgrep, a fast and syntax-aware semantic code pattern search for many languages: like grep but for code.

Currently it supports Python, Java, JavaScript, Go and C.

Use semgrep.dev to write semantic grep rule patterns.

A sample rule for Python code looks like

rules:
  - id: boto-client-ip
    patterns:
      - pattern-inside: boto3.client(host="...")
      - pattern-regex: '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
    message: "boto client using IP address"
    languages: [python]
    severity: ERROR
    metadata:
      owasp-web: a2
      owasp-mobile: m7
      cwe: cwe-1048
      foo: Some extra metadata

See semgrep documentation here.

Example: Semantic Grep Rule

Test your semgrep rules

$ libsast -s tests/assets/rules/semantic_grep/sgrep.yaml tests/assets/files/

Realworld Implementations

  • njsscan SAST is built with libsast pattern matcher and semantic grep.
  • nodejsscan nodejsscan is a static security code scanner for Node.js applications.
  • MobSF Static Code Analyzer for Android and iOS mobile applications.
  • mobsfscan mobsfscan is a static security code scanner for Mobile applications built for Android (Java, Kotlin) & iOS (Swift, Objective C).

Metadata

Release files for libsast 3.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for libsast 3.2.0
File Size Uploaded Provenance Actions
libsast-3.2.0.tar.gz 43.9 kB With provenance Details for libsast-3.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for libsast 3.2.0
File Interpreter ABI Platform Actions
libsast-3.2.0-py3-none-any.whl Python 3 none any Details for libsast-3.2.0-py3-none-any.whl

Total release size: 91.5 kB

Release files / libsast-3.2.0.tar.gz

Download URL libsast-3.2.0.tar.gz
Size 43.9 kB
Tags Source
SHA-256 checksum
How to use checksums
3f82789e7e7f5a7f167e9bfc95de4e49d37dc6aed6de4b6e40270553a3f2aecc
BLAKE2b-256 checksum
How to use checksums
e658074ac1394d60e78024c7e82de0cb9ea9f9a917d0e8d5adfe00cb69760502
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Release files / libsast-3.2.0-py3-none-any.whl

Download URL libsast-3.2.0-py3-none-any.whl
Size 47.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a3a72352f613cbd2b383e0d4f838a4ef4f063096548e40ae020783c22b11d2ad
BLAKE2b-256 checksum
How to use checksums
4ac4e8e5ecb04fc67b47f1d439f8e04c84afbb5ef049cb755a59a0fa8b6828ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Security for libsast 3.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for libsast-3.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

3.2.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page