-
Notifications
You must be signed in to change notification settings - Fork 15
feat: verify fixedIssueIds/preventedIssueIds against an in-process sc… #48
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
Open
MaVictor
wants to merge
4
commits into
main
Choose a base branch
from
feat/memcache-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,257 @@ | ||
| /* | ||
| * © 2025 Snyk Limited | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package mcp | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "sort" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/rs/zerolog" | ||
| "github.com/snyk/studio-mcp/internal/code" | ||
| "github.com/snyk/studio-mcp/internal/oss" | ||
| "github.com/snyk/studio-mcp/internal/types" | ||
| ) | ||
|
|
||
| type scanType string | ||
|
|
||
| const ( | ||
| scanTypeSAST scanType = "sast" | ||
| scanTypeSCA scanType = "sca" | ||
| ) | ||
|
|
||
| // verificationState is the per-call tag attached to a snyk_send_feedback | ||
| // analytics event, summarizing how well fixedIssueIds/preventedIssueIds claims | ||
| // checked out against the scan-result cache. | ||
| type verificationState string | ||
|
|
||
| const ( | ||
| verificationVerified verificationState = "verified" | ||
| verificationUnverifiable verificationState = "unverifiable" | ||
| verificationMismatch verificationState = "mismatch" | ||
| ) | ||
|
|
||
| // verificationPrecedence orders states from weakest to strongest signal for | ||
| // the worst-case reduction below: mismatch > unverifiable > verified. | ||
| var verificationPrecedence = map[verificationState]int{ | ||
| verificationVerified: 1, | ||
| verificationUnverifiable: 2, | ||
| verificationMismatch: 3, | ||
| } | ||
|
|
||
| // scanCacheEntry is the freshest known finding set for one file path, as | ||
| // reported by the most recent snyk_code_scan/snyk_sca_scan call whose results | ||
| // included that file. ids are already scan-type-prefixed (e.g. "sast:rule", | ||
| // "sca:SNYK-JS-...") so they can be compared directly against | ||
| // fixedIssueIds/preventedIssueIds entries. | ||
| type scanCacheEntry struct { | ||
| scanType scanType | ||
| ids map[string]struct{} | ||
| updatedAt time.Time | ||
| } | ||
|
|
||
| type scanCache struct { | ||
| entries map[string]*scanCacheEntry | ||
|
|
||
| // maxEntries bounds the cache; see MaxScanCacheEntries. | ||
| maxEntries int | ||
| } | ||
|
|
||
| // UpdateSASTIssues and UpdateSCAIssues upsert one entry per file path found | ||
| // in the scan's own results (not the tool call's path argument), fully | ||
| // replacing each file's prior record. | ||
| // | ||
| // scanPath additionally clears stale entries for any file/directory it | ||
| // covers that reported no issues, so a clean re-scan can supersede an old | ||
| // vulnerable record even though it produces no findings of its own to do | ||
| // so. scanPath must be the tool call's own path argument exactly as given | ||
| // (a file or a directory) - not a derived working directory, which for a | ||
| // single-file call is that file's parent and would wrongly widen this | ||
| // scan's authority to every cached sibling file. A single-file scanPath | ||
| // clears just that file; a directory scanPath clears every already-cached | ||
| // file (of the same scan type) nested under it, matching this tool's | ||
| // recursive scan behavior. Without this, a genuine fix could be | ||
| // misreported as verification=mismatch because the cache never learned its | ||
| // file (or containing directory) had gone clean. | ||
| func (s *scanCache) UpdateSASTIssues(scanPath string, issues []types.IssueData) { | ||
| now := time.Now() | ||
| s.clearEntries(now, scanTypeSAST, scanPath, issues) | ||
| s.addEntries(now, scanTypeSAST, issues) | ||
| } | ||
|
|
||
| func (s *scanCache) UpdateSCAIssues(scanPath string, issues []types.IssueData) { | ||
| now := time.Now() | ||
| s.clearEntries(now, scanTypeSCA, scanPath, issues) | ||
| s.addEntries(now, scanTypeSCA, issues) | ||
| } | ||
|
|
||
| func (s *scanCache) VerifyID(id string) verificationState { | ||
| scanType, ok := scanTypeFromID(id) | ||
| if !ok { | ||
| // No recognized scan-type prefix (including an empty string or a | ||
| // typo'd prefix): there's no relevant scan type to check against. | ||
| return verificationUnverifiable | ||
| } | ||
|
|
||
| foundRelevantScan := false | ||
| for _, entry := range s.entries { | ||
| if entry.scanType != scanType { | ||
| continue | ||
| } | ||
| foundRelevantScan = true | ||
| if _, present := entry.ids[id]; present { | ||
| return verificationMismatch | ||
| } | ||
| } | ||
| if !foundRelevantScan { | ||
| return verificationUnverifiable | ||
| } | ||
|
|
||
| return verificationVerified | ||
| } | ||
|
|
||
| // clearEntries implements the scanPath-seeding half of UpdateSASTIssues / | ||
| // UpdateSCAIssues described above: it clears stale ids for path itself (or, | ||
| // when path is a directory, for every already-cached file of scanType nested | ||
| // under it) so that a clean re-scan can supersede a stale vulnerable record | ||
| // even when it reports no issues of its own. | ||
| func (s *scanCache) clearEntries(now time.Time, scanType scanType, path string, issues []types.IssueData) { | ||
| if info, err := os.Stat(path); err == nil { | ||
| if info.IsDir() { | ||
| for filePath, entry := range s.entries { | ||
| if entry.scanType != scanType { | ||
| continue | ||
| } | ||
|
|
||
| if isWithinDir(filePath, path) { | ||
| entry.ids = make(map[string]struct{}) | ||
| entry.updatedAt = now | ||
| } | ||
| } | ||
| } else { | ||
| if entry, ok := s.entries[path]; ok { | ||
| // Only clear an existing entry when it's the same scan type; | ||
| // otherwise leave the other scan type's record untouched, | ||
| // matching the directory branch's skip logic above. | ||
| if entry.scanType == scanType { | ||
| entry.ids = make(map[string]struct{}) | ||
| entry.updatedAt = now | ||
| } | ||
| } else { | ||
| s.entries[path] = &scanCacheEntry{ | ||
| scanType: scanType, | ||
| ids: map[string]struct{}{}, | ||
| updatedAt: now, | ||
| } | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| func (s *scanCache) addEntries(now time.Time, scanType scanType, issues []types.IssueData) { | ||
| // Group issue IDs by file path | ||
| prefix := string(scanType + ":") | ||
| idsByFile := make(map[string]map[string]struct{}) | ||
| for _, issue := range issues { | ||
| if issue.FilePath == "" || issue.ID == "" { | ||
| continue | ||
| } | ||
| set, ok := idsByFile[issue.FilePath] | ||
| if !ok { | ||
| set = make(map[string]struct{}) | ||
| idsByFile[issue.FilePath] = set | ||
| } | ||
| set[prefix+issue.ID] = struct{}{} | ||
| } | ||
|
|
||
| // Insert grouped ids (it's ok to over-insert, we will evict excess entries before we return) | ||
| for filePath, ids := range idsByFile { | ||
| s.entries[filePath] = &scanCacheEntry{ | ||
| scanType: scanType, | ||
| ids: ids, | ||
| updatedAt: now, | ||
| } | ||
| } | ||
|
|
||
| s.evictExcess() | ||
| } | ||
|
|
||
| // evictExcess removes the least-recently-updated entries, oldest first, | ||
| // until the cache is back within maxEntries. | ||
| func (s *scanCache) evictExcess() { | ||
| excess := len(s.entries) - s.maxEntries | ||
| if excess <= 0 { | ||
| return | ||
| } | ||
|
|
||
| keys := make([]string, 0, len(s.entries)) | ||
| for key := range s.entries { | ||
| keys = append(keys, key) | ||
| } | ||
| sort.Slice(keys, func(i, j int) bool { | ||
| return s.entries[keys[i]].updatedAt.Before(s.entries[keys[j]].updatedAt) | ||
| }) | ||
|
|
||
| for _, key := range keys[:excess] { | ||
| delete(s.entries, key) | ||
| } | ||
| } | ||
|
|
||
| func parseScanOutput(logger *zerolog.Logger, toolDef SnykMcpToolsDefinition, output string, workDir string, includeIgnores bool) (scanType, []types.IssueData, error) { | ||
| switch toolDef.Name { | ||
| case ToolName.CodeTest: | ||
| issues, err := code.ConvertSARIFJSONToIssues(logger, []byte(output), workDir, includeIgnores) | ||
| return scanTypeSAST, issues, err | ||
| case ToolName.ScaTest: | ||
| issues, err := oss.ConvertOssJsonToIssues(workDir, []byte(output), includeIgnores) | ||
| return scanTypeSCA, issues, err | ||
| default: | ||
| return "", nil, fmt.Errorf("unsupported tool name %q", toolDef.Name) | ||
| } | ||
| } | ||
|
|
||
| // isWithinDir reports whether filePath is dir itself or nested inside it, | ||
| // comparing cleaned paths so callers don't need to worry about trailing | ||
| // separators or relative segments. | ||
| func isWithinDir(filePath, dir string) bool { | ||
| cleanDir := filepath.Clean(dir) | ||
| cleanFile := filepath.Clean(filePath) | ||
| if cleanFile == cleanDir { | ||
| return true | ||
| } | ||
| rel, err := filepath.Rel(cleanDir, cleanFile) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| return rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) | ||
| } | ||
|
|
||
| // scanTypeFromID extracts the scan type from a scan-type-prefixed issue ID | ||
| // ("sast:"/"sca:"). ok is false for an empty string or an unrecognized prefix. | ||
| func scanTypeFromID(id string) (scanType scanType, ok bool) { | ||
| switch { | ||
| case strings.HasPrefix(id, "sast:"): | ||
| return scanTypeSAST, true | ||
| case strings.HasPrefix(id, "sca:"): | ||
| return scanTypeSCA, true | ||
| default: | ||
| return "", false | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Eviction yields false verified state
Medium Severity
VerifyIDtreats a claimed issue asverifiedwhenever that ID is absent from every remaining cache entry of the matching scan type. After LRU eviction removes the only entries that ever held the ID, unrelated scans can still leave other same-type entries in the cache, sofoundRelevantScanstays true and feedback can be taggedverifiedwith no fresh evidence for that file or ID.Reviewed by Cursor Bugbot for commit d889a2d. Configure here.