lsp-semgrep.el (7693B)
1 ;;; lsp-semgrep.el --- semgrep support -*- lexical-binding: t; -*- 2 ;; 3 ;; Copyright (C) 2023 Austin Theriault 4 ;; 5 ;; Author: Austin Theriault <austin@cutedogs.org> 6 ;; Keywords: language tools sast 7 ;; 8 ;; This program is free software; you can redistribute it and/or modify 9 ;; it under the terms of the GNU General Public License as published by 10 ;; the Free Software Foundation, either version 3 of the License, or 11 ;; (at your option) any later version. 12 ;; 13 ;; This program is distributed in the hope that it will be useful, 14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 ;; GNU General Public License for more details. 17 ;; 18 ;; You should have received a copy of the GNU General Public License 19 ;; along with this program. If not, see <https://www.gnu.org/licenses/>. 20 ;; This file is not part of GNU Emacs. 21 ;; 22 ;;; Commentary: 23 ;; 24 ;; Semgrep support for lsp-mode 25 ;; 26 ;;; Code: 27 28 29 30 (require 'lsp-mode) 31 32 (defgroup lsp-semgrep nil 33 "LSP support for Semgrep." 34 :group 'lsp-mode 35 :link `(url-link "https://github.com/returntocorp/semgrep")) 36 37 (defgroup lsp-semgrep-scan nil 38 "Semgrep LS scan options." 39 :group 'lsp-semgrep 40 ) 41 42 (defgroup lsp-semgrep-metrics nil 43 "Semgrep LS metrics options." 44 :group 'lsp-semgrep) 45 46 ;; General options 47 48 (defcustom lsp-semgrep-trace-server "off" 49 "Trace Semgrep LS server" 50 :group 'lsp-semgrep 51 :type '(choice (const "off") 52 (const "messages") 53 (const "verbose"))) 54 55 (defcustom lsp-semgrep-server-command '("semgrep" "lsp") 56 "Semgrep LS server command." 57 :group 'lsp-semgrep 58 :type '(repeat string)) 59 60 (defcustom lsp-semgrep-languages '("apex" 61 "bash" 62 "sh" 63 "c" 64 "cairo" 65 "clojure" 66 "cpp" 67 "c++" 68 "csharp" 69 "c#" 70 "dart" 71 "dockerfile" 72 "docker" 73 "ex" 74 "elixir" 75 "go" 76 "golang" 77 "hack" 78 "html" 79 "java" 80 "js" 81 "javascript" 82 "json" 83 "jsonnet" 84 "julia" 85 "kt" 86 "kotlin" 87 "lisp" 88 "lua" 89 "ocaml" 90 "php" 91 "python2" 92 "python3" 93 "py" 94 "python" 95 "r" 96 "regex" 97 "none" 98 "ruby" 99 "rust" 100 "scala" 101 "scheme" 102 "solidity" 103 "sol" 104 "swift" 105 "tf" 106 "hcl" 107 "terraform" 108 "ts" 109 "typescript" 110 "vue" 111 "xml" 112 "yaml") 113 "List of languages to enable Semgrep LS for." 114 :group 'lsp-semgrep 115 :type '(repeat string)) 116 ;; Scan options 117 118 (defcustom lsp-semgrep-scan-configuration [] 119 "Semgrep rule files, or registry rules to scan with, e.g. ['r/all','rules.yaml']." 120 :group 'lsp-semgrep-scan 121 :type '(repeat string)) 122 123 (defcustom lsp-semgrep-scan-exclude [] 124 "List of files or directories to exclude from scan." 125 :group 'lsp-semgrep-scan 126 :type '(repeat string)) 127 128 (defcustom lsp-semgrep-scan-include [] 129 "List of files or directories to include in scan." 130 :group 'lsp-semgrep-scan 131 :type '(repeat string)) 132 133 (defcustom lsp-semgrep-scan-jobs 1 134 "Number of parallel jobs to run." 135 :group 'lsp-semgrep-scan 136 :type 'integer) 137 138 (defcustom lsp-semgrep-scan-max-memory 0 139 "Maximum memory to use for scan, in MB." 140 :group 'lsp-semgrep-scan 141 :type 'integer) 142 143 (defcustom lsp-semgrep-scan-max-target-bytes 1000000 144 "Maximum size of target file to scan, in bytes." 145 :group 'lsp-semgrep-scan 146 :type 'integer) 147 148 (defcustom lsp-semgrep-scan-timeout 30 149 "Maximum time to wait for scan to complete, in seconds." 150 :group 'lsp-semgrep-scan 151 :type 'integer) 152 153 (defcustom lsp-semgrep-scan-timeout-threshold 30 154 "Maximum time to wait for scan to complete, in seconds." 155 :group 'lsp-semgrep-scan 156 :type 'integer) 157 158 (defcustom lsp-semgrep-scan-only-git-dirty t 159 "Only scan files that are dirty in git." 160 :group 'lsp-semgrep-scan 161 :type 'boolean) 162 163 ;; Metrics options 164 165 (defcustom lsp-semgrep-metrics-enabled t 166 "Enable metrics collection." 167 :group 'lsp-semgrep-metrics 168 :type 'boolean) 169 170 (defcustom lsp-semgrep-metrics-extension-type "emacs" 171 "Extension host type." 172 :group 'lsp-semgrep-metrics 173 :type 'string) 174 175 ;; Custom commands 176 177 (defun semgrep-scan-workspace (full) 178 "Scan workspace with Semgrep. 179 If FULL is non-nil, scan all files in workspace, regardless of git status." 180 (interactive (list (lsp--completing-read "Scan: " (list "Changed files in workspace" "All files in workspace") 'identity))) 181 (lsp-notify "semgrep/scanWorkspace" (list :full (if (string= full "All files in workspace") t :json-false)))) 182 183 (defun semgrep-refresh-rules () 184 "Refresh Semgrep rules." 185 (interactive) 186 (lsp-notify "semgrep/refreshRules" lsp--empty-ht)) 187 188 189 (defun semgrep-login () 190 "Login to Semgrep." 191 (interactive) 192 (lsp-request-async "semgrep/login" lsp--empty-ht 193 (lambda (result) 194 (list 195 (browse-url (lsp-get result :url)) 196 (lsp-message "Please login to Semgrep and return to Emacs.") 197 (lsp-notify "semgrep/loginFinish" result))))) 198 199 (defun semgrep-logout () 200 "Logout from Semgrep." 201 (interactive) 202 (lsp-notify "semgrep/logout" lsp--empty-ht)) 203 204 (lsp-register-client 205 (make-lsp-client 206 :new-connection (lsp-stdio-connection (lambda () lsp-semgrep-server-command)) 207 :activation-fn (lambda (_file-name _mode) 208 (-contains? lsp-semgrep-languages (lsp-buffer-language))) 209 :server-id 'semgrep-ls 210 :priority -1 211 :add-on? t 212 :initialization-options 213 (lambda () 214 (list 215 :scan (list 216 :configuration lsp-semgrep-scan-configuration 217 :exclude lsp-semgrep-scan-exclude 218 :include lsp-semgrep-scan-include 219 :jobs lsp-semgrep-scan-jobs 220 :maxMemory lsp-semgrep-scan-max-memory 221 :maxTargetBytes lsp-semgrep-scan-max-target-bytes 222 :timeout lsp-semgrep-scan-timeout 223 :timeoutThreshold lsp-semgrep-scan-timeout-threshold 224 :onlyGitDirty lsp-semgrep-scan-only-git-dirty) 225 :metrics (list 226 :enabled lsp-semgrep-metrics-enabled 227 :extensionType lsp-semgrep-metrics-extension-type) 228 :trace (list 229 :server lsp-semgrep-trace-server))))) 230 231 (lsp-consistency-check lsp-semgrep) 232 233 (provide 'lsp-semgrep) 234 ;;; lsp-semgrep.el ends here