-
Notifications
You must be signed in to change notification settings - Fork 113
feat(diagnostics): gitleaks builtin #315
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
Merged
+169
−0
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7976f61
fix(formatting): npm_groovy_lint fix timeout and no error
bb23518
Merge branch 'nvimtools:main' into main
b6e0e04
chore: Auto generate docs
mosheavni fa4c77e
Merge remote-tracking branch 'none/main'
0e57d12
chore: Auto generate docs
mosheavni 1c3be48
Merge remote-tracking branch 'tools/main'
ad91cfd
Merge remote-tracking branch 'tools/main'
c68aa3f
Merge branch 'nvimtools:main' into main
mosheavni 13316ec
feat: add gitleaks diagnostic builtin
mosheavni 0e69a43
add tests, convert to stdin
mosheavni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| local h = require("null-ls.helpers") | ||
| local methods = require("null-ls.methods") | ||
|
|
||
| local DIAGNOSTICS = methods.internal.DIAGNOSTICS | ||
|
|
||
| local handle_gitleaks_output = function(params) | ||
| local parser = h.diagnostics.from_json({ | ||
| attributes = { | ||
| code = "code", | ||
| }, | ||
| diagnostic = { | ||
| source = "gitleaks", | ||
| }, | ||
| }) | ||
|
|
||
| local offenses = {} | ||
| for _, finding in ipairs(params.output or {}) do | ||
| table.insert(offenses, { | ||
| message = finding.Description, | ||
| ruleId = finding.RuleID, | ||
| code = finding.RuleID, | ||
| line = finding.StartLine, | ||
| column = finding.StartColumn, | ||
| endLine = finding.EndLine, | ||
| endColumn = finding.EndColumn, | ||
| }) | ||
| end | ||
|
|
||
| return parser({ output = offenses }) | ||
| end | ||
|
|
||
| return h.make_builtin({ | ||
| name = "gitleaks", | ||
| meta = { | ||
| url = "https://github.com/gitleaks/gitleaks", | ||
| description = "Gitleaks is a SAST tool for detecting and preventing hardcoded secrets like passwords, API keys, and tokens in git repos.", | ||
| }, | ||
| method = DIAGNOSTICS, | ||
| filetypes = {}, | ||
| generator_opts = { | ||
| command = "gitleaks", | ||
| args = { | ||
| "stdin", | ||
| "--report-format", | ||
| "json", | ||
| "--report-path", | ||
| "-", | ||
| "--exit-code", | ||
| "0", | ||
| "--no-banner", | ||
| }, | ||
| format = "json", | ||
| to_stdin = true, | ||
| from_stderr = true, | ||
| ignore_stderr = true, | ||
| check_exit_code = function(code) | ||
| return code == 0 | ||
| end, | ||
| on_output = handle_gitleaks_output, | ||
| }, | ||
| factory = h.generator_factory, | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| local diagnostics = require("null-ls.builtins").diagnostics | ||
|
|
||
| describe("diagnostics gitleaks", function() | ||
| local parser = diagnostics.gitleaks._opts.on_output | ||
|
|
||
| it("should create a diagnostic from gitleaks output", function() | ||
| local output = vim.json.decode([[ | ||
| [ | ||
| { | ||
| "RuleID": "generic-api-key", | ||
| "Description": "Detected a Generic API Key, potentially exposing access to various services and sensitive operations.", | ||
| "StartLine": 192, | ||
| "EndLine": 192, | ||
| "StartColumn": 8, | ||
| "EndColumn": 67, | ||
| "Match": "ocp-apim-subscription-key: 5ccb5b137e7444d885be752eda7f767a'", | ||
| "Secret": "5ccb5b137e7444d885be752eda7f767a", | ||
| "File": "zsh/zsh.d/functions.zsh", | ||
| "SymlinkFile": "", | ||
| "Commit": "", | ||
| "Entropy": 3.5695488, | ||
| "Author": "", | ||
| "Email": "", | ||
| "Date": "", | ||
| "Message": "", | ||
| "Tags": [], | ||
| "Fingerprint": "zsh/zsh.d/functions.zsh:generic-api-key:192" | ||
| } | ||
| ] | ||
| ]]) | ||
| local diagnostic = parser({ output = output }) | ||
| assert.same({ | ||
| { | ||
| row = 192, | ||
| col = 8, | ||
| end_row = 192, | ||
| end_col = 67, | ||
| message = "Detected a Generic API Key, potentially exposing access to various services and sensitive operations.", | ||
| source = "gitleaks", | ||
| code = "generic-api-key", | ||
| }, | ||
| }, diagnostic) | ||
| end) | ||
|
|
||
| it("should handle multiple findings", function() | ||
| local output = vim.json.decode([[ | ||
| [ | ||
| { | ||
| "RuleID": "generic-api-key", | ||
| "Description": "Detected a Generic API Key, potentially exposing access to various services and sensitive operations.", | ||
| "StartLine": 10, | ||
| "EndLine": 10, | ||
| "StartColumn": 5, | ||
| "EndColumn": 30, | ||
| "Match": "api_key = 'abc123'", | ||
| "Secret": "abc123", | ||
| "File": "config.py", | ||
| "Fingerprint": "config.py:generic-api-key:10" | ||
| }, | ||
| { | ||
| "RuleID": "aws-access-token", | ||
| "Description": "Detected AWS Access Token, risking unauthorized cloud resource access and data breaches.", | ||
| "StartLine": 25, | ||
| "EndLine": 25, | ||
| "StartColumn": 12, | ||
| "EndColumn": 50, | ||
| "Match": "AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI", | ||
| "Secret": "wJalrXUtnFEMI", | ||
| "File": "env.sh", | ||
| "Fingerprint": "env.sh:aws-access-token:25" | ||
| } | ||
| ] | ||
| ]]) | ||
| local diagnostic = parser({ output = output }) | ||
| assert.same({ | ||
| { | ||
| row = 10, | ||
| col = 5, | ||
| end_row = 10, | ||
| end_col = 30, | ||
| message = "Detected a Generic API Key, potentially exposing access to various services and sensitive operations.", | ||
| source = "gitleaks", | ||
| code = "generic-api-key", | ||
| }, | ||
| { | ||
| row = 25, | ||
| col = 12, | ||
| end_row = 25, | ||
| end_col = 50, | ||
| message = "Detected AWS Access Token, risking unauthorized cloud resource access and data breaches.", | ||
| source = "gitleaks", | ||
| code = "aws-access-token", | ||
| }, | ||
| }, diagnostic) | ||
| end) | ||
|
|
||
| it("should handle empty output", function() | ||
| local output = vim.json.decode("[]") | ||
| local diagnostic = parser({ output = output }) | ||
| assert.same({}, diagnostic) | ||
| end) | ||
|
|
||
| it("should handle nil output", function() | ||
| local diagnostic = parser({ output = nil }) | ||
| assert.same({}, diagnostic) | ||
| end) | ||
| end) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: nvimtools/none-ls.nvim
Length of output: 678
Fix output stream configuration:
from_stderrshould befalse.The JSON report is output to stdout, not stderr. Gitleaks outputs only info/warning logs to stderr (e.g.,
"12:27PM INF scanned ~25 bytes"). Withfrom_stderr = true, null-ls will attempt to parse stderr log messages as JSON, which will fail and prevent diagnostics from being generated.Change line 54 to
from_stderr = falseto read the JSON report from stdout.🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we do ignore_stderr here.